|
| 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 | + skipAmpMock?: 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_sourcegraph_amp: props?.skipAmpMock ? "true" : "false", |
| 47 | + install_agentapi: props?.skipAgentAPIMock ? "true" : "false", |
| 48 | + sourcegraph_amp_model: "test-model", |
| 49 | + ...props?.moduleVariables, |
| 50 | + }, |
| 51 | + registerCleanup, |
| 52 | + projectDir, |
| 53 | + skipAgentAPIMock: props?.skipAgentAPIMock, |
| 54 | + agentapiMockScript: props?.agentapiMockScript, |
| 55 | + }); |
| 56 | + |
| 57 | + // Place the AMP mock CLI binary inside the container |
| 58 | + if (!props?.skipAmpMock) { |
| 59 | + await writeExecutable({ |
| 60 | + containerId: id, |
| 61 | + filePath: "/usr/bin/amp", |
| 62 | + content: await loadTestFile(`${import.meta.dir}`, "amp-mock.sh"), |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return { id }; |
| 67 | +}; |
| 68 | + |
| 69 | +setDefaultTimeout(60 * 1000); |
| 70 | + |
| 71 | +describe("sourcegraph-amp", async () => { |
| 72 | + beforeAll(async () => { |
| 73 | + await runTerraformInit(import.meta.dir); |
| 74 | + }); |
| 75 | + |
| 76 | + test("happy-path", async () => { |
| 77 | + const { id } = await setup(); |
| 78 | + await execModuleScript(id); |
| 79 | + await expectAgentAPIStarted(id); |
| 80 | + }); |
| 81 | + |
| 82 | + test("api-key", async () => { |
| 83 | + const apiKey = "test-api-key-123"; |
| 84 | + const { id } = await setup({ |
| 85 | + moduleVariables: { |
| 86 | + sourcegraph_amp_api_key: apiKey, |
| 87 | + }, |
| 88 | + }); |
| 89 | + await execModuleScript(id); |
| 90 | + const resp = await readFileContainer( |
| 91 | + id, |
| 92 | + "/home/coder/.sourcegraph-amp-module/agentapi-start.log", |
| 93 | + ); |
| 94 | + expect(resp).toContain("sourcegraph_amp_api_key provided !"); |
| 95 | + }); |
| 96 | + |
| 97 | + test("custom-folder", async () => { |
| 98 | + const folder = "/tmp/sourcegraph-amp-test"; |
| 99 | + const { id } = await setup({ |
| 100 | + moduleVariables: { |
| 101 | + folder, |
| 102 | + }, |
| 103 | + }); |
| 104 | + await execModuleScript(id); |
| 105 | + const resp = await readFileContainer( |
| 106 | + id, |
| 107 | + "/home/coder/.sourcegraph-amp-module/install.log", |
| 108 | + ); |
| 109 | + expect(resp).toContain(folder); |
| 110 | + }); |
| 111 | + |
| 112 | + test("pre-post-install-scripts", async () => { |
| 113 | + const { id } = await setup({ |
| 114 | + moduleVariables: { |
| 115 | + pre_install_script: "#!/bin/bash\necho 'pre-install-script'", |
| 116 | + post_install_script: "#!/bin/bash\necho 'post-install-script'", |
| 117 | + }, |
| 118 | + }); |
| 119 | + await execModuleScript(id); |
| 120 | + const preLog = await readFileContainer( |
| 121 | + id, |
| 122 | + "/home/coder/.sourcegraph-amp-module/pre_install.log", |
| 123 | + ); |
| 124 | + expect(preLog).toContain("pre-install-script"); |
| 125 | + const postLog = await readFileContainer( |
| 126 | + id, |
| 127 | + "/home/coder/.sourcegraph-amp-module/post_install.log", |
| 128 | + ); |
| 129 | + expect(postLog).toContain("post-install-script"); |
| 130 | + }); |
| 131 | + |
| 132 | + test("system-prompt", async () => { |
| 133 | + const prompt = "this is a system prompt for AMP"; |
| 134 | + const { id } = await setup(); |
| 135 | + await execModuleScript(id, { |
| 136 | + SOURCEGRAPH_AMP_SYSTEM_PROMPT: prompt, |
| 137 | + }); |
| 138 | + const resp = await readFileContainer( |
| 139 | + id, |
| 140 | + "/home/coder/.sourcegraph-amp-module/SYSTEM_PROMPT.md", |
| 141 | + ); |
| 142 | + expect(resp).toContain(prompt); |
| 143 | + }); |
| 144 | + |
| 145 | + test("task-prompt", async () => { |
| 146 | + const prompt = "this is a task prompt for AMP"; |
| 147 | + const { id } = await setup(); |
| 148 | + await execModuleScript(id, { |
| 149 | + SOURCEGRAPH_AMP_TASK_PROMPT: prompt, |
| 150 | + }); |
| 151 | + const resp = await readFileContainer( |
| 152 | + id, |
| 153 | + "/home/coder/.sourcegraph-amp-module/agentapi-start.log", |
| 154 | + ); |
| 155 | + expect(resp).toContain(`sourcegraph amp task prompt provided : ${prompt}`); |
| 156 | + }); |
| 157 | +}); |
0 commit comments