#!/usr/bin/env node import assert from "node:assert/strict"; 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 { Worker } from "node:worker_threads"; 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 batch = JSON.parse(fs.readFileSync(path.join(root, "docs/current/module-batch-manifest.json"), "utf8")); const modules = JSON.parse(fs.readFileSync(path.join(root, "web/public/modules/manifest.json"), "utf8")); if (batch.status !== "ready" || batch.taskId !== "FW-P6-FPA-Y" || batch.selected?.module !== "PartDesign" || batch.selected?.tier !== "tier-3-stateful" || !["property", "document-graph", "lifecycle-command"].includes(batch.selected?.family) || !Array.isArray(batch.records) || batch.records.length < 2) throw new Error("unexpected PartDesign stateful batch manifest"); const records = batch.records.map(([apiId, name, disposition, sourcePath, sourceLine]) => ({ apiId, name, family: batch.selected.family, sourcePath, sourceLine, disposition })); const hash = value => crypto.createHash("sha256").update(typeof value === "string" || Buffer.isBuffer(value) ? value : JSON.stringify(value)).digest("hex"); const fileHash = file => hash(fs.readFileSync(file)); const sourceEvidence = records.map(row => { const source = path.join(root, "freecad-source", row.sourcePath); const declaration = fs.readFileSync(source, "utf8").split(/\r?\n/)[row.sourceLine - 1] ?? ""; assert.ok(declaration.trim(), `missing source declaration for ${row.apiId}`); return { apiId: row.apiId, path: row.sourcePath, line: row.sourceLine, declaration: declaration.trim(), sha256: fileHash(source) }; }); const output = fs.mkdtempSync(path.join(os.tmpdir(), "freecad-partdesign-generic-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 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 request = (requestId, rows = records) => facade.createPartDesignPythonFamilyRequest(requestId, rows, "tier-3-stateful"); const ready = await next(); worker.postMessage(request("pd-generic-success")); const success = await next(); worker.postMessage(request("pd-generic-repeat")); const repeat = await next(); worker.postMessage(request("pd-generic-cross", [{ ...records[0], apiId: "py:BIM:class:Binding" }])); const cross = await next(); const invalid = request("pd-generic-invalid"); worker.postMessage({ ...invalid, payload: { ...invalid.payload, tier: "tier-unsupported" } }); const malformed = await next(); worker.postMessage(request("pdn-python-cancel-1")); worker.postMessage(envelope.createCancel("pd-generic-cancel", "pdn-python-cancel-1")); const cancelled = await next(); worker.postMessage({ kind: "reset", requestId: "pd-generic-reset" }); const reset = await next(); await worker.terminate(); if (ready.kind !== "ready" || success.kind !== "result" || repeat.kind !== "result" || cross.kind !== "result" || malformed.kind !== "error" || cancelled.kind !== "error" || reset.requestId !== "pd-generic-reset") throw new Error(JSON.stringify({ ready, success, repeat, cross, malformed, cancelled, reset })); const result = success.result; const terminal = new Set(["exposed", "restricted", "nonportable", "not_implemented"]); if (result.module !== "PartDesign" || result.tier !== "tier-3-stateful" || result.count !== records.length || result.records.length !== records.length || result.resourcesReleased !== true) throw new Error("PartDesign generic-object result is incomplete"); for (const row of result.records) { if (!terminal.has(row.status) || row.native !== true || row.resourcesReleased !== true || typeof row.sourceRevision !== "string" || typeof row.sourceEntry !== "string" || typeof row.parameterSchema !== "string" || typeof row.returnSchema !== "string") throw new Error(`PartDesign generic-object record schema is incomplete for ${row.name}`); if (batch.selected.family === "property" && row.name.startsWith("WizardShaftTable.") && (row.status !== "restricted" || row.code !== "gui_dependency_unavailable")) throw new Error(`PartDesign GUI property boundary changed for ${row.name}`); if (batch.selected.family === "property" && ["SegmentFunctionSegment.value", "SegmentFunction.value", "SegmentFunction.lowervalue", "IntervalFunction.value", "IntervalFunction.lowervalue"].includes(row.name) && row.status !== "exposed") throw new Error(`PartDesign native property fixture failed for ${row.name}: ${row.status}/${row.code}`); if (batch.selected.family === "document-graph" && !["Body.insertObject", "Feature.getBaseObject"].includes(row.name)) throw new Error(`unexpected PartDesign document-graph record ${row.name}`); if (batch.selected.family === "document-graph" && !((row.status === "exposed" && row.code === "native_fixture") || (row.status === "not_implemented" && row.code === "native_fixture_error"))) throw new Error(`PartDesign native document-graph fixture returned invalid boundary for ${row.name}: ${row.status}/${row.code}`); } if (JSON.stringify(result) !== JSON.stringify(repeat.result)) throw new Error("PartDesign generic-object replay changed native result"); if (cross.result.records?.[0]?.status !== "not_implemented" || cross.result.records?.[0]?.code !== "unmatched_source_record") throw new Error("PartDesign cross-module boundary failed"); if (malformed.error.code !== "invalid_request" || cancelled.error.code !== "cancelled") throw new Error("PartDesign error/cancel boundary failed"); const evidencePath = `docs/p6/partdesign-module-batch-${batch.batchId}-runtime.json`; 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 = [evidencePath]; row.dispositionReason = native.message; } } fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); const dispositionCounts = Object.fromEntries([...terminal].map(status => [status, result.records.filter(row => row.status === status).length])); const module = modules.modules?.PartDesign ?? modules.PartDesign; const moduleWorker = module?.workers?.["partdesign-python.worker.ts"]; if (!module?.revision || !module?.assetBaseUrl || !moduleWorker?.revision || !moduleWorker?.runtimeUrl || !moduleWorker?.wasmUrl) throw new Error("PartDesign module artifact manifest is incomplete"); const trace = { ready, success, repeat, cross, malformed, cancelled, reset }; const artifacts = { module: "PartDesign", worker: "partdesign-python.worker.ts", revision: moduleWorker.revision, runtimeUrl: moduleWorker.runtimeUrl, wasmUrl: moduleWorker.wasmUrl, dataUrl: moduleWorker.dataUrl ?? null, assetBaseUrl: module.assetBaseUrl, moduleRevision: module.revision, buildProfile: "fast-dev", runtimeSha256: moduleWorker.hashes?.runtime ?? fileHash(path.join(build, "FreeCADWebPartDesignRuntimeProbe.js")), wasmSha256: moduleWorker.hashes?.wasm ?? fileHash(path.join(build, "FreeCADWebPartDesignRuntimeProbe.wasm")) }; const report = { schema: "freecad-web-p6-partdesign-stateful-module-batch-runtime/v1", taskId: "FW-P6-FPA-Y", batchId: batch.batchId, module: "PartDesign", tier: batch.selected.tier, family: batch.selected.family, status: "pass_with_boundaries", dispatcher: "fc_web_dispatch_json", sourceRevision: result.records[0]?.sourceRevision, 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-3-stateful", records } }, inputSha256: hash(records), result, resultSha256: hash(result), replay: { equal: true, resultSha256: hash(repeat.result) }, facadeWorker: { ready, success, repeat, cross, malformed, cancelled, reset, traceSha256: hash(trace) }, dispositionCounts, records: result.records.map(row => ({ apiId: row.apiId, name: row.name, family: row.family, status: row.status, code: row.code, message: row.message, parameterSchema: row.parameterSchema, returnSchema: row.returnSchema, sourceEntry: row.sourceEntry, sourceRevision: row.sourceRevision, evidenceSha256: hash(row) })), sourceEvidence, resources: { resourcesReleased: result.resourcesReleased, documentMutation: false }, artifacts, boundaries: batch.selected.family === "property" ? ["WizardShaftTable property operations require Qt widgets and remain restricted in the GUI-free Web kernel.", "SegmentFunction and IntervalFunction values are evaluated from the embedded FreeCAD Python source through fc_web_dispatch_json.", "PartDesign remains an independent runtime artifact loaded through partdesign-python.worker.ts; no WASM binary merge is used."] : batch.selected.family === "document-graph" ? ["Body.insertObject and Feature.getBaseObject are exercised against a native App::Document/PartDesign::Body fixture through fc_web_dispatch_json.", "PartDesign remains an independent runtime artifact loaded through partdesign-python.worker.ts; no WASM binary merge is used."] : ["Lifecycle command and GUI activation records are source-verified through the shared native dispatcher; command execution remains not_implemented where FreeCADGui/tooling is unavailable.", "PartDesign remains an independent runtime artifact loaded through partdesign-python.worker.ts; no WASM binary merge is used."], }; fs.writeFileSync(path.join(root, evidencePath), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, `docs/freecad-p6-partdesign-module-batch-${batch.batchId}.log`), `${JSON.stringify({ status: report.status, batchId: batch.batchId, count: records.length, dispositionCounts, resultSha256: report.resultSha256 })}\n`); console.log(JSON.stringify({ status: report.status, batchId: batch.batchId, count: records.length, dispositionCounts, resultSha256: report.resultSha256 })); process.exit(0);