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

cliproxyApi.test.ts

723 lines25.8 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import { describe, expect, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Schema from "effect/Schema";
6import * as TestClock from "effect/testing/TestClock";
7import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
8​
9import { makeCliproxyApi } from "./cliproxyApi.ts";
10​
11const config = {
12 kind: "cliproxy",
13 url: "http://hub.test:8317",
14 managementKey: "management-secret",
15 enabled: true,
16} as const;
17const accounts = [
18 {
19 id: "first.json",
20 auth_index: "a",
21 provider: "codex",
22 email: "first@example.com",
23 id_token: { chatgpt_account_id: "account-a" },
24 },
25 {
26 id: "second.json",
27 auth_index: "b",
28 provider: "codex",
29 email: "second@example.com",
30 id_token: { chatgpt_account_id: "account-b" },
31 },
32];
33const credit = (id: string, expires_at = "2099-01-01T00:00:00Z") => ({
34 id,
35 expires_at,
36 status: "available",
37 reset_type: "codex_rate_limits",
38});
39const RequestBody = Schema.Struct({
40 auth_index: Schema.String,
41 method: Schema.optional(Schema.String),
42 url: Schema.optional(Schema.String),
43 header: Schema.optional(Schema.Record(Schema.String, Schema.String)),
44 data: Schema.optional(Schema.String),
45});
46type RequestBody = typeof RequestBody.Type;
47const decodeRequest = Schema.decodeUnknownSync(Schema.fromJsonString(RequestBody));
48const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
49​
50function fixture(
51 options: {
52 accounts?: Array<{
53 id: string;
54 auth_index: string;
55 provider: string;
56 email?: string;
57 disabled?: boolean;
58 project_id?: string;
59 id_token?: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string };
60 }>;
61 upstream?: (request: RequestBody) => { status: number; body: unknown };
62 cooldownStatus?: number;
63 } = {},
64) {
65 const requests: Array<{ path: string; body?: RequestBody }> = [];
66 const http = HttpClient.make((request) =>
67 Effect.sync(() => {
68 expect(request.headers.authorization).toBe("Bearer management-secret");
69 const path = new URL(request.url).pathname;
70 const body =
71 request.body._tag === "Uint8Array"
72 ? decodeRequest(new TextDecoder().decode(request.body.body))
73 : undefined;
74 requests.push({ path, ...(body ? { body } : {}) });
75 if (path.endsWith("/auth-files"))
76 return HttpClientResponse.fromWeb(
77 request,
78 Response.json({ files: options.accounts ?? accounts }),
79 );
80 if (path.endsWith("/reset-quota"))
81 return HttpClientResponse.fromWeb(
82 request,
83 Response.json({}, { status: options.cooldownStatus ?? 200 }),
84 );
85 expect(path).toMatch(/\/v0\/management\/api-call$/);
86 expect(body?.header?.Authorization).toBe("Bearer $TOKEN$");
87 const upstream = options.upstream?.(body!) ?? {
88 status: 200,
89 body: body?.url?.endsWith("/consume")
90 ? { code: "reset" }
91 : body?.url?.endsWith("/rate-limit-reset-credits")
92 ? {
93 credits: [
94 credit("later", "2099-02-01T00:00:00Z"),
95 credit("first"),
96 credit("expired", "2000-01-01T00:00:00Z"),
97 { ...credit("used"), status: "redeemed" },
98 ],
99 }
100 : {
101 plan_type: "pro",
102 rate_limit: {
103 secondary_window: {
104 used_percent: 78,
105 reset_at: 4070908800,
106 limit_window_seconds: 604800,
107 },
108 },
109 },
110 };
111 return HttpClientResponse.fromWeb(
112 request,
113 Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }),
114 );
115 }),
116 );
117 return {
118 requests,
119 api: makeCliproxyApi.pipe(
120 Effect.provideService(HttpClient.HttpClient, http),
121 Effect.provide(NodeCrypto.layer),
122 ),
123 };
125​
126const unreachableHub = (
127 fail: (
128 request: Parameters<Parameters<typeof HttpClient.make>[0]>[0],
129 ) => HttpClientError.HttpClientError,
130) =>
131 makeCliproxyApi.pipe(
132 Effect.provideService(
133 HttpClient.HttpClient,
134 HttpClient.make((request) => Effect.fail(fail(request))),
135 ),
136 Effect.provide(NodeCrypto.layer),
137 );
138​
139describe("CLIProxyAPI hub failures", () => {
140 it.effect("names the TLS code when the hub's certificate is not trusted", () =>
141 Effect.gen(function* () {
142 const api = yield* unreachableHub(
143 (request) =>
144 new HttpClientError.HttpClientError({
145 reason: new HttpClientError.TransportError({
146 request,
147 // Node's fetch wraps the TLS error, which carries the code, in a generic TypeError.
148 cause: new TypeError("fetch failed", {
149 cause: Object.assign(new Error("unable to get local issuer certificate"), {
150 code: "UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
151 }),
152 }),
153 }),
154 }),
155 );
156 const error = yield* Effect.flip(api.readAccounts(config));
157 expect(error.detail).toContain("UNABLE_TO_GET_ISSUER_CERT_LOCALLY");
158 expect(error.detail).toContain("NODE_EXTRA_CA_CERTS");
159 }),
160 );
161​
162 // regression: #18168 Limits hub failures name their TLS, HTTP, or timeout cause
163 it.effect("ignores a transport code that is not a short Node error code", () =>
164 Effect.gen(function* () {
165 const api = yield* unreachableHub(
166 (request) =>
167 new HttpClientError.HttpClientError({
168 reason: new HttpClientError.TransportError({
169 request,
170 cause: Object.assign(new Error("odd"), { code: "X".repeat(200) }),
171 }),
172 }),
173 );
174 const error = yield* Effect.flip(api.readAccounts(config));
175 expect(error.detail).toBe("The hub management request failed.");
176 }),
177 );
178​
179 it.effect("names the status, not the body, when the hub rejects the request", () =>
180 Effect.gen(function* () {
181 const test = HttpClient.make((request) =>
182 Effect.succeed(
183 HttpClientResponse.fromWeb(
184 request,
185 Response.json({ error: "management-secret is wrong" }, { status: 401 }),
186 ),
187 ),
188 );
189 const api = yield* makeCliproxyApi.pipe(
190 Effect.provideService(HttpClient.HttpClient, test),
191 Effect.provide(NodeCrypto.layer),
192 );
193 const error = yield* Effect.flip(api.readAccounts(config));
194 expect(error.detail).toContain("401");
195 expect(error.detail).not.toContain("management-secret");
196 }),
197 );
198});
199​
200describe("CLIProxyAPI built-in management API", () => {
201 it.effect(
202 "reads both accounts and their earliest unexpired credits without plugin endpoints",
203 () =>
204 Effect.gen(function* () {
205 yield* TestClock.setTime(1788710400000);
206 const test = fixture();
207 const api = yield* test.api;
208 const result = yield* api.readAccounts(config);
209 expect(result.map((account) => account.usageLimits.resetCredits)).toEqual([
210 { availableCount: 2, nextCreditId: "first", nextExpiresAt: "2099-01-01T00:00:00.000Z" },
211 { availableCount: 2, nextCreditId: "first", nextExpiresAt: "2099-01-01T00:00:00.000Z" },
212 ]);
213 expect(result[0]?.usageLimits.windows).toMatchObject([
214 { id: "secondary", usedPercent: 78, kind: "weekly" },
215 ]);
216 const calls = test.requests.filter((request) => request.body?.url);
217 expect(calls.map((request) => request.body?.auth_index).sort()).toEqual([
218 "a",
219 "a",
220 "b",
221 "b",
222 ]);
223 expect(
224 calls.find((request) => request.body?.auth_index === "b")?.body?.header?.[
225 "Chatgpt-Account-Id"
226 ],
227 ).toBe("account-b");
228 }),
229 );
230​
231 it.effect("resolves a missing usage plan from current, then legacy, token plans", () =>
232 Effect.gen(function* () {
233 const test = fixture({
234 accounts: [
235 { ...accounts[0]!, id_token: { chatgpt_account_id: "account-a", plan_type: "free" } },
236 {
237 ...accounts[1]!,
238 id_token: {
239 chatgpt_account_id: "account-b",
240 plan_type: "go",
241 chatgpt_plan_type: "plus",
242 },
243 },
244 {
245 ...accounts[1]!,
246 id: "third.json",
247 auth_index: "c",
248 id_token: { chatgpt_account_id: "account-c", chatgpt_plan_type: "free" },
249 },
250 ],
251 upstream: (request) => ({
252 status: 200,
253 body: {
254 ...(request.auth_index === "a" ? { plan_type: "plus" } : {}),
255 rate_limit: { primary_window: { used_percent: 12 } },
256 },
257 }),
258 });
259 const api = yield* test.api;
260 const result = yield* api.readAccounts(config);
261 expect(result.map((account) => [account.plan, account.usageLimits.windows[0]?.kind])).toEqual(
262 [
263 ["ChatGPT Plus Subscription", "session"],
264 ["ChatGPT Go Subscription", "monthly"],
265 ["ChatGPT Free Subscription", "monthly"],
266 ],
267 );
268 }),
269 );
270​
271 it.effect.each([
272 "https://tools.test/cliproxy",
273 "https://tools.test/cliproxy/",
274 "https://tools.test/cliproxy/management.html",
275 "https://tools.test/cliproxy/management.html/",
276 "https://tools.test/cliproxy/MANAGEMENT.HTML",
277 "https://tools.test/cliproxy/management.html?tab=accounts",
278 "https://tools.test/cliproxy/management.html#accounts",
279 "https://tools.test/cliproxy/management.html/?tab=accounts",
280 "https://tools.test/cliproxy/management.html/#accounts",
281 ])("keeps the path of a hub served under a prefix: %s", (url) =>
282 Effect.gen(function* () {
283 const test = fixture();
284 const api = yield* test.api;
285 yield* api.readAccounts({ ...config, url });
286 expect(test.requests.length).toBeGreaterThan(0);
287 expect(
288 test.requests.every((request) => request.path.startsWith("/cliproxy/v0/management/")),
289 ).toBe(true);
290 }),
291 );
292​
293 it.effect("preserves a hub path that only ends with the panel filename", () =>
294 Effect.gen(function* () {
295 const test = fixture();
296 const api = yield* test.api;
297 yield* api.readAccounts({ ...config, url: "https://tools.test/team-management.html" });
298 expect(test.requests.length).toBeGreaterThan(0);
299 expect(
300 test.requests.every((request) =>
301 request.path.startsWith("/team-management.html/v0/management/"),
302 ),
303 ).toBe(true);
304 }),
305 );
306​
307 it.effect("keeps usage when the credits endpoint fails", () =>
308 Effect.gen(function* () {
309 const test = fixture({
310 upstream: (request) =>
311 request.url?.endsWith("rate-limit-reset-credits")
312 ? { status: 503, body: { token: "do-not-publish" } }
313 : { status: 200, body: { rate_limit: { primary_window: { used_percent: 12 } } } },
314 });
315 const api = yield* test.api;
316 const result = yield* api.readAccounts(config);
317 expect(result[0]?.usageLimits.windows[0]?.usedPercent).toBe(12);
318 expect(result[0]?.usageLimits.resetCredits).toBeUndefined();
319 }),
320 );
321​
322 it.effect("isolates a failed account and never publishes upstream error bodies", () =>
323 Effect.gen(function* () {
324 const test = fixture({
325 upstream: (request) =>
326 request.auth_index === "a"
327 ? { status: 401, body: { token: "do-not-publish" } }
328 : {
329 status: 200,
330 body: request.url?.endsWith("rate-limit-reset-credits")
331 ? { credits: [] }
332 : { rate_limit: { primary_window: { used_percent: 12 } } },
333 },
334 });
335 const api = yield* test.api;
336 const result = yield* api.readAccounts(config);
337 expect(result[0]?.usageLimits.unavailable?.reason).toBe("probeFailed");
338 expect(result[1]?.usageLimits.windows[0]?.usedPercent).toBe(12);
339 expect(encodeJson(result)).not.toContain("do-not-publish");
340 }),
341 );
342​
343 it.effect("maps Claude scoped windows without a scheduler plugin", () =>
344 Effect.gen(function* () {
345 const test = fixture({
346 accounts: [{ ...accounts[0]!, provider: "claude" }],
347 // The usage endpoint answers 429 to requests without a Claude Code user agent.
348 upstream: (request) => ({
349 status: request.header?.["User-Agent"]?.startsWith("claude-code/") ? 200 : 429,
350 body: {
351 five_hour: { utilization: 10, resets_at: null },
352 seven_day: { utilization: 50, resets_at: "2099-01-01T00:00:00Z" },
353 limits: [
354 {
355 kind: "weekly_scoped",
356 percent: 80,
357 resets_at: null,
358 scope: { model: { display_name: "Fable" } },
359 },
360 ],
361 },
362 }),
363 });
364 const api = yield* test.api;
365 const result = yield* api.readAccounts(config);
366 expect(
367 result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]),
368 ).toEqual([
369 ["five_hour", 10],
370 ["seven_day", 50],
371 ["seven_day_fable", 80],
372 ]);
373 }),
374 );
375​
376 it.effect.each([
377 {
378 name: "spend budget",
379 budget: {
380 spend: {
381 enabled: true,
382 used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
383 limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
384 },
385 },
386 },
387 {
388 name: "extra usage",
389 budget: {
390 extra_usage: {
391 is_enabled: true,
392 used_credits: 4_631,
393 monthly_limit: 50_000,
394 utilization: 9,
395 },
396 },
397 },
398 ])("keeps Claude monthly spend from $name beside rolling windows", ({ budget }) =>
399 Effect.gen(function* () {
400 const test = fixture({
401 accounts: [{ ...accounts[0]!, provider: "claude" }],
402 upstream: () => ({
403 status: 200,
404 body: { five_hour: { utilization: 10, resets_at: null }, ...budget },
405 }),
406 });
407 const result = yield* (yield* test.api).readAccounts(config);
408 expect(
409 result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]),
410 ).toEqual([
411 ["five_hour", 10],
412 ["monthly_spend", expect.closeTo(9.262, 10)],
413 ]);
414 }),
415 );
416​
417 it.effect("omits disabled Claude budgets without losing rolling windows", () =>
418 Effect.gen(function* () {
419 const test = fixture({
420 accounts: [{ ...accounts[0]!, provider: "claude" }],
421 upstream: () => ({
422 status: 200,
423 body: {
424 five_hour: { utilization: 10, resets_at: null },
425 spend: { enabled: false, used: null, limit: null },
426 extra_usage: {
427 is_enabled: false,
428 used_credits: null,
429 monthly_limit: null,
430 utilization: null,
431 },
432 },
433 }),
434 });
435 const result = yield* (yield* test.api).readAccounts(config);
436 expect(result[0]?.usageLimits.windows.map((window) => window.id)).toEqual(["five_hour"]);
437 }),
438 );
439​
440 it.effect("keeps a Claude account whose window reports no utilization", () =>
441 Effect.gen(function* () {
442 const test = fixture({
443 accounts: [{ ...accounts[0]!, provider: "claude" }],
444 upstream: () => ({
445 status: 200,
446 body: {
447 five_hour: { utilization: null, resets_at: null },
448 seven_day: { utilization: 50, resets_at: "2099-01-01T00:00:00Z" },
449 },
450 }),
451 });
452 const result = yield* (yield* test.api).readAccounts(config);
453 expect(result[0]?.usageLimits.unavailable).toBeUndefined();
454 expect(
455 result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]),
456 ).toEqual([["seven_day", 50]]);
457 }),
458 );
459​
460 const antigravitySummary = {
461 buckets: [
462 {
463 bucketId: "gemini-weekly",
464 window: "weekly",
465 remainingFraction: 0.75,
466 resetTime: "2099-01-01T00:00:00Z",
467 },
468 ],
469 };
470​
471 it.effect("reads an xAI account as Grok with Grok CLI headers", () =>
472 Effect.gen(function* () {
473 const test = fixture({
474 accounts: [{ id: "xai.json", auth_index: "x", provider: "xai", email: "x@example.com" }],
475 upstream: () => ({
476 status: 200,
477 body: {
478 config: {
479 creditUsagePercent: 40,
480 currentPeriod: { type: "USAGE_PERIOD_TYPE_WEEKLY", end: "2099-01-01T00:00:00Z" },
481 },
482 },
483 }),
484 });
485 const [account] = yield* (yield* test.api).readAccounts(config);
486 expect(account).toMatchObject({ driver: "grok", email: "x@example.com" });
487 expect(account?.usageLimits.windows).toMatchObject([
488 { id: "subscription", kind: "weekly", usedPercent: 40 },
489 ]);
490 const call = test.requests.find((request) => request.body?.url)?.body;
491 expect(call?.method).toBe("GET");
492 expect(call?.url).toBe("https://cli-chat-proxy.grok.com/v1/billing?format=credits");
493 expect(call?.header).toMatchObject({
494 "x-xai-token-auth": "xai-grok-cli",
495 "x-grok-client-version": expect.any(String),
496 "User-Agent": expect.stringMatching(/^grok-cli\//),
497 });
498 }),
499 );
500​
501 it.effect("reads an Antigravity summary and keys the account by its Google ID", () =>
502 Effect.gen(function* () {
503 const test = fixture({
504 accounts: [
505 {
506 id: "antigravity-g@example.com.json",
507 auth_index: "g",
508 provider: "antigravity",
509 email: "g@example.com",
510 project_id: "project-7",
511 },
512 ],
513 upstream: (request) => ({
514 status: 200,
515 body: request.url?.endsWith("/userinfo") ? { id: "google-42" } : antigravitySummary,
516 }),
517 });
518 const [account] = yield* (yield* test.api).readAccounts(config);
519 expect(account?.driver).toBe("antigravity");
520 expect(account?.email).toBeUndefined();
521 expect(account?.usageLimits.credentialFingerprint).toBe(
522 // sha256("google-account\0google-42"), as a local Antigravity probe reports it.
523 "136d4952771fd67a3600053f37492cbd05828712e0b1c89ebab03d4c22ab6554",
524 );
525 expect(account?.usageLimits.windows).toMatchObject([
526 { id: "gemini-weekly", kind: "weekly", usedPercent: 25 },
527 ]);
528 const summary = test.requests.find((request) => request.body?.method === "POST")?.body;
529 expect(summary?.url).toBe(
530 "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary",
531 );
532 expect(summary?.data).toBe(encodeJson({ project: "project-7" }));
533 expect(summary).toMatchObject({
534 auth_index: "g",
535 method: "POST",
536 header: {
537 Authorization: "Bearer $TOKEN$",
538 "Content-Type": "application/json",
539 "User-Agent":
540 "antigravity/hub/2.24.0 (aidev_client; os_type=linux; arch=x64; cl=963137146)",
541 },
542 });
543 const identityCalls = test.requests.filter((request) =>
544 request.body?.url?.endsWith("/userinfo"),
545 );
546 expect(identityCalls).toHaveLength(1);
547 expect(identityCalls[0]?.body).toMatchObject({
548 method: "GET",
549 url: "https://www.googleapis.com/oauth2/v2/userinfo",
550 header: { Authorization: "Bearer $TOKEN$" },
551 });
552 }).pipe(
553 Effect.provideService(HostProcess.Platform, "linux"),
554 Effect.provideService(HostProcess.Architecture, "x64"),
555 ),
556 );
557​
558 it.effect("keeps Antigravity limits and listing email when its identity read fails", () =>
559 Effect.gen(function* () {
560 const test = fixture({
561 accounts: [
562 {
563 id: "antigravity.json",
564 auth_index: "g",
565 provider: "antigravity",
566 email: "g@example.com",
567 project_id: "project-7",
568 },
569 ],
570 upstream: (request) =>
571 request.url?.endsWith("/userinfo")
572 ? { status: 403, body: { error: "denied" } }
573 : {
574 status: 200,
575 body: antigravitySummary,
576 },
577 });
578 const result = yield* (yield* test.api).readAccounts(config);
579 expect(result).toHaveLength(1);
580 const [account] = result;
581 expect(account).toMatchObject({ driver: "antigravity", email: "g@example.com" });
582 expect(account?.usageLimits.credentialFingerprint).toBeUndefined();
583 expect(account?.usageLimits.unavailable).toBeUndefined();
584 expect(account?.usageLimits.windows).toMatchObject([
585 { id: "gemini-weekly", kind: "weekly", usedPercent: 25 },
586 ]);
587 }),
588 );
589​
590 it.effect.each([
591 { name: "a refused xAI call", provider: "xai", status: 403, body: { error: "denied" } },
592 { name: "a malformed xAI answer", provider: "xai", status: 200, body: { config: "x" } },
593 { name: "a refused Antigravity call", provider: "antigravity", status: 401, body: {} },
594 { name: "a malformed Antigravity answer", provider: "antigravity", status: 200, body: [] },
595 { name: "an empty Antigravity summary", provider: "antigravity", status: 200, body: {} },
596 ])("reports $name as unreadable", ({ provider, status, body }) =>
597 Effect.gen(function* () {
598 const test = fixture({
599 accounts: [{ id: `${provider}.json`, auth_index: "a", provider, project_id: "project-7" }],
600 upstream: () => ({ status, body }),
601 });
602 const [account] = yield* (yield* test.api).readAccounts(config);
603 expect(account?.usageLimits.unavailable?.reason).toBe("probeFailed");
604 }),
605 );
606​
607 it.effect.each([undefined, "", " "])(
608 "rejects a missing or blank Antigravity project (%s) before upstream calls",
609 (project_id) =>
610 Effect.gen(function* () {
611 const test = fixture({
612 accounts: [
613 {
614 id: "antigravity.json",
615 auth_index: "a",
616 provider: "antigravity",
617 ...(project_id === undefined ? {} : { project_id }),
618 },
619 ],
620 });
621 const [account] = yield* (yield* test.api).readAccounts(config);
622 expect(account?.usageLimits.unavailable?.reason).toBe("probeFailed");
623 expect(test.requests.map((request) => request.path)).toEqual(["/v0/management/auth-files"]);
624 }),
625 );
626​
627 it.effect("skips accounts of providers the hub table does not know", () =>
628 Effect.gen(function* () {
629 const test = fixture({
630 accounts: [
631 { id: "kiro.json", auth_index: "k", provider: "kiro", email: "k@example.com" },
632 { ...accounts[0]!, provider: "claude" },
633 ],
634 upstream: () => ({ status: 200, body: { five_hour: { utilization: 1, resets_at: null } } }),
635 });
636 const result = yield* (yield* test.api).readAccounts(config);
637 expect(result.map((account) => account.driver)).toEqual(["claudeAgent"]);
638 expect(test.requests.map((request) => request.body?.auth_index)).not.toContain("k");
639 }),
640 );
641​
642 it.effect("pins redemption to the displayed credit and clears only that account's cooldown", () =>
643 Effect.gen(function* () {
644 const test = fixture();
645 const api = yield* test.api;
646 expect(yield* api.consume(config, "second.json", "credit-b")).toEqual({ outcome: "reset" });
647 expect(yield* api.consume(config, "second.json", "credit-b")).toEqual({ outcome: "reset" });
648 const redemptions = test.requests.filter((request) =>
649 request.body?.url?.endsWith("/consume"),
650 );
651 expect(redemptions).toHaveLength(2);
652 expect(redemptions[0]?.body?.data).toBe(redemptions[1]?.body?.data);
653 expect(redemptions[0]?.body?.data).toBe(
654 encodeJson({
655 redeem_request_id: "519d5243-011a-5b7b-91f3-44d85f095705",
656 credit_id: "credit-b",
657 }),
658 );
659 expect(
660 test.requests
661 .filter((request) => request.path.endsWith("/reset-quota"))
662 .map((request) => request.body?.auth_index),
663 ).toEqual(["b", "b"]);
664 }),
665 );
666​
667 it.effect("reports redemption success even if cooldown clearing fails", () =>
668 Effect.gen(function* () {
669 const api = yield* fixture({ cooldownStatus: 404 }).api;
670 const result = yield* api.consume(config, "first.json", "credit");
671 expect(result.outcome).toBe("reset");
672 expect(result.warning).toContain("cooldown");
673 }),
674 );
675​
676 it.effect("skips disabled accounts and rejects redemption on them", () =>
677 Effect.gen(function* () {
678 const test = fixture({ accounts: [{ ...accounts[0]!, disabled: true }] });
679 const api = yield* test.api;
680 expect(yield* api.readAccounts(config)).toEqual([]);
681 expect((yield* api.consume(config, "first.json", "credit").pipe(Effect.result))._tag).toBe(
682 "Failure",
683 );
684 expect(test.requests.every((request) => request.path.endsWith("/auth-files"))).toBe(true);
685 }),
686 );
687​
688 it.effect("keeps the same redemption id after an uncertain upstream failure", () =>
689 Effect.gen(function* () {
690 let attempts = 0;
691 const test = fixture({
692 upstream: () =>
693 ++attempts === 1
694 ? { status: 503, body: {} }
695 : { status: 200, body: { code: "already_redeemed" } },
696 });
697 const api = yield* test.api;
698 expect((yield* api.consume(config, "first.json", "credit").pipe(Effect.result))._tag).toBe(
699 "Failure",
700 );
701 expect(yield* api.consume(config, "first.json", "credit")).toEqual({
702 outcome: "alreadyRedeemed",
703 });
704 const data = test.requests
705 .filter((request) => request.body?.url?.endsWith("/consume"))
706 .map((request) => request.body?.data);
707 expect(data[0]).toBe(data[1]);
708 expect(test.requests.filter((request) => request.path.endsWith("/reset-quota"))).toHaveLength(
709 1,
710 );
711 }),
712 );
713​
714 it.effect("rejects unknown accounts without forwarding a redemption", () =>
715 Effect.gen(function* () {
716 const test = fixture();
717 const api = yield* test.api;
718 const result = yield* api.consume(config, "missing.json", "credit").pipe(Effect.result);
719 expect(result._tag).toBe("Failure");
720 expect(test.requests).toHaveLength(1);
721 }),
722 );
723});