|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawn } from "node:child_process"; |
| 4 | +import { constants as fsConstants } from "node:fs"; |
| 5 | +import { access } from "node:fs/promises"; |
| 6 | +import path from "node:path"; |
| 7 | +import process from "node:process"; |
| 8 | +import { fileURLToPath } from "node:url"; |
| 9 | + |
| 10 | +const ROOT_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 11 | + |
| 12 | +function resolveBinaryPath() { |
| 13 | + if (process.platform === "linux") { |
| 14 | + if (process.arch === "x64") { |
| 15 | + return path.join( |
| 16 | + ROOT_DIR, |
| 17 | + "apps/desktop/src-tauri/binaries/openlinear-sidecar-x86_64-unknown-linux-gnu", |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + if (process.arch === "arm64") { |
| 22 | + return path.join( |
| 23 | + ROOT_DIR, |
| 24 | + "apps/desktop/src-tauri/binaries/openlinear-sidecar-aarch64-unknown-linux-gnu", |
| 25 | + ); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if (process.platform === "darwin") { |
| 30 | + if (process.arch === "x64") { |
| 31 | + return path.join( |
| 32 | + ROOT_DIR, |
| 33 | + "apps/desktop/src-tauri/binaries/openlinear-sidecar-x86_64-apple-darwin", |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + if (process.arch === "arm64") { |
| 38 | + return path.join( |
| 39 | + ROOT_DIR, |
| 40 | + "apps/desktop/src-tauri/binaries/openlinear-sidecar-aarch64-apple-darwin", |
| 41 | + ); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + throw new Error(`Unsupported smoke test platform: ${process.platform}/${process.arch}`); |
| 46 | +} |
| 47 | + |
| 48 | +function wait(ms) { |
| 49 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 50 | +} |
| 51 | + |
| 52 | +async function main() { |
| 53 | + const binaryPath = resolveBinaryPath(); |
| 54 | + await access(binaryPath, fsConstants.X_OK); |
| 55 | + |
| 56 | + const child = spawn(binaryPath, [], { |
| 57 | + cwd: ROOT_DIR, |
| 58 | + env: { ...process.env }, |
| 59 | + stdio: ["ignore", "pipe", "pipe"], |
| 60 | + }); |
| 61 | + |
| 62 | + let output = ""; |
| 63 | + let ready = false; |
| 64 | + let exitCode = null; |
| 65 | + let spawnError = null; |
| 66 | + |
| 67 | + const appendOutput = (chunk) => { |
| 68 | + const text = chunk.toString(); |
| 69 | + output += text; |
| 70 | + process.stdout.write(text); |
| 71 | + |
| 72 | + if (output.includes("Server running on http://localhost:3001")) { |
| 73 | + ready = true; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + child.stdout.on("data", appendOutput); |
| 78 | + child.stderr.on("data", appendOutput); |
| 79 | + |
| 80 | + child.on("exit", (code) => { |
| 81 | + exitCode = code; |
| 82 | + }); |
| 83 | + |
| 84 | + child.on("error", (error) => { |
| 85 | + spawnError = error; |
| 86 | + }); |
| 87 | + |
| 88 | + const deadline = Date.now() + 15000; |
| 89 | + while (!ready && exitCode === null && !spawnError && Date.now() < deadline) { |
| 90 | + await wait(200); |
| 91 | + } |
| 92 | + |
| 93 | + if (!ready) { |
| 94 | + if (exitCode === null) { |
| 95 | + child.kill("SIGTERM"); |
| 96 | + await wait(500); |
| 97 | + } |
| 98 | + |
| 99 | + if (spawnError) { |
| 100 | + throw spawnError; |
| 101 | + } |
| 102 | + |
| 103 | + throw new Error( |
| 104 | + `Sidecar smoke test failed before readiness. Exit code: ${exitCode ?? "still running"}\n${output}`, |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + child.kill("SIGTERM"); |
| 109 | + await wait(1000); |
| 110 | +} |
| 111 | + |
| 112 | +main().catch((error) => { |
| 113 | + console.error(error.message || String(error)); |
| 114 | + process.exit(1); |
| 115 | +}); |
0 commit comments