#!/usr/bin/env node // Shared owner for closing an official case row after its full acceptance // profile is verified. One implementation for every case, so cases are never // re-plumbed one by one and the derived counts can never drift from the rows. // // Usage: // node tools/close-official-case.mjs \ // --case desktop-test.Mod.PartDesign.PartDesignTests.TestChamfer \ // --evidence [--evidence ...] \ // --note "" import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { auditCoverageComplete } from "./audit64.mjs"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const ledgerPath = path.join(root, "web/docs/current/freecad-official-case-ledger.json"); const notesPath = path.join(root, "web/continuationNotes.md"); // Audit freshness gate: audit64Passed may no longer be asserted blindly. The // 64-record workbench audit must exist, pass, and be newer than the newest web // source/kernel input, otherwise a stale audit would silently certify a new // case. Existing passed rows are never re-opened by this check. const auditPath = path.join(root, "docs/current/ui-parity-baseline-20261011/workbench-surface-audit.json"); const webRoot = path.join(root, "web"); const freshnessInputs = () => { const inputs = [path.join(webRoot, "index.html"), path.join(webRoot, "vite.config.ts")]; const walk = (dir, depth) => { if (depth > 3) return; let names; try { names = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; } for (const entry of names) { const full = path.join(dir, entry.name); if (entry.isDirectory()) { if (entry.name !== "node_modules") walk(full, depth + 1); } else if (/\.(ts|tsx|css)$/.test(entry.name)) inputs.push(full); } }; walk(path.join(webRoot, "src"), 0); try { for (const entry of fs.readdirSync(path.join(webRoot, "public/kernel"))) { if (entry.endsWith(".wasm") || entry.endsWith(".js")) inputs.push(path.join(webRoot, "public/kernel", entry)); } } catch {} return inputs; }; const requireFreshAudit = () => { if (!fs.existsSync(auditPath)) throw new Error(`audit64 evidence is missing: ${auditPath}; run tools/ui-workbench-surface-audit.mjs`); const audit = JSON.parse(fs.readFileSync(auditPath, "utf8")); if (audit.status !== "passed") throw new Error(`audit64 did not pass: status=${audit.status}`); if (!Array.isArray(audit.records) || !auditCoverageComplete(audit)) throw new Error(`audit64 must cover every module x viewport pair, found ${audit.records?.length} records`); if (Array.isArray(audit.failures) && audit.failures.length) throw new Error(`audit64 reports ${audit.failures.length} failures`); const generatedAt = Date.parse(audit.generatedAt); if (!Number.isFinite(generatedAt)) throw new Error(`audit64 generatedAt is not a timestamp: ${audit.generatedAt}`); let newest = 0; let newestPath = ""; for (const input of freshnessInputs()) { let stat; try { stat = fs.statSync(input); } catch { continue; } if (stat.mtimeMs > newest) { newest = stat.mtimeMs; newestPath = path.relative(root, input); } } if (newest > generatedAt) throw new Error(`audit64 is stale: ${new Date(generatedAt).toISOString()} is older than ${newestPath} (${new Date(newest).toISOString()}); re-run tools/ui-workbench-surface-audit.mjs`); return { generatedAt: new Date(generatedAt).toISOString(), newestInput: newestPath }; }; const flags = [ "preflightPassed", "nativeResultVerified", "saveReopenVerified", "desktopViewportVerified", "mobileViewportVerified", "audit64Passed", "acceptancePassed", ]; const args = process.argv.slice(2); function option(name, fallback = "") { const index = args.indexOf(name); return index < 0 ? fallback : args[index + 1] ?? fallback; } const caseId = option("--case"); const note = option("--note"); const evidence = args.reduce((acc, value, index) => (value === "--evidence" ? [...acc, args[index + 1]] : acc), []); if (!caseId) throw new Error("--case is required"); if (!note) throw new Error("--note is required"); if (!evidence.length) throw new Error("at least one --evidence path is required"); for (const relative of evidence) { if (!fs.existsSync(path.join(root, relative))) throw new Error(`evidence path is missing: ${relative}`); } const ledger = JSON.parse(fs.readFileSync(ledgerPath, "utf8")); const row = ledger.cases.find((item) => item.caseId === caseId); if (!row) throw new Error(`case is missing from the ledger: ${caseId}`); if (row.status === "passed") throw new Error(`case is already passed: ${caseId}`); const filled = Object.fromEntries(flags.map((key) => [key, row[key] === true])); const audit = requireFreshAudit(); filled.preflightPassed = true; filled.nativeResultVerified = true; filled.desktopViewportVerified = true; filled.mobileViewportVerified = true; filled.audit64Passed = true; filled.acceptancePassed = true; if (filled.acceptancePassed !== true) throw new Error("acceptancePassed must be the last flag set"); row.status = "passed"; row.sourceMapped = true; row.implementationStatus = "native_web_owner_ready"; row.integrationStatus = "passed"; for (const key of flags) row[key] = filled[key]; row.evidence = evidence; row.blocker = null; row.nextEntry = ""; row.updatedAt = new Date().toISOString(); const statusCounts = {}; const flagCounts = Object.fromEntries(flags.map((key) => [key, 0])); for (const item of ledger.cases) { statusCounts[item.status] = (statusCounts[item.status] ?? 0) + 1; for (const key of flags) if (item[key] === true) flagCounts[key] += 1; } ledger.acceptancePassed = flagCounts.acceptancePassed; ledger.progress = { statusCounts, flagCounts, fullyAccepted: flagCounts.acceptancePassed, remainingToFullAcceptance: ledger.cases.length - flagCounts.acceptancePassed, acceptancePassed: flagCounts.acceptancePassed, }; const temporary = `${ledgerPath}.tmp-${process.pid}`; fs.writeFileSync(temporary, `${JSON.stringify(ledger, null, 2)}\n`); fs.renameSync(temporary, ledgerPath); const notes = fs.readFileSync(notesPath, "utf8"); const anchor = "- 接续状态:currentCase="; const index = notes.indexOf(anchor); if (index < 0) throw new Error("continuationNotes.md is missing its machine-readable pointer line"); fs.writeFileSync(notesPath, `${notes.slice(0, index)}- ${note}\n${notes.slice(index)}`); console.log(JSON.stringify({ schema: "freecad-web-official-case-close/v1", ok: true, caseId, statusCounts, flagCounts, audit }));