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

antigravityUsageLimits.test.ts

178 lines6.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as HostProcess from "@t3tools/shared/HostProcess";
7​
8import { makeAntigravityUsageProbe } from "./antigravityUsageLimits.ts";
9​
10// Recorded from `agy usage --json` 1.5.22 (descriptions trimmed).
11const USAGE_OUTPUT = {
12 conversation_id: "",
13 status: "SUCCESS",
14 response: "",
15 command: {
16 name: "usage",
17 data: {
18 groups: [
19 {
20 name: "Gemini Models",
21 buckets: [
22 {
23 id: "gemini-weekly",
24 name: "Weekly Limit Remaining",
25 window: "weekly",
26 remaining_fraction: 0.4327886402606964,
27 reset_time: "2026-12-08T06:54:52Z",
28 },
29 {
30 id: "gemini-5h",
31 name: "Five Hour Limit Remaining",
32 window: "5h",
33 remaining_fraction: 0,
34 reset_time: "2026-12-02T20:37:12Z",
35 },
36 ],
37 },
38 {
39 name: "Claude and GPT models",
40 buckets: [
41 {
42 id: "3p-5h",
43 name: "Five Hour Limit Remaining",
44 window: "5h",
45 remaining_fraction: 1,
46 reset_time: "1970-01-01T00:00:00Z",
47 },
48 ],
49 },
50 ],
51 },
52 },
53};
54​
55const shellQuote = (value: string) => `'${value.replaceAll("'", `'\\''`)}'`;
56​
57const probeWith = Effect.fn("probeWith")(function* (input: {
58 readonly script: string;
59 readonly version?: string | null;
60 readonly signedIn?: boolean;
61}) {
62 const fs = yield* FileSystem.FileSystem;
63 const path = yield* Path.Path;
64 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-usage-" });
65 const profileDirectory = path.join(root, "profile");
66 if (input.signedIn !== false) {
67 yield* fs.makeDirectory(path.join(profileDirectory, "antigravity-acp"), { recursive: true });
68 yield* fs.writeFileString(
69 path.join(profileDirectory, "antigravity-acp", "antigravity-oauth-token"),
70 "instance-token",
71 );
72 }
73 const executablePath = path.join(root, "agy");
74 yield* fs.writeFileString(executablePath, `#!/bin/sh\n${input.script}\n`);
75 yield* fs.chmod(executablePath, 0o755);
76 const usage = yield* makeAntigravityUsageProbe({
77 profileDirectory,
78 authMethod: "oauth-personal",
79 environment: { PATH: "/usr/bin:/bin" },
80 executable: Effect.succeed({
81 executablePath,
82 version: input.version === undefined ? "1.5.22" : input.version,
83 }),
84 });
85 return yield* usage.probe;
86});
87​
88// The fake agy is a POSIX shell script.
89describe("Antigravity usage limits", () => {
90 it.layer(NodeServices.layer)((it) => {
91 it.effect("reads `agy usage --json` with the instance's sign-in", () =>
92 Effect.gen(function* () {
93 if (yield* HostProcess.isWindows) return;
94 const limits = yield* probeWith({
95 script: [
96 `[ "$1 $2" = "usage --json" ] || exit 9`,
97 `[ "$(cat "$HOME/.gemini/antigravity-cli/antigravity-oauth-token")" = instance-token ] || exit 8`,
98 `printf '%s' ${shellQuote(JSON.stringify(USAGE_OUTPUT))}`,
99 ].join("\n"),
100 });
101 expect(limits.unavailable).toBeUndefined();
102 expect(limits.windows).toEqual([
103 {
104 id: "3p-5h",
105 kind: "session",
106 label: "Claude and GPT models · 5-hour",
107 usedPercent: 0,
108 windowDurationMins: 300,
109 },
110 {
111 id: "gemini-5h",
112 kind: "session",
113 label: "Gemini Models · 5-hour",
114 usedPercent: 100,
115 resetsAt: "2026-12-02T20:37:12.000Z",
116 windowDurationMins: 300,
117 },
118 {
119 id: "gemini-weekly",
120 kind: "weekly",
121 label: "Gemini Models · Weekly",
122 usedPercent: expect.closeTo(56.72, 2),
123 resetsAt: "2026-12-08T06:54:52.000Z",
124 windowDurationMins: 10080,
125 },
126 ]);
127 }).pipe(Effect.scoped),
128 );
129​
130 it.effect.each([
131 {
132 name: "a runtime older than 1.5.22",
133 version: "1.5.21",
134 script: "exit 9",
135 expected: { reason: "unsupported", message: expect.stringContaining("1.5.22") },
136 },
137 {
138 name: "an unversioned runtime without the subcommand",
139 version: null,
140 script: `echo 'Error: unexpected argument "usage".' >&2; exit 1`,
141 expected: { reason: "unsupported", message: expect.stringContaining("1.5.22") },
142 },
143 {
144 name: "a signed-out CLI",
145 script: "echo 'Error: Please sign in to view account usage.' >&2; exit 1",
146 expected: { reason: "unsupported", message: expect.stringContaining("Sign in") },
147 },
148 {
149 name: "a failed read",
150 script: "echo 'Error: network unreachable' >&2; exit 1",
151 expected: { reason: "probeFailed" },
152 },
153 {
154 name: "an unexpected payload",
155 script: `echo '{"status":"SUCCESS","command":{"name":"usage","data":{}}}'`,
156 expected: { reason: "probeFailed" },
157 },
158 ])("reports $name as unavailable", ({ script, version, expected }) =>
159 Effect.gen(function* () {
160 if (yield* HostProcess.isWindows) return;
161 const limits = yield* probeWith({ script, ...(version === undefined ? {} : { version }) });
162 expect(limits.windows).toEqual([]);
163 expect(limits.unavailable).toMatchObject(expected);
164 }).pipe(Effect.scoped),
165 );
166​
167 it.effect("asks for sign-in without running agy when the instance has no token", () =>
168 Effect.gen(function* () {
169 if (yield* HostProcess.isWindows) return;
170 const limits = yield* probeWith({ script: "exit 9", signedIn: false });
171 expect(limits.unavailable).toEqual({
172 reason: "unsupported",
173 message: "Sign in with Google to see Antigravity limits.",
174 });
175 }).pipe(Effect.scoped),
176 );
177 });
178});