|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | +import { msUntilRelativeTimeChange, relativeTime } from "./time"; |
| 3 | + |
| 4 | +const at = "2026-10-19T12:00:00.000Z"; |
| 5 | +const timestamp = Date.parse(at); |
| 6 | + |
| 7 | +describe("relative time", () => { |
| 8 | + it.each([ |
| 9 | + [59_999, "<1m", "1m"], |
| 10 | + [119_999, "1m", "2m"], |
| 11 | + [3_599_999, "59m", "1h"], |
| 12 | + [7_199_999, "1h", "2h"], |
| 13 | + [86_399_999, "23h", "1d"], |
| 14 | + [172_799_999, "1d", "2d"], |
| 15 | + ] as const)("updates at the next boundary after %i ms", (elapsed, before, after) => { |
| 16 | + const nowMs = timestamp + elapsed; |
| 17 | + expect(relativeTime(at, nowMs)).toBe(before); |
| 18 | + expect(msUntilRelativeTimeChange(at, nowMs)).toBe(1); |
| 19 | + expect(relativeTime(at, nowMs + 1)).toBe(after); |
| 20 | + }); |
| 21 | + |
| 22 | + it.each([ |
| 23 | + [0, 60_000], |
| 24 | + [60_000, 60_000], |
| 25 | + [3_600_000, 3_600_000], |
| 26 | + [86_400_000, 86_400_000], |
| 27 | + ])("waits a full label interval at %i ms", (elapsed, delay) => { |
| 28 | + expect(msUntilRelativeTimeChange(at, timestamp + elapsed)).toBe(delay); |
| 29 | + }); |
| 30 | + |
| 31 | + it("keeps invalid timestamps static", () => { |
| 32 | + expect(relativeTime("invalid", timestamp)).toBe("<1m"); |
| 33 | + expect(msUntilRelativeTimeChange("invalid", timestamp)).toBeNull(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("waits until a future timestamp is a minute old, with bounded timers", () => { |
| 37 | + expect(relativeTime(at, timestamp - 30_000)).toBe("<1m"); |
| 38 | + expect(msUntilRelativeTimeChange(at, timestamp - 30_000)).toBe(90_000); |
| 39 | + expect(msUntilRelativeTimeChange(at, timestamp - 30 * 86_400_000)).toBe(86_400_000); |
| 40 | + }); |
| 41 | +}); |
0 commit comments