|
| 1 | +import { |
| 2 | + test, |
| 3 | + afterEach, |
| 4 | + describe, |
| 5 | + setDefaultTimeout, |
| 6 | + beforeAll, |
| 7 | + expect, |
| 8 | +} from "bun:test"; |
| 9 | +import { execContainer, readFileContainer, runTerraformInit } from "~test"; |
| 10 | +import { |
| 11 | + loadTestFile, |
| 12 | + writeExecutable, |
| 13 | + setup as setupUtil, |
| 14 | + execModuleScript, |
| 15 | + expectAgentAPIStarted, |
| 16 | +} from "../../../coder/modules/agentapi/test-util"; |
| 17 | + |
| 18 | +let cleanupFunctions: (() => Promise<void>)[] = []; |
| 19 | +const registerCleanup = (cleanup: () => Promise<void>) => { |
| 20 | + cleanupFunctions.push(cleanup); |
| 21 | +}; |
| 22 | +afterEach(async () => { |
| 23 | + const cleanupFnsCopy = cleanupFunctions.slice().reverse(); |
| 24 | + cleanupFunctions = []; |
| 25 | + for (const cleanup of cleanupFnsCopy) { |
| 26 | + try { |
| 27 | + await cleanup(); |
| 28 | + } catch (error) { |
| 29 | + console.error("Error during cleanup:", error); |
| 30 | + } |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +interface SetupProps { |
| 35 | + skipAgentAPIMock?: boolean; |
| 36 | + skipGeminiMock?: boolean; |
| 37 | + moduleVariables?: Record<string, string>; |
| 38 | + agentapiMockScript?: string; |
| 39 | +} |
| 40 | + |
| 41 | +const setup = async (props?: SetupProps): Promise<{ id: string }> => { |
| 42 | + const projectDir = "/home/coder/project"; |
| 43 | + const { id } = await setupUtil({ |
| 44 | + moduleDir: import.meta.dir, |
| 45 | + moduleVariables: { |
| 46 | + install_gemini: props?.skipGeminiMock ? "true" : "false", |
| 47 | + install_agentapi: props?.skipAgentAPIMock ? "true" : "false", |
| 48 | + gemini_model: "test-model", |
| 49 | + ...props?.moduleVariables, |
| 50 | + }, |
| 51 | + registerCleanup, |
| 52 | + projectDir, |
| 53 | + skipAgentAPIMock: props?.skipAgentAPIMock, |
| 54 | + agentapiMockScript: props?.agentapiMockScript, |
| 55 | + }); |
| 56 | + if (!props?.skipGeminiMock) { |
| 57 | + await writeExecutable({ |
| 58 | + containerId: id, |
| 59 | + filePath: "/usr/bin/gemini", |
| 60 | + content: await loadTestFile(import.meta.dir, "gemini-mock.sh"), |
| 61 | + }); |
| 62 | + } |
| 63 | + return { id }; |
| 64 | +}; |
| 65 | + |
| 66 | +setDefaultTimeout(60 * 1000); |
| 67 | + |
| 68 | +describe("gemini", async () => { |
| 69 | + beforeAll(async () => { |
| 70 | + await runTerraformInit(import.meta.dir); |
| 71 | + }); |
| 72 | + |
| 73 | + test("happy-path", async () => { |
| 74 | + const { id } = await setup(); |
| 75 | + await execModuleScript(id); |
| 76 | + await expectAgentAPIStarted(id); |
| 77 | + }); |
| 78 | + |
| 79 | + test("install-gemini-version", async () => { |
| 80 | + const version_to_install = "0.1.13"; |
| 81 | + const { id } = await setup({ |
| 82 | + skipGeminiMock: true, |
| 83 | + moduleVariables: { |
| 84 | + install_gemini: "true", |
| 85 | + gemini_version: version_to_install, |
| 86 | + }, |
| 87 | + }); |
| 88 | + await execModuleScript(id); |
| 89 | + const resp = await execContainer(id, [ |
| 90 | + "bash", |
| 91 | + "-c", |
| 92 | + `cat /home/coder/.gemini-module/install.log || true`, |
| 93 | + ]); |
| 94 | + expect(resp.stdout).toContain(version_to_install); |
| 95 | + }); |
| 96 | + |
| 97 | + test("gemini-settings-json", async () => { |
| 98 | + const settings = '{"foo": "bar"}'; |
| 99 | + const { id } = await setup({ |
| 100 | + moduleVariables: { |
| 101 | + gemini_settings_json: settings, |
| 102 | + }, |
| 103 | + }); |
| 104 | + await execModuleScript(id); |
| 105 | + const resp = await readFileContainer(id, "/home/coder/.gemini/settings.json"); |
| 106 | + expect(resp).toContain("foo"); |
| 107 | + expect(resp).toContain("bar"); |
| 108 | + }); |
| 109 | + |
| 110 | + test("gemini-api-key", async () => { |
| 111 | + const apiKey = "test-api-key-123"; |
| 112 | + const { id } = await setup({ |
| 113 | + moduleVariables: { |
| 114 | + gemini_api_key: apiKey, |
| 115 | + }, |
| 116 | + }); |
| 117 | + await execModuleScript(id); |
| 118 | + |
| 119 | + const resp = await readFileContainer(id, "/home/coder/.gemini-module/agentapi-start.log"); |
| 120 | + expect(resp).toContain("gemini_api_key provided !"); |
| 121 | + }); |
| 122 | + |
| 123 | + test("use-vertexai", async () => { |
| 124 | + const { id } = await setup({ |
| 125 | + skipGeminiMock: false, |
| 126 | + moduleVariables: { |
| 127 | + use_vertexai: "true", |
| 128 | + }, |
| 129 | + }); |
| 130 | + await execModuleScript(id); |
| 131 | + const resp = await readFileContainer(id, "/home/coder/.gemini-module/install.log"); |
| 132 | + expect(resp).toContain('GOOGLE_GENAI_USE_VERTEXAI=\'true\''); |
| 133 | + }); |
| 134 | + |
| 135 | + test("gemini-model", async () => { |
| 136 | + const model = "gemini-2.5-pro"; |
| 137 | + const { id } = await setup({ |
| 138 | + skipGeminiMock: false, |
| 139 | + moduleVariables: { |
| 140 | + gemini_model: model, |
| 141 | + }, |
| 142 | + }); |
| 143 | + await execModuleScript(id); |
| 144 | + const resp = await readFileContainer(id, "/home/coder/.gemini-module/install.log"); |
| 145 | + expect(resp).toContain(model); |
| 146 | + }); |
| 147 | + |
| 148 | + test("pre-post-install-scripts", async () => { |
| 149 | + const { id } = await setup({ |
| 150 | + moduleVariables: { |
| 151 | + pre_install_script: "#!/bin/bash\necho 'pre-install-script'", |
| 152 | + post_install_script: "#!/bin/bash\necho 'post-install-script'", |
| 153 | + }, |
| 154 | + }); |
| 155 | + await execModuleScript(id); |
| 156 | + const preInstallLog = await readFileContainer(id, "/home/coder/.gemini-module/pre_install.log"); |
| 157 | + expect(preInstallLog).toContain("pre-install-script"); |
| 158 | + const postInstallLog = await readFileContainer(id, "/home/coder/.gemini-module/post_install.log"); |
| 159 | + expect(postInstallLog).toContain("post-install-script"); |
| 160 | + }); |
| 161 | + |
| 162 | + test("folder-variable", async () => { |
| 163 | + const folder = "/tmp/gemini-test-folder"; |
| 164 | + const { id } = await setup({ |
| 165 | + skipGeminiMock: false, |
| 166 | + moduleVariables: { |
| 167 | + folder, |
| 168 | + }, |
| 169 | + }); |
| 170 | + await execModuleScript(id); |
| 171 | + const resp = await readFileContainer(id, "/home/coder/.gemini-module/install.log"); |
| 172 | + expect(resp).toContain(folder); |
| 173 | + }); |
| 174 | + |
| 175 | + test("additional-extensions", async () => { |
| 176 | + const additional = '{"custom": {"enabled": true}}'; |
| 177 | + const { id } = await setup({ |
| 178 | + moduleVariables: { |
| 179 | + additional_extensions: additional, |
| 180 | + }, |
| 181 | + }); |
| 182 | + await execModuleScript(id); |
| 183 | + const resp = await readFileContainer(id, "/home/coder/.gemini/settings.json"); |
| 184 | + expect(resp).toContain("custom"); |
| 185 | + expect(resp).toContain("enabled"); |
| 186 | + }); |
| 187 | + |
| 188 | + test("gemini-system-prompt", async () => { |
| 189 | + const prompt = "This is a system prompt for Gemini."; |
| 190 | + const { id } = await setup({ |
| 191 | + moduleVariables: { |
| 192 | + gemini_system_prompt: prompt, |
| 193 | + }, |
| 194 | + }); |
| 195 | + await execModuleScript(id); |
| 196 | + const resp = await readFileContainer(id, "/home/coder/GEMINI.md"); |
| 197 | + expect(resp).toContain(prompt); |
| 198 | + }); |
| 199 | + |
| 200 | + test("start-without-prompt", async () => { |
| 201 | + const { id } = await setup(); |
| 202 | + await execModuleScript(id); |
| 203 | + const prompt = await execContainer(id, ["ls", "-l", "/home/coder/GEMINI.md"]); |
| 204 | + expect(prompt.exitCode).not.toBe(0); |
| 205 | + expect(prompt.stderr).toContain("No such file or directory"); |
| 206 | + }); |
| 207 | +}); |
0 commit comments