| 1 | import { |
| 2 | EnvironmentId, |
| 3 | ProviderDriverKind, |
| 4 | ProviderInstanceId, |
| 5 | UsageLimitSourceId, |
| 6 | type ServerProvider, |
| 7 | type ServerProviderUsageWindow, |
| 8 | type UsageLimitSourceSnapshots, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { describe, expect, it } from "vite-plus/test"; |
| 11 | |
| 12 | import { sidebarUsageProfiles } from "./sidebarUsageProfiles"; |
| 13 | |
| 14 | const checkedAt = "2026-10-15T12:00:00.000Z"; |
| 15 | const session: ServerProviderUsageWindow = { |
| 16 | id: "five_hour", |
| 17 | kind: "session", |
| 18 | label: "Session", |
| 19 | usedPercent: 20, |
| 20 | }; |
| 21 | |
| 22 | function provider(overrides: Partial<ServerProvider> = {}): ServerProvider { |
| 23 | return { |
| 24 | instanceId: ProviderInstanceId.make("personal"), |
| 25 | driver: ProviderDriverKind.make("claudeAgent"), |
| 26 | displayName: "Personal", |
| 27 | accentColor: "#abc123", |
| 28 | enabled: true, |
| 29 | installed: true, |
| 30 | version: null, |
| 31 | status: "ready", |
| 32 | auth: { status: "authenticated", email: "person@example.com" }, |
| 33 | checkedAt, |
| 34 | models: [], |
| 35 | slashCommands: [], |
| 36 | skills: [], |
| 37 | usageLimits: { checkedAt, windows: [session] }, |
| 38 | ...overrides, |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | function environment( |
| 43 | providers: readonly ServerProvider[], |
| 44 | phase: "connected" | "offline" = "connected", |
| 45 | usageLimitSources: UsageLimitSourceSnapshots = [], |
| 46 | ) { |
| 47 | return { |
| 48 | entry: { target: { label: "Laptop" } }, |
| 49 | connection: { phase }, |
| 50 | serverConfig: { providers, usageLimitSources }, |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | describe("sidebar usage profiles", () => { |
| 55 | it("summarizes the most constrained window and preserves native identity", () => { |
| 56 | const weekly = { ...session, id: "seven_day", kind: "weekly" as const, usedPercent: 55 }; |
| 57 | const result = sidebarUsageProfiles( |
| 58 | new Map([ |
| 59 | [ |
| 60 | EnvironmentId.make("laptop"), |
| 61 | environment([provider({ usageLimits: { checkedAt, windows: [session, weekly] } })]), |
| 62 | ], |
| 63 | ]), |
| 64 | ); |
| 65 | |
| 66 | expect(result).toHaveLength(1); |
| 67 | expect(result[0]).toMatchObject({ |
| 68 | remainingPercent: 45, |
| 69 | window: weekly, |
| 70 | account: { displayName: "Personal", accentColor: "#abc123", limits: { checkedAt } }, |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | it("excludes Fable by id or label only for Claude", () => { |
| 75 | const windows = [ |
| 76 | session, |
| 77 | { ...session, id: "seven_day_fable", usedPercent: 100 }, |
| 78 | { ...session, id: "seven_day_overage_included", label: "Weekly · Fable", usedPercent: 99 }, |
| 79 | ]; |
| 80 | const profiles = sidebarUsageProfiles( |
| 81 | new Map([ |
| 82 | [ |
| 83 | EnvironmentId.make("laptop"), |
| 84 | environment([ |
| 85 | provider({ usageLimits: { checkedAt, windows } }), |
| 86 | provider({ |
| 87 | instanceId: ProviderInstanceId.make("codex"), |
| 88 | driver: ProviderDriverKind.make("codex"), |
| 89 | usageLimits: { checkedAt, windows }, |
| 90 | }), |
| 91 | ]), |
| 92 | ], |
| 93 | ]), |
| 94 | ); |
| 95 | |
| 96 | expect(profiles.map((profile) => profile.remainingPercent)).toEqual([80, 0]); |
| 97 | }); |
| 98 | |
| 99 | it("keeps Fable-only and failed profiles visible with an unknown ring", () => { |
| 100 | const profiles = sidebarUsageProfiles( |
| 101 | new Map([ |
| 102 | [ |
| 103 | EnvironmentId.make("laptop"), |
| 104 | environment([ |
| 105 | provider({ |
| 106 | usageLimits: { checkedAt, windows: [{ ...session, id: "seven_day_fable" }] }, |
| 107 | }), |
| 108 | provider({ |
| 109 | instanceId: ProviderInstanceId.make("unreadable"), |
| 110 | auth: { status: "authenticated", email: "other@example.com" }, |
| 111 | usageLimits: { |
| 112 | checkedAt, |
| 113 | windows: [session], |
| 114 | unavailable: { reason: "probeFailed" }, |
| 115 | }, |
| 116 | }), |
| 117 | ]), |
| 118 | ], |
| 119 | ]), |
| 120 | ); |
| 121 | |
| 122 | expect(profiles).toHaveLength(2); |
| 123 | expect(profiles.map(({ remainingPercent, window }) => ({ remainingPercent, window }))).toEqual([ |
| 124 | { remainingPercent: null, window: null }, |
| 125 | { remainingPercent: null, window: null }, |
| 126 | ]); |
| 127 | }); |
| 128 | |
| 129 | it("does not present cached offline usage as a full ring", () => { |
| 130 | const profiles = sidebarUsageProfiles( |
| 131 | new Map([ |
| 132 | [ |
| 133 | EnvironmentId.make("laptop"), |
| 134 | environment( |
| 135 | [provider({ usageLimits: { checkedAt, windows: [{ ...session, usedPercent: 0 }] } })], |
| 136 | "offline", |
| 137 | ), |
| 138 | ], |
| 139 | ]), |
| 140 | ); |
| 141 | |
| 142 | expect(profiles[0]?.remainingPercent).toBeNull(); |
| 143 | }); |
| 144 | |
| 145 | it.each([ |
| 146 | { phase: "connected" as const, error: undefined, remaining: 80 }, |
| 147 | { phase: "connected" as const, error: "Could not refresh", remaining: null }, |
| 148 | { phase: "offline" as const, error: undefined, remaining: null }, |
| 149 | ])( |
| 150 | "represents hub-only profiles in $phase with source error $error", |
| 151 | ({ phase, error, remaining }) => { |
| 152 | const profiles = sidebarUsageProfiles( |
| 153 | new Map([ |
| 154 | [ |
| 155 | EnvironmentId.make("laptop"), |
| 156 | environment([], phase, [ |
| 157 | { |
| 158 | id: UsageLimitSourceId.make("hub"), |
| 159 | kind: "cliproxy", |
| 160 | label: "Hub", |
| 161 | checkedAt, |
| 162 | error, |
| 163 | accounts: [ |
| 164 | { |
| 165 | id: "profile.json", |
| 166 | driver: ProviderDriverKind.make("claudeAgent"), |
| 167 | email: "person@example.com", |
| 168 | usageLimits: { checkedAt, windows: [session] }, |
| 169 | }, |
| 170 | ], |
| 171 | }, |
| 172 | ]), |
| 173 | ], |
| 174 | ]), |
| 175 | ); |
| 176 | |
| 177 | expect(profiles).toHaveLength(1); |
| 178 | expect(profiles[0]?.remainingPercent).toBe(remaining); |
| 179 | expect(profiles[0]?.account).toMatchObject({ displayName: null, sourceLabel: "Hub" }); |
| 180 | }, |
| 181 | ); |
| 182 | |
| 183 | it("deduplicates native, offline, and hub copies while retaining the connected snapshot", () => { |
| 184 | const profiles = sidebarUsageProfiles( |
| 185 | new Map([ |
| 186 | [EnvironmentId.make("laptop"), environment([provider()], "offline")], |
| 187 | [ |
| 188 | EnvironmentId.make("desktop"), |
| 189 | { |
| 190 | ...environment([provider()]), |
| 191 | serverConfig: { |
| 192 | providers: [provider()], |
| 193 | usageLimitSources: [ |
| 194 | { |
| 195 | id: UsageLimitSourceId.make("hub"), |
| 196 | kind: "cliproxy" as const, |
| 197 | label: "Hub", |
| 198 | checkedAt, |
| 199 | accounts: [ |
| 200 | { |
| 201 | id: "personal.json", |
| 202 | driver: ProviderDriverKind.make("claudeAgent"), |
| 203 | email: "PERSON@example.com", |
| 204 | usageLimits: { |
| 205 | checkedAt: "2026-10-15T12:01:00.000Z", |
| 206 | windows: [{ ...session, usedPercent: 60 }], |
| 207 | }, |
| 208 | }, |
| 209 | ], |
| 210 | }, |
| 211 | ], |
| 212 | }, |
| 213 | }, |
| 214 | ], |
| 215 | ]), |
| 216 | ); |
| 217 | |
| 218 | expect(profiles).toHaveLength(1); |
| 219 | expect(profiles[0]).toMatchObject({ |
| 220 | remainingPercent: 40, |
| 221 | account: { |
| 222 | displayName: "Personal", |
| 223 | environments: [ |
| 224 | { environmentId: "laptop", label: "Laptop" }, |
| 225 | { environmentId: "desktop", label: "Laptop" }, |
| 226 | ], |
| 227 | }, |
| 228 | }); |
| 229 | }); |
| 230 | |
| 231 | it("does not synthesize a refill after the reported reset time", () => { |
| 232 | const profiles = sidebarUsageProfiles( |
| 233 | new Map([ |
| 234 | [ |
| 235 | EnvironmentId.make("laptop"), |
| 236 | environment([ |
| 237 | provider({ |
| 238 | usageLimits: { |
| 239 | checkedAt, |
| 240 | windows: [{ ...session, usedPercent: 100, resetsAt: checkedAt }], |
| 241 | }, |
| 242 | }), |
| 243 | ]), |
| 244 | ], |
| 245 | ]), |
| 246 | ); |
| 247 | |
| 248 | expect(profiles[0]?.remainingPercent).toBe(0); |
| 249 | }); |
| 250 | |
| 251 | it("keeps workspace quotas separate and omits disabled or unsupported profiles", () => { |
| 252 | const profiles = sidebarUsageProfiles( |
| 253 | new Map([ |
| 254 | [ |
| 255 | EnvironmentId.make("laptop"), |
| 256 | environment([ |
| 257 | provider({ |
| 258 | auth: { status: "authenticated", email: "person@example.com", workspaceId: "one" }, |
| 259 | }), |
| 260 | provider({ |
| 261 | instanceId: ProviderInstanceId.make("two"), |
| 262 | auth: { status: "authenticated", email: "person@example.com", workspaceId: "two" }, |
| 263 | }), |
| 264 | provider({ instanceId: ProviderInstanceId.make("disabled"), enabled: false }), |
| 265 | provider({ |
| 266 | instanceId: ProviderInstanceId.make("api-key"), |
| 267 | usageLimits: { checkedAt, windows: [], unavailable: { reason: "unsupported" } }, |
| 268 | }), |
| 269 | ]), |
| 270 | ], |
| 271 | ]), |
| 272 | ); |
| 273 | |
| 274 | expect(profiles).toHaveLength(2); |
| 275 | }); |
| 276 | }); |