projectTimelineDates.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { timelineDateHeadings } from "@/project-sidebar/timelineDates.js";
  3. const date = (month: number, day: number, hour = 12) =>
  4. new Date(2026, month - 1, day, hour).getTime();
  5. afterEach(() => vi.unstubAllEnvs());
  6. describe("SBV03: timeline calendar headings", () => {
  7. it("matches staging date buckets without reordering or counting headings as tasks", () => {
  8. const times = [
  9. date(9, 18),
  10. date(9, 17),
  11. date(9, 16),
  12. date(9, 15),
  13. date(9, 14),
  14. date(9, 10),
  15. date(9, 1),
  16. date(8, 1),
  17. date(7, 1),
  18. ];
  19. expect(timelineDateHeadings(times, date(9, 18, 23), "zh-CN").map((h) => h?.key)).toEqual([
  20. "today",
  21. "yesterday",
  22. "daysAgo:2",
  23. "daysAgo:3",
  24. "thisWeek",
  25. "lastWeek",
  26. "thisMonth",
  27. "lastMonth",
  28. "older",
  29. ]);
  30. expect(timelineDateHeadings([date(9, 16)], date(9, 18), "zh-CN")[0]).toMatchObject({
  31. id: "taskTimeline.daysAgo",
  32. values: { days: "2" },
  33. });
  34. });
  35. it("adds no duplicate heading when the next page continues the same date bucket", () => {
  36. const times = [...Array<number>(22).fill(date(9, 18)), date(9, 17)];
  37. const first = timelineDateHeadings(times.slice(0, 20), date(9, 18), "en-US");
  38. const expanded = timelineDateHeadings(times, date(9, 18), "en-US");
  39. expect(expanded.slice(0, 20)).toEqual(first);
  40. expect(expanded.filter(Boolean).map((h) => h?.key)).toEqual(["today", "yesterday"]);
  41. expect(expanded).toHaveLength(times.length);
  42. });
  43. it("derives new headings after an update moves a task to another day", () => {
  44. expect(
  45. timelineDateHeadings([date(9, 17), date(9, 17)], date(9, 18), "zh-CN").map((h) => h?.key),
  46. ).toEqual(["yesterday", undefined]);
  47. expect(
  48. timelineDateHeadings([date(9, 18), date(9, 17)], date(9, 18), "zh-CN").map((h) => h?.key),
  49. ).toEqual(["today", "yesterday"]);
  50. });
  51. it("uses the locale week boundary", () => {
  52. expect(timelineDateHeadings([date(9, 20)], date(9, 24), "en-US")[0]?.key).toBe("thisWeek");
  53. expect(timelineDateHeadings([date(9, 20)], date(9, 24), "zh-CN")[0]?.key).toBe("lastWeek");
  54. });
  55. it("uses calendar days across daylight saving and year boundaries", () => {
  56. vi.stubEnv("TZ", "America/New_York");
  57. expect(
  58. timelineDateHeadings(
  59. [new Date(2026, 2, 7, 23).getTime()],
  60. new Date(2026, 2, 9, 1).getTime(),
  61. "en-US",
  62. )[0]?.key,
  63. ).toBe("daysAgo:2");
  64. expect(
  65. timelineDateHeadings(
  66. [new Date(2025, 11, 31, 23).getTime()],
  67. new Date(2026, 0, 1, 1).getTime(),
  68. "en-US",
  69. )[0]?.key,
  70. ).toBe("yesterday");
  71. expect(timelineDateHeadings([], date(9, 18), "en-US")).toEqual([]);
  72. });
  73. });