#!/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 { Worker } from "node:worker_threads"; import { execFileSync } from "node:child_process"; 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/configure/bin")); const batchPath = path.resolve(process.env.PARTDESIGN_BATCH_MANIFEST || path.join(root, "docs/current/module-batch-manifest.json")); const batch = JSON.parse(fs.readFileSync(batchPath, "utf8")); const hash = (value) => crypto.createHash("sha256").update(typeof value === "string" ? value : JSON.stringify(value)).digest("hex"); const fileHash = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); if (batch.status !== "ready" || batch.selected?.module !== "PartDesign" || batch.selected?.tier !== "object-contract" || batch.selected?.family !== "geometry-recompute" || batch.records.length < 1 || batch.records.length > 32) throw new Error("unexpected PartDesign geometry module batch manifest"); const records = batch.records.map(([apiId, name, disposition, sourcePath]) => ({ apiId, name, family: batch.selected.family, sourcePath, disposition })); const output = fs.mkdtempSync(path.join(os.tmpdir(), "freecad-partdesign-geometry-module-batch-")); execFileSync(path.join(root, "web/node_modules/.bin/tsc"), ["--target", "ES2022", "--module", "ESNext", "--moduleResolution", "Bundler", "--strict", "--skipLibCheck", "--outDir", output, "--declaration", "false", "--noEmit", "false", path.join(root, "web/src/facade-envelope.ts"), path.join(root, "web/src/partdesign-geometry-family-facade.ts")], { cwd: root, stdio: "inherit" }); const facade = await import(pathToFileURL(path.join(output, "partdesign-geometry-family-facade.js"))); const envelope = await import(pathToFileURL(path.join(output, "facade-envelope.js"))); const worker = new Worker(path.join(root, "tools/partdesign-geometry-family-facade-smoke-worker.mjs"), { workerData: { buildDirectory: build, facadePath: pathToFileURL(path.join(output, "partdesign-geometry-family-facade.js")).href, envelopePath: pathToFileURL(path.join(output, "facade-envelope.js")).href } }); const next = () => new Promise((resolve, reject) => { const onMessage = (value) => { worker.off("error", onError); resolve(value); }; const onError = (error) => { worker.off("message", onMessage); reject(error); }; worker.once("message", onMessage); worker.once("error", onError); }); const request = (requestId, rows = records) => facade.createPartDesignGeometryFamilyRequest(requestId, rows.map((row) => ({ name: row.name }))); const ready = await next(); worker.postMessage(request("pd-geometry-batch-success")); const success = await next(); worker.postMessage(request("pd-geometry-batch-repeat")); const repeat = await next(); worker.postMessage(request("pd-geometry-batch-cross", [{ ...records[0], name: "PartDesign::Missing" }])); const cross = await next(); worker.postMessage({ ...request("pd-geometry-batch-invalid"), payload: { ...request("pd-geometry-batch-invalid").payload, records: [] } }); const malformed = await next(); const cancelRequest = request("pd-geometry-cancel-1"); worker.postMessage(cancelRequest); worker.postMessage(envelope.createCancel("pd-geometry-cancel-control", cancelRequest.requestId)); const cancelled = await next(); await worker.terminate(); if (ready.kind !== "ready" || success.kind !== "result" || repeat.kind !== "result" || cross.kind !== "result" || malformed.kind !== "error" || cancelled.kind !== "error") throw new Error(JSON.stringify({ ready, success, repeat, cross, malformed, cancelled })); const result = success.result; if (result.module !== "PartDesign" || result.count !== records.length || result.records.length !== records.length || result.resourcesReleased !== true) throw new Error("PartDesign geometry batch result is incomplete"); if (result.records.some((row) => !["passed", "blocked"].includes(row.status) || row.native !== true || row.resourcesReleased !== true)) throw new Error("PartDesign geometry batch record schema is incomplete"); if (JSON.stringify(result) !== JSON.stringify(repeat.result)) throw new Error("PartDesign geometry batch replay changed native result"); if (cross.result.records?.[0]?.status !== "blocked") throw new Error("PartDesign geometry missing-type boundary failed"); if (!(["invalid_fixture", "invalid_request"].includes(malformed.error.code)) || cancelled.error.code !== "cancelled") throw new Error(JSON.stringify({ message: "PartDesign geometry error/cancel boundary failed", malformed, cancelled })); const matrixPath = path.join(root, "docs/p6/object-contract-matrix.json"); const matrix = JSON.parse(fs.readFileSync(matrixPath, "utf8")); const byName = new Map(result.records.map((row) => [row.name, row])); const evidencePath = `docs/p6/partdesign-geometry-module-batch-${batch.batchId}-runtime.json`; for (const row of matrix.records) { const native = byName.get(row.name); if (!native || row.module !== "PartDesign") continue; row.status = native.status; row.dispositionEvidence = [evidencePath]; row.dispositionReason = native.detail; } fs.writeFileSync(matrixPath, `${JSON.stringify(matrix, null, 2)}\n`); const statusCounts = Object.fromEntries(["passed", "blocked"].map((status) => [status, result.records.filter((row) => row.status === status).length])); const report = { schema: "freecad-web-partdesign-geometry-module-batch-runtime/v1", taskId: "FW-P6-FPA-Y", batchId: batch.batchId, module: "PartDesign", tier: "object-contract", family: "geometry-recompute", status: statusCounts.blocked === 0 ? "pass" : "pass_with_boundaries", sourceRevision: result.records[0]?.detail ? "2778e3f9c215da48bf0cfd562478d45d624aaa47" : null, route: { command: "partdesign.geometryFamily", native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/PartDesignRuntimeProbe.cpp:fc_web_dispatch_json", facade: "web/src/partdesign-geometry-family-facade.ts", worker: "web/src/partdesign.worker.ts" }, input: { cmd: "partdesign.geometryFamily", args: { records: records.map((row) => ({ name: row.name })) } }, inputSha256: hash(records), result, resultSha256: hash(result), replay: { equal: true, resultSha256: hash(repeat.result) }, facadeWorker: { ready, success, repeat, crossModule: cross, malformed, cancelled, traceSha256: hash({ ready, success, repeat, cross, malformed, cancelled }) }, statusCounts, records: result.records.map((row) => ({ name: row.name, status: row.status, code: row.code, detail: row.detail, evidenceSha256: hash(row) })), resources: { resourcesReleased: true, documentMutation: false }, artifacts: { wasm: { path: "web/public/kernel/FreeCADWebPartDesignRuntimeProbe.wasm", sha256: fileHash(path.join(root, "web/public/kernel/FreeCADWebPartDesignRuntimeProbe.wasm")) }, loader: { path: "web/public/kernel/FreeCADWebPartDesignRuntimeProbe.js", sha256: fileHash(path.join(root, "web/public/kernel/FreeCADWebPartDesignRuntimeProbe.js")) } }, boundaries: ["Every status and value is returned by FreeCAD through fc_web_dispatch_json; JavaScript only validates and transports the batch envelope.", "A missing registered type remains blocked and is not promoted."] }; fs.writeFileSync(path.join(root, evidencePath), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, `docs/freecad-p6-partdesign-geometry-module-batch-${batch.batchId}.log`), `${JSON.stringify({ status: report.status, batchId: batch.batchId, count: records.length, statusCounts, resultSha256: report.resultSha256 })}\n`); console.log(JSON.stringify({ status: report.status, batchId: batch.batchId, count: records.length, statusCounts, resultSha256: report.resultSha256 }));