|
1 | 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
3 | 4 | import * as Layer from "effect/Layer"; |
4 | 5 | import { describe, expect, it } from "@effect/vitest"; |
5 | 6 | import * as Context from "effect/Context"; |
@@ -10,7 +11,11 @@ import * as Path from "effect/Path"; |
10 | 11 | import * as TestClock from "effect/testing/TestClock"; |
11 | 12 | import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http"; |
12 | 13 |
|
13 | | -import { makeAntigravityUsageProbe } from "./antigravityUsageLimits.ts"; |
| 14 | +import { |
| 15 | + decodeGoKeyringSecret, |
| 16 | + makeAntigravityUsageProbe, |
| 17 | + readWindowsCliCredential, |
| 18 | +} from "./antigravityUsageLimits.ts"; |
14 | 19 |
|
15 | 20 | const summary = { |
16 | 21 | groups: [ |
@@ -66,6 +71,9 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* ( |
66 | 71 | userInfoAt?: number; |
67 | 72 | missingToken?: boolean; |
68 | 73 | authMethod?: "oauth-personal" | "oauth-business" | "gemini-api-key"; |
| 74 | + keyringSecret?: string; |
| 75 | + /** Reads the OS keyring instead of `keyringSecret`. */ |
| 76 | + nativeKeyring?: boolean; |
69 | 77 | } = {}, |
70 | 78 | ) { |
71 | 79 | const fs = yield* FileSystem.FileSystem; |
@@ -111,7 +119,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* ( |
111 | 119 | : Response.json({}, { status: 403 }), |
112 | 120 | ); |
113 | 121 | } |
114 | | - expect(JSON.parse(body)).toEqual({ project: "project-id" }); |
| 122 | + expect(JSON.parse(body)).toEqual(options.missingToken ? {} : { project: "project-id" }); |
115 | 123 | expect(request.headers["user-agent"]).toMatch(/^antigravity\/hub\/2\.24\.0 /); |
116 | 124 | return HttpClientResponse.fromWeb( |
117 | 125 | request, |
@@ -132,6 +140,7 @@ const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* ( |
132 | 140 | const usage = yield* makeAntigravityUsageProbe({ |
133 | 141 | profileDirectory, |
134 | 142 | authMethod: options.authMethod ?? "oauth-personal", |
| 143 | + ...(options.nativeKeyring ? {} : { readKeyringSecret: Effect.succeed(options.keyringSecret) }), |
135 | 144 | }).pipe(Effect.provideService(HttpClient.HttpClient, http)); |
136 | 145 | return { ...usage, requests, writeToken, tokenPath }; |
137 | 146 | }); |
@@ -654,6 +663,101 @@ describe("Antigravity subscription limits", () => { |
654 | 663 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
655 | 664 | ); |
656 | 665 |
|
| 666 | + it.effect("reads the native CLI's stored sign-in when acp_token.json is absent", () => |
| 667 | + Effect.gen(function* () { |
| 668 | + const fs = yield* FileSystem.FileSystem; |
| 669 | + const path = yield* Path.Path; |
| 670 | + const { probe, tokenPath } = yield* fixture({ missingToken: true }); |
| 671 | + // The 1.5.x CLI never writes acp_token.json; without a keyring it stores this file instead. |
| 672 | + yield* fs.writeFileString( |
| 673 | + path.join(path.dirname(tokenPath), "antigravity-oauth-token"), |
| 674 | + JSON.stringify({ |
| 675 | + token: { |
| 676 | + access_token: "access-token", |
| 677 | + token_type: "Bearer", |
| 678 | + refresh_token: "refresh-a", |
| 679 | + expiry: "2099-01-01T00:00:00Z", |
| 680 | + expires_in: 3600, |
| 681 | + }, |
| 682 | + auth_method: "consumer", |
| 683 | + |
| 684 | + }), |
| 685 | + ); |
| 686 | + const limits = yield* probe; |
| 687 | + expect(limits.unavailable?.message).not.toBe( |
| 688 | + "Sign in with Google to see Antigravity limits.", |
| 689 | + ); |
| 690 | + expect(limits.unavailable?.reason).not.toBe("unsupported"); |
| 691 | + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 692 | + ); |
| 693 | + |
| 694 | + it.effect("reads the native CLI's keyring sign-in and reports when it has expired", () => |
| 695 | + Effect.gen(function* () { |
| 696 | + yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-09T12:00:00Z"))); |
| 697 | + const keyringSecret = (expiry: string) => |
| 698 | + JSON.stringify({ token: { access_token: "access-token", refresh_token: "r", expiry } }); |
| 699 | + const signedIn = yield* fixture({ |
| 700 | + missingToken: true, |
| 701 | + userId: "google-user-1", |
| 702 | + keyringSecret: keyringSecret("2026-10-09T14:00:00.123456789+01:00"), |
| 703 | + }); |
| 704 | + const limits = yield* signedIn.probe; |
| 705 | + expect(limits.unavailable).toBeUndefined(); |
| 706 | + expect(limits.windows).toHaveLength(4); |
| 707 | + expect(signedIn.requests.some((request) => request.url.endsWith("/token"))).toBe(false); |
| 708 | + |
| 709 | + const expired = yield* fixture({ |
| 710 | + missingToken: true, |
| 711 | + keyringSecret: keyringSecret("2026-10-09T12:00:30Z"), |
| 712 | + }); |
| 713 | + expect((yield* expired.probe).unavailable?.reason).toBe("probeFailed"); |
| 714 | + expect(expired.requests).toEqual([]); |
| 715 | + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 716 | + ); |
| 717 | + |
| 718 | + it.effect.runIf(HostProcess.Platform.defaultValue() === "win32")( |
| 719 | + "reads go-keyring's Windows credential target without changing it", |
| 720 | + (ctx) => |
| 721 | + Effect.gen(function* () { |
| 722 | + // Never overwrite a real sign-in on a developer's machine. |
| 723 | + if (readWindowsCliCredential(HostProcess.Architecture.defaultValue())) return ctx.skip(); |
| 724 | + yield* TestClock.setTime( |
| 725 | + DateTime.toEpochMillis(DateTime.makeUnsafe("2026-10-09T12:00:00Z")), |
| 726 | + ); |
| 727 | + const { AsyncEntry } = yield* Effect.promise(() => import("@napi-rs/keyring")); |
| 728 | + // go-keyring writes the UTF-8 JSON at `<service>:<user>`. |
| 729 | + const target = AsyncEntry.withTarget("gemini:antigravity", "gemini", "antigravity"); |
| 730 | + const stored = new TextEncoder().encode( |
| 731 | + JSON.stringify({ |
| 732 | + token: { access_token: "access-token", expiry: "2026-10-09T14:00:00Z" }, |
| 733 | + }), |
| 734 | + ); |
| 735 | + yield* Effect.acquireRelease( |
| 736 | + Effect.promise(() => target.setSecret(stored)), |
| 737 | + () => Effect.promise(() => target.deleteCredential()), |
| 738 | + ); |
| 739 | + const { probe } = yield* fixture({ missingToken: true, nativeKeyring: true }); |
| 740 | + const limits = yield* probe; |
| 741 | + expect(limits.unavailable).toBeUndefined(); |
| 742 | + expect(limits.windows).toHaveLength(4); |
| 743 | + expect(readWindowsCliCredential(HostProcess.Architecture.defaultValue())).toEqual( |
| 744 | + Buffer.from(stored), |
| 745 | + ); |
| 746 | + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 747 | + ); |
| 748 | + |
| 749 | + it("decodes go-keyring's macOS encodings", () => { |
| 750 | + const value = '{"token":{}}'; |
| 751 | + const bytes = (text: string) => new TextEncoder().encode(text); |
| 752 | + expect(decodeGoKeyringSecret(bytes(value))).toBe(value); |
| 753 | + expect( |
| 754 | + decodeGoKeyringSecret(bytes(`go-keyring-base64:${Buffer.from(value).toString("base64")}`)), |
| 755 | + ).toBe(value); |
| 756 | + expect( |
| 757 | + decodeGoKeyringSecret(bytes(`go-keyring-encoded:${Buffer.from(value).toString("hex")}`)), |
| 758 | + ).toBe(value); |
| 759 | + }); |
| 760 | + |
657 | 761 | it.effect("does not make network requests for API keys or missing sign-ins", () => |
658 | 762 | Effect.gen(function* () { |
659 | 763 | for (const options of [{ authMethod: "gemini-api-key" as const }, { missingToken: true }]) { |
|