Skip to content

Commit 538acf4

Browse files
committed
test(ui): cover usage refresh policy surface
1 parent 1b9f57d commit 538acf4

2 files changed

Lines changed: 60 additions & 67 deletions

File tree

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,74 @@
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";
44

5-
// Mirrors the module-private default TTL; the tests assert that contract.
65
const USAGE_PAYLOAD_TTL_MS = 5 * 60_000;
7-
86
const NOW_MS = 1_000_000;
97

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");
2119
});
2220

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();
3424
});
3525

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();
4634
});
4735

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();
5847
});
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();
6857
});
6958

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();
8073
});
8174
});

‎ui/src/pages/usage/refresh-policy.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const USAGE_PAYLOAD_TTL_MS = 5 * 60_000;
33
type UsageRefreshReason = "focus" | "manual" | "poll" | "reconnect";
44
type UsageRefreshDecision = "defer" | "fetch" | "skip";
55

6-
export function decideUsageRefresh(params: {
6+
function decideUsageRefresh(params: {
77
reason: UsageRefreshReason;
88
visible: boolean;
99
interrupted: boolean;

0 commit comments

Comments
 (0)