|
| 1 | +import { test, afterEach, describe, setDefaultTimeout, beforeAll, expect } from "bun:test"; |
| 2 | +import { execContainer, readFileContainer, runTerraformInit, runTerraformApply, writeFileContainer, runContainer, removeContainer, findResourceInstance } from "~test"; |
| 3 | +import dedent from "dedent"; |
| 4 | + |
| 5 | +let cleanupFunctions: (() => Promise<void>)[] = []; |
| 6 | +const registerCleanup = (cleanup: () => Promise<void>) => { |
| 7 | + cleanupFunctions.push(cleanup); |
| 8 | +}; |
| 9 | + |
| 10 | +afterEach(async () => { |
| 11 | + const cleanupFnsCopy = cleanupFunctions.slice().reverse(); |
| 12 | + cleanupFunctions = []; |
| 13 | + for (const cleanup of cleanupFnsCopy) { |
| 14 | + try { |
| 15 | + await cleanup(); |
| 16 | + } catch (error) { |
| 17 | + console.error("Error during cleanup:", error); |
| 18 | + } |
| 19 | + } |
| 20 | +}); |
| 21 | + |
| 22 | +const writeExecutable = async (containerId: string, filePath: string, content: string) => { |
| 23 | + await writeFileContainer(containerId, filePath, content, { user: "root" }); |
| 24 | + await execContainer(containerId, ["bash", "-c", `chmod 755 ${filePath}`], ["--user", "root"]); |
| 25 | +}; |
| 26 | + |
| 27 | +const loadTestFile = async (...relativePath: string[]) => { |
| 28 | + return await Bun.file(new URL(`./testdata/${relativePath.join("/")}`, import.meta.url)).text(); |
| 29 | +}; |
| 30 | + |
| 31 | +const setup = async (vars?: Record<string, string>): Promise<{ id: string }> => { |
| 32 | + const state = await runTerraformApply(import.meta.dir, { |
| 33 | + agent_id: "foo", |
| 34 | + install_cursor_cli: "false", |
| 35 | + ...vars, |
| 36 | + }); |
| 37 | + const coderScript = findResourceInstance(state, "coder_script"); |
| 38 | + const id = await runContainer("codercom/enterprise-node:latest"); |
| 39 | + registerCleanup(async () => removeContainer(id)); |
| 40 | + await writeExecutable(id, "/home/coder/script.sh", coderScript.script); |
| 41 | + await writeExecutable(id, "/usr/bin/cursor", await loadTestFile("cursor-mock.sh")); |
| 42 | + return { id }; |
| 43 | +}; |
| 44 | + |
| 45 | +setDefaultTimeout(60 * 1000); |
| 46 | + |
| 47 | +describe("cursor-cli", async () => { |
| 48 | + beforeAll(async () => { |
| 49 | + await runTerraformInit(import.meta.dir); |
| 50 | + }); |
| 51 | + |
| 52 | + test("happy-path-interactive", async () => { |
| 53 | + const { id } = await setup(); |
| 54 | + const resp = await execContainer(id, ["bash", "-c", "cd /home/coder && ./script.sh"]); |
| 55 | + if (resp.exitCode !== 0) { |
| 56 | + console.log(resp.stdout); |
| 57 | + console.log(resp.stderr); |
| 58 | + } |
| 59 | + expect(resp.exitCode).toBe(0); |
| 60 | + const startLog = await readFileContainer(id, "/home/coder/.cursor-cli-module/start.log"); |
| 61 | + expect(startLog).toContain("agent"); |
| 62 | + expect(startLog).toContain("--interactive"); |
| 63 | + }); |
| 64 | + |
| 65 | + test("non-interactive-with-cmd", async () => { |
| 66 | + const { id } = await setup({ interactive: "false", non_interactive_cmd: "run --once" }); |
| 67 | + const resp = await execContainer(id, ["bash", "-c", "cd /home/coder && ./script.sh"]); |
| 68 | + expect(resp.exitCode).toBe(0); |
| 69 | + const startLog = await readFileContainer(id, "/home/coder/.cursor-cli-module/start.log"); |
| 70 | + expect(startLog).toContain("run"); |
| 71 | + expect(startLog).toContain("--once"); |
| 72 | + expect(startLog).not.toContain("--interactive"); |
| 73 | + }); |
| 74 | + |
| 75 | + test("model-and-force-and-extra-args", async () => { |
| 76 | + const { id } = await setup({ model: "test-model", force: "true" }); |
| 77 | + const resp = await execContainer(id, ["bash", "-c", "cd /home/coder && ./script.sh"], ["--env", "TF_VAR_extra_args=--foo\nbar"]); |
| 78 | + expect(resp.exitCode).toBe(0); |
| 79 | + const startLog = await readFileContainer(id, "/home/coder/.cursor-cli-module/start.log"); |
| 80 | + expect(startLog).toContain("--model"); |
| 81 | + expect(startLog).toContain("test-model"); |
| 82 | + expect(startLog).toContain("--force"); |
| 83 | + }); |
| 84 | + |
| 85 | + test("additional-settings-merge", async () => { |
| 86 | + const settings = dedent` |
| 87 | + {"mcpServers": {"coder": {"command": "coder", "args": ["exp","mcp","server"], "type": "stdio"}}} |
| 88 | + `; |
| 89 | + const { id } = await setup({ additional_settings: settings }); |
| 90 | + const resp = await execContainer(id, ["bash", "-c", "cd /home/coder && ./script.sh"]); |
| 91 | + expect(resp.exitCode).toBe(0); |
| 92 | + const cfg = await readFileContainer(id, "/home/coder/.cursor/settings.json"); |
| 93 | + expect(cfg).toContain("mcpServers"); |
| 94 | + expect(cfg).toContain("coder"); |
| 95 | + }); |
| 96 | +}); |
0 commit comments