droboticflow/scripts/test-dashboard-presentation.mjs

111 lines
4.0 KiB
JavaScript

#!/usr/bin/env node
// One local command: real HTTP, synthetic memory store, no Mongo/BOS/company access.
import { createServer } from "node:net";
import { spawn } from "node:child_process";
import { once } from "node:events";
import { mkdirSync, openSync, closeSync, existsSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const out = path.join(
root,
"local-data/test-results",
`dashboard-presentation-${Date.now()}-${process.pid}`,
"customer-dashboard-20261020",
);
mkdirSync(out, { recursive: true, mode: 0o700 });
const freePort = async () => {
const server = createServer();
server.listen(0, "127.0.0.1");
await once(server, "listening");
const port = server.address().port;
await new Promise((resolve) => server.close(resolve));
return port;
};
const apiPort = await freePort();
let webPort = await freePort();
while (webPort === apiPort) webPort = await freePort();
const edge = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge";
const env = {
...process.env,
...(process.env.BROWSER_EXECUTABLE ? {} : existsSync(edge) ? { BROWSER_EXECUTABLE: edge } : {}),
VITE_API_BASE_URL: "",
VITE_DEV_API_BASE_URL: `http://127.0.0.1:${apiPort}`,
VITE_DEV_WORKER_BASE_URL: "http://127.0.0.1:1",
ANNOTATION_OUTPUT_ACCOUNTS: path.join(out, "accounts.json"),
ANNOTATION_OUTPUT_VERIFY_DIR: out,
};
const active = new Set();
function start(command, args, log) {
const fd = log ? openSync(path.join(out, log), "w", 0o600) : null;
const child = spawn(command, args, { cwd: root, env, stdio: fd === null ? "inherit" : ["ignore", fd, fd] });
if (fd !== null) closeSync(fd);
active.add(child);
child.once("exit", () => active.delete(child));
child.once("error", () => active.delete(child));
return child;
}
async function run(command, args) {
const child = start(command, args);
const [code] = await once(child, "exit");
if (code !== 0) throw new Error(`${path.basename(command)} failed: ${code}`);
}
async function ready(url, child) {
const deadline = Date.now() + 45000;
while (Date.now() < deadline) {
if (child.exitCode !== null || child.signalCode !== null)
throw new Error(`Local server exited before ready: ${url}`);
try {
const r = await fetch(url, { signal: AbortSignal.timeout(1500) });
await r.body?.cancel();
if (r.ok) return;
} catch {}
await new Promise((resolve) => setTimeout(resolve, 150));
}
throw new Error(`Local server did not become ready: ${url}`);
}
for (const signal of ["SIGINT", "SIGTERM"])
process.on(signal, () => {
for (const child of active) child.kill("SIGTERM");
});
try {
const binary = path.join(out, "dashboard-fixture");
await run("go", ["build", "-o", binary, "./tests/browser/support/annotation-output-replica"]);
const api = start(
binary,
[
"-memory",
"-web-port",
String(webPort),
"-api-port",
String(apiPort),
"-accounts",
env.ANNOTATION_OUTPUT_ACCOUNTS,
],
"api.log",
);
const web = start(
process.execPath,
["node_modules/vite/bin/vite.js", "--host", "127.0.0.1", "--port", String(webPort), "--strictPort"],
"web.log",
);
await Promise.all([ready(`http://127.0.0.1:${apiPort}/api/health`, api), ready(`http://127.0.0.1:${webPort}/`, web)]);
await run(process.execPath, ["--test", "tests/frontend/dashboard-presentation.test.mjs"]);
await run(process.execPath, ["tests/browser/annotation-output-replica.mjs"]);
await run(process.execPath, ["tests/browser/dashboard-presentation-replica.mjs"]);
console.log(`Dashboard presentation regression passed. Evidence: ${out}`);
} finally {
await Promise.all(
[...active].map(async (child) => {
const exited = once(child, "exit");
child.kill("SIGTERM");
const timer = setTimeout(() => child.kill("SIGKILL"), 5000);
try {
await exited;
} finally {
clearTimeout(timer);
}
}),
);
}