|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { existsSync, mkdirSync, rmSync } from "node:fs"; |
| 5 | +import { mkdtemp } from "node:fs/promises"; |
| 6 | +import { createServer } from "node:net"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { dirname, join, resolve } from "node:path"; |
| 9 | +import { fileURLToPath } from "node:url"; |
| 10 | + |
| 11 | +const root = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); |
| 12 | +const binary = resolve( |
| 13 | + root, |
| 14 | + process.env.SIMDECK_INTEGRATION_SERVICE_BINARY ?? join("build", "simdeck"), |
| 15 | +); |
| 16 | +const enabled = |
| 17 | + process.env.SIMDECK_INTEGRATION_LAUNCHAGENT === "1" || |
| 18 | + process.env.CI === "true"; |
| 19 | + |
| 20 | +if (process.platform !== "darwin") { |
| 21 | + console.log("Skipping LaunchAgent restart integration test on non-macOS."); |
| 22 | + process.exit(0); |
| 23 | +} |
| 24 | + |
| 25 | +if (!enabled) { |
| 26 | + console.log( |
| 27 | + "Skipping LaunchAgent restart integration test. Set SIMDECK_INTEGRATION_LAUNCHAGENT=1 to run it.", |
| 28 | + ); |
| 29 | + process.exit(0); |
| 30 | +} |
| 31 | + |
| 32 | +if (!existsSync(binary)) { |
| 33 | + throw new Error( |
| 34 | + `Missing SimDeck binary at ${binary}. Run npm run build:cli first.`, |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +const tempRoot = await mkdtemp(join(tmpdir(), "simdeck-launchagent-it-")); |
| 39 | +const home = join(tempRoot, "home"); |
| 40 | +const projectRoot = join(tempRoot, "project"); |
| 41 | +mkdirSync(home, { recursive: true }); |
| 42 | +mkdirSync(projectRoot, { recursive: true }); |
| 43 | + |
| 44 | +const servicePort = await findFreePort(); |
| 45 | +const clientRoot = join(root, "packages", "client", "dist"); |
| 46 | +const env = { |
| 47 | + ...process.env, |
| 48 | + HOME: home, |
| 49 | +}; |
| 50 | + |
| 51 | +let blocker = null; |
| 52 | + |
| 53 | +try { |
| 54 | + blocker = await listenIfAvailable("127.0.0.1", 4310); |
| 55 | + runJson(["service", "off"], { allowFailure: true }); |
| 56 | + |
| 57 | + const onArgs = [ |
| 58 | + "service", |
| 59 | + "on", |
| 60 | + "--port", |
| 61 | + String(servicePort), |
| 62 | + "--bind", |
| 63 | + "127.0.0.1", |
| 64 | + "--video-codec", |
| 65 | + "software", |
| 66 | + "--stream-quality", |
| 67 | + "tiny", |
| 68 | + ]; |
| 69 | + if (existsSync(clientRoot)) { |
| 70 | + onArgs.push("--client-root", clientRoot); |
| 71 | + } |
| 72 | + |
| 73 | + const installed = runJson(onArgs); |
| 74 | + assertEqual(installed.ok, true, "service on should succeed"); |
| 75 | + assertEqual( |
| 76 | + installed.port, |
| 77 | + servicePort, |
| 78 | + "service on should install requested port", |
| 79 | + ); |
| 80 | + |
| 81 | + await waitForHealth(servicePort); |
| 82 | + |
| 83 | + const restarted = runJson(["service", "restart"]); |
| 84 | + assertEqual(restarted.ok, true, "service restart should succeed"); |
| 85 | + assertEqual( |
| 86 | + restarted.port, |
| 87 | + servicePort, |
| 88 | + "service restart without --port should preserve the installed LaunchAgent port", |
| 89 | + ); |
| 90 | + |
| 91 | + await waitForHealth(servicePort); |
| 92 | + const status = runJson(["service", "status"]); |
| 93 | + assertEqual(status.healthy, true, "service should be healthy after restart"); |
| 94 | + assertEqual( |
| 95 | + status.service?.port, |
| 96 | + servicePort, |
| 97 | + "status should report the preserved LaunchAgent port", |
| 98 | + ); |
| 99 | + |
| 100 | + console.log( |
| 101 | + JSON.stringify( |
| 102 | + { |
| 103 | + ok: true, |
| 104 | + binary, |
| 105 | + servicePort, |
| 106 | + defaultPortBlocked: Boolean(blocker), |
| 107 | + }, |
| 108 | + null, |
| 109 | + 2, |
| 110 | + ), |
| 111 | + ); |
| 112 | +} finally { |
| 113 | + try { |
| 114 | + runJson(["service", "off"], { allowFailure: true }); |
| 115 | + } finally { |
| 116 | + if (blocker) { |
| 117 | + await new Promise((resolveClose) => blocker.close(resolveClose)); |
| 118 | + } |
| 119 | + rmSync(tempRoot, { recursive: true, force: true }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function runJson(args, options = {}) { |
| 124 | + const result = spawnSync(binary, args, { |
| 125 | + cwd: projectRoot, |
| 126 | + env, |
| 127 | + encoding: "utf8", |
| 128 | + }); |
| 129 | + const output = `${result.stdout ?? ""}${result.stderr ?? ""}`.trim(); |
| 130 | + if (result.status !== 0) { |
| 131 | + if (options.allowFailure) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + throw new Error( |
| 135 | + `${binary} ${args.join(" ")} failed with ${result.status}:\n${output}`, |
| 136 | + ); |
| 137 | + } |
| 138 | + if (!output) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + try { |
| 142 | + return JSON.parse(result.stdout); |
| 143 | + } catch (error) { |
| 144 | + throw new Error( |
| 145 | + `${binary} ${args.join(" ")} did not print JSON: ${error.message}\n${output}`, |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +async function waitForHealth(port) { |
| 151 | + const deadline = Date.now() + 15_000; |
| 152 | + let lastError = null; |
| 153 | + while (Date.now() < deadline) { |
| 154 | + try { |
| 155 | + const response = await fetch(`http://127.0.0.1:${port}/api/health`); |
| 156 | + if (response.ok) { |
| 157 | + return; |
| 158 | + } |
| 159 | + lastError = new Error(`/api/health returned ${response.status}`); |
| 160 | + } catch (error) { |
| 161 | + lastError = error; |
| 162 | + } |
| 163 | + await sleep(50); |
| 164 | + } |
| 165 | + throw new Error( |
| 166 | + `Timed out waiting for SimDeck service on ${port}: ${lastError?.message ?? "unknown error"}`, |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +async function findFreePort() { |
| 171 | + const server = await listenIfAvailable("127.0.0.1", 0); |
| 172 | + const address = server.address(); |
| 173 | + await new Promise((resolveClose) => server.close(resolveClose)); |
| 174 | + return address.port; |
| 175 | +} |
| 176 | + |
| 177 | +function listenIfAvailable(host, port) { |
| 178 | + return new Promise((resolveListen, rejectListen) => { |
| 179 | + const server = createServer(); |
| 180 | + server.once("error", (error) => { |
| 181 | + if (error.code === "EADDRINUSE" && port !== 0) { |
| 182 | + resolveListen(null); |
| 183 | + return; |
| 184 | + } |
| 185 | + rejectListen(error); |
| 186 | + }); |
| 187 | + server.listen(port, host, () => resolveListen(server)); |
| 188 | + }); |
| 189 | +} |
| 190 | + |
| 191 | +function sleep(ms) { |
| 192 | + return new Promise((resolveSleep) => setTimeout(resolveSleep, ms)); |
| 193 | +} |
| 194 | + |
| 195 | +function assertEqual(actual, expected, message) { |
| 196 | + if (actual !== expected) { |
| 197 | + throw new Error(`${message}: expected ${expected}, got ${actual}`); |
| 198 | + } |
| 199 | +} |
0 commit comments