|
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + USAGE_CONTRACT_VERSION, |
| 4 | + UsageDay, |
| 5 | + type UsageSummary, |
| 6 | + type UsageSummaryInput, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import { expect, it, vi } from "vite-plus/test"; |
| 9 | + |
| 10 | +import { render } from "../../test/render"; |
| 11 | +import { useUsage, type EnvironmentUsageStatus } from "./usage"; |
| 12 | + |
| 13 | +const state = vi.hoisted(() => ({ environments: [] as readonly EnvironmentUsageStatus[] })); |
| 14 | + |
| 15 | +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => state.environments })); |
| 16 | +vi.mock("@t3tools/client-runtime/state/usage", () => ({ |
| 17 | + needsCursorKeychainAccess: () => false, |
| 18 | + refreshUsage: vi.fn(), |
| 19 | +})); |
| 20 | +vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} })); |
| 21 | +vi.mock("./presentation", () => ({ environmentPresentations: {} })); |
| 22 | +vi.mock("./server", () => ({ serverEnvironment: {} })); |
| 23 | +vi.mock("./session", () => ({ environmentSession: {}, readEnvironmentScope: vi.fn() })); |
| 24 | + |
| 25 | +function setup() { |
| 26 | + const window: UsageSummaryInput = { |
| 27 | + sinceDay: UsageDay.make("2026-10-16"), |
| 28 | + untilDay: UsageDay.make("2026-10-23"), |
| 29 | + timeZone: "UTC", |
| 30 | + resolution: "day", |
| 31 | + }; |
| 32 | + const summary: UsageSummary = { |
| 33 | + contractVersion: USAGE_CONTRACT_VERSION, |
| 34 | + ...window, |
| 35 | + readAt: "2026-10-23T12:00:00.000Z", |
| 36 | + buckets: [], |
| 37 | + sources: [], |
| 38 | + pricing: { status: "cached", source: "test", fetchedAt: null, knownModels: 0 }, |
| 39 | + scanDurationMs: 0, |
| 40 | + }; |
| 41 | + const answered: EnvironmentUsageStatus = { |
| 42 | + environmentId: EnvironmentId.make("test"), |
| 43 | + label: "Test", |
| 44 | + isPending: false, |
| 45 | + isConnected: true, |
| 46 | + canReadDiagnostics: true, |
| 47 | + error: null, |
| 48 | + summary, |
| 49 | + needsCursorKeychainAccess: false, |
| 50 | + }; |
| 51 | + state.environments = [answered]; |
| 52 | + return { window, answered }; |
| 53 | +} |
| 54 | + |
| 55 | +function UsageProbe({ window }: { readonly window: UsageSummaryInput }) { |
| 56 | + const usage = useUsage(window); |
| 57 | + return ( |
| 58 | + <output> |
| 59 | + {usage.shown === null |
| 60 | + ? "Loading usage" |
| 61 | + : `${usage.shown.window.resolution ?? "day"} usage: ${usage.selectedEnvironments[0]?.error ?? "ready"}`} |
| 62 | + </output> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +it.each(["day", "hour"] as const)( |
| 67 | + "drops the previous view when switching from %s resolution, including on failure", |
| 68 | + async (resolution) => { |
| 69 | + const { window, answered } = setup(); |
| 70 | + const initial = { ...window, resolution }; |
| 71 | + await using screen = await render(<UsageProbe window={initial} />); |
| 72 | + await expect.element(screen.getByText(`${resolution} usage: ready`)).toBeVisible(); |
| 73 | + |
| 74 | + const next = { ...window, resolution: resolution === "day" ? "hour" : "day" } as const; |
| 75 | + state.environments = [{ ...answered, summary: null, isPending: true }]; |
| 76 | + await screen.rerender(<UsageProbe window={next} />); |
| 77 | + await expect.element(screen.getByText("Loading usage")).toBeVisible(); |
| 78 | + |
| 79 | + state.environments = [{ ...answered, summary: null, error: "unavailable" }]; |
| 80 | + await screen.rerender(<UsageProbe window={next} />); |
| 81 | + await expect.element(screen.getByText(`${next.resolution} usage: unavailable`)).toBeVisible(); |
| 82 | + |
| 83 | + state.environments = [answered]; |
| 84 | + await screen.rerender(<UsageProbe window={next} />); |
| 85 | + await expect.element(screen.getByText(`${next.resolution} usage: ready`)).toBeVisible(); |
| 86 | + }, |
| 87 | +); |
| 88 | + |
| 89 | +it("keeps the previous view while another window of the same resolution loads", async () => { |
| 90 | + const { window, answered } = setup(); |
| 91 | + await using screen = await render(<UsageProbe window={window} />); |
| 92 | + await expect.element(screen.getByText("day usage: ready")).toBeVisible(); |
| 93 | + |
| 94 | + state.environments = [{ ...answered, summary: null, isPending: true }]; |
| 95 | + await screen.rerender( |
| 96 | + <UsageProbe window={{ ...window, sinceDay: UsageDay.make("2026-10-01") }} />, |
| 97 | + ); |
| 98 | + await expect.element(screen.getByText("day usage: ready")).toBeVisible(); |
| 99 | +}); |
0 commit comments