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 { 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/configure/bin")); const output = fs.mkdtempSync(path.join(os.tmpdir(), "freecad-part-shape-max-vol-")); const sourceFiles = ["facade-envelope.ts", "part-python-method-facade.ts", "api-method-bridge.ts"]; execFileSync(path.join(root, "web/node_modules/.bin/tsc"), [ "--target", "ES2022", "--module", "ESNext", "--moduleResolution", "Bundler", "--strict", "--skipLibCheck", "--outDir", output, "--declaration", "false", "--noEmit", "false", ...sourceFiles.map((file) => path.join(root, "web/src", file)), ], { cwd: root, stdio: "inherit" }); const facadePath = pathToFileURL(path.join(output, "part-python-method-facade.js")).href; const envelopePath = pathToFileURL(path.join(output, "facade-envelope.js")).href; const facade = await import(facadePath); const envelope = await import(envelopePath); const shapeArgs = { volumes: [6, 24, 12] }; const createShape = (id, args = shapeArgs) => facade.createPartShapeOfMaxVolRequest(id, args); const worker = new Worker(path.join(root, "tools/part-python-method-facade-smoke-worker.mjs"), { workerData: { buildDirectory: build, facadePath, envelopePath } }); 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(createShape("part-shape-success")); const success = await next(); worker.postMessage(createShape("part-shape-repeat")); const repeat = await next(); worker.postMessage(createShape("part-shape-invalid", { volumes: [0, 24] })); const invalid = await next(); worker.postMessage(createShape("part-shape-cancel")); worker.postMessage(envelope.createCancel("part-shape-cancel-control", "part-shape-cancel", "part-method-document")); const cancelled = await next(); worker.postMessage({ ...createShape("part-shape-cross"), payload: { ...createShape("part-shape-cross").payload, module: "BIM" } }); const crossModule = await next(); await worker.terminate(); globalThis.__dirname = `${build}${path.sep}`; globalThis.require = createRequire(import.meta.url); const loader = await import(pathToFileURL(path.join(build, "FreeCADWebPartBoxProbe.js"))); const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebPartBoxProbe.wasm")), print: () => {}, printErr: () => {} }); const invoke = (value) => JSON.parse(String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify(value)]))); const direct = invoke({ cmd: "part.pythonMethod", args: createShape("part-shape-direct").payload }); const crossDirect = invoke({ cmd: "part.pythonMethod", args: { ...createShape("part-shape-cross-direct").payload, module: "BIM" } }); const malformed = invoke({ cmd: "part.pythonMethod", args: { ...createShape("part-shape-malformed").payload, tier: "tier-1-readonly" } }); const unsupported = invoke({ cmd: "part.unknownMethod", args: {} }); const diagnostics = { ready, success, repeat, invalid, cancelled, crossModule, direct, crossDirect, malformed, unsupported }; if (ready.kind !== "ready" || success.kind !== "result" || success.result.status !== "exposed" || success.result.result.compoundShapeType !== "Compound" || success.result.result.childCount !== 3 || Math.abs(success.result.result.selectedVolume - 24) > 1e-9 || repeat.kind !== "result" || JSON.stringify(success.result) !== JSON.stringify(repeat.result) || invalid.kind !== "error" || invalid.error.code !== "invalid_arguments" || cancelled.kind !== "error" || cancelled.error.code !== "cancelled" || crossModule.kind !== "error" || crossModule.error.code !== "invalid_request" || direct.ok !== true || Math.abs(direct.result.selectedVolume - 24) > 1e-9 || crossDirect.ok !== false || crossDirect.code !== "unmatched_source_record" || malformed.ok !== false || malformed.code !== "unmatched_source_record" || unsupported.ok !== false || unsupported.code !== "unsupported_command") { console.error(JSON.stringify(diagnostics)); throw new Error("Part.shapeOfMaxVol Python method gate assertion failed"); } const apiBridge = await import(pathToFileURL(path.join(output, "api-method-bridge.js"))); const runtimeIndex = JSON.parse(fs.readFileSync(path.join(root, "web/public/api/python-api-index.json"), "utf8")); const bridgeRouter = { request: async (_module, _command, payload) => envelope.createSuccess("router-shape", await facade.handlePartPythonMethod(payload, new AbortController().signal, kernel)), }; const bridge = new apiBridge.ApiMethodBridge(bridgeRouter, runtimeIndex); const bridgeSuccess = await bridge.call({ apiId: "py:Part:function:shapeOfMaxVol", args: [[6, 24, 12]] }); const bridgeInvalid = await bridge.call({ apiId: "py:Part:function:shapeOfMaxVol", args: [[6, 0, 12]] }); if (bridgeSuccess.kind !== "result" || Math.abs(bridgeSuccess.result.result.selectedVolume - 24) > 1e-9 || bridgeInvalid.kind !== "error" || bridgeInvalid.error.code !== "invalid_arguments") { console.error(JSON.stringify({ bridgeSuccess, bridgeInvalid })); throw new Error("Part.shapeOfMaxVol API bridge assertion failed"); } const manifestPath = path.join(root, "docs/p6/python-api-manifest.json"); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); const row = manifest.records.find((item) => item.id === "py:Part:function:shapeOfMaxVol"); if (!row) throw new Error("py:Part:function:shapeOfMaxVol is missing from the source manifest"); row.disposition = "exposed"; row.dispositionEvidence = ["docs/p6/part-shape-max-vol-method-runtime.json"]; row.dispositionReason = "typed Part.shapeOfMaxVol official Python source replay through fc_web_dispatch_json"; fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); 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 report = { schema: "freecad-web-p6-part-shape-max-vol-method-runtime/v1", taskId: "FW-P6-FPA-Y", module: "Part", tier: "tier-2-pure", apiId: "py:Part:function:shapeOfMaxVol", status: "pass", sourceEntry: "src/Mod/Part/JoinFeatures.py", sourceRevision: "2778e3f9c215da48bf0cfd562478d45d624aaa47", route: { command: "part.pythonMethod", worker: "web/src/part.worker.ts", facade: "web/src/part-python-method-facade.ts", apiBridge: "web/src/api-method-bridge.ts", native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/PartBoxProbe.cpp:fc_web_dispatch_json" }, request: createShape("part-shape-success"), requestSha256: hash(createShape("part-shape-success")), trace: { ready, success, repeat, invalid, cancelled, crossModule, direct, crossDirect, malformed, unsupported, bridgeSuccess, bridgeInvalid }, replay: { equal: true, resultSha256: hash(repeat.result) }, resources: { resourcesReleased: success.result.resourcesReleased === true, documentMutation: false }, resultSha256: hash(success.result), artifacts: { wasm: { path: "web/public/kernel/FreeCADWebPartBoxProbe.wasm", sha256: fileHash(path.join(root, "web/public/kernel/FreeCADWebPartBoxProbe.wasm")) }, loader: { path: "web/public/kernel/FreeCADWebPartBoxProbe.js", sha256: fileHash(path.join(root, "web/public/kernel/FreeCADWebPartBoxProbe.js")) }, }, boundaries: ["The typed fixture creates native Part boxes and invokes official JoinFeatures.shapeOfMaxVol on a real compound.", "Equal-volume inputs remain a source_error from the official function; GUI-only records remain restricted.", "All values are returned by compiled FreeCAD through fc_web_dispatch_json; no JavaScript geometry substitute is used."], }; fs.writeFileSync(path.join(root, "docs/p6/part-shape-max-vol-method-runtime.json"), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/freecad-p6-part-shape-max-vol-method-gate.log"), `${JSON.stringify({ status: report.status, apiId: report.apiId, selectedVolume: success.result.result.selectedVolume, resultSha256: report.resultSha256 })}\n`); console.log(JSON.stringify({ status: report.status, apiId: report.apiId, selectedVolume: success.result.result.selectedVolume, resultSha256: report.resultSha256 }));