import { afterEach, describe, expect, it, vi } from "vitest"; import { answerWithGemini } from "@/lib/gemini.server"; afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); }); describe("Gemini request behavior", () => { it("uses the private key only for Google and does not retry exhausted quota", async () => { vi.stubEnv("GEMINI_API_KEY", "test-only-key"); const request = vi.fn().mockResolvedValue(new Response("", { status: 429 })); vi.stubGlobal("fetch", request); const result = await answerWithGemini([{ role: "user", text: "Hello" }]); expect(request).toHaveBeenCalledTimes(1); expect(request.mock.calls[0]?.[0]).toBe("https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent"); expect(result.answer).toBeNull(); expect(result.error).toContain("quota is reached"); }); it("rejects arbitrary external tasks from the model", async () => { vi.stubEnv("GEMINI_API_KEY", "test-only-key"); vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ candidates: [{ finishReason: "STOP", content: { role: "model", parts: [{ functionCall: { name: "run_shell", args: { command: "touch file" } } }] } }] }))); const result = await answerWithGemini([{ role: "user", text: "Run code" }]); expect(result.answer).toBeNull(); expect(result.error).toBe("Echo requested an unsupported action."); }); });