| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import { execSync } from "child_process"; |
| 4 | import { existsSync, readFileSync } from "fs"; |
| 5 | import { resolve, dirname } from "path"; |
| 6 | import { fileURLToPath } from "url"; |
| 7 | |
| 8 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | |
| 10 | // Colors for output |
| 11 | const colors = { |
| 12 | green: "\x1b[32m", |
| 13 | red: "\x1b[31m", |
| 14 | yellow: "\x1b[33m", |
| 15 | reset: "\x1b[0m", |
| 16 | }; |
| 17 | |
| 18 | let testsPassed = 0; |
| 19 | let testsFailed = 0; |
| 20 | |
| 21 | function runTest(name, testFn) { |
| 22 | process.stdout.write(`Testing ${name}... `); |
| 23 | try { |
| 24 | testFn(); |
| 25 | console.log(`${colors.green}✓${colors.reset}`); |
| 26 | testsPassed++; |
| 27 | } catch (error) { |
| 28 | console.log(`${colors.red}✗${colors.reset}`); |
| 29 | console.error(` Error: ${error.message}`); |
| 30 | testsFailed++; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | function execCommand(command, options = {}) { |
| 35 | return execSync(command, { |
| 36 | cwd: __dirname, |
| 37 | encoding: "utf8", |
| 38 | stdio: "pipe", |
| 39 | ...options, |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | console.log("🧪 Running smoke tests for bundled CLI...\n"); |
| 44 | |
| 45 | // Test 1: Check if bundle exists |
| 46 | runTest("Bundle file exists", () => { |
| 47 | if (!existsSync(resolve(__dirname, "dist/index.js"))) { |
| 48 | throw new Error("dist/index.js not found"); |
| 49 | } |
| 50 | if (!existsSync(resolve(__dirname, "dist/cn.js"))) { |
| 51 | throw new Error("dist/cn.js not found"); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | // Test 2: Check if wrapper script is executable |
| 56 | runTest("Wrapper script has shebang", () => { |
| 57 | const content = readFileSync(resolve(__dirname, "dist/cn.js"), "utf8"); |
| 58 | if (!content.startsWith("#!/usr/bin/env node")) { |
| 59 | throw new Error("Wrapper script missing shebang"); |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | // Cross-platform command execution helper |
| 64 | function getCLICommand(args = "") { |
| 65 | const isWindows = process.platform === "win32"; |
| 66 | if (isWindows) { |
| 67 | return `node dist/cn.js ${args}`; |
| 68 | } else { |
| 69 | return `./dist/cn.js ${args}`; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Test 3: Version command works |
| 74 | runTest("Version command", () => { |
| 75 | const output = execCommand(getCLICommand("--version")); |
| 76 | const packageJson = JSON.parse( |
| 77 | readFileSync(resolve(__dirname, "package.json"), "utf8"), |
| 78 | ); |
| 79 | if (!output.includes(packageJson.version)) { |
| 80 | throw new Error( |
| 81 | `Version mismatch. Expected ${packageJson.version}, got: ${output}`, |
| 82 | ); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | // Test 4: Help command works |
| 87 | runTest("Help command", () => { |
| 88 | const output = execCommand(getCLICommand("--help")); |
| 89 | if (!output.includes("Continue CLI") || !output.includes("--version")) { |
| 90 | throw new Error("Help output missing expected content"); |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | // Test 5: Check bundle size |
| 95 | runTest("Bundle size is reasonable", () => { |
| 96 | const isWindows = process.platform === "win32"; |
| 97 | const command = isWindows |
| 98 | ? `powershell -Command "(Get-Item dist/index.js).length / 1MB"` |
| 99 | : `ls -lh dist/index.js`; |
| 100 | |
| 101 | let sizeInMB; |
| 102 | |
| 103 | if (isWindows) { |
| 104 | try { |
| 105 | const output = execCommand(command); |
| 106 | sizeInMB = parseFloat(output.trim()); |
| 107 | } catch { |
| 108 | // Fallback for Windows if PowerShell fails |
| 109 | const stats = readFileSync(resolve(__dirname, "dist/index.js")); |
| 110 | sizeInMB = stats.length / (1024 * 1024); |
| 111 | } |
| 112 | } else { |
| 113 | const stats = execCommand(command); |
| 114 | const sizeMatch = stats.match(/(\d+(?:\.\d+)?[MK])/); |
| 115 | if (sizeMatch) { |
| 116 | const size = sizeMatch[1]; |
| 117 | const numSize = parseFloat(size); |
| 118 | const unit = size.slice(-1); |
| 119 | sizeInMB = unit === "M" ? numSize : numSize / 1024; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | console.log(`(${sizeInMB.toFixed(1)}M)`); |
| 124 | |
| 125 | // This is arbitrary. We might go over at some point, |
| 126 | // in which case you can just increase this. |
| 127 | if (sizeInMB > 20) { |
| 128 | throw new Error(`Bundle too large: ${sizeInMB.toFixed(1)}M`); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | // Test 6: Check that local packages are bundled |
| 133 | runTest("Local packages are bundled", () => { |
| 134 | const bundleContent = readFileSync( |
| 135 | resolve(__dirname, "dist/index.js"), |
| 136 | "utf8", |
| 137 | ); |
| 138 | |
| 139 | // Check for code from @continuedev/config-yaml |
| 140 | if ( |
| 141 | !bundleContent.includes("AssistantUnrolled") && |
| 142 | !bundleContent.includes("config-yaml") |
| 143 | ) { |
| 144 | throw new Error("@continuedev/config-yaml not properly bundled"); |
| 145 | } |
| 146 | |
| 147 | // Check for code from @continuedev/openai-adapters |
| 148 | // Since the bundle is minified, check for strings that would be present |
| 149 | // even after minification (e.g., error messages, property names) |
| 150 | if ( |
| 151 | !bundleContent.includes("anthropic") && |
| 152 | !bundleContent.includes("gemini") && |
| 153 | !bundleContent.includes("openai") && |
| 154 | !bundleContent.includes("azure") && |
| 155 | !bundleContent.includes("bedrock") |
| 156 | ) { |
| 157 | throw new Error("@continuedev/openai-adapters not properly bundled"); |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | // Test 7: Test that the CLI can be invoked programmatically |
| 162 | runTest("CLI can be invoked", () => { |
| 163 | try { |
| 164 | // Test that the CLI runs without crashing when given no args |
| 165 | const isWindows = process.platform === "win32"; |
| 166 | const nullDevice = isWindows ? "nul" : "/dev/null"; |
| 167 | execCommand(`${getCLICommand("--help")} > ${nullDevice} 2>&1`); |
| 168 | } catch (error) { |
| 169 | throw new Error(`CLI invocation failed: ${error.message}`); |
| 170 | } |
| 171 | }); |
| 172 | |
| 173 | // Test 8: Check metadata file |
| 174 | runTest("Build metadata exists", () => { |
| 175 | if (!existsSync(resolve(__dirname, "dist/meta.json"))) { |
| 176 | throw new Error("dist/meta.json not found"); |
| 177 | } |
| 178 | |
| 179 | const meta = JSON.parse( |
| 180 | readFileSync(resolve(__dirname, "dist/meta.json"), "utf8"), |
| 181 | ); |
| 182 | if (!meta.inputs || !meta.outputs) { |
| 183 | throw new Error("Invalid metadata structure"); |
| 184 | } |
| 185 | }); |
| 186 | |
| 187 | // Test 9: Verify no missing external dependencies |
| 188 | runTest("No missing runtime dependencies", () => { |
| 189 | // This would fail in Test 3 if dependencies were missing, but let's be explicit |
| 190 | const output = execCommand(`${getCLICommand("--version")} 2>&1`, { |
| 191 | env: { ...process.env, NODE_ENV: "production" }, |
| 192 | }); |
| 193 | |
| 194 | if ( |
| 195 | output.includes("Cannot find module") || |
| 196 | output.includes("MODULE_NOT_FOUND") |
| 197 | ) { |
| 198 | throw new Error("Missing module detected in output"); |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | // Test 10: Test npm link scenario |
| 203 | runTest("CLI works via npm link", () => { |
| 204 | try { |
| 205 | // Simply test that we can execute with node directly |
| 206 | const output = execCommand("node dist/cn.js --version 2>&1"); |
| 207 | const packageJson = JSON.parse( |
| 208 | readFileSync(resolve(__dirname, "package.json"), "utf8"), |
| 209 | ); |
| 210 | if (!output.includes(packageJson.version)) { |
| 211 | throw new Error("Version not found when running via node"); |
| 212 | } |
| 213 | } catch (error) { |
| 214 | throw new Error(`npm link scenario failed: ${error.message}`); |
| 215 | } |
| 216 | }); |
| 217 | |
| 218 | // Summary |
| 219 | console.log("\n" + "=".repeat(50)); |
| 220 | if (testsFailed === 0) { |
| 221 | console.log( |
| 222 | `${colors.green}✅ All ${testsPassed} tests passed!${colors.reset}`, |
| 223 | ); |
| 224 | process.exit(0); |
| 225 | } else { |
| 226 | console.log( |
| 227 | `${colors.red}❌ ${testsFailed} test(s) failed, ${testsPassed} passed${colors.reset}`, |
| 228 | ); |
| 229 | process.exit(1); |
| 230 | } |