|
1 | | -import { describe, expect, it } from "bun:test"; |
2 | | -import { runTerraformApply, runTerraformInit } from "~test"; |
| 1 | +import { |
| 2 | + describe, |
| 3 | + expect, |
| 4 | + it, |
| 5 | + beforeAll, |
| 6 | + afterEach, |
| 7 | + setDefaultTimeout, |
| 8 | +} from "bun:test"; |
| 9 | +import { |
| 10 | + runTerraformApply, |
| 11 | + runTerraformInit, |
| 12 | + runContainer, |
| 13 | + execContainer, |
| 14 | + removeContainer, |
| 15 | + findResourceInstance, |
| 16 | +} from "~test"; |
| 17 | + |
| 18 | +// Set timeout to 2 minutes for tests that install packages |
| 19 | +setDefaultTimeout(2 * 60 * 1000); |
| 20 | + |
| 21 | +let cleanupContainers: string[] = []; |
| 22 | + |
| 23 | +afterEach(async () => { |
| 24 | + for (const id of cleanupContainers) { |
| 25 | + try { |
| 26 | + await removeContainer(id); |
| 27 | + } catch { |
| 28 | + // Ignore cleanup errors |
| 29 | + } |
| 30 | + } |
| 31 | + cleanupContainers = []; |
| 32 | +}); |
3 | 33 |
|
4 | 34 | describe("vscode-web", async () => { |
5 | | - await runTerraformInit(import.meta.dir); |
| 35 | + beforeAll(async () => { |
| 36 | + await runTerraformInit(import.meta.dir); |
| 37 | + }); |
6 | 38 |
|
7 | | - it("accept_license should be set to true", () => { |
8 | | - const t = async () => { |
| 39 | + it("accept_license should be set to true", async () => { |
| 40 | + try { |
9 | 41 | await runTerraformApply(import.meta.dir, { |
10 | 42 | agent_id: "foo", |
11 | | - accept_license: "false", |
| 43 | + accept_license: false, |
12 | 44 | }); |
13 | | - }; |
14 | | - expect(t).toThrow("Invalid value for variable"); |
| 45 | + throw new Error("Expected terraform apply to fail"); |
| 46 | + } catch (ex) { |
| 47 | + expect((ex as Error).message).toContain("Invalid value for variable"); |
| 48 | + } |
15 | 49 | }); |
16 | 50 |
|
17 | | - it("use_cached and offline can not be used together", () => { |
18 | | - const t = async () => { |
| 51 | + it("use_cached and offline can not be used together", async () => { |
| 52 | + try { |
19 | 53 | await runTerraformApply(import.meta.dir, { |
20 | 54 | agent_id: "foo", |
21 | | - accept_license: "true", |
22 | | - use_cached: "true", |
23 | | - offline: "true", |
| 55 | + accept_license: true, |
| 56 | + use_cached: true, |
| 57 | + offline: true, |
24 | 58 | }); |
25 | | - }; |
26 | | - expect(t).toThrow("Offline and Use Cached can not be used together"); |
| 59 | + throw new Error("Expected terraform apply to fail"); |
| 60 | + } catch (ex) { |
| 61 | + expect((ex as Error).message).toContain( |
| 62 | + "Offline and Use Cached can not be used together", |
| 63 | + ); |
| 64 | + } |
27 | 65 | }); |
28 | 66 |
|
29 | | - it("offline and extensions can not be used together", () => { |
30 | | - const t = async () => { |
| 67 | + it("offline and extensions can not be used together", async () => { |
| 68 | + try { |
31 | 69 | await runTerraformApply(import.meta.dir, { |
32 | 70 | agent_id: "foo", |
33 | | - accept_license: "true", |
34 | | - offline: "true", |
35 | | - extensions: '["1", "2"]', |
| 71 | + accept_license: true, |
| 72 | + offline: true, |
| 73 | + extensions: '["ms-python.python"]', |
36 | 74 | }); |
37 | | - }; |
38 | | - expect(t).toThrow("Offline mode does not allow extensions to be installed"); |
| 75 | + throw new Error("Expected terraform apply to fail"); |
| 76 | + } catch (ex) { |
| 77 | + expect((ex as Error).message).toContain( |
| 78 | + "Offline mode does not allow extensions to be installed", |
| 79 | + ); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + it("creates settings file with correct content", async () => { |
| 84 | + const state = await runTerraformApply(import.meta.dir, { |
| 85 | + agent_id: "foo", |
| 86 | + accept_license: true, |
| 87 | + use_cached: true, |
| 88 | + settings: '{"editor.fontSize": 14}', |
| 89 | + }); |
| 90 | + |
| 91 | + const containerId = await runContainer("ubuntu:22.04"); |
| 92 | + cleanupContainers.push(containerId); |
| 93 | + |
| 94 | + // Create a mock code-server CLI that the script expects |
| 95 | + await execContainer(containerId, [ |
| 96 | + "bash", |
| 97 | + "-c", |
| 98 | + `mkdir -p /tmp/vscode-web/bin && cat > /tmp/vscode-web/bin/code-server << 'MOCKEOF' |
| 99 | +#!/bin/bash |
| 100 | +echo "Mock code-server running" |
| 101 | +exit 0 |
| 102 | +MOCKEOF |
| 103 | +chmod +x /tmp/vscode-web/bin/code-server`, |
| 104 | + ]); |
| 105 | + |
| 106 | + const script = findResourceInstance(state, "coder_script"); |
| 107 | + |
| 108 | + const scriptResult = await execContainer(containerId, [ |
| 109 | + "bash", |
| 110 | + "-c", |
| 111 | + script.script, |
| 112 | + ]); |
| 113 | + expect(scriptResult.exitCode).toBe(0); |
| 114 | + |
| 115 | + // Check that settings file was created |
| 116 | + const settingsResult = await execContainer(containerId, [ |
| 117 | + "cat", |
| 118 | + "/root/.vscode-server/data/Machine/settings.json", |
| 119 | + ]); |
| 120 | + |
| 121 | + expect(settingsResult.exitCode).toBe(0); |
| 122 | + expect(settingsResult.stdout).toContain("editor.fontSize"); |
| 123 | + expect(settingsResult.stdout).toContain("14"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("merges settings with existing settings file", async () => { |
| 127 | + const state = await runTerraformApply(import.meta.dir, { |
| 128 | + agent_id: "foo", |
| 129 | + accept_license: true, |
| 130 | + use_cached: true, |
| 131 | + settings: '{"new.setting": "new_value"}', |
| 132 | + }); |
| 133 | + |
| 134 | + const containerId = await runContainer("ubuntu:22.04"); |
| 135 | + cleanupContainers.push(containerId); |
| 136 | + |
| 137 | + // Install jq and create mock code-server CLI |
| 138 | + await execContainer(containerId, ["apt-get", "update", "-qq"]); |
| 139 | + await execContainer(containerId, ["apt-get", "install", "-y", "-qq", "jq"]); |
| 140 | + await execContainer(containerId, [ |
| 141 | + "bash", |
| 142 | + "-c", |
| 143 | + `mkdir -p /tmp/vscode-web/bin && cat > /tmp/vscode-web/bin/code-server << 'MOCKEOF' |
| 144 | +#!/bin/bash |
| 145 | +echo "Mock code-server running" |
| 146 | +exit 0 |
| 147 | +MOCKEOF |
| 148 | +chmod +x /tmp/vscode-web/bin/code-server`, |
| 149 | + ]); |
| 150 | + |
| 151 | + // Pre-create an existing settings file |
| 152 | + await execContainer(containerId, [ |
| 153 | + "bash", |
| 154 | + "-c", |
| 155 | + `mkdir -p /root/.vscode-server/data/Machine && echo '{"existing.setting": "existing_value"}' > /root/.vscode-server/data/Machine/settings.json`, |
| 156 | + ]); |
| 157 | + |
| 158 | + const script = findResourceInstance(state, "coder_script"); |
| 159 | + |
| 160 | + const scriptResult = await execContainer(containerId, [ |
| 161 | + "bash", |
| 162 | + "-c", |
| 163 | + script.script, |
| 164 | + ]); |
| 165 | + expect(scriptResult.exitCode).toBe(0); |
| 166 | + |
| 167 | + // Check that settings were merged (both existing and new should be present) |
| 168 | + const settingsResult = await execContainer(containerId, [ |
| 169 | + "cat", |
| 170 | + "/root/.vscode-server/data/Machine/settings.json", |
| 171 | + ]); |
| 172 | + |
| 173 | + expect(settingsResult.exitCode).toBe(0); |
| 174 | + // Should contain both existing and new settings |
| 175 | + expect(settingsResult.stdout).toContain("existing.setting"); |
| 176 | + expect(settingsResult.stdout).toContain("existing_value"); |
| 177 | + expect(settingsResult.stdout).toContain("new.setting"); |
| 178 | + expect(settingsResult.stdout).toContain("new_value"); |
| 179 | + }); |
| 180 | + |
| 181 | + it("merges settings using python3 fallback when jq unavailable", async () => { |
| 182 | + const state = await runTerraformApply(import.meta.dir, { |
| 183 | + agent_id: "foo", |
| 184 | + accept_license: true, |
| 185 | + use_cached: true, |
| 186 | + settings: '{"new.setting": "new_value"}', |
| 187 | + }); |
| 188 | + |
| 189 | + const containerId = await runContainer("ubuntu:22.04"); |
| 190 | + cleanupContainers.push(containerId); |
| 191 | + |
| 192 | + // Install python3 (ubuntu:22.04 doesn't have it by default) |
| 193 | + await execContainer(containerId, ["apt-get", "update", "-qq"]); |
| 194 | + await execContainer(containerId, [ |
| 195 | + "apt-get", |
| 196 | + "install", |
| 197 | + "-y", |
| 198 | + "-qq", |
| 199 | + "python3", |
| 200 | + ]); |
| 201 | + |
| 202 | + // Create mock code-server CLI (no jq installed) |
| 203 | + await execContainer(containerId, [ |
| 204 | + "bash", |
| 205 | + "-c", |
| 206 | + `mkdir -p /tmp/vscode-web/bin && cat > /tmp/vscode-web/bin/code-server << 'MOCKEOF' |
| 207 | +#!/bin/bash |
| 208 | +echo "Mock code-server running" |
| 209 | +exit 0 |
| 210 | +MOCKEOF |
| 211 | +chmod +x /tmp/vscode-web/bin/code-server`, |
| 212 | + ]); |
| 213 | + |
| 214 | + // Pre-create an existing settings file |
| 215 | + await execContainer(containerId, [ |
| 216 | + "bash", |
| 217 | + "-c", |
| 218 | + `mkdir -p /root/.vscode-server/data/Machine && echo '{"existing.setting": "existing_value"}' > /root/.vscode-server/data/Machine/settings.json`, |
| 219 | + ]); |
| 220 | + |
| 221 | + const script = findResourceInstance(state, "coder_script"); |
| 222 | + |
| 223 | + const scriptResult = await execContainer(containerId, [ |
| 224 | + "bash", |
| 225 | + "-c", |
| 226 | + script.script, |
| 227 | + ]); |
| 228 | + expect(scriptResult.exitCode).toBe(0); |
| 229 | + |
| 230 | + // Check that settings were merged using python3 fallback |
| 231 | + const settingsResult = await execContainer(containerId, [ |
| 232 | + "cat", |
| 233 | + "/root/.vscode-server/data/Machine/settings.json", |
| 234 | + ]); |
| 235 | + |
| 236 | + expect(settingsResult.exitCode).toBe(0); |
| 237 | + // Should contain both existing and new settings |
| 238 | + expect(settingsResult.stdout).toContain("existing.setting"); |
| 239 | + expect(settingsResult.stdout).toContain("existing_value"); |
| 240 | + expect(settingsResult.stdout).toContain("new.setting"); |
| 241 | + expect(settingsResult.stdout).toContain("new_value"); |
39 | 242 | }); |
40 | 243 |
|
41 | | - // More tests depend on shebang refactors |
| 244 | + it("preserves existing settings when neither jq nor python3 available", async () => { |
| 245 | + const state = await runTerraformApply(import.meta.dir, { |
| 246 | + agent_id: "foo", |
| 247 | + accept_license: true, |
| 248 | + use_cached: true, |
| 249 | + settings: '{"new.setting": "new_value"}', |
| 250 | + }); |
| 251 | + |
| 252 | + // Use ubuntu without installing jq or python3 (neither available by default) |
| 253 | + const containerId = await runContainer("ubuntu:22.04"); |
| 254 | + cleanupContainers.push(containerId); |
| 255 | + |
| 256 | + // Create mock code-server CLI |
| 257 | + await execContainer(containerId, [ |
| 258 | + "bash", |
| 259 | + "-c", |
| 260 | + `mkdir -p /tmp/vscode-web/bin && cat > /tmp/vscode-web/bin/code-server << 'MOCKEOF' |
| 261 | +#!/bin/bash |
| 262 | +echo "Mock code-server running" |
| 263 | +exit 0 |
| 264 | +MOCKEOF |
| 265 | +chmod +x /tmp/vscode-web/bin/code-server`, |
| 266 | + ]); |
| 267 | + |
| 268 | + // Pre-create an existing settings file |
| 269 | + await execContainer(containerId, [ |
| 270 | + "bash", |
| 271 | + "-c", |
| 272 | + `mkdir -p /root/.vscode-server/data/Machine && echo '{"existing.setting": "existing_value"}' > /root/.vscode-server/data/Machine/settings.json`, |
| 273 | + ]); |
| 274 | + |
| 275 | + const script = findResourceInstance(state, "coder_script"); |
| 276 | + |
| 277 | + // Run script - should warn but not fail |
| 278 | + const scriptResult = await execContainer(containerId, [ |
| 279 | + "bash", |
| 280 | + "-c", |
| 281 | + script.script, |
| 282 | + ]); |
| 283 | + expect(scriptResult.exitCode).toBe(0); |
| 284 | + expect(scriptResult.stdout).toContain("Could not merge settings"); |
| 285 | + |
| 286 | + // Existing settings should be preserved (not overwritten) |
| 287 | + const settingsResult = await execContainer(containerId, [ |
| 288 | + "cat", |
| 289 | + "/root/.vscode-server/data/Machine/settings.json", |
| 290 | + ]); |
| 291 | + |
| 292 | + expect(settingsResult.exitCode).toBe(0); |
| 293 | + expect(settingsResult.stdout).toContain("existing.setting"); |
| 294 | + expect(settingsResult.stdout).toContain("existing_value"); |
| 295 | + expect(settingsResult.stdout).not.toContain("new.setting"); |
| 296 | + expect(settingsResult.stdout).not.toContain("new_value"); |
| 297 | + }); |
42 | 298 | }); |
0 commit comments