Open History to see the last commit
1import {
2 ProviderDriverKind,
3 ProviderInstanceId,
4 type ServerProvider,
5 type ServerProviderUsageLimits,
6 type UsageLimitSourceSnapshot,
7 UsageLimitSourceId,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "vite-plus/test";
10
11import {
12 poolSources,
13 resolveThreadDetailsLimits,
14 resolveThreadHttpQuotas,
15} from "./ThreadDetailsUsage.logic";
16
17const now = Date.parse("2026-10-30T12:00:00.000Z");
18const checkedAt = "2026-10-30T11:59:00.000Z";
19const claude = ProviderDriverKind.make("claudeAgent");
20const codex = ProviderDriverKind.make("codex");
21const instanceId = ProviderInstanceId.make("claudeAgent");
22
23function limitsAt(usedPercent: number): ServerProviderUsageLimits {
24 return {
25 checkedAt,
26 windows: [
27 {
28 id: "five_hour",
29 kind: "session",
30 label: "Session",
31 usedPercent,
32 windowDurationMins: 300,
33 resetsAt: "2026-10-30T13:17:00.000Z",
34 },
35 ],
36 };
37}
38
39const unsupported: ServerProviderUsageLimits = {
40 checkedAt,
41 windows: [],
42 unavailable: { reason: "unsupported" },
43};
44
45function claudeInstance(usageLimits: ServerProviderUsageLimits): ServerProvider {
46 return {
47 instanceId,
48 driver: claude,
49 enabled: true,
50 installed: true,
51 version: null,
52 status: "ready",
53 auth: {
54 status: "authenticated",
55 type: "customEndpoint",
56 label: "Custom API endpoint",
57 endpoint: "http://127.0.0.1:8317",
58 },
59 checkedAt,
60 models: [],
61 slashCommands: [],
62 skills: [],
63 usageLimits,
64 };
65}
66
67interface HubAccountInput {
68 readonly id: string;
69 readonly driver?: ProviderDriverKind;
70 readonly models?: readonly string[];
71 readonly limits: ServerProviderUsageLimits;
72}
73
74function hub(
75 label: string,
76 accounts: readonly HubAccountInput[],
77 origin = "http://127.0.0.1:8317",
78): UsageLimitSourceSnapshot {
79 return {
80 id: UsageLimitSourceId.make(label),
81 kind: "cliproxy",
82 label,
83 origin,
84 checkedAt,
85 accounts: accounts.map((account) => ({
86 id: account.id,
87 driver: account.driver ?? claude,
88 ...(account.models ? { models: account.models } : {}),
89 usageLimits: account.limits,
90 })),
91 };
92}
93
94function shown(
95 own: ServerProviderUsageLimits,
96 sources: readonly UsageLimitSourceSnapshot[],
97 model: string | null,
98) {
99 const resolved = resolveThreadDetailsLimits(
100 instanceId,
101 model,
102 [claudeInstance(own)],
103 sources,
104 now,
105 );
106 if (resolved?.kind !== "pool") return resolved;
107 return {
108 kind: resolved.kind,
109 rows: resolved.windows.map((window) => ({
110 label: window.label,
111 left: window.remainingPercent,
112 accounts: window.members.length,
113 from: poolSources(window),
114 })),
115 };
116}
117
118describe("resolveThreadDetailsLimits", () => {
119 it("keeps an instance's failed reading instead of a hub it is not sent to", () => {
120 const failed: ServerProviderUsageLimits = {
121 checkedAt,
122 windows: [],
123 unavailable: { reason: "probeFailed" },
124 };
125 const direct = { ...claudeInstance(failed), auth: { status: "authenticated" as const } };
126 const elsewhere = claudeInstance(unsupported);
127 const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]);
128 const otherHub = hub("Other", [{ id: "a.json", limits: limitsAt(20) }], "http://hub");
129 expect(resolveThreadDetailsLimits(instanceId, null, [direct], [readableHub], now)).toEqual({
130 kind: "instance",
131 limits: failed,
132 });
133 expect(resolveThreadDetailsLimits(instanceId, null, [elsewhere], [otherHub], now)).toEqual({
134 kind: "instance",
135 limits: unsupported,
136 });
137 });
138
139 it("pools only the accounts the hub routes the model to, even of another driver", () => {
140 const sources = [
141 hub("Hub", [
142 { id: "a.json", models: ["claude-opus-5-5"], limits: limitsAt(10) },
143 { id: "c.json", driver: codex, models: ["gpt-5.6-sol"], limits: limitsAt(30) },
144 ]),
145 ];
146 expect(shown(unsupported, sources, "gpt-5.6-sol[1m]")).toEqual({
147 kind: "pool",
148 rows: [{ label: "Session", left: 70, accounts: 1, from: { hubs: ["Hub"], signedIn: false } }],
149 });
150 });
151});
152
153describe("resolveThreadHttpQuotas", () => {
154 it("shows only the quota accounts bound to the thread's provider instance", () => {
155 const window = { id: "monthly", kind: "monthly", label: "Monthly", usedPercent: 35 } as const;
156 const source: UsageLimitSourceSnapshot = {
157 id: UsageLimitSourceId.make("quota"),
158 kind: "http",
159 label: "Quota server",
160 checkedAt,
161 accounts: [],
162 quotaAccounts: [
163 { id: "bound", label: "Work plan", providerInstanceId: instanceId, windows: [window] },
164 { id: "unbound", label: "Unbound", windows: [window] },
165 {
166 id: "other",
167 label: "Other plan",
168 providerInstanceId: ProviderInstanceId.make("other"),
169 windows: [window],
170 },
171 ],
172 };
173 expect(
174 resolveThreadHttpQuotas(instanceId, [source]).map((quota) => quota.accountLabel),
175 ).toEqual(["Work plan"]);
176 expect(resolveThreadHttpQuotas(null, [source])).toEqual([]);
177 });
178});