Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3import { readUsagePagePreferences, saveUsagePagePreferences } from "./usagePagePreferences";
4
5const key = "t3code:usage-page-preferences:v1";
6let values: Map<string, string>;
7let storage: Pick<Storage, "getItem" | "setItem">;
8
9beforeEach(() => {
10 values = new Map();
11 storage = {
12 getItem: (key) => values.get(key) ?? null,
13 setItem: (key, value) => {
14 values.set(key, value);
15 },
16 };
17 vi.stubGlobal("window", { localStorage: storage });
18 vi.spyOn(console, "error").mockImplementation(() => {});
19});
20
21afterEach(() => {
22 vi.restoreAllMocks();
23 vi.unstubAllGlobals();
24});
25
26describe("Usage page preferences", () => {
27 it("round-trips hidden providers and reads older saves without them", () => {
28 saveUsagePagePreferences({ metric: "cost", windowDays: 7, hiddenProviders: ["codex"] });
29 expect(readUsagePagePreferences().hiddenProviders).toEqual(["codex"]);
30
31 values.set(key, '{"metric":"tokens","windowDays":30}');
32 expect(readUsagePagePreferences()).toEqual({ metric: "tokens", windowDays: 30 });
33 });
34
35 // regression: #10189 an invalid saved usage selection was never replaced
36 it.each([
37 "not-json",
38 '{"metric":"unknown","windowDays":7}',
39 '{"metric":"cost","windowDays":365}',
40 ])("replaces invalid preferences on the next save: %s", (value) => {
41 values.set(key, value);
42 expect(readUsagePagePreferences()).toEqual({ metric: "limits", windowDays: 30 });
43 saveUsagePagePreferences({ metric: "tokens", windowDays: 7 });
44 expect(readUsagePagePreferences()).toEqual({ metric: "tokens", windowDays: 7 });
45 });
46});