Files
plan2code/scripts/test-app-smoke-frame.mjs
T

114 lines
4.9 KiB
JavaScript

// Tests for plan2code-app/scripts/smoke-frame.mjs, the check CI's macOS smoke
// run uses to prove the app's dashboard frame connected to the console. Run with:
// node --test scripts/test-app-smoke-frame.mjs
//
// No app and no real console: a session handle under a throwaway console home
// points at a tiny local server whose /health answers with a browser count the
// test controls.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const SCRIPT = path.join(HERE, "../plan2code-app/scripts/smoke-frame.mjs");
const SID = "20261108-000000-abcdef";
// A /health server answering with `browsers()` for this session.
async function healthServer(browsers) {
const server = http.createServer((req, res) => {
if (req.url !== "/health") return res.writeHead(404).end();
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ ok: true, sid: SID, browsers: browsers(), rev: 1, phase: "unknown" }));
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
return server;
}
// A console home holding one session handle, the way server.mjs writes it.
function consoleHomeFor(port) {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-smoke-frame-home-"));
const dir = path.join(home, "runtime", "0123456789abcdef");
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, `${SID}.json`), JSON.stringify({ v: 1, sid: SID, pid: process.pid, port, url: `http://127.0.0.1:${port}/` }));
return home;
}
function run(args, env = {}) {
return new Promise((resolve) => {
const child = spawn(process.execPath, [SCRIPT, ...args], { env: { ...process.env, ...env }, stdio: ["ignore", "pipe", "pipe"] });
let output = "";
child.stdout.on("data", (chunk) => (output += chunk));
child.stderr.on("data", (chunk) => (output += chunk));
child.on("exit", (code) => resolve({ code, output }));
});
}
test("smoke frame: passes once /health counts a browser", async (t) => {
let asked = 0;
const server = await healthServer(() => (++asked < 3 ? 0 : 1));
const home = consoleHomeFor(server.address().port);
t.after(() => {
server.close();
fs.rmSync(home, { recursive: true, force: true });
});
const { code, output } = await run([home, "--timeout-ms", "10000"]);
assert.equal(code, 0, output);
assert.ok(asked >= 3, "it kept polling while no browser was connected");
assert.match(output, /connected/);
});
test("smoke frame: fails with the last answer printed when no browser ever connects", async (t) => {
const server = await healthServer(() => 0);
const home = consoleHomeFor(server.address().port);
t.after(() => {
server.close();
fs.rmSync(home, { recursive: true, force: true });
});
const { code, output } = await run([home, "--timeout-ms", "1500"]);
assert.equal(code, 1, output);
assert.match(output, /"browsers":0/, "the last /health answer is printed");
});
test("smoke frame: fails when no session ever appears, and reads the home from PLAN2CODE_CONSOLE_HOME", async (t) => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-smoke-frame-empty-"));
t.after(() => fs.rmSync(home, { recursive: true, force: true }));
const { code, output } = await run(["--timeout-ms", "1000"], { PLAN2CODE_CONSOLE_HOME: home });
assert.equal(code, 1, output);
assert.match(output, /no session/i);
});
test("smoke frame: refuses to run with no console home", async () => {
const env = { ...process.env };
delete env.PLAN2CODE_CONSOLE_HOME;
const result = await new Promise((resolve) => {
const child = spawn(process.execPath, [SCRIPT], { env, stdio: ["ignore", "pipe", "pipe"] });
let output = "";
child.stderr.on("data", (chunk) => (output += chunk));
child.on("exit", (code) => resolve({ code, output }));
});
assert.equal(result.code, 2, result.output);
});
test("smoke frame: the CI step keeps the console from opening a browser, so only the frame can count", () => {
// Inside the app the console opens no browser of its own. If the app's signal
// to it were lost, `open` would start the runner's browser, which signs in by
// cookie and would pass this check with the frame dead.
const workflow = fs.readFileSync(path.join(HERE, "../.github/workflows/app.yml"), "utf8");
const start = workflow.indexOf("- name: Smoke-test the dashboard frame connects (macOS)");
assert.ok(start >= 0, "the frame step is there");
// The step runs to the blank line that ends it.
const end = workflow.indexOf("\n\n", start);
const step = workflow.slice(start, end === -1 ? undefined : end);
assert.match(step, /\n\s+export PLAN2CODE_NO_BROWSER=1\n/, step);
assert.ok(step.indexOf("PLAN2CODE_NO_BROWSER") < step.indexOf("target/release/plan2code-app"), "set before the app starts");
});