|
1 | 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
2 | 3 | import { describe, expect, it } from "@effect/vitest"; |
3 | 4 | import * as Effect from "effect/Effect"; |
4 | 5 | import * as Schema from "effect/Schema"; |
@@ -48,12 +49,15 @@ const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
48 | 49 |
|
49 | 50 | function fixture( |
50 | 51 | options: { |
51 | | - accounts?: Array< |
52 | | - (typeof accounts)[number] & { |
53 | | - disabled?: boolean; |
54 | | - id_token: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string }; |
55 | | - } |
56 | | - >; |
| 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 | + }>; |
57 | 61 | upstream?: (request: RequestBody) => { status: number; body: unknown }; |
58 | 62 | cooldownStatus?: number; |
59 | 63 | } = {}, |
@@ -433,6 +437,188 @@ describe("CLIProxyAPI built-in management API", () => { |
433 | 437 | }), |
434 | 438 | ); |
435 | 439 |
|
| 440 | + const antigravitySummary = { |
| 441 | + buckets: [ |
| 442 | + { |
| 443 | + bucketId: "gemini-weekly", |
| 444 | + window: "weekly", |
| 445 | + remainingFraction: 0.75, |
| 446 | + resetTime: "2099-01-01T00:00:00Z", |
| 447 | + }, |
| 448 | + ], |
| 449 | + }; |
| 450 | + |
| 451 | + it.effect("reads an xAI account as Grok with Grok CLI headers", () => |
| 452 | + Effect.gen(function* () { |
| 453 | + const test = fixture({ |
| 454 | + accounts: [{ id: "xai.json", auth_index: "x", provider: "xai", email: "[email protected]" }], |
| 455 | + upstream: () => ({ |
| 456 | + status: 200, |
| 457 | + body: { |
| 458 | + config: { |
| 459 | + creditUsagePercent: 40, |
| 460 | + currentPeriod: { type: "USAGE_PERIOD_TYPE_WEEKLY", end: "2099-01-01T00:00:00Z" }, |
| 461 | + }, |
| 462 | + }, |
| 463 | + }), |
| 464 | + }); |
| 465 | + const [account] = yield* (yield* test.api).readAccounts(config); |
| 466 | + expect(account).toMatchObject({ driver: "grok", email: "[email protected]" }); |
| 467 | + expect(account?.usageLimits.windows).toMatchObject([ |
| 468 | + { id: "subscription", kind: "weekly", usedPercent: 40 }, |
| 469 | + ]); |
| 470 | + const call = test.requests.find((request) => request.body?.url)?.body; |
| 471 | + expect(call?.method).toBe("GET"); |
| 472 | + expect(call?.url).toBe("https://cli-chat-proxy.grok.com/v1/billing?format=credits"); |
| 473 | + expect(call?.header).toMatchObject({ |
| 474 | + "x-xai-token-auth": "xai-grok-cli", |
| 475 | + "x-grok-client-version": expect.any(String), |
| 476 | + "User-Agent": expect.stringMatching(/^grok-cli\//), |
| 477 | + }); |
| 478 | + }), |
| 479 | + ); |
| 480 | + |
| 481 | + it.effect("reads an Antigravity summary and keys the account by its Google ID", () => |
| 482 | + Effect.gen(function* () { |
| 483 | + const test = fixture({ |
| 484 | + accounts: [ |
| 485 | + { |
| 486 | + |
| 487 | + auth_index: "g", |
| 488 | + provider: "antigravity", |
| 489 | + |
| 490 | + project_id: "project-7", |
| 491 | + }, |
| 492 | + ], |
| 493 | + upstream: (request) => ({ |
| 494 | + status: 200, |
| 495 | + body: request.url?.endsWith("/userinfo") ? { id: "google-42" } : antigravitySummary, |
| 496 | + }), |
| 497 | + }); |
| 498 | + const [account] = yield* (yield* test.api).readAccounts(config); |
| 499 | + expect(account?.driver).toBe("antigravity"); |
| 500 | + expect(account?.email).toBeUndefined(); |
| 501 | + expect(account?.usageLimits.credentialFingerprint).toBe( |
| 502 | + // sha256("google-account\0google-42"), as a local Antigravity probe reports it. |
| 503 | + "136d4952771fd67a3600053f37492cbd05828712e0b1c89ebab03d4c22ab6554", |
| 504 | + ); |
| 505 | + expect(account?.usageLimits.windows).toMatchObject([ |
| 506 | + { id: "gemini-weekly", kind: "weekly", usedPercent: 25 }, |
| 507 | + ]); |
| 508 | + const summary = test.requests.find((request) => request.body?.method === "POST")?.body; |
| 509 | + expect(summary?.url).toBe( |
| 510 | + "https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary", |
| 511 | + ); |
| 512 | + expect(summary?.data).toBe(encodeJson({ project: "project-7" })); |
| 513 | + expect(summary).toMatchObject({ |
| 514 | + auth_index: "g", |
| 515 | + method: "POST", |
| 516 | + header: { |
| 517 | + Authorization: "Bearer $TOKEN$", |
| 518 | + "Content-Type": "application/json", |
| 519 | + "User-Agent": |
| 520 | + "antigravity/hub/2.24.0 (aidev_client; os_type=linux; arch=x64; cl=963137146)", |
| 521 | + }, |
| 522 | + }); |
| 523 | + const identityCalls = test.requests.filter((request) => |
| 524 | + request.body?.url?.endsWith("/userinfo"), |
| 525 | + ); |
| 526 | + expect(identityCalls).toHaveLength(1); |
| 527 | + expect(identityCalls[0]?.body).toMatchObject({ |
| 528 | + method: "GET", |
| 529 | + url: "https://www.googleapis.com/oauth2/v2/userinfo", |
| 530 | + header: { Authorization: "Bearer $TOKEN$" }, |
| 531 | + }); |
| 532 | + }).pipe( |
| 533 | + Effect.provideService(HostProcess.Platform, "linux"), |
| 534 | + Effect.provideService(HostProcess.Architecture, "x64"), |
| 535 | + ), |
| 536 | + ); |
| 537 | + |
| 538 | + it.effect("keeps Antigravity limits and listing email when its identity read fails", () => |
| 539 | + Effect.gen(function* () { |
| 540 | + const test = fixture({ |
| 541 | + accounts: [ |
| 542 | + { |
| 543 | + id: "antigravity.json", |
| 544 | + auth_index: "g", |
| 545 | + provider: "antigravity", |
| 546 | + |
| 547 | + project_id: "project-7", |
| 548 | + }, |
| 549 | + ], |
| 550 | + upstream: (request) => |
| 551 | + request.url?.endsWith("/userinfo") |
| 552 | + ? { status: 403, body: { error: "denied" } } |
| 553 | + : { |
| 554 | + status: 200, |
| 555 | + body: antigravitySummary, |
| 556 | + }, |
| 557 | + }); |
| 558 | + const result = yield* (yield* test.api).readAccounts(config); |
| 559 | + expect(result).toHaveLength(1); |
| 560 | + const [account] = result; |
| 561 | + expect(account).toMatchObject({ driver: "antigravity", email: "[email protected]" }); |
| 562 | + expect(account?.usageLimits.credentialFingerprint).toBeUndefined(); |
| 563 | + expect(account?.usageLimits.unavailable).toBeUndefined(); |
| 564 | + expect(account?.usageLimits.windows).toMatchObject([ |
| 565 | + { id: "gemini-weekly", kind: "weekly", usedPercent: 25 }, |
| 566 | + ]); |
| 567 | + }), |
| 568 | + ); |
| 569 | + |
| 570 | + it.effect.each([ |
| 571 | + { name: "a refused xAI call", provider: "xai", status: 403, body: { error: "denied" } }, |
| 572 | + { name: "a malformed xAI answer", provider: "xai", status: 200, body: { config: "x" } }, |
| 573 | + { name: "a refused Antigravity call", provider: "antigravity", status: 401, body: {} }, |
| 574 | + { name: "a malformed Antigravity answer", provider: "antigravity", status: 200, body: [] }, |
| 575 | + { name: "an empty Antigravity summary", provider: "antigravity", status: 200, body: {} }, |
| 576 | + ])("reports $name as unreadable", ({ provider, status, body }) => |
| 577 | + Effect.gen(function* () { |
| 578 | + const test = fixture({ |
| 579 | + accounts: [{ id: `${provider}.json`, auth_index: "a", provider, project_id: "project-7" }], |
| 580 | + upstream: () => ({ status, body }), |
| 581 | + }); |
| 582 | + const [account] = yield* (yield* test.api).readAccounts(config); |
| 583 | + expect(account?.usageLimits.unavailable?.reason).toBe("probeFailed"); |
| 584 | + }), |
| 585 | + ); |
| 586 | + |
| 587 | + it.effect.each([undefined, "", " "])( |
| 588 | + "rejects a missing or blank Antigravity project (%s) before upstream calls", |
| 589 | + (project_id) => |
| 590 | + Effect.gen(function* () { |
| 591 | + const test = fixture({ |
| 592 | + accounts: [ |
| 593 | + { |
| 594 | + id: "antigravity.json", |
| 595 | + auth_index: "a", |
| 596 | + provider: "antigravity", |
| 597 | + ...(project_id === undefined ? {} : { project_id }), |
| 598 | + }, |
| 599 | + ], |
| 600 | + }); |
| 601 | + const [account] = yield* (yield* test.api).readAccounts(config); |
| 602 | + expect(account?.usageLimits.unavailable?.reason).toBe("probeFailed"); |
| 603 | + expect(test.requests.map((request) => request.path)).toEqual(["/v0/management/auth-files"]); |
| 604 | + }), |
| 605 | + ); |
| 606 | + |
| 607 | + it.effect("skips accounts of providers the hub table does not know", () => |
| 608 | + Effect.gen(function* () { |
| 609 | + const test = fixture({ |
| 610 | + accounts: [ |
| 611 | + { id: "kiro.json", auth_index: "k", provider: "kiro", email: "[email protected]" }, |
| 612 | + { ...accounts[0]!, provider: "claude" }, |
| 613 | + ], |
| 614 | + upstream: () => ({ status: 200, body: { five_hour: { utilization: 1, resets_at: null } } }), |
| 615 | + }); |
| 616 | + const result = yield* (yield* test.api).readAccounts(config); |
| 617 | + expect(result.map((account) => account.driver)).toEqual(["claudeAgent"]); |
| 618 | + expect(test.requests.map((request) => request.body?.auth_index)).not.toContain("k"); |
| 619 | + }), |
| 620 | + ); |
| 621 | + |
436 | 622 | it.effect("pins redemption to the displayed credit and clears only that account's cooldown", () => |
437 | 623 | Effect.gen(function* () { |
438 | 624 | const test = fixture(); |
|
0 commit comments