Skip to content

Commit ea79fa3

Browse files
saphidclaude
andcommitted
fix(web): saved usage keeps one read per window and model filter
A read under one model filter replaced the saved read of the same window under another, while the page remembered both as saved, so switching back to a cached filter left no offline snapshot for it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7d55f39 commit ea79fa3

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎apps/web/src/state/savedUsage.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import {
77
} from "@t3tools/contracts";
88
import { describe, expect, it, vi } from "vite-plus/test";
99

10-
import { pickSavedUsage, type SavedUsage, trimSavedUsage, saveUsage } from "./savedUsage";
10+
import {
11+
loadSavedUsage,
12+
pickSavedUsage,
13+
type SavedUsage,
14+
trimSavedUsage,
15+
saveUsage,
16+
} from "./savedUsage";
1117

1218
const bucket = (day: string, hourStart?: string): UsageBucket => ({
1319
day: UsageDay.make(day),
@@ -215,4 +221,40 @@ describe("saveUsage", () => {
215221
vi.unstubAllGlobals();
216222
}
217223
});
224+
225+
it("keeps reads of one window under different model filters side by side", async () => {
226+
const stored = new Map<string, unknown>();
227+
const succeed = (result: unknown, then?: () => void) => {
228+
const request = Object.assign(new EventTarget(), { result });
229+
queueMicrotask(() => {
230+
request.dispatchEvent(new Event("success"));
231+
if (then) queueMicrotask(then);
232+
});
233+
return request;
234+
};
235+
const database = {
236+
transaction: () => {
237+
const transaction: EventTarget & { objectStore?: () => unknown } = new EventTarget();
238+
const complete = () => transaction.dispatchEvent(new Event("complete"));
239+
transaction.objectStore = () => ({
240+
get: (key: string) => succeed(stored.get(key), complete),
241+
put: (value: unknown, key: string) => stored.set(key, structuredClone(value)),
242+
});
243+
return transaction;
244+
},
245+
};
246+
vi.stubGlobal("indexedDB", { open: () => succeed(database) });
247+
try {
248+
const filtered = { ...month.input, modelFilter: { exclude: [{ model: "claude-opus-5-5" }] } };
249+
expect(await saveUsage("env", month)).toBe(true);
250+
expect(await saveUsage("env", { ...month, input: filtered })).toBe(true);
251+
const saved = await loadSavedUsage("env");
252+
expect(saved.map((entry) => entry.input.modelFilter ?? null)).toEqual([
253+
filtered.modelFilter,
254+
null,
255+
]);
256+
} finally {
257+
vi.unstubAllGlobals();
258+
}
259+
});
218260
});

‎apps/web/src/state/savedUsage.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const windowKey = (input: UsageSummaryInput) =>
7272
input.resolution ?? "day",
7373
input.sinceTime ?? null,
7474
input.untilTime ?? null,
75+
input.modelFilter ?? null,
7576
]);
7677

7778
function decodeSaved(stored: unknown): readonly SavedUsage[] {

0 commit comments

Comments
 (0)