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

100 lines
3.7 KiB
JavaScript
Raw Normal View History

// Checks the app's dashboard frame connected to its console, for CI's macOS smoke
// run (.github/workflows/app.yml). A window that comes up but never signs its
// frame in looks alive to a process check; the console's own browser count
// does not lie.
//
// node smoke-frame.mjs [<console home>] [--timeout-ms N]
//
// The console home defaults to PLAN2CODE_CONSOLE_HOME. Waits for a session
// handle there (runtime/<project hash>/<sid>.json), then polls that session's
// open GET /health until it counts a browser. Exit 0 once one connects; exit 1
// when the time (60 s by default, for both waits together) runs out, with the
// last answer printed; exit 2 for a usage error.
import { readFileSync, readdirSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
const POLL_MS = 500;
const REQUEST_MS = 2000;
let parsed;
try {
parsed = parseArgs({ allowPositionals: true, options: { 'timeout-ms': { type: 'string' } } });
} catch (err) {
usage(err.message);
}
const home = parsed.positionals[0] || process.env.PLAN2CODE_CONSOLE_HOME;
if (!home) usage('no console home: pass one, or set PLAN2CODE_CONSOLE_HOME');
const timeoutMs = Number(parsed.values['timeout-ms'] ?? 60000);
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) usage('--timeout-ms takes a positive number');
const deadline = Date.now() + timeoutMs;
let last = null;
while (Date.now() < deadline) {
// Read again each round: a reconnect can resume the session on a new port.
const handle = newestHandle(home);
if (handle) {
last = await health(handle);
if (last.body && last.body.sid === handle.sid && last.body.browsers >= 1) {
console.log(`smoke-frame: the dashboard frame connected (browsers ${last.body.browsers}, port ${handle.port})`);
process.exit(0);
}
}
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
}
if (!last) {
console.error(`smoke-frame: no session appeared under ${home} within ${timeoutMs} ms`);
} else {
console.error(`smoke-frame: no browser connected within ${timeoutMs} ms; last /health answer from port ${last.port}: ${last.text}`);
}
process.exit(1);
function usage(message) {
console.error(`smoke-frame: ${message}\nusage: node smoke-frame.mjs [<console home>] [--timeout-ms N]`);
process.exit(2);
}
// The most recently written session handle under the home, or null.
function newestHandle(consoleHome) {
const runtime = join(consoleHome, 'runtime');
let newest = null;
for (const hash of safeList(runtime)) {
for (const name of safeList(join(runtime, hash))) {
if (!name.endsWith('.json')) continue;
const file = join(runtime, hash, name);
try {
const handle = JSON.parse(readFileSync(file, 'utf8'));
const mtime = statSync(file).mtimeMs;
if (Number.isInteger(handle.port) && handle.sid && (!newest || mtime > newest.mtime)) newest = { ...handle, mtime };
} catch {
// Half written or removed between the listing and the read: the next round sees it whole.
}
}
}
return newest;
}
function safeList(dir) {
try {
return readdirSync(dir);
} catch {
return [];
}
}
// One /health answer: the raw text for the failure message, and the body when it parsed.
async function health(handle) {
try {
const res = await fetch(`http://127.0.0.1:${handle.port}/health`, { signal: AbortSignal.timeout(REQUEST_MS) });
const text = await res.text();
let body = null;
try {
body = JSON.parse(text);
} catch {
// Not JSON: kept as text for the message.
}
return { port: handle.port, text: `${res.status} ${text}`, body };
} catch (err) {
return { port: handle.port, text: `no answer (${err.message})`, body: null };
}
}