test(dashboard): extract .data from SWR cache envelope in hook tests

swrCache.writeCache wraps values in a { savedAt, data } envelope, but
useChatRooms/useEvals/useInsights/useResearch tests still parsed
localStorage entries as bare arrays. Pull the .data field out before
indexing so the cache assertions hit the actual payload. Fixes 6
failing tests across the four hook test files.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-05-19 17:53:59 -07:00
parent 38adeba79f
commit e47c5e4507
4 changed files with 11 additions and 11 deletions

View File

@@ -134,7 +134,7 @@ describe("useChatRooms", () => {
renderHook(() => useChatRooms("proj-1"));
await waitFor(() => {
expect(JSON.parse(window.localStorage.getItem(`${SWR_CACHE_KEYS.CHAT_ROOMS}:proj-1`) ?? "[]")).toEqual(rooms);
expect(JSON.parse(window.localStorage.getItem(`${SWR_CACHE_KEYS.CHAT_ROOMS}:proj-1`) ?? "{}").data).toEqual(rooms);
});
});
@@ -191,8 +191,8 @@ describe("useChatRooms", () => {
const { result } = renderHook(() => useChatRooms("proj-1"));
await waitFor(() => expect(result.current.roomsLoading).toBe(false));
const cached = JSON.parse(window.localStorage.getItem(`${SWR_CACHE_KEYS.CHAT_ROOMS}:proj-1`) ?? "[]") as Array<Record<string, unknown>>;
expect(cached[0]).not.toHaveProperty("messages");
const cached = JSON.parse(window.localStorage.getItem(`${SWR_CACHE_KEYS.CHAT_ROOMS}:proj-1`) ?? "{}").data as Array<Record<string, unknown>>;
expect(cached?.[0]).not.toHaveProperty("messages");
});
it("handles room message SSE for active and inactive rooms", async () => { const older = room("room-1", "one", "2026-05-09T01:00:00.000Z");

View File

@@ -52,8 +52,8 @@ describe("useEvals", () => {
expect(result.current.results[0]).toMatchObject({ id: "ER-1", runId: "RUN-1", taskId: "FN-1", taskTitle: "Task One" });
});
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.EVALS_RESULTS_PREFIX}p1`) ?? "[]");
expect(cached[0]?.id).toBe("ER-1");
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.EVALS_RESULTS_PREFIX}p1`) ?? "{}").data;
expect(cached?.[0]?.id).toBe("ER-1");
act(() => result.current.setFilters((prev) => ({ ...prev, q: "fn-1", runId: "RUN-1", scoreMin: "0.5", scoreMax: "1" })));
@@ -85,7 +85,7 @@ describe("useEvals", () => {
renderHook(() => useEvals({ projectId: "p1" }));
await waitFor(() => {
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.EVALS_RESULTS_PREFIX}p1`) ?? "[]");
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.EVALS_RESULTS_PREFIX}p1`) ?? "{}").data;
expect(cached).toHaveLength(500);
});
});

View File

@@ -187,8 +187,8 @@ describe("useInsights", () => {
expect(architectureSection?.items).toHaveLength(1);
expect(architectureSection?.items[0].id).toBe("INS-2");
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.INSIGHTS_PREFIX}project-1`) ?? "[]");
expect(cached[0]?.id).toBe("INS-1");
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.INSIGHTS_PREFIX}project-1`) ?? "{}").data;
expect(cached?.[0]?.id).toBe("INS-1");
expect(cached).not.toHaveProperty("dismissStates");
});
@@ -212,7 +212,7 @@ describe("useInsights", () => {
renderHook(() => useInsights("project-1"));
await waitFor(() => {
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.INSIGHTS_PREFIX}project-1`) ?? "[]");
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.INSIGHTS_PREFIX}project-1`) ?? "{}").data;
expect(cached).toHaveLength(500);
});
});

View File

@@ -67,8 +67,8 @@ describe("useResearch", () => {
expect(result.current.availability.available).toBe(true);
});
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.RESEARCH_RUNS_PREFIX}p1`) ?? "[]");
expect(cached[0]?.id).toBe("RR-1");
const cached = JSON.parse(localStorage.getItem(`${SWR_CACHE_KEYS.RESEARCH_RUNS_PREFIX}p1`) ?? "{}").data;
expect(cached?.[0]?.id).toBe("RR-1");
});
it("isolates cache by project", async () => {