The best way to control Claude Code, Codex and more from any computer or phone

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