fix: expire frontend caches
This commit is contained in:
@@ -51,6 +51,47 @@ describe("api helpers", () => {
|
||||
await expect(getJSON("/api/fail")).rejects.toThrow("boom");
|
||||
});
|
||||
|
||||
it("cleans expired JSON cache entries", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
vi.setSystemTime(new Date("2026-05-31T00:00:00Z"));
|
||||
const { getJSON, readJSONCache } = await loadApi();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ items: [1] }));
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
await getJSON("/api/posts");
|
||||
expect(readJSONCache("/api/posts")).toEqual({ items: [1] });
|
||||
|
||||
vi.setSystemTime(new Date("2026-05-31T00:05:01Z"));
|
||||
expect(readJSONCache("/api/posts")).toBeNull();
|
||||
expect(window.localStorage.length).toBe(0);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("prunes old JSON cache entries beyond the entry limit", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const { getJSON, readJSONCache } = await loadApi();
|
||||
const fetchMock = vi.fn((url: string) =>
|
||||
Promise.resolve(jsonResponse({ url })),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
for (let index = 0; index < 81; index += 1) {
|
||||
vi.setSystemTime(new Date(2026, 4, 31, 0, 0, index));
|
||||
await getJSON(`/api/cache-${index}`);
|
||||
}
|
||||
|
||||
expect(readJSONCache("/api/cache-0")).toBeNull();
|
||||
expect(readJSONCache("/api/cache-80")).toEqual({ url: "/api/cache-80" });
|
||||
expect(window.localStorage.length).toBe(80);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("posts JSON with optional bearer token", async () => {
|
||||
const { postJSON } = await loadApi();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ id: 1 }));
|
||||
|
||||
Reference in New Issue
Block a user