Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http";
8
9import { makeAntigravityUsageProbe } from "./antigravityUsageLimits.ts";
10
11const summary = {
12 groups: [
13 {
14 displayName: "Gemini Models",
15 buckets: [
16 {
17 bucketId: "gemini-weekly",
18 window: "weekly",
19 remainingFraction: 0.96,
20 resetTime: "2026-10-12T19:41:43Z",
21 },
22 {
23 bucketId: "gemini-5h",
24 window: "5h",
25 remainingFraction: 0.91,
26 resetTime: "2026-10-09T16:06:01Z",
27 },
28 ],
29 },
30 {
31 displayName: "Claude and GPT models",
32 buckets: [
33 {
34 bucketId: "3p-weekly",
35 window: "weekly",
36 remainingFraction: 0,
37 resetTime: "2026-10-10T15:28:22Z",
38 },
39 {
40 bucketId: "3p-5h",
41 window: "5h",
42 remainingFraction: 0.44,
43 resetTime: "2026-10-09T16:06:30Z",
44 },
45 ],
46 },
47 ],
48};
49
50const fixture = Effect.fn("AntigravityUsageTest.fixture")(function* (
51 options: {
52 summary?: unknown;
53 models?: unknown;
54 summaryStatus?: number;
55 tokenStatus?: number;
56 missingToken?: boolean;
57 authMethod?: "oauth-personal" | "gemini-api-key";
58 } = {},
59) {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const profileDirectory = yield* fs.makeTempDirectoryScoped();
63 yield* fs.makeDirectory(path.join(profileDirectory, "antigravity-acp"));
64 const tokenPath = path.join(profileDirectory, "antigravity-acp", "acp_token.json");
65 const writeToken = (refreshToken: string) =>
66 fs.writeFileString(
67 tokenPath,
68 JSON.stringify({
69 client_id: "client-id",
70 client_secret: "client-secret",
71 refresh_token: refreshToken,
72 project_id: "project-id",
73 }),
74 );
75 if (!options.missingToken) yield* writeToken("refresh-a");
76 const requests: Array<{ url: string; body: string }> = [];
77 const http = HttpClient.make((request, _url, _signal, fiber) =>
78 Effect.sync(() => {
79 const init = Context.getOrUndefined(fiber.context, FetchHttpClient.RequestInit);
80 expect(init?.redirect).toBe("manual");
81 const body =
82 request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "";
83 requests.push({ url: request.url, body });
84 if (request.url.endsWith("/token")) {
85 expect(new URLSearchParams(body).get("grant_type")).toBe("refresh_token");
86 return HttpClientResponse.fromWeb(
87 request,
88 Response.json(
89 { access_token: "access-token", expires_in: 3600 },
90 { status: options.tokenStatus ?? 200 },
91 ),
92 );
93 }
94 expect(request.headers.authorization).toBe("Bearer access-token");
95 expect(JSON.parse(body)).toEqual({ project: "project-id" });
96 return HttpClientResponse.fromWeb(
97 request,
98 request.url.endsWith(":retrieveUserQuotaSummary")
99 ? Response.json(options.summary ?? summary, { status: options.summaryStatus ?? 200 })
100 : Response.json(options.models ?? { models: {} }),
101 );
102 }),
103 );
104 const usage = yield* makeAntigravityUsageProbe({
105 profileDirectory,
106 authMethod: options.authMethod ?? "oauth-personal",
107 }).pipe(Effect.provideService(HttpClient.HttpClient, http));
108 return { ...usage, requests, writeToken, tokenPath };
109});
110
111describe("Antigravity subscription limits", () => {
112 it.effect("reports separate Gemini and shared Claude/GPT five-hour and weekly limits", () =>
113 Effect.gen(function* () {
114 const { probe, requests } = yield* fixture();
115 const limits = yield* probe;
116 expect(limits.windows).toHaveLength(4);
117 expect(limits.unavailable).toBeUndefined();
118 expect(limits.credentialFingerprint).toMatch(/^[0-9a-f]{64}$/);
119 const byId = new Map(limits.windows.map((window) => [window.id, window]));
120 expect(byId.get("gemini-weekly")?.usedPercent).toBeCloseTo(4);
121 expect(byId.get("gemini-5h")?.usedPercent).toBeCloseTo(9);
122 expect(byId.get("3p-weekly")).toMatchObject({
123 label: "Claude & GPT (shared) · Weekly",
124 kind: "weekly",
125 usedPercent: 100,
126 windowDurationMins: 10080,
127 resetsAt: "2026-10-10T15:28:22.000Z",
128 });
129 expect(byId.get("3p-5h")).toMatchObject({ kind: "session", windowDurationMins: 300 });
130 expect(byId.get("3p-5h")?.usedPercent).toBeCloseTo(56);
131 yield* probe;
132 expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(1);
133 expect(
134 requests.filter((request) => request.url.endsWith(":retrieveUserQuotaSummary")),
135 ).toHaveLength(2);
136 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
137 );
138
139 it.effect("re-reads changed credentials and clears cached access after sign-out", () =>
140 Effect.gen(function* () {
141 const { probe, clear, requests, writeToken, tokenPath } = yield* fixture();
142 const first = yield* probe;
143 yield* writeToken("refresh-b");
144 const second = yield* probe;
145 expect(second.credentialFingerprint).not.toBe(first.credentialFingerprint);
146 expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(2);
147 yield* (yield* FileSystem.FileSystem).remove(tokenPath);
148 expect((yield* probe).unavailable?.reason).toBe("unsupported");
149 yield* writeToken("refresh-b");
150 yield* probe;
151 expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(3);
152 yield* clear;
153 yield* probe;
154 expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(4);
155 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
156 );
157
158 it.effect("falls back to model quotas without inventing a window duration", () =>
159 Effect.gen(function* () {
160 const { probe } = yield* fixture({
161 summaryStatus: 404,
162 models: {
163 models: {
164 "gemini-a": {
165 modelProvider: "MODEL_PROVIDER_GOOGLE",
166 quotaInfo: { remainingFraction: 0.8, resetTime: "2026-10-10T12:00:00Z" },
167 },
168 "gemini-b": {
169 modelProvider: "MODEL_PROVIDER_GOOGLE",
170 quotaInfo: { remainingFraction: 0.6, resetTime: "2026-10-10T12:00:00Z" },
171 },
172 "claude-a": { quotaInfo: { remainingFraction: 0.2 } },
173 },
174 },
175 });
176 const limits = yield* probe;
177 expect(limits.windows).toHaveLength(2);
178 expect(limits.windows.find((window) => window.id === "gemini-quota")?.usedPercent).toBe(40);
179 expect(
180 limits.windows.every(
181 (window) => window.kind === "other" && window.windowDurationMins === undefined,
182 ),
183 ).toBe(true);
184 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
185 );
186
187 it.effect("skips disabled and missing fractions and clamps valid percentages", () =>
188 Effect.gen(function* () {
189 const { probe } = yield* fixture({
190 summary: {
191 buckets: [
192 { bucketId: "disabled", remainingFraction: 0, disabled: true },
193 { bucketId: "missing", resetTime: "2026-10-10T12:00:00Z" },
194 { bucketId: "gemini-5h", window: "5h", remainingFraction: 2, resetTime: "invalid" },
195 ],
196 },
197 });
198 expect((yield* probe).windows).toEqual([
199 {
200 id: "gemini-5h",
201 kind: "session",
202 label: "Gemini · 5-hour",
203 usedPercent: 0,
204 windowDurationMins: 300,
205 },
206 ]);
207 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
208 );
209
210 it.effect("preserves cached access token when quota endpoints return no usable windows", () =>
211 Effect.gen(function* () {
212 const { probe, requests } = yield* fixture({
213 summary: { groups: [] },
214 models: { models: {} },
215 });
216 const first = yield* probe;
217 expect(first.unavailable?.reason).toBe("probeFailed");
218 expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(1);
219 const second = yield* probe;
220 expect(second.unavailable?.reason).toBe("probeFailed");
221 expect(requests.filter((request) => request.url.endsWith("/token"))).toHaveLength(1);
222 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
223 );
224
225 it.effect("returns a safe failure when OAuth refresh fails", () =>
226 Effect.gen(function* () {
227 const { probe } = yield* fixture({ tokenStatus: 401 });
228 const limits = yield* probe;
229 expect(limits.windows).toEqual([]);
230 expect(limits.unavailable?.reason).toBe("probeFailed");
231 expect(JSON.stringify(limits)).not.toMatch(/client-secret|refresh-a|access-token/);
232 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
233 );
234
235 it.effect("does not make network requests for API keys or missing sign-ins", () =>
236 Effect.gen(function* () {
237 for (const options of [{ authMethod: "gemini-api-key" as const }, { missingToken: true }]) {
238 const { probe, requests } = yield* fixture(options);
239 expect((yield* probe).unavailable?.reason).toBe("unsupported");
240 expect(requests).toEqual([]);
241 }
242 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
243 );
244});