mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-05 08:15:41 -08:00
Fail the macOS smoke run when the dashboard frame never connects, and update the checklist
AI Assisted
This commit is contained in:
1 parent
a9e0bf49f0
commit
615e98e11e
10 files changed
+418
-3
No files matched your search
@@ -0,0 +1,109 @@
|
||||
// Tests for plan2code-app/scripts/smoke-agent.mjs, the stand-in agent CI's
|
||||
// macOS smoke run starts in place of a real agent CLI. Run with:
|
||||
// node --test scripts/test-app-smoke-agent.mjs
|
||||
//
|
||||
// The stand-in has one job: open a real web console the way an agent would,
|
||||
// so the app's dashboard frame has something to connect to. This runs it
|
||||
// outside the app, under a throwaway console home, and checks a live session
|
||||
// appears and the stand-in ends when its input closes.
|
||||
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
const AGENT = path.join(HERE, "../plan2code-app/scripts/smoke-agent.mjs");
|
||||
const CONSOLE_DIR = path.join(HERE, "../src/web-console");
|
||||
|
||||
// The desktop app's own variables would have the console announce itself to
|
||||
// a live window. Deleted from the spawned environment, never inherited.
|
||||
const APP_VARS = ["PLAN2CODE_IN_APP", "PLAN2CODE_APP_TOKEN", "PLAN2CODE_APP_VERSION", "PLAN2CODE_APP_ANNOUNCE", "PLAN2CODE_EMBED_ORIGIN", "PLAN2CODE_CONSOLE_SID"];
|
||||
|
||||
function outsideAppEnv(extra) {
|
||||
const env = { ...process.env, ...extra };
|
||||
for (const name of APP_VARS) delete env[name];
|
||||
return env;
|
||||
}
|
||||
|
||||
// Every session handle under a console home: runtime/<project hash>/<sid>.json.
|
||||
function handles(consoleHome) {
|
||||
const runtime = path.join(consoleHome, "runtime");
|
||||
if (!fs.existsSync(runtime)) return [];
|
||||
return fs.readdirSync(runtime).flatMap((hash) =>
|
||||
fs
|
||||
.readdirSync(path.join(runtime, hash))
|
||||
.filter((name) => name.endsWith(".json"))
|
||||
.map((name) => JSON.parse(fs.readFileSync(path.join(runtime, hash, name), "utf8")))
|
||||
);
|
||||
}
|
||||
|
||||
async function waitFor(check, ms) {
|
||||
const end = Date.now() + ms;
|
||||
while (Date.now() < end) {
|
||||
const value = check();
|
||||
if (value) return value;
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
return check();
|
||||
}
|
||||
|
||||
function pidAlive(pid) {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return err.code === "EPERM";
|
||||
}
|
||||
}
|
||||
|
||||
test("smoke agent: is an executable Node script with no dependencies", { skip: process.platform === "win32" }, () => {
|
||||
const source = fs.readFileSync(AGENT, "utf8");
|
||||
assert.ok(source.startsWith("#!/usr/bin/env node\n"), "starts with a node shebang");
|
||||
assert.ok((fs.statSync(AGENT).mode & 0o111) !== 0, "has its executable bits set");
|
||||
for (const [, name] of source.matchAll(/from\s+["']([^"']+)["']/g)) {
|
||||
assert.ok(name.startsWith("node:"), `imports only Node built-ins, not ${name}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("smoke agent: opens a live dashboard session, then ends when its input closes", async (t) => {
|
||||
const consoleHome = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-smoke-agent-home-"));
|
||||
const project = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-smoke-agent-project-"));
|
||||
const env = outsideAppEnv({ PLAN2CODE_CONSOLE_HOME: consoleHome, PLAN2CODE_NO_BROWSER: "1", PLAN2CODE_CONSOLE_DIR: CONSOLE_DIR });
|
||||
let agent;
|
||||
t.after(() => {
|
||||
if (agent && agent.exitCode === null) agent.kill("SIGKILL");
|
||||
// --all stops this folder's sessions, so it runs from the project, under the same home.
|
||||
spawnSync(process.execPath, [path.join(CONSOLE_DIR, "console.mjs"), "stop", "--all"], { cwd: project, env, timeout: 15000 });
|
||||
fs.rmSync(consoleHome, { recursive: true, force: true });
|
||||
fs.rmSync(project, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
agent = spawn(process.execPath, [AGENT, "--some-agent-flag"], { cwd: project, env, stdio: ["pipe", "pipe", "pipe"] });
|
||||
let output = "";
|
||||
agent.stdout.on("data", (chunk) => (output += chunk));
|
||||
agent.stderr.on("data", (chunk) => (output += chunk));
|
||||
const exited = new Promise((resolve) => agent.on("exit", (code) => resolve(code)));
|
||||
|
||||
const handle = await waitFor(() => handles(consoleHome)[0], 20000);
|
||||
assert.ok(handle, `a session appeared under the console home; agent said: ${output}`);
|
||||
const health = await (await fetch(`http://127.0.0.1:${handle.port}/health`)).json();
|
||||
assert.equal(health.ok, true);
|
||||
assert.equal(health.sid, handle.sid, "the port answers for this session");
|
||||
const state = JSON.parse(fs.readFileSync(path.join(consoleHome, "sessions", handle.sid, "state.json"), "utf8"));
|
||||
assert.equal(state.workflow, "dashboard");
|
||||
|
||||
// Still running until its input closes, like an agent waiting for the person.
|
||||
assert.equal(agent.exitCode, null, "the stand-in waits on its input");
|
||||
agent.stdin.end();
|
||||
const code = await Promise.race([exited, new Promise((resolve) => setTimeout(() => resolve("timeout"), 10000))]);
|
||||
assert.equal(code, 0, `the stand-in exits cleanly once its input closes; agent said: ${output}`);
|
||||
|
||||
// The console outlives the agent (it is detached), so the stop below is what ends it.
|
||||
const stop = spawnSync(process.execPath, [path.join(CONSOLE_DIR, "console.mjs"), "stop", "--all"], { cwd: project, env, encoding: "utf8", timeout: 15000 });
|
||||
assert.equal(stop.status, 0, stop.stderr);
|
||||
assert.equal(await waitFor(() => !pidAlive(handle.pid), 10000), true, "the console server is gone after stop --all");
|
||||
});
|
||||
@@ -0,0 +1,113 @@
|
||||
// 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");
|
||||
});
|
||||
@@ -3,6 +3,7 @@
|
||||
"test-agent-lookup.mjs",
|
||||
"test-agents-file.mjs",
|
||||
"test-app-meter.mjs",
|
||||
"test-app-smoke-frame.mjs",
|
||||
"test-browser-helper.mjs",
|
||||
"test-check-console-openings.mjs",
|
||||
"test-check-doc-size.mjs",
|
||||
@@ -53,6 +54,7 @@
|
||||
"test-web-console-tones.mjs"
|
||||
],
|
||||
"skipFast": [
|
||||
"test-app-smoke-agent.mjs",
|
||||
"test-console-stub.mjs",
|
||||
"test-console-wire-budgets.mjs",
|
||||
"test-fast.mjs",
|
||||
|
||||
Reference in new issue
Block a user