#!/usr/bin/env node // Runs a manual scenario in the real app, asserts UI state and captures // full-page / element screenshots. Usage: // node capture.mjs --scenario ../../manual/scenarios/ch01-ui.json [--headed] import fs from "node:fs"; import path from "node:path"; import { createRequire } from "node:module"; import { fileURLToPath } from "node:url"; import { createActionRunner } from "./lib/actions.mjs"; const here = path.dirname(fileURLToPath(import.meta.url)); const webRoot = path.resolve(here, "../.."); const require = createRequire(path.join(webRoot, "package.json")); const { chromium } = require("playwright"); const args = process.argv.slice(2); const option = (name, fallback = "") => { const index = args.indexOf(name); return index < 0 ? fallback : args[index + 1] ?? fallback; }; const scenarioPath = path.resolve(option("--scenario")); if (!fs.existsSync(scenarioPath)) throw new Error(`missing scenario ${scenarioPath}`); const scenario = JSON.parse(fs.readFileSync(scenarioPath, "utf8")); const origin = option("--origin", "http://127.0.0.1:5181"); const headed = args.includes("--headed"); const chapter = scenario.chapter; const workflow = scenario.workflow; const shotDir = path.join(webRoot, "manual", "assets", chapter, workflow); const reportDir = path.join(webRoot, "manual", "data", "capture"); fs.mkdirSync(shotDir, { recursive: true }); fs.mkdirSync(reportDir, { recursive: true }); async function runOnce() { const browser = await chromium.launch({ executablePath: "/usr/bin/google-chrome", headless: !headed, args: ["--no-sandbox", "--disable-dev-shm-usage", "--enable-unsafe-swiftshader", "--use-gl=angle", "--use-angle=swiftshader", "--window-size=1600,900"], }); const page = await browser.newPage({ viewport: { width: 1440, height: 900 } }); const pageErrors = []; const consoleErrors = []; page.on("pageerror", error => pageErrors.push(String(error))); page.on("console", message => { if (message.type() === "error") consoleErrors.push(message.text().slice(0, 300)); }); const trace = []; const shots = []; const record = entry => trace.push({ at: new Date().toISOString(), ...entry }); // The action interpreter is shared with the headed functional suite; only the // evidence destinations and the record sink differ. const runner = createActionRunner(page, { origin, shotDir, shotPrefix: `${chapter}/${workflow}/`, record, shotIndex: shots, fixturePath: path.join(webRoot, "manual", "fixtures", `${chapter}-${workflow}.FCStd`), }); try { for (const action of scenario.actions) await runner.run(action); if (pageErrors.length || consoleErrors.length) throw new Error(`page errors: ${JSON.stringify({ pageErrors, consoleErrors })}`); const report = { schema: "freecad-web-manual/capture/v1", generatedAt: new Date().toISOString(), chapter, workflow, scenario: path.relative(webRoot, scenarioPath), origin, ok: true, trace, pageErrors, consoleErrors, shots: shots.map(item => ({ ...item, file: path.relative(webRoot, item.file) })), }; fs.writeFileSync(path.join(reportDir, `${chapter}-${workflow}.json`), `${JSON.stringify(report, null, 2)}\n`); return report; } finally { await browser.close(); } } try { const report = await runOnce(); console.log(JSON.stringify({ ok: true, chapter, workflow, shots: report.shots.length })); } catch (error) { console.error(JSON.stringify({ ok: false, error: String(error?.stack ?? error) })); process.exitCode = 1; }