|
| 1 | +/** |
| 2 | + * Per-engine E2E tests. |
| 3 | + * Runs the full sandbox lifecycle (start → exec → status → stop) |
| 4 | + * through each available container engine with the mock Claude API. |
| 5 | + */ |
| 6 | +import { describe, it, before, after } from 'node:test'; |
| 7 | +import assert from 'node:assert'; |
| 8 | +import { spawnSync } from 'child_process'; |
| 9 | +import os from 'os'; |
| 10 | +import path from 'path'; |
| 11 | +import { fileURLToPath } from 'url'; |
| 12 | +import { startMockServer, installMockCredentials, restoreCredentials } from '@flout/claude-mock-api'; |
| 13 | + |
| 14 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 15 | +const cli = path.resolve(__dirname, '..', '..', 'src', 'cli.ts'); |
| 16 | +const pkgRoot = path.resolve(__dirname, '..', '..'); |
| 17 | + |
| 18 | +let mockPort: number; |
| 19 | +let mockServer: { server: import('http').Server; port: number }; |
| 20 | +let credentialsBackup: string | null = null; |
| 21 | +const credentialsPath = path.join(os.homedir(), '.claude', '.credentials.json'); |
| 22 | + |
| 23 | +function flout(...args: string[]): { stdout: string; stderr: string; exitCode: number | null } { |
| 24 | + const result = spawnSync('npx', ['tsx', cli, ...args], { |
| 25 | + encoding: 'utf8', |
| 26 | + timeout: 300000, |
| 27 | + cwd: pkgRoot, |
| 28 | + env: { |
| 29 | + ...process.env, |
| 30 | + CLAUDE_CODE_API_BASE_URL: `http://127.0.0.1:${mockPort}`, |
| 31 | + }, |
| 32 | + }); |
| 33 | + return { stdout: result.stdout || '', stderr: result.stderr || '', exitCode: result.status }; |
| 34 | +} |
| 35 | + |
| 36 | +function extractContainerName(output: string): string | null { |
| 37 | + const match = output.match(/Container '(flout-\d{4}-\d{6}-[a-z0-9-]+)'/); |
| 38 | + return match ? match[1] : null; |
| 39 | +} |
| 40 | + |
| 41 | +interface EngineTestConfig { |
| 42 | + name: string; |
| 43 | + flag: string; // value for --engine |
| 44 | + skip: string | undefined; |
| 45 | + cleanupBinary: string; // binary to use for cleanup (rm -f) |
| 46 | + cleanupPrefix: string[]; // prefix args for cleanup |
| 47 | +} |
| 48 | + |
| 49 | +function isAvailable(binary: string): boolean { |
| 50 | + if (binary === 'colima-nerdctl') { |
| 51 | + return spawnSync('colima', ['status'], { stdio: 'ignore' }).status === 0; |
| 52 | + } |
| 53 | + const which = spawnSync('which', [binary], { stdio: 'ignore' }); |
| 54 | + if (which.status !== 0) return false; |
| 55 | + if (binary === 'docker' || binary === 'podman') { |
| 56 | + return spawnSync(binary, ['info'], { stdio: 'ignore', timeout: 5000 }).status === 0; |
| 57 | + } |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +const engines: EngineTestConfig[] = [ |
| 62 | + { |
| 63 | + name: 'docker', |
| 64 | + flag: 'docker', |
| 65 | + skip: !isAvailable('docker') ? 'docker not available' : undefined, |
| 66 | + cleanupBinary: 'docker', |
| 67 | + cleanupPrefix: [], |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'podman', |
| 71 | + flag: 'podman', |
| 72 | + skip: !isAvailable('podman') ? 'podman not available' : undefined, |
| 73 | + cleanupBinary: 'podman', |
| 74 | + cleanupPrefix: [], |
| 75 | + }, |
| 76 | + { |
| 77 | + name: 'containerd via colima', |
| 78 | + flag: 'containerd', |
| 79 | + skip: !isAvailable('colima-nerdctl') ? 'colima not running' : undefined, |
| 80 | + cleanupBinary: 'colima', |
| 81 | + cleanupPrefix: ['nerdctl', '--'], |
| 82 | + }, |
| 83 | +]; |
| 84 | + |
| 85 | +for (const engine of engines) { |
| 86 | + describe(`e2e: sandbox with ${engine.name}`, { skip: engine.skip }, () => { |
| 87 | + const containers: string[] = []; |
| 88 | + |
| 89 | + before(async () => { |
| 90 | + mockServer = await startMockServer(); |
| 91 | + mockPort = mockServer.port; |
| 92 | + credentialsBackup = installMockCredentials(credentialsPath); |
| 93 | + }); |
| 94 | + |
| 95 | + after(() => { |
| 96 | + mockServer.server.close(); |
| 97 | + restoreCredentials(credentialsPath, credentialsBackup); |
| 98 | + for (const c of containers) { |
| 99 | + spawnSync(engine.cleanupBinary, [...engine.cleanupPrefix, 'rm', '-f', c], { stdio: 'ignore' }); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('starts a container', () => { |
| 104 | + const result = flout('sandbox', 'start', '--name', `e2e-${engine.flag}`, '--engine', engine.flag); |
| 105 | + assert.strictEqual(result.exitCode, 0, |
| 106 | + `start failed: stdout=${result.stdout} stderr=${result.stderr}`); |
| 107 | + const name = extractContainerName(result.stdout); |
| 108 | + assert.ok(name, `should output container name, got: ${result.stdout}`); |
| 109 | + containers.push(name!); |
| 110 | + }); |
| 111 | + |
| 112 | + it('shows the container in status', () => { |
| 113 | + const result = flout('sandbox', 'status', '--engine', engine.flag); |
| 114 | + assert.strictEqual(result.exitCode, 0); |
| 115 | + assert.ok(result.stdout.includes(containers[0]), |
| 116 | + `status should list ${containers[0]}`); |
| 117 | + }); |
| 118 | + |
| 119 | + it('can exec into the container', () => { |
| 120 | + const name = containers[0]; |
| 121 | + const result = spawnSync(engine.cleanupBinary, |
| 122 | + [...engine.cleanupPrefix, 'exec', name, 'echo', 'hello-from-sandbox'], |
| 123 | + { encoding: 'utf8' }); |
| 124 | + assert.strictEqual(result.status, 0, |
| 125 | + `exec failed: ${result.stderr}`); |
| 126 | + assert.ok(result.stdout.includes('hello-from-sandbox')); |
| 127 | + }); |
| 128 | + |
| 129 | + it('stops the container', () => { |
| 130 | + const name = containers[0]; |
| 131 | + const result = flout('sandbox', 'stop', name, '--engine', engine.flag); |
| 132 | + assert.strictEqual(result.exitCode, 0, |
| 133 | + `stop failed: stdout=${result.stdout} stderr=${result.stderr}`); |
| 134 | + assert.ok(result.stdout.includes('removed')); |
| 135 | + }); |
| 136 | + |
| 137 | + it('status shows no containers after cleanup', () => { |
| 138 | + const result = flout('sandbox', 'status', '--engine', engine.flag); |
| 139 | + assert.strictEqual(result.exitCode, 0); |
| 140 | + // Container was removed, should not appear |
| 141 | + if (containers[0]) { |
| 142 | + assert.ok(!result.stdout.includes(containers[0]), |
| 143 | + `should not list removed container ${containers[0]}`); |
| 144 | + } |
| 145 | + }); |
| 146 | + }); |
| 147 | +} |
0 commit comments