import assert from "node:assert/strict" import { spawnSync } from "node:child_process" import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join, resolve } from "node:path" import test from "node:test" const script = resolve("scripts/test-dev-branch") const supertestLegionScript = resolve("scripts/supertest-legion") function normalizeMacosTempPath(path: string): string { return path.replace(/^\/private\/var\//, "/var/") } function normalizeEnvPaths(env: Record): Record { return Object.fromEntries( Object.entries(env).map(([key, value]) => [key, normalizeMacosTempPath(value)]) ) } test("test-dev-branch helper is valid Perl", () => { const result = spawnSync("perl", ["-c", script], { encoding: "utf8" }) assert.equal(result.status, 0, result.stderr) assert.match(result.stderr, /syntax OK/) }) test("test-dev-branch helper documents core commands", () => { const result = spawnSync(script, ["--help"], { encoding: "utf8" }) assert.equal(result.status, 0, result.stderr) assert.match(result.stdout, /prepare/) assert.match(result.stdout, /reset/) assert.match(result.stdout, /ssh/) assert.match(result.stdout, /rpc/) assert.match(result.stdout, /--plugin NAME/) assert.match(result.stdout, /self-signed/) }) test("supertest-legion helper is valid bash", () => { const result = spawnSync("bash", ["-n", supertestLegionScript], { encoding: "utf8" }) assert.equal(result.status, 0, result.stderr) }) test("supertest-legion helper documents ssh and rpc commands", () => { const result = spawnSync(supertestLegionScript, ["--help"], { encoding: "utf8" }) assert.equal(result.status, 0, result.stderr) assert.match(result.stdout, /node list --json/) assert.match(result.stdout, /ssh /) assert.match(result.stdout, /rpc /) assert.match(result.stdout, /latest\.json/) }) test("supertest-legion helper reuses latest run metadata", () => { const root = mkdtempSync(join(tmpdir(), "supertest-legion-")) const legionRepo = join(root, "legion") const latestPath = join(root, "latest.json") try { mkdirSync(legionRepo) writeFileSync( join(root, "latest.json"), JSON.stringify({ legionRepo, legionCliEntry: "cli-main.ts", legionStateDir: join(root, "state"), legionCacheDir: join(root, "cache"), certMode: "self-signed" }) ) writeFileSync( join(root, "legion", "cli-main.ts"), `console.log(JSON.stringify({ cwd: process.cwd(), args: process.argv.slice(2), env: { LEGION_STATE_DIR: process.env.LEGION_STATE_DIR, LEGION_CACHE_DIR: process.env.LEGION_CACHE_DIR, LEGION_APP_ROOT: process.env.LEGION_APP_ROOT, LEGION_TEST_CERT_MODE: process.env.LEGION_TEST_CERT_MODE } }))\n` ) const result = spawnSync(supertestLegionScript, ["rpc", "node-a", "1 + 1"], { encoding: "utf8", env: { ...process.env, LEGION_UNLOCK_PASSWORD: "unlock-secret", SUPERTEST_LATEST: latestPath } }) assert.equal(result.status, 0, result.stderr) const output = JSON.parse(result.stdout) as { cwd: string args: string[] env: Record } assert.equal(normalizeMacosTempPath(output.cwd), normalizeMacosTempPath(legionRepo)) assert.deepEqual(output.args, ["node", "rpc", "node-a", "--", "1 + 1"]) assert.deepEqual(normalizeEnvPaths(output.env), { LEGION_STATE_DIR: normalizeMacosTempPath(join(root, "state")), LEGION_CACHE_DIR: normalizeMacosTempPath(join(root, "cache")), LEGION_APP_ROOT: normalizeMacosTempPath(legionRepo), LEGION_TEST_CERT_MODE: "self-signed" }) } finally { rmSync(root, { recursive: true, force: true }) } })