|
| 1 | +import { describe, it, expect, vi, afterEach } from "vitest"; |
| 2 | +import * as dateUtils from "./dateUtils"; |
| 3 | +import { getNow, getCutoffDate } from "./dateUtils"; |
| 4 | + |
| 5 | +const MS_PER_DAY = 24 * 60 * 60 * 1000; |
| 6 | + |
| 7 | +// Fixed reference instant used across all tests. |
| 8 | +const FIXED_NOW = new Date("2026-04-24T12:00:00.000Z"); |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | +}); |
| 13 | + |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +// getNow |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | +describe("getNow", () => { |
| 18 | + it("returns the real current time when not mocked", () => { |
| 19 | + const before = Date.now(); |
| 20 | + const result = getNow().getTime(); |
| 21 | + const after = Date.now(); |
| 22 | + expect(result).toBeGreaterThanOrEqual(before); |
| 23 | + expect(result).toBeLessThanOrEqual(after); |
| 24 | + }); |
| 25 | + |
| 26 | + it("can be mocked via vi.spyOn for deterministic tests", () => { |
| 27 | + vi.spyOn(dateUtils, "getNow").mockReturnValue(FIXED_NOW); |
| 28 | + expect(getNow()).toBe(FIXED_NOW); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | +// getCutoffDate |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | +describe("getCutoffDate", () => { |
| 36 | + it("returns a date exactly 7 days before `now` for the 7D range", () => { |
| 37 | + const cutoff = getCutoffDate("7D", FIXED_NOW); |
| 38 | + expect(cutoff.getTime()).toBe(FIXED_NOW.getTime() - 7 * MS_PER_DAY); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns a date exactly 30 days before `now` for the 1M range", () => { |
| 42 | + const cutoff = getCutoffDate("1M", FIXED_NOW); |
| 43 | + expect(cutoff.getTime()).toBe(FIXED_NOW.getTime() - 30 * MS_PER_DAY); |
| 44 | + }); |
| 45 | + |
| 46 | + it("returns a date exactly 90 days before `now` for the 3M range", () => { |
| 47 | + const cutoff = getCutoffDate("3M", FIXED_NOW); |
| 48 | + expect(cutoff.getTime()).toBe(FIXED_NOW.getTime() - 90 * MS_PER_DAY); |
| 49 | + }); |
| 50 | + |
| 51 | + it("does not mutate the `now` argument", () => { |
| 52 | + const now = new Date(FIXED_NOW.getTime()); |
| 53 | + getCutoffDate("1M", now); |
| 54 | + expect(now.getTime()).toBe(FIXED_NOW.getTime()); |
| 55 | + }); |
| 56 | + |
| 57 | + it("uses getNow() when no `now` argument is provided", () => { |
| 58 | + // Fake the system clock so new Date() inside getNow() returns FIXED_NOW. |
| 59 | + // vi.spyOn on the named export cannot intercept the in-module call used |
| 60 | + // by the default parameter, but fake timers mock Date() at the runtime level. |
| 61 | + vi.useFakeTimers(); |
| 62 | + vi.setSystemTime(FIXED_NOW); |
| 63 | + try { |
| 64 | + const cutoff = getCutoffDate("7D"); |
| 65 | + expect(cutoff.getTime()).toBe(FIXED_NOW.getTime() - 7 * MS_PER_DAY); |
| 66 | + } finally { |
| 67 | + vi.useRealTimers(); |
| 68 | + } |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +// --------------------------------------------------------------------------- |
| 73 | +// Filtering simulation (the core chart behaviour) |
| 74 | +// --------------------------------------------------------------------------- |
| 75 | +describe("time-range filtering with mocked system time", () => { |
| 76 | + /** Build a synthetic history point `daysAgo` days before FIXED_NOW. */ |
| 77 | + function makePoint(daysAgo: number) { |
| 78 | + return { |
| 79 | + date: new Date(FIXED_NOW.getTime() - daysAgo * MS_PER_DAY).toISOString(), |
| 80 | + value: 100 + daysAgo, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + const history = [ |
| 85 | + makePoint(0), // today |
| 86 | + makePoint(5), // 5 days ago – inside 7D |
| 87 | + makePoint(7), // exactly 7 days ago – boundary (inclusive) |
| 88 | + makePoint(8), // 8 days ago – outside 7D, inside 1M |
| 89 | + makePoint(29), // 29 days ago – inside 1M |
| 90 | + makePoint(30), // exactly 30 days ago – boundary (inclusive) |
| 91 | + makePoint(31), // 31 days ago – outside 1M, inside 3M |
| 92 | + makePoint(89), // 89 days ago – inside 3M |
| 93 | + makePoint(90), // exactly 90 days ago – boundary (inclusive) |
| 94 | + makePoint(91), // 91 days ago – outside all finite ranges |
| 95 | + ]; |
| 96 | + |
| 97 | + function filter(range: Exclude<dateUtils.TimeRange, "ALL">) { |
| 98 | + const cutoff = getCutoffDate(range, FIXED_NOW); |
| 99 | + return history.filter(p => new Date(p.date) >= cutoff); |
| 100 | + } |
| 101 | + |
| 102 | + it("7D: keeps points on or after the 7-day cutoff", () => { |
| 103 | + const result = filter("7D"); |
| 104 | + // today (0d), 5d, 7d should be included; 8d+ excluded |
| 105 | + expect(result).toHaveLength(3); |
| 106 | + const daysAgo = result.map(p => |
| 107 | + Math.round((FIXED_NOW.getTime() - new Date(p.date).getTime()) / MS_PER_DAY), |
| 108 | + ); |
| 109 | + expect(daysAgo).toEqual([0, 5, 7]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("1M: keeps points on or after the 30-day cutoff", () => { |
| 113 | + const result = filter("1M"); |
| 114 | + // 0d, 5d, 7d, 8d, 29d, 30d included; 31d+ excluded |
| 115 | + expect(result).toHaveLength(6); |
| 116 | + }); |
| 117 | + |
| 118 | + it("3M: keeps points on or after the 90-day cutoff", () => { |
| 119 | + const result = filter("3M"); |
| 120 | + // all except the 91-day point |
| 121 | + expect(result).toHaveLength(9); |
| 122 | + }); |
| 123 | +}); |
0 commit comments