|
1 | | -// @vitest-environment node |
2 | | -import { describe, expect, it } from "vitest"; |
3 | | -import { decideUsageRefresh } from "./refresh-policy.ts"; |
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { UsageRefreshPolicy } from "./refresh-policy.ts"; |
4 | 4 |
|
5 | | -// Mirrors the module-private default TTL; the tests assert that contract. |
6 | 5 | const USAGE_PAYLOAD_TTL_MS = 5 * 60_000; |
7 | | - |
8 | 6 | const NOW_MS = 1_000_000; |
9 | 7 |
|
10 | | -describe("decideUsageRefresh", () => { |
11 | | - it("skips a reconnect while the cached payload is within the TTL", () => { |
12 | | - expect( |
13 | | - decideUsageRefresh({ |
14 | | - reason: "reconnect", |
15 | | - visible: true, |
16 | | - interrupted: false, |
17 | | - nowMs: NOW_MS, |
18 | | - lastLoadedAtMs: NOW_MS - USAGE_PAYLOAD_TTL_MS + 1, |
19 | | - }), |
20 | | - ).toBe("skip"); |
| 8 | +function createPolicy(isLoading = () => false) { |
| 9 | + const reload = vi.fn(); |
| 10 | + return { policy: new UsageRefreshPolicy({ isLoading, reload }), reload }; |
| 11 | +} |
| 12 | + |
| 13 | +describe("UsageRefreshPolicy", () => { |
| 14 | + beforeEach(() => { |
| 15 | + vi.useFakeTimers(); |
| 16 | + vi.setSystemTime(NOW_MS); |
| 17 | + vi.spyOn(document, "hasFocus").mockReturnValue(true); |
| 18 | + vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible"); |
21 | 19 | }); |
22 | 20 |
|
23 | | - it("defers a stale reconnect until the page is visible", () => { |
24 | | - const stale = { |
25 | | - nowMs: NOW_MS, |
26 | | - lastLoadedAtMs: NOW_MS - USAGE_PAYLOAD_TTL_MS, |
27 | | - }; |
28 | | - expect( |
29 | | - decideUsageRefresh({ reason: "reconnect", visible: false, interrupted: false, ...stale }), |
30 | | - ).toBe("defer"); |
31 | | - expect( |
32 | | - decideUsageRefresh({ reason: "focus", visible: true, interrupted: false, ...stale }), |
33 | | - ).toBe("fetch"); |
| 21 | + afterEach(() => { |
| 22 | + vi.useRealTimers(); |
| 23 | + vi.restoreAllMocks(); |
34 | 24 | }); |
35 | 25 |
|
36 | | - it("always fetches for a manual refresh", () => { |
37 | | - expect( |
38 | | - decideUsageRefresh({ |
39 | | - reason: "manual", |
40 | | - visible: false, |
41 | | - interrupted: false, |
42 | | - nowMs: NOW_MS, |
43 | | - lastLoadedAtMs: NOW_MS, |
44 | | - }), |
45 | | - ).toBe("fetch"); |
| 26 | + it("skips automatic refresh while the cached payload is within the TTL", () => { |
| 27 | + const { policy, reload } = createPolicy(); |
| 28 | + policy.setLastLoadedAtMs(NOW_MS - USAGE_PAYLOAD_TTL_MS + 1); |
| 29 | + |
| 30 | + policy.request("reconnect"); |
| 31 | + policy.request("poll"); |
| 32 | + |
| 33 | + expect(reload).not.toHaveBeenCalled(); |
46 | 34 | }); |
47 | 35 |
|
48 | | - it("fetches on a visible reconnect after the TTL", () => { |
49 | | - expect( |
50 | | - decideUsageRefresh({ |
51 | | - reason: "reconnect", |
52 | | - visible: true, |
53 | | - interrupted: false, |
54 | | - nowMs: NOW_MS, |
55 | | - lastLoadedAtMs: NOW_MS - USAGE_PAYLOAD_TTL_MS, |
56 | | - }), |
57 | | - ).toBe("fetch"); |
| 36 | + it("defers stale work until the page is visible", () => { |
| 37 | + const visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden"); |
| 38 | + const { policy, reload } = createPolicy(); |
| 39 | + policy.setLastLoadedAtMs(NOW_MS - USAGE_PAYLOAD_TTL_MS); |
| 40 | + |
| 41 | + policy.request("reconnect"); |
| 42 | + expect(reload).not.toHaveBeenCalled(); |
| 43 | + |
| 44 | + visibility.mockReturnValue("visible"); |
| 45 | + policy.request("focus"); |
| 46 | + expect(reload).toHaveBeenCalledOnce(); |
58 | 47 | }); |
59 | | - it("defers interrupted work while hidden, then fetches once active despite a fresh payload", () => { |
60 | | - const fresh = { |
61 | | - reason: "reconnect" as const, |
62 | | - nowMs: NOW_MS, |
63 | | - lastLoadedAtMs: NOW_MS, |
64 | | - interrupted: true, |
65 | | - }; |
66 | | - expect(decideUsageRefresh({ ...fresh, visible: false })).toBe("defer"); |
67 | | - expect(decideUsageRefresh({ ...fresh, reason: "focus", visible: true })).toBe("fetch"); |
| 48 | + |
| 49 | + it("always fetches for a manual refresh", () => { |
| 50 | + vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden"); |
| 51 | + const { policy, reload } = createPolicy(); |
| 52 | + policy.setLastLoadedAtMs(NOW_MS); |
| 53 | + |
| 54 | + policy.request("manual"); |
| 55 | + |
| 56 | + expect(reload).toHaveBeenCalledOnce(); |
68 | 57 | }); |
69 | 58 |
|
70 | | - it("applies the same TTL to automatic settle polling", () => { |
71 | | - expect( |
72 | | - decideUsageRefresh({ |
73 | | - reason: "poll", |
74 | | - visible: true, |
75 | | - interrupted: false, |
76 | | - nowMs: NOW_MS, |
77 | | - lastLoadedAtMs: NOW_MS - USAGE_PAYLOAD_TTL_MS + 1, |
78 | | - }), |
79 | | - ).toBe("skip"); |
| 59 | + it("retries interrupted work once active despite a fresh payload", () => { |
| 60 | + let loading = true; |
| 61 | + const visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden"); |
| 62 | + const { policy, reload } = createPolicy(() => loading); |
| 63 | + policy.setLastLoadedAtMs(NOW_MS); |
| 64 | + policy.interrupt(); |
| 65 | + loading = false; |
| 66 | + |
| 67 | + policy.request("reconnect"); |
| 68 | + expect(reload).not.toHaveBeenCalled(); |
| 69 | + |
| 70 | + visibility.mockReturnValue("visible"); |
| 71 | + policy.request("focus"); |
| 72 | + expect(reload).toHaveBeenCalledOnce(); |
80 | 73 | }); |
81 | 74 | }); |
0 commit comments