import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { execFileSync } from "node:child_process"; import { fileURLToPath } from "node:url"; import { resolveOfficialFixture } from "./official-fixture-resolver.mjs"; import { auditCoverage, auditCoverageComplete } from "./audit64.mjs"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const fixtureInput = process.argv.slice(2).find(arg => !arg.startsWith("-")); const auditRequested = process.argv.includes("--audit"); const fixture = resolveOfficialFixture(root, fixtureInput, [".brep", ".step", ".stp"]); const fixtureName = path.basename(fixture); const fixtureArg = path.relative(path.join(root, "freecad-source/data"), fixture).split(path.sep).join("/"); const stem = path.basename(fixtureName, path.extname(fixtureName)).toLowerCase(); const expectedSha256 = crypto.createHash("sha256").update(fs.readFileSync(fixture)).digest("hex"); const evidenceRoot = path.join(root, "docs/current/ui-parity-baseline-20261011"); const nativeEvidence = path.join(evidenceRoot, `${stem}-native-boundary.json`); const roundtripEvidence = path.join(evidenceRoot, `${stem}-project-roundtrip.json`); const auditEvidence = path.join(evidenceRoot, "workbench-surface-audit.json"); const profileEvidence = path.join(evidenceRoot, `${stem}-profile.json`); const origin = process.env.FREECAD_BROWSER_ORIGIN ?? "http://127.0.0.1:5281"; function run(command, args, env = {}) { try { const output = execFileSync(process.execPath, [path.join(root, "tools", command), ...args], { cwd: root, env: { ...process.env, ...env }, encoding: "utf8", maxBuffer: 1024 * 1024, }); return { ok: true, output: output.trim().split(/\r?\n/).filter(Boolean).at(-1) ?? "" }; } catch (error) { return { ok: false, output: String(error.stdout ?? error.stderr ?? error.message).trim().split(/\r?\n/).filter(Boolean).at(-1) ?? "" }; } } function readJson(file) { return JSON.parse(fs.readFileSync(file, "utf8")); } function fail(stage, reason, extra = {}) { const report = { schema: "freecad-web-official-native-shape-profile/v1", fixture: { path: path.relative(root, fixture), sha256: expectedSha256 }, origin, stage, status: "blocked", reason, ...extra }; fs.writeFileSync(profileEvidence, `${JSON.stringify(report, null, 2)}\n`); fs.writeSync(1, `${JSON.stringify({ fixture: fixtureName, status: report.status, stage, reason })}\n`); process.exit(1); } const preflightCommand = /\.brep$/i.test(fixtureName) ? "probe-official-brep-native-preflight.mjs" : "probe-official-step-native-preflight.mjs"; const preflightArgs = [fixtureArg]; if (!fs.existsSync(nativeEvidence)) { const result = run(preflightCommand, preflightArgs); if (!result.ok) fail("native-preflight", "runner_failed", { runner: result.output }); } if (!fs.existsSync(nativeEvidence)) fail("native-preflight", "evidence_missing"); const native = readJson(nativeEvidence); const nativeFixture = native.fixture ?? {}; const nativeObjects = native.snapshot?.objects ?? (native.snapshot?.object ? [native.snapshot] : []); const nativeTriangles = nativeObjects.reduce((sum, object) => sum + Number(object.triangleCount ?? 0), 0); if (native.status !== "passed" || nativeFixture.sha256 !== expectedSha256 || nativeObjects.length === 0 || nativeTriangles <= 0) fail("native-preflight", "contract_failed", { native: { status: native.status, sha256: nativeFixture.sha256, objectCount: nativeObjects.length, triangles: nativeTriangles } }); if (!fs.existsSync(roundtripEvidence) || readJson(roundtripEvidence).status !== "passed" || readJson(roundtripEvidence).fixture?.sha256 !== expectedSha256 || !readJson(roundtripEvidence).recompute?.ok || readJson(roundtripEvidence).nativeComparison?.length !== nativeGeometryCount(nativeObjects)) { const result = run("ui-official-brep-project-roundtrip.mjs", [fixtureArg], { FREECAD_BROWSER_ORIGIN: origin }); if (!result.ok) fail("web-roundtrip", "runner_failed", { runner: result.output }); } if (!fs.existsSync(roundtripEvidence)) fail("web-roundtrip", "evidence_missing"); const roundtrip = readJson(roundtripEvidence); if (roundtrip.status !== "passed" || roundtrip.fixture?.sha256 !== expectedSha256 || Number(roundtrip.opened?.objectCount ?? 0) <= 0 || Number(roundtrip.opened?.triangleCount ?? 0) <= 0 || !roundtrip.recompute?.ok || roundtrip.nativeComparison?.length !== nativeGeometryCount(nativeObjects) || Number(roundtrip.reopened?.triangleCount ?? 0) <= 0 || roundtrip.pageErrors?.length || roundtrip.viewports?.length !== 2 || roundtrip.viewports.some(view => view.overflow || view.frameOverflow || !view.inViewport)) fail("web-roundtrip", "contract_failed", { roundtrip: { status: roundtrip.status, objectCount: roundtrip.opened?.objectCount, triangles: roundtrip.opened?.triangleCount, reopenedTriangles: roundtrip.reopened?.triangleCount, viewports: roundtrip.viewports?.length, pageErrors: roundtrip.pageErrors?.length ?? 0 } }); let audit = fs.existsSync(auditEvidence) ? readJson(auditEvidence) : null; if (auditRequested || !audit || audit.status !== "passed" || audit.origin !== origin || !auditCoverageComplete(audit)) { const result = run("ui-workbench-surface-audit.mjs", [], { FREECAD_BROWSER_ORIGIN: origin }); if (!result.ok) fail("audit64", "runner_failed", { runner: result.output }); audit = fs.existsSync(auditEvidence) ? readJson(auditEvidence) : null; } if (!audit || audit.status !== "passed" || audit.origin !== origin || !auditCoverageComplete(audit) || audit.failures?.length) fail("audit64", "contract_failed", { audit: { status: audit?.status, origin: audit?.origin, records: audit?.records?.length, failures: audit?.failures?.length } }); const report = { schema: "freecad-web-official-native-shape-profile/v1", fixture: { path: path.relative(root, fixture), sha256: expectedSha256 }, origin, status: "passed", native: { evidence: path.relative(root, nativeEvidence), objectCount: nativeObjects.length, triangles: nativeTriangles }, web: { evidence: path.relative(root, roundtripEvidence), objectCount: Number(roundtrip.opened.objectCount), triangles: Number(roundtrip.opened.triangleCount), reopenedTriangles: Number(roundtrip.reopened.triangleCount), viewports: roundtrip.viewports.length, pageErrors: roundtrip.pageErrors.length }, audit64: { evidence: path.relative(root, auditEvidence), records: audit.records.length, expectedRecords: auditCoverage(audit).expected, failures: audit.failures.length }, next: "Update the case ledger and continuationNotes only after reviewing this profile evidence." }; fs.writeFileSync(profileEvidence, `${JSON.stringify(report, null, 2)}\n`); console.log(JSON.stringify({ fixture: fixtureName, status: report.status, nativeObjects: nativeObjects.length, nativeTriangles: nativeTriangles, webTriangles: report.web.triangles, audit64: `${audit.records.length}/${report.audit64.expectedRecords}` })); function nativeGeometryCount(objects) { return objects.filter(object => Number(object.triangleCount) > 0 && Number.isFinite(Number(object.volume))).length; }