#!/usr/bin/env node import crypto from "node:crypto"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { execFileSync } from "node:child_process"; import { createRequire } from "node:module"; import { pathToFileURL } from "node:url"; const root = "/home/mes123456/FreeCADWasmDev/working/FreeCAD"; const build = path.resolve(process.argv[2] ?? path.join(root, "freecad-source/build-web/techdraw-native-full-pthread/bin")); const tier = process.argv[3] ?? "tier-1-readonly"; const expectedCount = tier === "tier-2-pure" ? 66 : 101; if (!new Set(["tier-1-readonly", "tier-2-pure"]).has(tier)) throw new Error(`unsupported TechDraw tier: ${tier}`); const manifestPath = path.join(root, "docs/p6/python-api-manifest.json"); const sourceFacadePath = path.join(root, "web/src/techdraw-python-family-facade.ts"); const out = fs.mkdtempSync(path.join(os.tmpdir(), "freecad-p6-techdraw-family-")); execFileSync(path.join(root, "web/node_modules/.bin/tsc"), ["--target", "ES2022", "--module", "ESNext", "--moduleResolution", "Bundler", "--strict", "--skipLibCheck", "--outDir", out, "--declaration", "false", "--noEmit", "false", "web/src/facade-envelope.ts", "web/src/techdraw-python-family-facade.ts"], { cwd: root, stdio: "inherit" }); const facadePath = path.join(out, "techdraw-python-family-facade.js"); const envelopePath = path.join(out, "facade-envelope.js"); const hash = (value) => crypto.createHash("sha256").update(JSON.stringify(value)).digest("hex"); const fileHash = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); const records = manifest.records .filter((row) => row.module === "TechDraw" && row.tier === tier) .map((row) => ({ apiId: row.id, name: row.name, family: row.family, kind: row.kind, sourcePath: row.evidence?.path, disposition: row.disposition, })); if (records.length !== expectedCount) throw new Error(`expected ${expectedCount} TechDraw ${tier} records, got ${records.length}`); const facade = await import(pathToFileURL(facadePath)); const envelope = await import(pathToFileURL(envelopePath)); globalThis.__dirname = `${build}${path.sep}`; globalThis.require = createRequire(import.meta.url); const workerJs = path.join(build, "FreeCADWebTechDrawRuntimeProbe.worker.js"); const workerCjs = path.join(build, "FreeCADWebTechDrawRuntimeProbe.worker.cjs"); if (!fs.existsSync(workerCjs) && fs.existsSync(workerJs)) fs.copyFileSync(workerJs, workerCjs); const loader = await import(pathToFileURL(path.join(build, "FreeCADWebTechDrawRuntimeProbe.js"))); const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebTechDrawRuntimeProbe.wasm")), locateFile: file => file.endsWith(".worker.js") && fs.existsSync(workerCjs) ? workerCjs : path.join(build, file), print: () => {}, printErr: () => {} }); const queue = new envelope.FacadeSerialQueue(); const events = ["native-ready"]; const dispatch = (request, source = kernel) => facade.dispatchTechDrawPythonFamily(queue, request, source); const successRequest = facade.createTechDrawPythonFamilyRequest("techdraw-family-gate-success", records, tier); const success = await dispatch(successRequest); const repeated = await dispatch(facade.createTechDrawPythonFamilyRequest("techdraw-family-gate-replay", records, tier)); const crossRecords = [{ ...records[0], apiId: "py:BIM:class:Camera" }]; const cross = await dispatch(facade.createTechDrawPythonFamilyRequest("techdraw-family-gate-cross-module", crossRecords, tier)); const malformedTier = "tier-unsupported"; const malformedRequest = { ...successRequest, requestId: "techdraw-family-gate-invalid-tier", payload: { ...successRequest.payload, tier: malformedTier } }; const malformed = await dispatch(malformedRequest); const cancelRequest = facade.createTechDrawPythonFamilyRequest("techdraw-family-cancel-1", records, tier); const cancelledPromise = dispatch(cancelRequest, new Promise((resolve) => setTimeout(() => resolve(kernel), 25))); queue.cancel(envelope.createCancel("techdraw-family-gate-cancel", cancelRequest.requestId)); const cancelled = await cancelledPromise; if (success.kind !== "result") throw new Error(`TechDraw family dispatch failed: ${JSON.stringify(success)}`); const result = success.result; if (result.module !== "TechDraw" || result.tier !== tier || result.count !== records.length || result.records.length !== records.length || result.resourcesReleased !== true) throw new Error("TechDraw family result is incomplete"); const terminal = new Set(["exposed", "restricted", "nonportable", "not_implemented"]); if (result.records.some((record) => !terminal.has(record.status) || record.native !== true || record.resourcesReleased !== true || !record.sourceEntry || !record.sourceRevision || !record.parameterSchema || !record.returnSchema)) throw new Error("TechDraw family result contains an invalid terminal record"); if (JSON.stringify(result) !== JSON.stringify(repeated.result)) throw new Error("TechDraw family replay changed the native result"); if (cross.kind !== "result" || cross.result.records?.[0]?.status !== "not_implemented" || cross.result.records?.[0]?.code !== "unmatched_source_record") throw new Error("TechDraw cross-module boundary failed"); if (malformed.kind !== "error" || malformed.error.code !== "invalid_request") throw new Error("TechDraw invalid tier was not rejected"); if (cancelled.kind !== "error" || cancelled.error.code !== "cancelled") throw new Error("TechDraw cancellation was not reported"); const dispositionCounts = Object.fromEntries([...terminal].map((status) => [status, result.records.filter((record) => record.status === status).length])); const byId = new Map(result.records.map((record) => [record.apiId, record])); for (const row of manifest.records) { if (row.module !== "TechDraw" || row.tier !== tier) continue; const native = byId.get(row.id); if (!native) throw new Error(`native result missing ${row.id}`); row.disposition = native.status; row.dispositionEvidence = [`docs/p6/techdraw-${tier === "tier-2-pure" ? "tier2-pure" : "tier1-readonly"}-family-runtime.json`]; row.dispositionReason = native.status === "restricted" ? native.message : native.status === "not_implemented" ? native.message : "native TechDraw Python class/symbol was resolved by the FreeCAD Web runtime"; } fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); const report = { schema: `freecad-web-p6-techdraw-${tier === "tier-2-pure" ? "tier2-pure" : "tier1-readonly"}-family-runtime/v1`, taskId: tier === "tier-2-pure" ? "FW-P6-FPA-F2-TECHDRAW" : "FW-P6-FPA-F1-TECHDRAW", module: "TechDraw", tier, status: "pass_with_boundaries", sourceRevision: result.records[0]?.sourceRevision, route: { worker: "web/src/techdraw-python.worker.ts", facade: "web/src/techdraw-python-family-facade.ts", command: facade.TECHDRAW_PYTHON_FAMILY_COMMAND, native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/TechDrawRuntimeProbe.cpp:fc_web_dispatch_json", }, input: { cmd: facade.TECHDRAW_PYTHON_FAMILY_COMMAND, args: { tier, records } }, inputSha256: hash({ cmd: facade.TECHDRAW_PYTHON_FAMILY_COMMAND, args: { tier, records } }), result, resultSha256: hash(result), replay: { equal: true, resultSha256: hash(repeated.result) }, crossModuleBoundary: { result: cross, resultSha256: hash(cross) }, malformedRequest: { result: malformed, resultSha256: hash(malformed) }, cancellation: { result: cancelled, resultSha256: hash(cancelled) }, dispositionCounts, records: result.records.map((record) => ({ apiId: record.apiId, name: record.name, family: record.family, status: record.status, code: record.code, parameterSchema: record.parameterSchema, returnSchema: record.returnSchema, replay: record.replay, sourceEntry: record.sourceEntry, sourceRevision: record.sourceRevision, evidenceSha256: hash(record), })), resources: { resourcesReleased: true, documentMutation: false }, artifacts: { wasm: { path: path.relative(root, path.join(build, "FreeCADWebTechDrawRuntimeProbe.wasm")), sha256: fileHash(path.join(build, "FreeCADWebTechDrawRuntimeProbe.wasm")) }, loader: { path: path.relative(root, path.join(build, "FreeCADWebTechDrawRuntimeProbe.js")), sha256: fileHash(path.join(build, "FreeCADWebTechDrawRuntimeProbe.js")) }, }, events, boundaries: [ "TechDraw view-resource and TechDrawTools APIs remain restricted because FreeCADGui is unavailable in the headless Web runtime.", "Method records remain not_implemented until a method-specific native fixture supplies typed arguments and return replay; source-declared schemas are retained.", "All statuses, schemas and hashes derive from compiled FreeCAD through fc_web_dispatch_json; no JavaScript API result is substituted.", ], }; const evidenceStem = tier === "tier-2-pure" ? "techdraw-tier2-pure-family" : "techdraw-tier1-readonly-family"; fs.writeFileSync(path.join(root, `docs/p6/${evidenceStem}-runtime.json`), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, `docs/freecad-p6-${evidenceStem}.log`), `${JSON.stringify({ status: report.status, count: records.length, dispositionCounts, resultSha256: report.resultSha256 })}\n`); console.log(JSON.stringify({ status: report.status, count: records.length, dispositionCounts, resultSha256: report.resultSha256 })); // The TechDraw probe uses pthread-enabled Emscripten libraries; terminate // after the deterministic evidence has been flushed so worker handles do not // keep a successful gate alive. process.exit(0);