Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageProgress.test.ts

148 lines5.1 KB

1import {
2 USAGE_CONTRACT_VERSION,
3 UsageDay,
4 type UsageProviderKind,
5 type UsageSummary,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8​
9import { usageEnvironmentProgress, usageLoadingState, usageProgress } from "./usageProgress.ts";
10​
11function summary(refreshing: readonly UsageProviderKind[] = []): UsageSummary {
12 return {
13 contractVersion: USAGE_CONTRACT_VERSION,
14 readAt: "2026-09-05T12:00:00Z",
15 timeZone: "UTC",
16 sinceDay: UsageDay.make("2026-09-05"),
17 untilDay: UsageDay.make("2026-09-05"),
18 buckets: [],
19 sources: (["codex", ...refreshing] as const).map((provider) => ({
20 fingerprint: { hostId: "host", provider, resolvedHomePath: "/home", volumeId: "1:1" },
21 status: "ok" as const,
22 scannedFiles: 1,
23 skippedFiles: 0,
24 malformedRecords: 0,
25 distinctSessions: 1,
26 message: null,
27 ...(provider === "codex" ? {} : { refreshing: true as const }),
28 })),
29 pricing: { status: "fresh", source: "test", fetchedAt: null, knownModels: 1 },
30 scanDurationMs: 1,
31 };
33​
34function environment(
35 label: string,
36 overrides: Partial<{
37 isConnected: boolean;
38 isPending: boolean;
39 error: string | null;
40 summary: UsageSummary | null;
41 }> = {},
42) {
43 return {
44 label,
45 isConnected: true,
46 isPending: false,
47 error: null as string | null,
48 summary: summary(),
49 ...overrides,
50 };
52​
53const options = { providerLabel: (provider: UsageProviderKind) => `${provider} label` };
54​
55describe("usageProgress", () => {
56 it("dims a lone environment while it refetches over its old totals", () => {
57 expect(usageProgress([environment("a", { isPending: true })], options)).toEqual({
58 dimmed: true,
59 label: "Updating…",
60 });
61 expect(usageProgress([environment("a")], options)).toEqual({ dimmed: false, label: null });
62 });
63​
64 it("undims once one device answers and names the slow one", () => {
65 const waiting = [environment("a", { isPending: true }), environment("b", { isPending: true })];
66 expect(usageProgress(waiting, options)).toEqual({
67 dimmed: true,
68 label: "Updating 2 environments…",
69 });
70 expect(
71 usageProgress([environment("a"), ...waiting.slice(1), environment("c")], options),
72 ).toEqual({ dimmed: false, label: "Updating b…" });
73 expect(
74 usageProgress(
75 [environment("a"), environment("b", { summary: null, isPending: true })],
76 options,
77 ),
78 ).toEqual({ dimmed: false, label: "Updating b…" });
79 });
80​
81 it("names a slow source without dimming once its environment answers from cache", () => {
82 const partway = environment("a", { isPending: true, summary: summary(["cursor"]) });
83 expect(usageEnvironmentProgress(partway)).toEqual({ phase: "partway", providers: ["cursor"] });
84 expect(usageProgress([partway, environment("b")], options)).toEqual({
85 dimmed: false,
86 label: "Updating cursor label…",
87 });
88 expect(
89 usageProgress(
90 [partway, environment("b", { isPending: true, summary: summary(["cursor", "grok"]) })],
91 options,
92 ),
93 ).toEqual({ dimmed: false, label: "Updating 2 providers…" });
94 expect(usageProgress([{ ...partway, isPending: false }], options)).toEqual({
95 dimmed: false,
96 label: null,
97 });
98 });
99​
100 it("ignores reconnecting and failed environments", () => {
101 const ignored = [
102 environment("reconnecting", { isPending: true, isConnected: false }),
103 environment("failed", { error: "Offline", summary: null }),
104 ];
105 expect(usageProgress([environment("a"), ...ignored], options)).toEqual({
106 dimmed: false,
107 label: null,
108 });
109 expect(
110 usageProgress(
111 [environment("a"), ...ignored, environment("slow", { summary: null, isPending: true })],
112 options,
113 ),
114 ).toEqual({ dimmed: false, label: "Updating slow…" });
115 expect(usageEnvironmentProgress(ignored[0]!)).toEqual({ phase: "inactive" });
116 });
117​
118 it("dims every answered environment while a manual refresh runs", () => {
119 expect(
120 usageProgress([environment("a"), environment("b")], { ...options, refreshing: true }),
121 ).toEqual({ dimmed: true, label: "Updating 2 environments…" });
122 });
123});
124​
125describe("usageLoadingState", () => {
126 it("marks only refreshing providers while the rest have answered", () => {
127 const state = usageLoadingState([
128 environment("a", { isPending: true, summary: summary(["cursor"]) }),
129 environment("b"),
130 ]);
131 expect(state).toEqual({ partial: true, everyProvider: false, providers: new Set(["cursor"]) });
132 });
133​
134 it("marks every provider while an environment has not answered", () => {
135 const state = usageLoadingState([environment("a"), environment("b", { summary: null })]);
136 expect(state.everyProvider).toBe(true);
137 expect(state.partial).toBe(true);
138 });
139​
140 it("ignores environments that cannot answer and settles once all have", () => {
141 const state = usageLoadingState([
142 environment("a"),
143 environment("b", { summary: null, isConnected: false }),
144 environment("c", { summary: null, error: "Offline" }),
145 ]);
146 expect(state).toEqual({ partial: false, everyProvider: false, providers: new Set() });
147 });
148});