resultsSorting.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. SEARCH_RESULTS_SORT,
  5. toSearchItemIsoDateKey,
  6. formatSearchItemDateDe,
  7. sortSearchItems,
  8. } from "./resultsSorting.js";
  9. describe("lib/frontend/search/resultsSorting", () => {
  10. it("builds ISO date keys (YYYY-MM-DD) with zero padding", () => {
  11. expect(toSearchItemIsoDateKey({ year: "2025", month: "1", day: "2" })).toBe(
  12. "2025-01-02"
  13. );
  14. });
  15. it("formats German date strings (DD.MM.YYYY) with zero padding", () => {
  16. expect(formatSearchItemDateDe({ year: "2025", month: "1", day: "2" })).toBe(
  17. "02.01.2025"
  18. );
  19. });
  20. it("RELEVANCE keeps backend order (shallow copy)", () => {
  21. const items = [{ filename: "b.pdf" }, { filename: "a.pdf" }];
  22. const out = sortSearchItems(items, SEARCH_RESULTS_SORT.RELEVANCE);
  23. expect(out.map((x) => x.filename)).toEqual(["b.pdf", "a.pdf"]);
  24. // Ensure we did not return the same array instance (mutation safety).
  25. expect(out).not.toBe(items);
  26. });
  27. it("DATE_DESC sorts newest date first, then branch asc, then filename asc", () => {
  28. const items = [
  29. // Same date, different branches
  30. {
  31. branch: "NL10",
  32. year: "2025",
  33. month: "12",
  34. day: "18",
  35. filename: "b.pdf",
  36. },
  37. {
  38. branch: "NL2",
  39. year: "2025",
  40. month: "12",
  41. day: "18",
  42. filename: "a.pdf",
  43. },
  44. // Older date
  45. {
  46. branch: "NL01",
  47. year: "2025",
  48. month: "01",
  49. day: "02",
  50. filename: "z.pdf",
  51. },
  52. ];
  53. const out = sortSearchItems(items, SEARCH_RESULTS_SORT.DATE_DESC);
  54. expect(
  55. out.map((x) => `${toSearchItemIsoDateKey(x)}|${x.branch}|${x.filename}`)
  56. ).toEqual([
  57. "2025-12-18|NL2|a.pdf", // NL2 before NL10 (numeric)
  58. "2025-12-18|NL10|b.pdf",
  59. "2025-01-02|NL01|z.pdf",
  60. ]);
  61. });
  62. it("BRANCH_ASC sorts branch asc (numeric), then newest date first, then filename asc", () => {
  63. const items = [
  64. // NL10 should come after NL2
  65. {
  66. branch: "NL10",
  67. year: "2025",
  68. month: "12",
  69. day: "18",
  70. filename: "b.pdf",
  71. },
  72. {
  73. branch: "NL2",
  74. year: "2025",
  75. month: "12",
  76. day: "18",
  77. filename: "a.pdf",
  78. },
  79. // Same branch (NL2), older date + different filename
  80. {
  81. branch: "NL2",
  82. year: "2025",
  83. month: "01",
  84. day: "02",
  85. filename: "z.pdf",
  86. },
  87. {
  88. branch: "NL2",
  89. year: "2025",
  90. month: "12",
  91. day: "18",
  92. filename: "c.pdf",
  93. },
  94. ];
  95. const out = sortSearchItems(items, SEARCH_RESULTS_SORT.BRANCH_ASC);
  96. expect(
  97. out.map((x) => `${x.branch}|${toSearchItemIsoDateKey(x)}|${x.filename}`)
  98. ).toEqual([
  99. // NL2 group first
  100. "NL2|2025-12-18|a.pdf",
  101. "NL2|2025-12-18|c.pdf", // same date -> filename tie-breaker
  102. "NL2|2025-01-02|z.pdf",
  103. // then NL10
  104. "NL10|2025-12-18|b.pdf",
  105. ]);
  106. });
  107. it("unknown sort mode fails safe to backend order", () => {
  108. const items = [{ filename: "b.pdf" }, { filename: "a.pdf" }];
  109. const out = sortSearchItems(items, "unknown");
  110. expect(out.map((x) => x.filename)).toEqual(["b.pdf", "a.pdf"]);
  111. });
  112. });