dashboard-logs-time-range-utils.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, test } from "vitest";
  2. import {
  3. dateStringWithClockToTimestamp,
  4. formatClockFromTimestamp,
  5. getQuickDateRange,
  6. inclusiveEndTimestampFromExclusive,
  7. parseClockString,
  8. } from "@/app/[locale]/dashboard/logs/_utils/time-range";
  9. describe("dashboard logs time range utils", () => {
  10. test("parseClockString supports HH:MM and defaults seconds to 0", () => {
  11. expect(parseClockString("01:02")).toEqual({ hours: 1, minutes: 2, seconds: 0 });
  12. });
  13. test("parseClockString falls back to 0 for invalid numbers", () => {
  14. expect(parseClockString("xx:yy:zz")).toEqual({ hours: 0, minutes: 0, seconds: 0 });
  15. expect(parseClockString("01:02:xx")).toEqual({ hours: 1, minutes: 2, seconds: 0 });
  16. });
  17. test("dateStringWithClockToTimestamp combines local date + clock", () => {
  18. const ts = dateStringWithClockToTimestamp("2026-01-01", "01:02:03");
  19. const expected = new Date(2026, 0, 1, 1, 2, 3, 0).getTime();
  20. expect(ts).toBe(expected);
  21. });
  22. test("dateStringWithClockToTimestamp returns undefined for invalid date", () => {
  23. expect(dateStringWithClockToTimestamp("not-a-date", "01:02:03")).toBeUndefined();
  24. expect(dateStringWithClockToTimestamp("2026-13-40", "01:02:03")).toBeUndefined();
  25. });
  26. test("exclusive end time round-trips to inclusive end time (+/-1s)", () => {
  27. const inclusive = dateStringWithClockToTimestamp("2026-01-02", "04:05:06");
  28. expect(inclusive).toBeDefined();
  29. const exclusive = inclusive! + 1000;
  30. expect(inclusiveEndTimestampFromExclusive(exclusive)).toBe(inclusive);
  31. });
  32. test("inclusiveEndTimestampFromExclusive clamps at 0", () => {
  33. expect(inclusiveEndTimestampFromExclusive(0)).toBe(0);
  34. expect(inclusiveEndTimestampFromExclusive(500)).toBe(0);
  35. });
  36. test("formatClockFromTimestamp uses HH:MM:SS", () => {
  37. const ts = new Date(2026, 0, 1, 1, 2, 3, 0).getTime();
  38. expect(formatClockFromTimestamp(ts)).toBe("01:02:03");
  39. });
  40. test("getQuickDateRange uses server timezone for today/yesterday", () => {
  41. const now = new Date("2024-01-02T02:00:00Z");
  42. const tz = "America/Los_Angeles";
  43. expect(getQuickDateRange("today", tz, now)).toEqual({
  44. startDate: "2024-01-01",
  45. endDate: "2024-01-01",
  46. });
  47. expect(getQuickDateRange("yesterday", tz, now)).toEqual({
  48. startDate: "2023-12-31",
  49. endDate: "2023-12-31",
  50. });
  51. });
  52. });