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