/* @vitest-environment node */ import { describe, it, expect } from "vitest"; import { SEARCH_RESULTS_SORT, toSearchItemIsoDateKey, formatSearchItemDateDe, sortSearchItems, } from "./resultsSorting.js"; describe("lib/frontend/search/resultsSorting", () => { it("builds ISO date keys (YYYY-MM-DD) with zero padding", () => { expect(toSearchItemIsoDateKey({ year: "2025", month: "1", day: "2" })).toBe( "2025-01-02" ); }); it("formats German date strings (DD.MM.YYYY) with zero padding", () => { expect(formatSearchItemDateDe({ year: "2025", month: "1", day: "2" })).toBe( "02.01.2025" ); }); it("RELEVANCE keeps backend order", () => { const items = [{ filename: "b.pdf" }, { filename: "a.pdf" }]; const out = sortSearchItems(items, SEARCH_RESULTS_SORT.RELEVANCE); expect(out.map((x) => x.filename)).toEqual(["b.pdf", "a.pdf"]); }); it("DATE_DESC sorts newest date first, then filename asc", () => { const items = [ { year: "2025", month: "12", day: "18", filename: "b.pdf" }, { year: "2025", month: "12", day: "18", filename: "a.pdf" }, { year: "2025", month: "01", day: "02", filename: "z.pdf" }, ]; const out = sortSearchItems(items, SEARCH_RESULTS_SORT.DATE_DESC); expect( out.map((x) => `${toSearchItemIsoDateKey(x)}|${x.filename}`) ).toEqual(["2025-12-18|a.pdf", "2025-12-18|b.pdf", "2025-01-02|z.pdf"]); }); it("FILENAME_ASC sorts by filename", () => { const items = [ { filename: "b.pdf" }, { filename: "a.pdf" }, { filename: "c.pdf" }, ]; const out = sortSearchItems(items, SEARCH_RESULTS_SORT.FILENAME_ASC); expect(out.map((x) => x.filename)).toEqual(["a.pdf", "b.pdf", "c.pdf"]); }); });