The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { it as effectIt } from "@effect/vitest"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { HttpClient, HttpClientResponse, UrlParams } from "effect/http"; |
| 9 | import { describe, expect, it } from "vite-plus/test"; |
| 10 | |
| 11 | import * as ClaudeResetCredits from "./claudeResetCredits.ts"; |
| 12 | |
| 13 | const NOW = Date.parse("2026-09-22T12:00:00.000Z"); |
| 14 | const grant = (overrides: Record<string, unknown>) => ({ |
| 15 | id: "grant_a", |
| 16 | resets_left: 1, |
| 17 | usable_now: true, |
| 18 | ...overrides, |
| 19 | }); |
| 20 | |
| 21 | const writeLogin = Effect.gen(function* () { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const path = yield* Path.Path; |
| 24 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 25 | yield* fs.writeFileString( |
| 26 | path.join(directory, ".credentials.json"), |
| 27 | '{"claudeAiOauth":{"accessToken":"oauth-token"}}', |
| 28 | ); |
| 29 | const accountConfigPath = path.join(directory, ".claude.json"); |
| 30 | yield* fs.writeFileString(accountConfigPath, '{"oauthAccount":{"organizationUuid":"org-1"}}'); |
| 31 | return { configDir: directory, accountConfigPath }; |
| 32 | }); |
| 33 | |
| 34 | const respond = (status: number, body: unknown) => |
| 35 | HttpClient.make((request) => |
| 36 | Effect.succeed(HttpClientResponse.fromWeb(request, Response.json(body, { status }))), |
| 37 | ); |
| 38 | const refuseRequests = HttpClient.make(() => Effect.die("must not send a request")); |
| 39 | |
| 40 | describe("claudeResetCreditsToContract", () => { |
| 41 | it("counts live grants and pins the next usable one", () => { |
| 42 | expect( |
| 43 | ClaudeResetCredits.claudeResetCreditsToContract( |
| 44 | { |
| 45 | eligible: true, |
| 46 | next_grant_id: "grant_a", |
| 47 | grants: [ |
| 48 | grant({ resets_left: 2, ends_at: "2026-10-01T00:00:00Z" }), |
| 49 | grant({ id: "paused", paused: true }), |
| 50 | grant({ id: "expired", ends_at: "2026-09-01T00:00:00Z" }), |
| 51 | grant({ id: "garbled", ends_at: "not a date" }), |
| 52 | grant({ id: "date_only", ends_at: "2026-10-01" }), |
| 53 | grant({ id: "impossible", ends_at: "2027-02-30T00:00:00Z" }), |
| 54 | grant({ id: "empty", ends_at: "" }), |
| 55 | grant({ id: "Not Valid" }), |
| 56 | grant({ id: "grant_b", resets_left: 3, usable_now: false }), |
| 57 | ], |
| 58 | }, |
| 59 | NOW, |
| 60 | ), |
| 61 | ).toEqual({ |
| 62 | availableCount: 2, |
| 63 | nextCreditId: "grant_a", |
| 64 | nextExpiresAt: "2026-10-01T00:00:00.000Z", |
| 65 | options: [ |
| 66 | expect.objectContaining({ |
| 67 | creditId: "grant_a", |
| 68 | label: "Full reset", |
| 69 | availableCount: 2, |
| 70 | expiresAt: "2026-10-01T00:00:00.000Z", |
| 71 | }), |
| 72 | ], |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | it("offers each kind of reset by its own grant", () => { |
| 77 | const credits = ClaudeResetCredits.claudeResetCreditsToContract( |
| 78 | { |
| 79 | eligible: true, |
| 80 | next_grant_id: "weekly_a", |
| 81 | grants: [ |
| 82 | grant({ id: "weekly_a", clears: ["seven_day"], ends_at: "2026-12-10T22:00:00Z" }), |
| 83 | grant({ |
| 84 | id: "fable_a", |
| 85 | clears: ["seven_day_overage_included"], |
| 86 | gives_back_weekly: true, |
| 87 | ends_at: "2027-03-09T07:59:00Z", |
| 88 | }), |
| 89 | grant({ id: "fable_b", gives_back_weekly: true, type: "weekly" }), |
| 90 | grant({ id: "session_a", type: "session" }), |
| 91 | grant({ id: "spent", clears: ["seven_day"], resets_left: 0 }), |
| 92 | ], |
| 93 | }, |
| 94 | NOW, |
| 95 | ); |
| 96 | expect( |
| 97 | credits?.options?.map(({ creditId, label, availableCount }) => [ |
| 98 | creditId, |
| 99 | label, |
| 100 | availableCount, |
| 101 | ]), |
| 102 | ).toEqual([ |
| 103 | ["session_a", "5-hour reset", 1], |
| 104 | ["weekly_a", "Weekly reset", 1], |
| 105 | ["fable_a", "Fable reset", 2], |
| 106 | ]); |
| 107 | expect(credits).toMatchObject({ availableCount: 4, nextCreditId: "weekly_a" }); |
| 108 | }); |
| 109 | |
| 110 | it("offers nothing to redeem without a usable next grant or an eligible account", () => { |
| 111 | expect( |
| 112 | ClaudeResetCredits.claudeResetCreditsToContract( |
| 113 | { eligible: true, next_grant_id: "grant_a", grants: [grant({ usable_now: false })] }, |
| 114 | NOW, |
| 115 | ), |
| 116 | ).toEqual({ availableCount: 0 }); |
| 117 | expect( |
| 118 | ClaudeResetCredits.claudeResetCreditsToContract({ eligible: true, grants: [grant({})] }, NOW), |
| 119 | ).toEqual({ |
| 120 | availableCount: 0, |
| 121 | }); |
| 122 | expect( |
| 123 | ClaudeResetCredits.claudeResetCreditsToContract( |
| 124 | { eligible: false, grants: [grant({})] }, |
| 125 | NOW, |
| 126 | ), |
| 127 | ).toBeUndefined(); |
| 128 | expect(ClaudeResetCredits.claudeResetCreditsToContract(undefined, NOW)).toBeUndefined(); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | effectIt.layer(NodeServices.layer)("readClaudeResetCredits", (it) => { |
| 133 | it.effect("reads the grants with the CLI's request", () => |
| 134 | Effect.gen(function* () { |
| 135 | const { configDir } = yield* writeLogin; |
| 136 | const client = HttpClient.make((request) => { |
| 137 | expect(request.method).toBe("GET"); |
| 138 | expect(request.url).toBe("https://api.anthropic.com/api/oauth/usage"); |
| 139 | expect(UrlParams.toString(request.urlParams)).toBe("cedar_ember=1&skip_spend=1"); |
| 140 | expect(request.headers.authorization).toBe("Bearer oauth-token"); |
| 141 | expect(request.headers["anthropic-beta"]).toBe("oauth-2025-04-20"); |
| 142 | expect(request.headers["user-agent"]).toBe("claude-cli/2.1.0 (external, cli)"); |
| 143 | return Effect.succeed( |
| 144 | HttpClientResponse.fromWeb( |
| 145 | request, |
| 146 | Response.json({ |
| 147 | cedar_ember: { eligible: true, next_grant_id: "grant_a", grants: [grant({})] }, |
| 148 | }), |
| 149 | ), |
| 150 | ); |
| 151 | }); |
| 152 | const credits = yield* ClaudeResetCredits.readClaudeResetCredits(configDir, "2.1.0").pipe( |
| 153 | Effect.provideService(HostProcess.Platform, "linux"), |
| 154 | Effect.provideService(HttpClient.HttpClient, client), |
| 155 | ); |
| 156 | expect(credits).toMatchObject({ availableCount: 1, nextCreditId: "grant_a" }); |
| 157 | }), |
| 158 | ); |
| 159 | |
| 160 | it.effect("reads nothing from keychain logins or failed requests", () => |
| 161 | Effect.gen(function* () { |
| 162 | const { configDir } = yield* writeLogin; |
| 163 | const darwin = yield* ClaudeResetCredits.readClaudeResetCredits(configDir, "2.1.0").pipe( |
| 164 | Effect.provideService(HostProcess.Platform, "darwin"), |
| 165 | Effect.provideService(HttpClient.HttpClient, refuseRequests), |
| 166 | ); |
| 167 | const limited = yield* ClaudeResetCredits.readClaudeResetCredits(configDir, "2.1.0").pipe( |
| 168 | Effect.provideService(HostProcess.Platform, "linux"), |
| 169 | Effect.provideService(HttpClient.HttpClient, respond(429, {})), |
| 170 | ); |
| 171 | expect([darwin, limited]).toEqual([undefined, undefined]); |
| 172 | }), |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | const ClaimBody = Schema.fromJsonString( |
| 177 | Schema.Struct({ program: Schema.String, grant_id: Schema.String, request_id: Schema.String }), |
| 178 | ); |
| 179 | const decodeClaimBody = Schema.decodeEffect(ClaimBody); |
| 180 | |
| 181 | const consume = (client: HttpClient.HttpClient, ids = { grantId: "grant_a", requestId: "r-1" }) => |
| 182 | Effect.gen(function* () { |
| 183 | const login = yield* writeLogin; |
| 184 | return yield* ClaudeResetCredits.consumeClaudeResetCredit({ |
| 185 | ...login, |
| 186 | version: "2.1.0", |
| 187 | ...ids, |
| 188 | }).pipe( |
| 189 | Effect.provideService(HostProcess.Platform, "linux"), |
| 190 | Effect.provideService(HttpClient.HttpClient, client), |
| 191 | Effect.result, |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | effectIt.layer(NodeServices.layer)("consumeClaudeResetCredit", (it) => { |
| 196 | it.effect("claims the grant for the organization", () => |
| 197 | Effect.gen(function* () { |
| 198 | const client = HttpClient.make((request) => |
| 199 | Effect.gen(function* () { |
| 200 | expect(request.method).toBe("POST"); |
| 201 | expect(request.url).toBe( |
| 202 | "https://api.anthropic.com/api/organizations/org-1/reset_rate_limits", |
| 203 | ); |
| 204 | expect(request.headers.authorization).toBe("Bearer oauth-token"); |
| 205 | const body = |
| 206 | request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : ""; |
| 207 | expect(yield* decodeClaimBody(body)).toEqual({ |
| 208 | program: "cedar_ember", |
| 209 | grant_id: "grant_a", |
| 210 | request_id: "r-1", |
| 211 | }); |
| 212 | return HttpClientResponse.fromWeb(request, Response.json({ result: "reset" })); |
| 213 | }).pipe(Effect.orDie), |
| 214 | ); |
| 215 | expect(yield* consume(client)).toMatchObject({ _tag: "Success", success: "reset" }); |
| 216 | }), |
| 217 | ); |
| 218 | |
| 219 | it.effect("maps each answer to an outcome or a failure", () => |
| 220 | Effect.gen(function* () { |
| 221 | for (const [result, outcome] of [ |
| 222 | ["not_limited", "nothingToReset"], |
| 223 | ["already_used", "alreadyRedeemed"], |
| 224 | ["ineligible", "noCredit"], |
| 225 | ] as const) { |
| 226 | expect(yield* consume(respond(200, { result }))).toMatchObject({ success: outcome }); |
| 227 | } |
| 228 | for (const client of [ |
| 229 | respond(200, { result: "cooldown" }), |
| 230 | respond(429, {}), |
| 231 | respond(401, {}), |
| 232 | ]) { |
| 233 | const result = yield* consume(client); |
| 234 | expect(result).toMatchObject({ _tag: "Failure" }); |
| 235 | if (result._tag === "Failure") { |
| 236 | expect(ClaudeResetCredits.isSettledClaudeResetCreditFailure(result.failure)).toBe(true); |
| 237 | } |
| 238 | } |
| 239 | for (const client of [respond(500, {}), respond(200, { result: "unavailable" })]) { |
| 240 | const unanswered = yield* consume(client); |
| 241 | expect(unanswered).toMatchObject({ _tag: "Failure" }); |
| 242 | if (unanswered._tag === "Failure") { |
| 243 | expect(ClaudeResetCredits.isSettledClaudeResetCreditFailure(unanswered.failure)).toBe( |
| 244 | false, |
| 245 | ); |
| 246 | } |
| 247 | } |
| 248 | }), |
| 249 | ); |
| 250 | |
| 251 | it.effect("refuses malformed ids without sending anything", () => |
| 252 | Effect.gen(function* () { |
| 253 | for (const ids of [ |
| 254 | { grantId: "Bad Grant", requestId: "r-1" }, |
| 255 | { grantId: "grant_a", requestId: "has space" }, |
| 256 | ]) { |
| 257 | expect(yield* consume(refuseRequests, ids)).toMatchObject({ _tag: "Failure" }); |
| 258 | } |
| 259 | }), |
| 260 | ); |
| 261 | }); |