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

usageLimits.test.ts

181 lines5.7 KB

1import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 collectLimitSources,
6 collectLimitsGroups,
7 elapsedShare,
8 formatResetsIn,
9 limitsNotice,
10 paceOf,
11 providersWithLimits,
12} from "./usageLimits.ts";
13​
14const now = Date.parse("2026-09-03T12:00:00.000Z");
15​
16const window = {
17 id: "five_hour",
18 kind: "session",
19 label: "Session",
20 usedPercent: 40,
21 windowDurationMins: 300,
22 resetsAt: "2026-09-03T14:00:00.000Z",
23} as const;
24​
25function provider(overrides: Partial<ServerProvider>): ServerProvider {
26 return {
27 instanceId: ProviderInstanceId.make("codex"),
28 driver: ProviderDriverKind.make("codex"),
29 enabled: true,
30 installed: true,
31 version: null,
32 status: "ready",
33 auth: { status: "authenticated" },
34 checkedAt: "2026-09-03T11:00:00.000Z",
35 models: [],
36 slashCommands: [],
37 skills: [],
38 ...overrides,
39 };
41​
42describe("pace", () => {
43 it("places the clock three fifths through a five-hour window with two hours left", () => {
44 expect(elapsedShare(window, now)).toBeCloseTo(0.6);
45 expect(paceOf(window, now)).toBe("under");
46 expect(paceOf({ ...window, usedPercent: 62 }, now)).toBe("on");
47 expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead");
48 });
49​
50 it("has no pace without a reset or a duration", () => {
51 expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull();
52 expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull();
53 expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull();
54 });
55​
56 it("phrases the reset as a countdown", () => {
57 expect(formatResetsIn(window, now)).toBe("resets in 2h 0m");
58 expect(formatResetsIn({ ...window, resetsAt: "2026-09-06T15:30:00.000Z" }, now)).toBe(
59 "resets in 3d 3h",
60 );
61 expect(formatResetsIn({ ...window, resetsAt: "2026-09-03T11:00:00.000Z" }, now)).toBe(
62 "resets now",
63 );
64 });
65});
66​
67describe("limitsNotice", () => {
68 it("explains empty bars and passes provider messages through", () => {
69 const checkedAt = "2026-09-03T11:00:00.000Z";
70 expect(limitsNotice({ checkedAt, windows: [window] })).toBeNull();
71 expect(limitsNotice({ checkedAt, windows: [] })).toBe("No limits reported.");
72 expect(limitsNotice({ checkedAt, windows: [], unavailable: { reason: "unsupported" } })).toBe(
73 "This account has no subscription limits.",
74 );
75 expect(
76 limitsNotice({
77 checkedAt,
78 windows: [],
79 unavailable: { reason: "probeFailed", message: "Codex timed out." },
80 }),
81 ).toBe("Codex timed out.");
82 });
83});
84​
85describe("providersWithLimits", () => {
86 it("keeps only usable providers whose driver reports limits at all", () => {
87 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
88 const codex = provider({ usageLimits: limits });
89 expect(
90 providersWithLimits([
91 codex,
92 provider({
93 instanceId: ProviderInstanceId.make("cursor"),
94 driver: ProviderDriverKind.make("cursor"),
95 }),
96 provider({
97 instanceId: ProviderInstanceId.make("off"),
98 enabled: false,
99 usageLimits: limits,
100 }),
101 provider({
102 instanceId: ProviderInstanceId.make("gone"),
103 installed: false,
104 usageLimits: limits,
105 }),
106 provider({
107 instanceId: ProviderInstanceId.make("shadow"),
108 availability: "unavailable",
109 usageLimits: limits,
110 }),
111 ]),
112 ).toEqual([codex]);
113 });
114});
115​
116describe("collectLimitsGroups", () => {
117 it("labels environments only when more than one reports limits", () => {
118 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
119 const codex = provider({ usageLimits: limits });
120 const one = new Map([
121 ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }],
122 [
123 "env-b",
124 { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [provider({})] } },
125 ],
126 ] as const);
127 expect(collectLimitsGroups(one as never).map((group) => group.environmentLabel)).toEqual([
128 null,
129 ]);
130​
131 const two = new Map([
132 ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }],
133 ["env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [codex] } }],
134 ] as const);
135 expect(collectLimitsGroups(two as never).map((group) => group.environmentLabel)).toEqual([
136 "Laptop",
137 "Desktop",
138 ]);
139 });
140});
141​
142describe("collectLimitSources", () => {
143 const source = {
144 id: "cliproxy-hub" as never,
145 kind: "cliproxy" as const,
146 label: "hub",
147 checkedAt: "2026-09-03T11:00:00.000Z",
148 accounts: [],
149 };
150​
151 it("keys sources per environment and names the environment only when several have some", () => {
152 const one = new Map([
153 [
154 "env-a",
155 { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } },
156 ],
157 [
158 "env-b",
159 { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [] } },
160 ],
161 ] as const);
162 expect(collectLimitSources(one as never).map((entry) => [entry.key, entry.label])).toEqual([
163 ["env-a:cliproxy-hub", "hub"],
164 ]);
165​
166 const two = new Map([
167 [
168 "env-a",
169 { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } },
170 ],
171 [
172 "env-b",
173 { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [source] } },
174 ],
175 ] as const);
176 expect(collectLimitSources(two as never).map((entry) => entry.label)).toEqual([
177 "Laptop · hub",
178 "Desktop · hub",
179 ]);
180 });
181});