|
| 1 | +import { |
| 2 | + test, |
| 3 | + afterEach, |
| 4 | + expect, |
| 5 | + describe, |
| 6 | + setDefaultTimeout, |
| 7 | + beforeAll, |
| 8 | +} from "bun:test"; |
| 9 | +import { execContainer, runTerraformInit } from "~test"; |
| 10 | +import { |
| 11 | + setupContainer, |
| 12 | + loadTestFile, |
| 13 | + writeExecutable, |
| 14 | + execModuleScript, |
| 15 | + expectAgentAPIStarted, |
| 16 | +} from "./test-util"; |
| 17 | + |
| 18 | +let cleanupFunctions: (() => Promise<void>)[] = []; |
| 19 | + |
| 20 | +const registerCleanup = (cleanup: () => Promise<void>) => { |
| 21 | + cleanupFunctions.push(cleanup); |
| 22 | +}; |
| 23 | + |
| 24 | +afterEach(async () => { |
| 25 | + const cleanupFnsCopy = cleanupFunctions.slice().reverse(); |
| 26 | + cleanupFunctions = []; |
| 27 | + for (const cleanup of cleanupFnsCopy) { |
| 28 | + try { |
| 29 | + await cleanup(); |
| 30 | + } catch (error) { |
| 31 | + console.error("Error during cleanup:", error); |
| 32 | + } |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +const moduleDir = import.meta.dir; |
| 37 | + |
| 38 | +beforeAll(async () => { |
| 39 | + await runTerraformInit(moduleDir); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("codex", () => { |
| 43 | + test("creates codex module with default configuration", async () => { |
| 44 | + const { id, coderScript, cleanup } = await setupContainer({ |
| 45 | + moduleDir, |
| 46 | + image: "codercom/enterprise-node:latest", |
| 47 | + }); |
| 48 | + registerCleanup(cleanup); |
| 49 | + |
| 50 | + // Execute the module script to install the mock CLI |
| 51 | + const scriptResult = await execModuleScript({ |
| 52 | + containerId: id, |
| 53 | + coderScript, |
| 54 | + }); |
| 55 | + expect(scriptResult.exitCode).toBe(0); |
| 56 | + |
| 57 | + // Test that the module installs correctly |
| 58 | + const result = await execContainer(id, ["which", "codex-cli"]); |
| 59 | + expect(result.exitCode).toBe(0); |
| 60 | + }); |
| 61 | + |
| 62 | + test("creates codex module with custom configuration", async () => { |
| 63 | + const { id, coderScript, cleanup } = await setupContainer({ |
| 64 | + moduleDir, |
| 65 | + image: "codercom/enterprise-node:latest", |
| 66 | + vars: { |
| 67 | + openai_model: "gpt-4", |
| 68 | + temperature: "0.7", |
| 69 | + max_tokens: "2048", |
| 70 | + folder: "/workspace", |
| 71 | + install_codex: "true", |
| 72 | + codex_version: "latest", |
| 73 | + order: "1", |
| 74 | + group: "AI Tools", |
| 75 | + }, |
| 76 | + }); |
| 77 | + registerCleanup(cleanup); |
| 78 | + |
| 79 | + // Execute the module script to install the mock CLI |
| 80 | + const scriptResult = await execModuleScript({ |
| 81 | + containerId: id, |
| 82 | + coderScript, |
| 83 | + }); |
| 84 | + expect(scriptResult.exitCode).toBe(0); |
| 85 | + |
| 86 | + // Test that the module installs correctly with custom configuration |
| 87 | + const result = await execContainer(id, ["which", "codex-cli"]); |
| 88 | + expect(result.exitCode).toBe(0); |
| 89 | + |
| 90 | + // Test that configuration is properly set |
| 91 | + const configResult = await execContainer(id, ["test", "-f", "/home/coder/.config/codex/config.toml"]); |
| 92 | + expect(configResult.exitCode).toBe(0); |
| 93 | + }); |
| 94 | + |
| 95 | + test("creates codex module with custom API key", async () => { |
| 96 | + const { id, coderScript, cleanup } = await setupContainer({ |
| 97 | + moduleDir, |
| 98 | + image: "codercom/enterprise-node:latest", |
| 99 | + vars: { |
| 100 | + openai_api_key: "sk-test-api-key", |
| 101 | + openai_model: "gpt-3.5-turbo", |
| 102 | + }, |
| 103 | + }); |
| 104 | + registerCleanup(cleanup); |
| 105 | + |
| 106 | + // Execute the module script to install the mock CLI |
| 107 | + const scriptResult = await execModuleScript({ |
| 108 | + containerId: id, |
| 109 | + coderScript, |
| 110 | + }); |
| 111 | + expect(scriptResult.exitCode).toBe(0); |
| 112 | + |
| 113 | + // Test that the module installs correctly |
| 114 | + const result = await execContainer(id, ["which", "codex-cli"]); |
| 115 | + expect(result.exitCode).toBe(0); |
| 116 | + }); |
| 117 | + |
| 118 | + test("creates codex module with installation disabled", async () => { |
| 119 | + const { id, cleanup } = await setupContainer({ |
| 120 | + moduleDir, |
| 121 | + image: "codercom/enterprise-node:latest", |
| 122 | + vars: { |
| 123 | + install_codex: "false", |
| 124 | + }, |
| 125 | + }); |
| 126 | + registerCleanup(cleanup); |
| 127 | + |
| 128 | + // Test that codex-cli is not installed when disabled |
| 129 | + const result = await execContainer(id, ["which", "codex-cli"]); |
| 130 | + expect(result.exitCode).toBe(1); |
| 131 | + }); |
| 132 | + |
| 133 | + test("validates temperature range", async () => { |
| 134 | + // Test with invalid temperature (should fail during terraform plan/apply) |
| 135 | + try { |
| 136 | + await setupContainer({ |
| 137 | + moduleDir, |
| 138 | + image: "codercom/enterprise-node:latest", |
| 139 | + vars: { |
| 140 | + temperature: "2.5", // Invalid - should be between 0.0 and 2.0 |
| 141 | + }, |
| 142 | + }); |
| 143 | + expect(true).toBe(false); // Should not reach here |
| 144 | + } catch (error) { |
| 145 | + expect((error as Error).message).toContain("Temperature must be between 0.0 and 2.0"); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + test("validates max_tokens range", async () => { |
| 150 | + // Test with invalid max_tokens (should fail during terraform plan/apply) |
| 151 | + try { |
| 152 | + await setupContainer({ |
| 153 | + moduleDir, |
| 154 | + image: "codercom/enterprise-node:latest", |
| 155 | + vars: { |
| 156 | + max_tokens: "5000", // Invalid - should be between 1 and 4096 |
| 157 | + }, |
| 158 | + }); |
| 159 | + expect(true).toBe(false); // Should not reach here |
| 160 | + } catch (error) { |
| 161 | + expect((error as Error).message).toContain("Max tokens must be between 1 and 4096"); |
| 162 | + } |
| 163 | + }); |
| 164 | +}); |
0 commit comments