|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createGoogleAdapter as createGoogleAdapterProduction } from "../src/adapters/google"; |
| 3 | +import type { OcxMessage, OcxParsedRequest, OcxProviderConfig } from "../src/types"; |
| 4 | +import { withTestTranslatorBudget } from "./helpers/translator-budget"; |
| 5 | + |
| 6 | +const createGoogleAdapter = (...args: Parameters<typeof createGoogleAdapterProduction>) => |
| 7 | + withTestTranslatorBudget(createGoogleAdapterProduction(...args)); |
| 8 | + |
| 9 | +const provider = { |
| 10 | + adapter: "google", |
| 11 | + baseUrl: "https://daily-cloudcode-pa.googleapis.com", |
| 12 | + googleMode: "cloud-code-assist", |
| 13 | + project: "proj-123", |
| 14 | + apiKey: "ya29.token", |
| 15 | +} as OcxProviderConfig; |
| 16 | + |
| 17 | +function parsed(messages: OcxMessage[], modelId = "claude-opus-4-6-thinking"): OcxParsedRequest { |
| 18 | + return { |
| 19 | + modelId, |
| 20 | + stream: false, |
| 21 | + options: {}, |
| 22 | + context: { messages, systemPrompt: [], tools: [] }, |
| 23 | + } as unknown as OcxParsedRequest; |
| 24 | +} |
| 25 | + |
| 26 | +async function envelopeContents(p: OcxParsedRequest): Promise<{ role: string; parts: unknown[] }[]> { |
| 27 | + const { body } = await createGoogleAdapter(provider).buildRequest(p); |
| 28 | + const envelope = JSON.parse(body); |
| 29 | + return envelope.request.contents; |
| 30 | +} |
| 31 | + |
| 32 | +describe("google claude prefill guard", () => { |
| 33 | + test("appends a user continue nudge when CCA Claude context ends with model turn", async () => { |
| 34 | + const contents = await envelopeContents(parsed([ |
| 35 | + { role: "user", content: "start", timestamp: 0 }, |
| 36 | + { role: "assistant", content: [{ type: "text", text: "partial answer" }], model: "claude", timestamp: 0 }, |
| 37 | + ])); |
| 38 | + |
| 39 | + expect(contents.at(-1)).toEqual({ role: "user", parts: [{ text: "(continue)" }] }); |
| 40 | + }); |
| 41 | + |
| 42 | + test("leaves context ending with user unchanged", async () => { |
| 43 | + const contents = await envelopeContents(parsed([ |
| 44 | + { role: "assistant", content: [{ type: "text", text: "answer" }], model: "claude", timestamp: 0 }, |
| 45 | + { role: "user", content: "follow up", timestamp: 0 }, |
| 46 | + ])); |
| 47 | + |
| 48 | + expect(contents.at(-1)!.role).toBe("user"); |
| 49 | + expect(JSON.stringify(contents.at(-1))).not.toContain("(continue)"); |
| 50 | + }); |
| 51 | + |
| 52 | + test("appends nudge when CCA Claude context is empty", async () => { |
| 53 | + const contents = await envelopeContents(parsed([])); |
| 54 | + |
| 55 | + expect(contents.at(-1)).toEqual({ role: "user", parts: [{ text: "(continue)" }] }); |
| 56 | + }); |
| 57 | + |
| 58 | + test("does not append nudge after tool result (tool result maps to user role)", async () => { |
| 59 | + const contents = await envelopeContents(parsed([ |
| 60 | + { |
| 61 | + role: "assistant", |
| 62 | + content: [{ type: "toolCall", id: "call_1", name: "read_file", arguments: { path: "README.md" } }], |
| 63 | + model: "claude", |
| 64 | + timestamp: 0, |
| 65 | + }, |
| 66 | + { |
| 67 | + role: "toolResult", |
| 68 | + toolCallId: "call_1", |
| 69 | + toolName: "read_file", |
| 70 | + content: "contents", |
| 71 | + isError: false, |
| 72 | + timestamp: 0, |
| 73 | + }, |
| 74 | + ] as OcxMessage[])); |
| 75 | + |
| 76 | + // toolResult maps to role:"user" in Gemini format, so no nudge needed |
| 77 | + expect(contents.at(-1)!.role).toBe("user"); |
| 78 | + expect(JSON.stringify(contents.at(-1))).not.toContain("(continue)"); |
| 79 | + }); |
| 80 | + |
| 81 | + test("does not append nudge for non-Claude models on Antigravity", async () => { |
| 82 | + const contents = await envelopeContents(parsed([ |
| 83 | + { role: "user", content: "start", timestamp: 0 }, |
| 84 | + { role: "assistant", content: [{ type: "text", text: "answer" }], model: "gemini", timestamp: 0 }, |
| 85 | + ], "gemini-3.7-flash")); |
| 86 | + |
| 87 | + // Gemini natively accepts model-tail; no nudge |
| 88 | + expect(contents.at(-1)!.role).toBe("model"); |
| 89 | + expect(JSON.stringify(contents)).not.toContain("(continue)"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments