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

AntigravityAcpSupport.test.ts

502 lines17.7 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import {
4 ANTIGRAVITY_DEFAULT_MODEL,
5 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
6 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
7 type ChatAttachment,
8 type RuntimeMode,
9} from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13import * as EffectAcpErrors from "effect-acp/errors";
14import type * as EffectAcpSchema from "effect-acp/schema";
15​
16import { resolveAttachmentPath } from "../../attachmentStore.ts";
17import {
18 ANTIGRAVITY_MAX_TEXT_ATTACHMENT_BYTES,
19 antigravityPermissionMode,
20 applyAntigravityAcpModelSelection,
21 buildAntigravityPrompt,
22} from "./AntigravityAcpSupport.ts";
23​
24const modelConfig = {
25 id: "model",
26 name: "Model",
27 type: "select",
28 currentValue: "gemini-default",
29 options: [
30 { value: "gemini-default", name: "Gemini default" },
31 { value: "gemini-saved", name: "Gemini saved" },
32 ],
33} satisfies EffectAcpSchema.SessionConfigOption;
34​
35function makeModelRuntime(
36 configOptions: ReadonlyArray<EffectAcpSchema.SessionConfigOption> = [modelConfig],
37 failure?: EffectAcpErrors.AcpError,
38) {
39 const selections: string[] = [];
40 const setModel = Effect.fn("AntigravityAcpSupportTest.setModel")(function* (model: string) {
41 if (failure) return yield* failure;
42 selections.push(model);
43 });
44 return {
45 runtime: { getConfigOptions: Effect.succeed(configOptions), setModel },
46 selections,
47 };
49​
50describe("applyAntigravityAcpModelSelection", () => {
51 it.effect("restores the saved model instead of the cold-resume default", () =>
52 Effect.gen(function* () {
53 const { runtime, selections } = makeModelRuntime();
54 const model = yield* applyAntigravityAcpModelSelection({
55 runtime,
56 model: "gemini-saved",
57 mapError: (cause) => cause,
58 });
59​
60 expect(model).toBe("gemini-saved");
61 expect(selections).toEqual(["gemini-saved"]);
62 }),
63 );
64​
65 it.effect("reapplies an explicit selection even when setup reports the same model", () =>
66 Effect.gen(function* () {
67 const { runtime, selections } = makeModelRuntime([
68 { ...modelConfig, currentValue: "gemini-saved" },
69 ]);
70 const model = yield* applyAntigravityAcpModelSelection({
71 runtime,
72 model: "gemini-saved",
73 mapError: (cause) => cause,
74 });
75​
76 expect(model).toBe("gemini-saved");
77 expect(selections).toEqual(["gemini-saved"]);
78 }),
79 );
80​
81 it.effect.each([undefined, null, ANTIGRAVITY_DEFAULT_MODEL])(
82 "uses the native default for %s without sending an internal model ID",
83 (requestedModel) =>
84 Effect.gen(function* () {
85 const { runtime, selections } = makeModelRuntime();
86 const model = yield* applyAntigravityAcpModelSelection({
87 runtime,
88 model: requestedModel,
89 mapError: (cause) => cause,
90 });
91​
92 expect(model).toBe("gemini-default");
93 expect(selections).toEqual([]);
94 }),
95 );
96​
97 it.effect("selects the manifest default for the alias when the account offers it", () =>
98 Effect.gen(function* () {
99 const { runtime, selections } = makeModelRuntime();
100 const model = yield* applyAntigravityAcpModelSelection({
101 runtime,
102 model: ANTIGRAVITY_DEFAULT_MODEL,
103 defaultModel: "gemini-saved",
104 mapError: (cause) => cause,
105 });
106 expect(model).toBe("gemini-saved");
107 expect(selections).toEqual(["gemini-saved"]);
108​
109 const { runtime: other, selections: otherSelections } = makeModelRuntime();
110 const fallback = yield* applyAntigravityAcpModelSelection({
111 runtime: other,
112 model: ANTIGRAVITY_DEFAULT_MODEL,
113 defaultModel: "gemini-not-offered",
114 mapError: (cause) => cause,
115 });
116 expect(fallback).toBe("gemini-default");
117 expect(otherSelections).toEqual([]);
118 }),
119 );
120​
121 it.effect.each(["gemini-removed", "Gemini saved", "gemini-saved[reasoning=high]"])(
122 "rejects unavailable or non-native model ID %s without selecting a fallback",
123 (model) =>
124 Effect.gen(function* () {
125 const { runtime, selections } = makeModelRuntime();
126 const error = yield* applyAntigravityAcpModelSelection({
127 runtime,
128 model,
129 mapError: (cause) => cause,
130 }).pipe(Effect.flip);
131​
132 expect(error).toMatchObject({
133 _tag: "AcpRequestError",
134 code: -32602,
135 errorMessage: expect.stringContaining(`'${model}' is unavailable`),
136 });
137 expect(selections).toEqual([]);
138 }),
139 );
140​
141 it.effect("accepts exact model IDs from grouped native options", () =>
142 Effect.gen(function* () {
143 const { runtime, selections } = makeModelRuntime([
144 {
145 ...modelConfig,
146 options: [{ group: "gemini", name: "Gemini", options: modelConfig.options }],
147 },
148 ]);
149 const model = yield* applyAntigravityAcpModelSelection({
150 runtime,
151 model: "gemini-saved",
152 mapError: (cause) => cause,
153 });
154​
155 expect(model).toBe("gemini-saved");
156 expect(selections).toEqual(["gemini-saved"]);
157 }),
158 );
159​
160 it.effect("reports a native model-selection failure through the adapter error mapper", () =>
161 Effect.gen(function* () {
162 const nativeError = EffectAcpErrors.AcpRequestError.invalidParams("Model access changed.");
163 const { runtime } = makeModelRuntime([modelConfig], nativeError);
164 const error = yield* applyAntigravityAcpModelSelection({
165 runtime,
166 model: "gemini-saved",
167 mapError: (cause) => ({ operation: "select-model", cause }),
168 }).pipe(Effect.flip);
169​
170 expect(error).toEqual({ operation: "select-model", cause: nativeError });
171 }),
172 );
173});
174​
175describe("antigravityPermissionMode", () => {
176 it.each([
177 { runtimeMode: "approval-required", nativeMode: "default" },
178 { runtimeMode: "auto", nativeMode: "default" },
179 { runtimeMode: "auto-accept-edits", nativeMode: "auto_edit" },
180 { runtimeMode: "full-access", nativeMode: "yolo" },
181 ] satisfies ReadonlyArray<{ runtimeMode: RuntimeMode; nativeMode: string }>)(
182 "maps $runtimeMode to $nativeMode",
183 ({ runtimeMode, nativeMode }) => {
184 expect(antigravityPermissionMode(runtimeMode)).toBe(nativeMode);
185 },
186 );
187});
188​
189const imageAttachment = {
190 type: "image",
191 id: "thread-00000000-0000-4000-8000-000000000001",
192 name: "screen.png",
193 mimeType: "image/png",
194 sizeBytes: 1,
195} satisfies ChatAttachment;
196​
197const textAttachment = {
198 type: "file",
199 id: "thread-00000000-0000-4000-8000-000000000002-tsx",
200 name: "example.tsx",
201 mimeType: "application/octet-stream",
202 sizeBytes: 1,
203} satisfies ChatAttachment;
204​
205const pdfAttachment = {
206 type: "file",
207 id: "thread-00000000-0000-4000-8000-000000000003-pdf",
208 name: "report.pdf",
209 mimeType: "application/pdf",
210 sizeBytes: 1,
211} satisfies ChatAttachment;
212​
213const makeAttachmentFixture = Effect.fn("AntigravityAcpSupportTest.makeAttachmentFixture")(
214 function* () {
215 const fs = yield* FileSystem.FileSystem;
216 const path = yield* Path.Path;
217 const attachmentsDir = yield* fs.makeTempDirectoryScoped({
218 prefix: "t3-antigravity-attachments-",
219 });
220 const write = Effect.fn("AntigravityAcpSupportTest.writeAttachment")(function* (
221 attachment: ChatAttachment,
222 content: string | Uint8Array,
223 ) {
224 const filePath = resolveAttachmentPath({ attachmentsDir, attachment });
225 if (filePath === null) throw new Error("Invalid test attachment path.");
226 if (typeof content === "string") yield* fs.writeFileString(filePath, content);
227 else yield* fs.writeFile(filePath, content);
228 return { filePath, uri: (yield* path.toFileUrl(filePath)).href };
229 });
230 return { fs, attachmentsDir, write };
231 },
232);
233​
234it.layer(NodeServices.layer)("buildAntigravityPrompt", (it) => {
235 it.effect("sends image bytes as native image content alongside the user prompt", () =>
236 Effect.gen(function* () {
237 const fixture = yield* makeAttachmentFixture();
238 const bytes = Buffer.from(
239 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jY9kAAAAASUVORK5CYII=",
240 "base64",
241 );
242 yield* fixture.write(imageAttachment, bytes);
243 const prompt = yield* buildAntigravityPrompt({
244 input: " Explain this image. ",
245 attachments: [imageAttachment],
246 attachmentsDir: fixture.attachmentsDir,
247 });
248​
249 expect(prompt).toEqual([
250 { type: "text", text: "Explain this image." },
251 { type: "image", data: bytes.toString("base64"), mimeType: "image/png" },
252 ]);
253 }),
254 );
255​
256 it.effect("embeds UTF-8 code from the selected environment and keeps the upload in place", () =>
257 Effect.gen(function* () {
258 const selectedEnvironment = yield* makeAttachmentFixture();
259 const otherEnvironment = yield* makeAttachmentFixture();
260 const source = ' const name = "caf\u00e9";\n';
261 const upload = yield* selectedEnvironment.write(textAttachment, source);
262 yield* otherEnvironment.write(textAttachment, "Different environment.");
263 const prompt = yield* buildAntigravityPrompt({
264 input: undefined,
265 attachments: [textAttachment],
266 attachmentsDir: selectedEnvironment.attachmentsDir,
267 });
268​
269 expect(prompt).toEqual([
270 {
271 type: "resource",
272 resource: { uri: upload.uri, mimeType: "application/octet-stream", text: source },
273 },
274 ]);
275 expect(yield* selectedEnvironment.fs.readFileString(upload.filePath)).toBe(source);
276 expect(
277 yield* selectedEnvironment.fs.readDirectory(selectedEnvironment.attachmentsDir),
278 ).toHaveLength(1);
279 }),
280 );
281​
282 it.effect("sends supported audio files as native audio content", () =>
283 Effect.gen(function* () {
284 const fixture = yield* makeAttachmentFixture();
285 const audioAttachment = {
286 ...textAttachment,
287 id: "recording-1",
288 name: "recording.wav",
289 mimeType: "audio/wav",
290 } satisfies ChatAttachment;
291 const bytes = Buffer.from("RIFF....WAVEfmt ", "latin1");
292 yield* fixture.write(audioAttachment, bytes);
293 const prompt = yield* buildAntigravityPrompt({
294 input: "Transcribe this.",
295 attachments: [audioAttachment],
296 attachmentsDir: fixture.attachmentsDir,
297 });
298​
299 expect(prompt).toEqual([
300 { type: "text", text: "Transcribe this." },
301 { type: "audio", data: bytes.toString("base64"), mimeType: "audio/wav" },
302 ]);
303 }),
304 );
305​
306 it.effect("uses a PDF file resource link without copying or reading its bytes", () =>
307 Effect.gen(function* () {
308 const fixture = yield* makeAttachmentFixture();
309 const upload = yield* fixture.write(pdfAttachment, "%PDF-1.7\n");
310 const forbidFileAccess = () =>
311 Effect.die("PDF prompt construction must only inspect the file.");
312 const prompt = yield* buildAntigravityPrompt({
313 input: undefined,
314 attachments: [pdfAttachment],
315 attachmentsDir: fixture.attachmentsDir,
316 }).pipe(
317 Effect.provideService(FileSystem.FileSystem, {
318 ...fixture.fs,
319 readFile: forbidFileAccess,
320 writeFile: forbidFileAccess,
321 writeFileString: forbidFileAccess,
322 copyFile: forbidFileAccess,
323 copy: forbidFileAccess,
324 rename: forbidFileAccess,
325 }),
326 );
327​
328 expect(prompt).toEqual([
329 { type: "resource_link", uri: upload.uri, name: "report.pdf", mimeType: "application/pdf" },
330 ]);
331 }),
332 );
333​
334 it.effect.each([
335 { ...imageAttachment, name: "animation.gif", mimeType: "image/gif" },
336 { ...textAttachment, name: "archive.zip", mimeType: "application/zip" },
337 { ...textAttachment, name: "recording.aiff", mimeType: "audio/aiff" },
338 ] satisfies ReadonlyArray<ChatAttachment>)(
339 "rejects $name instead of silently dropping it from a valid prompt",
340 (attachment) =>
341 Effect.gen(function* () {
342 const fixture = yield* makeAttachmentFixture();
343 yield* fixture.write(imageAttachment, new Uint8Array([1, 2, 3]));
344 const error = yield* buildAntigravityPrompt({
345 input: "Analyze every attachment.",
346 attachments: [imageAttachment, attachment],
347 attachmentsDir: fixture.attachmentsDir,
348 }).pipe(Effect.flip);
349​
350 expect(error).toMatchObject({
351 _tag: "AcpRequestError",
352 code: -32602,
353 errorMessage: expect.stringContaining(`does not support '${attachment.name}'`),
354 });
355 }),
356 );
357​
358 it.effect.each([
359 { attachment: textAttachment, bytes: ANTIGRAVITY_MAX_TEXT_ATTACHMENT_BYTES + 1 },
360 { attachment: imageAttachment, bytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1 },
361 { attachment: pdfAttachment, bytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES + 1 },
362 ])(
363 "rejects oversized $attachment.name using file size instead of upload metadata",
364 ({ attachment, bytes }) =>
365 Effect.gen(function* () {
366 const fixture = yield* makeAttachmentFixture();
367 const upload = yield* fixture.write(attachment, "");
368 yield* fixture.fs.truncate(upload.filePath, bytes);
369 const error = yield* buildAntigravityPrompt({
370 input: "Read this attachment.",
371 attachments: [attachment],
372 attachmentsDir: fixture.attachmentsDir,
373 }).pipe(Effect.flip);
374​
375 expect(error).toMatchObject({
376 _tag: "AcpRequestError",
377 code: -32602,
378 errorMessage: expect.stringContaining(`'${attachment.name}' is too large`),
379 });
380 }),
381 );
382​
383 it.effect("accepts 50 MiB in total but rejects one byte more across files", () =>
384 Effect.gen(function* () {
385 const fixture = yield* makeAttachmentFixture();
386 const secondAttachment = {
387 ...pdfAttachment,
388 id: `${pdfAttachment.id}-second`,
389 name: "second.pdf",
390 };
391 const first = yield* fixture.write(pdfAttachment, "");
392 const second = yield* fixture.write(secondAttachment, "");
393 yield* fixture.fs.truncate(first.filePath, PROVIDER_SEND_TURN_MAX_FILE_BYTES / 2);
394 yield* fixture.fs.truncate(second.filePath, PROVIDER_SEND_TURN_MAX_FILE_BYTES / 2);
395 const input = {
396 input: undefined,
397 attachments: [pdfAttachment, secondAttachment],
398 attachmentsDir: fixture.attachmentsDir,
399 };
400 const prompt = yield* buildAntigravityPrompt(input);
401 expect(prompt).toEqual([
402 { type: "resource_link", uri: first.uri, name: "report.pdf", mimeType: "application/pdf" },
403 { type: "resource_link", uri: second.uri, name: "second.pdf", mimeType: "application/pdf" },
404 ]);
405​
406 yield* fixture.fs.truncate(second.filePath, PROVIDER_SEND_TURN_MAX_FILE_BYTES / 2 + 1);
407 const error = yield* buildAntigravityPrompt(input).pipe(Effect.flip);
408 expect(error).toMatchObject({
409 _tag: "AcpRequestError",
410 code: -32602,
411 errorMessage: expect.stringContaining("'second.pdf' is too large"),
412 });
413 }),
414 );
415​
416 it.effect("rejects aggregate overflow when a text upload grows after its size check", () =>
417 Effect.gen(function* () {
418 const fixture = yield* makeAttachmentFixture();
419 const pdf = yield* fixture.write(pdfAttachment, "");
420 const text = yield* fixture.write(textAttachment, "a");
421 yield* fixture.fs.truncate(pdf.filePath, PROVIDER_SEND_TURN_MAX_FILE_BYTES - 1);
422 const error = yield* buildAntigravityPrompt({
423 input: undefined,
424 attachments: [pdfAttachment, textAttachment],
425 attachmentsDir: fixture.attachmentsDir,
426 }).pipe(
427 Effect.provideService(FileSystem.FileSystem, {
428 ...fixture.fs,
429 stat: Effect.fn("AntigravityAcpSupportTest.growAfterStat")(function* (filePath: string) {
430 const info = yield* fixture.fs.stat(filePath);
431 if (filePath === text.filePath) {
432 yield* fixture.fs.writeFileString(filePath, "ab");
433 }
434 return info;
435 }),
436 }),
437 Effect.flip,
438 );
439​
440 expect(error).toMatchObject({
441 _tag: "AcpRequestError",
442 code: -32602,
443 errorMessage: "Attachment 'example.tsx' changed while being read and is too large.",
444 });
445 }),
446 );
447​
448 it.effect.each([
449 { bytes: new Uint8Array([0xff, 0xfe, 0x61]), message: "is not a UTF-8 text file" },
450 { bytes: new Uint8Array([0x61, 0, 0x62]), message: "contains binary data" },
451 ])("rejects binary data disguised as code: $message", ({ bytes, message }) =>
452 Effect.gen(function* () {
453 const fixture = yield* makeAttachmentFixture();
454 yield* fixture.write(textAttachment, bytes);
455 const error = yield* buildAntigravityPrompt({
456 input: undefined,
457 attachments: [textAttachment],
458 attachmentsDir: fixture.attachmentsDir,
459 }).pipe(Effect.flip);
460​
461 expect(error).toMatchObject({
462 _tag: "AcpRequestError",
463 code: -32602,
464 errorMessage: expect.stringContaining(message),
465 });
466 }),
467 );
468​
469 it.effect("reports a missing upload instead of sending only the remaining text", () =>
470 Effect.gen(function* () {
471 const fixture = yield* makeAttachmentFixture();
472 const error = yield* buildAntigravityPrompt({
473 input: "Read this image.",
474 attachments: [imageAttachment],
475 attachmentsDir: fixture.attachmentsDir,
476 }).pipe(Effect.flip);
477​
478 expect(error).toMatchObject({
479 _tag: "AcpRequestError",
480 code: -32602,
481 errorMessage: "Could not read attachment 'screen.png'.",
482 });
483 }),
484 );
485​
486 it.effect("rejects an empty turn", () =>
487 Effect.gen(function* () {
488 const fixture = yield* makeAttachmentFixture();
489 const error = yield* buildAntigravityPrompt({
490 input: " ",
491 attachments: [],
492 attachmentsDir: fixture.attachmentsDir,
493 }).pipe(Effect.flip);
494​
495 expect(error).toMatchObject({
496 _tag: "AcpRequestError",
497 code: -32602,
498 errorMessage: "A turn requires text or supported attachments.",
499 });
500 }),
501 );
502});