// Run artefacts: manifest, results, checkpoint, markdown/JUnit/HTML reports. import fs from "node:fs"; import path from "node:path"; import { renderGapSections } from "./gap-ledger.mjs"; const ISO_STAMP = () => new Date().toISOString().replace(/[:.]/g, "-"); const esc = value => String(value ?? "").replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); export function createRunContext({ options, root, output, origin }) { return { schema: "freecad-web/functional-suite/v1", root, output, origin, startedAt: new Date().toISOString(), updatedAt: null, options, results: [], gaps: [], counts: { passed: 0, failed: 0, blocked: 0, reference_only: 0, running: 0 }, manifest: {}, }; } export function newRunDir(base, resume) { if (resume) return path.resolve(resume); const dir = path.join(base, `run-${ISO_STAMP()}`); fs.mkdirSync(dir, { recursive: true }); return dir; } export function writeManifest(ctx, extra = {}) { ctx.manifest = { schema: "freecad-web/functional-suite-manifest/v1", generatedAt: new Date().toISOString(), origin: ctx.origin, platform: process.platform, node: process.version, display: process.env.DISPLAY ?? null, headless: ctx.options.headless, command: process.argv.join(" "), ...extra, }; fs.writeFileSync(path.join(ctx.output, "manifest.json"), `${JSON.stringify(ctx.manifest, null, 2)}\n`); } export function recount(ctx) { const counts = { passed: 0, failed: 0, blocked: 0, reference_only: 0, running: 0 }; for (const result of ctx.results) counts[result.status] = (counts[result.status] ?? 0) + 1; ctx.counts = counts; return counts; } export function persist(ctx) { ctx.updatedAt = new Date().toISOString(); recount(ctx); fs.writeFileSync(path.join(ctx.output, "results.json"), `${JSON.stringify({ schema: ctx.schema, startedAt: ctx.startedAt, updatedAt: ctx.updatedAt, origin: ctx.origin, headless: ctx.options.headless, counts: ctx.counts, results: ctx.results, }, null, 2)}\n`); const completed = ctx.results.filter(item => item.status !== "running").map(item => ({ id: item.id, status: item.status, seconds: item.seconds })); fs.writeFileSync(path.join(ctx.output, "checkpoint.json"), `${JSON.stringify({ schema: "freecad-web/functional-suite-checkpoint/v1", updatedAt: ctx.updatedAt, current: ctx.results.find(item => item.status === "running")?.id ?? null, completed, }, null, 2)}\n`); } const statusCell = item => { if (item.status === "passed") return "passed"; if (item.status === "blocked") return `blocked (${item.failureClass})`; return `${item.status} (${item.classification})`; }; export function writeMarkdown(ctx) { const gaps = renderGapSections(ctx.gaps); const rows = ctx.results.map(item => `| ${item.id} | ${item.priority} | ${statusCell(item)} | ${Number(item.seconds ?? 0).toFixed(1)}s | ${item.steps.length} | ${item.error ? item.error.slice(0, 120).replace(/\|/g, "\\|") : ""} |`); return `# Web FreeCAD 功能测试(有头)\n\n` + `- 运行:${ctx.startedAt}\n- Origin:${ctx.origin}\n- 模式:${ctx.options.headless ? "headless" : "headed"}\n` + `- 计数:passed ${ctx.counts.passed} / failed ${ctx.counts.failed} / blocked ${ctx.counts.blocked} / reference_only ${ctx.counts.reference_only}\n\n` + `## 用例\n\n| id | 优先级 | 状态 | 耗时 | 步骤 | 错误 |\n|---|---|---|---|---|---|\n${rows.join("\n")}\n\n` + `## Blocked(前置未满足)\n\n${gaps.blocked}\n\n## Product(产品/UI 缺口)\n\n${gaps.product}\n`; } export function writeJUnit(ctx) { const body = ctx.results.map(item => { const time = Number(item.seconds ?? 0).toFixed(3); const failure = item.status === "passed" ? "" : item.status === "blocked" ? `\n ` : `\n ${esc(item.error)}`; return ` ${failure}\n `; }); return `\n\n${body.join("\n")}\n\n`; } export function writeIndex(ctx) { const rows = ctx.results.map(item => { const color = item.status === "passed" ? "#137333" : item.status === "blocked" ? "#8a6d00" : "#a50e0e"; return `${esc(item.id)}${esc(item.priority)}${esc(statusCell(item))}${Number(item.seconds ?? 0).toFixed(1)}s${esc(item.error ? item.error.slice(0, 160) : "")}`; }); return `Web FreeCAD functional suite

Web FreeCAD 功能测试(有头)

${esc(ctx.startedAt)} · origin ${esc(ctx.origin)} · passed ${ctx.counts.passed} / failed ${ctx.counts.failed} / blocked ${ctx.counts.blocked}

${rows.join("")}
id优先级状态耗时错误

report.md · results.json · junit.xml

`; } export function finalize(ctx) { persist(ctx); fs.writeFileSync(path.join(ctx.output, "report.md"), writeMarkdown(ctx)); fs.writeFileSync(path.join(ctx.output, "junit.xml"), writeJUnit(ctx)); fs.writeFileSync(path.join(ctx.output, "index.html"), writeIndex(ctx)); const gaps = renderGapSections(ctx.gaps); fs.writeFileSync(path.join(ctx.output, "GAPS.md"), `# Gap ledger\n\n## Blocked (preconditions): ${gaps.counts.blocked}\n\n${gaps.blocked}\n\n## Product regressions/gaps: ${gaps.counts.product}\n\n${gaps.product}\n`); } export function pruneRuns(base, keep) { if (!fs.existsSync(base)) return []; const dirs = fs.readdirSync(base, { withFileTypes: true }) .filter(entry => entry.isDirectory() && entry.name.startsWith("run-")) .map(entry => entry.name).sort(); const removed = dirs.slice(0, Math.max(0, dirs.length - keep)); for (const name of removed) fs.rmSync(path.join(base, name), { recursive: true, force: true }); return removed; }