Files
FreeCADWasmDev/tools/run_p6_partdesign_pure_function_module_batch_gate.mjs
wangdequan 9a7c4f0335 Save FreeCAD Web workspace sources and user validation evidence
IFC native binding remains incomplete; preserve accurate test results.
2026-09-15 22:01:58 +08:00

48 lines
7.7 KiB
JavaScript

#!/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.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 !== "tier-2-pure" || batch.selected?.family !== "pure-function" || batch.records.length !== 4) throw new Error("unexpected PartDesign pure-function 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-pure-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-python-family-facade.ts")], { cwd: root, stdio: "inherit" });
const facade = await import(pathToFileURL(path.join(output, "partdesign-python-family-facade.js")));
const envelope = await import(pathToFileURL(path.join(output, "facade-envelope.js")));
const request = (requestId, rows = records) => facade.createPartDesignPythonFamilyRequest(requestId, rows, "tier-2-pure");
const worker = new Worker(path.join(root, "tools/partdesign-python-family-facade-smoke-worker.mjs"), { workerData: { buildDirectory: build, facadePath: pathToFileURL(path.join(output, "partdesign-python-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 ready = await next();
worker.postMessage(request("pd-pure-success")); const success = await next();
worker.postMessage(request("pd-pure-repeat")); const repeat = await next();
worker.postMessage(request("pd-pure-cross", [{ ...records[0], apiId: "py:Part:function:makeBox" }])); const cross = await next();
worker.postMessage({ ...request("pd-pure-invalid"), payload: { ...request("pd-pure-invalid").payload, tier: "tier-unsupported" } }); const malformed = await next();
worker.postMessage(request("pdn-python-cancel-1")); worker.postMessage(envelope.createCancel("pd-pure-cancel", "pdn-python-cancel-1")); const cancelled = await next();
await worker.terminate();
if (success.kind !== "result" || repeat.kind !== "result" || JSON.stringify(success.result) !== JSON.stringify(repeat.result)) throw new Error("PartDesign pure function replay failed");
const result = success.result;
if (result.count !== 4 || result.records?.length !== 4 || result.resourcesReleased !== true || result.records.some(row => row.native !== true || row.resourcesReleased !== true || row.status !== "not_implemented" || row.code !== "source_disposition")) throw new Error("PartDesign pure function native boundary result is incomplete");
if (cross.kind !== "result" || cross.result?.records?.[0]?.code !== "unmatched_source_record") throw new Error("PartDesign pure function cross-module boundary failed");
if (malformed.kind !== "error" || !["invalid_request", "invalid_fixture"].includes(malformed.error?.code)) throw new Error("PartDesign pure function malformed request was not rejected");
if (cancelled.kind !== "error" || cancelled.error?.code !== "cancelled") throw new Error("PartDesign pure function cancellation was not reported");
const manifestPath = path.join(root, "docs/p6/python-api-manifest.json"); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); const byId = new Map(result.records.map(row => [row.apiId, row]));
for (const row of manifest.records) { const native = byId.get(row.id); if (native) { row.disposition = native.status; row.dispositionEvidence = [`docs/p6/partdesign-pure-function-module-batch-${batch.batchId}-runtime.json`]; row.dispositionReason = native.message; } }
fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
const counts = Object.fromEntries(["exposed", "restricted", "nonportable", "not_implemented"].map(status => [status, result.records.filter(row => row.status === status).length]));
const artifacts = { module: "PartDesign", worker: "partdesign-python.worker.ts", revision: result.records[0]?.sourceRevision, runtimeUrl: "/kernel/FreeCADWebPartDesignRuntimeProbe.js", wasmUrl: "/kernel/FreeCADWebPartDesignRuntimeProbe.wasm", dataUrl: null, assetBaseUrl: "/kernel/", buildProfile: "fast-dev", 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")) } };
const report = { schema: "freecad-web-p6-partdesign-pure-function-module-batch-runtime/v1", taskId: "FW-P6-FPA-Y", batchId: batch.batchId, module: "PartDesign", tier: "tier-2-pure", family: "pure-function", status: "pass_with_boundaries", sourceRevision: artifacts.revision, route: { command: facade.PARTDESIGN_PYTHON_FAMILY_COMMAND, native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/PartDesignRuntimeProbe.cpp:fc_web_dispatch_json", facade: "web/src/partdesign-python-family-facade.ts", worker: "web/src/partdesign-python.worker.ts" }, input: { cmd: facade.PARTDESIGN_PYTHON_FAMILY_COMMAND, args: { tier: "tier-2-pure", records } }, 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 }) }, dispositionCounts: counts, records: result.records.map(row => ({ apiId: row.apiId, name: row.name, status: row.status, code: row.code, message: row.message, sourceEntry: row.sourceEntry, sourceRevision: row.sourceRevision, evidenceSha256: hash(row) })), resources: { resourcesReleased: true, documentMutation: false }, artifacts, boundaries: ["The four APIs are FreeCAD Python workbench scripts without a typed native argument/return fixture.", "Their not_implemented status comes from compiled FreeCAD through fc_web_dispatch_json; JavaScript only transports and validates the envelope."] };
const evidencePath = path.join(root, `docs/p6/partdesign-pure-function-module-batch-${batch.batchId}-runtime.json`);
fs.writeFileSync(evidencePath, `${JSON.stringify(report, null, 2)}\n`);
fs.writeFileSync(path.join(root, `docs/freecad-p6-partdesign-pure-function-module-batch-${batch.batchId}.log`), `${JSON.stringify({ status: report.status, batchId: report.batchId, count: result.count, dispositionCounts: counts, resultSha256: report.resultSha256 })}\n`);
console.log(JSON.stringify({ status: report.status, batchId: report.batchId, count: result.count, dispositionCounts: counts, resultSha256: report.resultSha256 }));