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-make-box-method-")); 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/part-python-method-facade.ts"), path.join(root, "web/src/api-method-bridge.ts"), ], { 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 fixture = { length: 2, width: 3, height: 4 }; const create = (id, args = fixture) => facade.createPartMakeBoxRequest(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(create("part-box-success")); const success = await next(); worker.postMessage(create("part-box-repeat")); const repeat = await next(); worker.postMessage(create("part-box-invalid", { length: 2, width: 0, height: 4 })); const invalid = await next(); worker.postMessage(create("part-box-cancel")); worker.postMessage(envelope.createCancel("part-box-cancel-control", "part-box-cancel", "part-method-document")); const cancelled = 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 directRequest = create("part-box-direct").payload; const direct = invoke({ cmd: "part.pythonMethod", args: directRequest }); const directRepeat = invoke({ cmd: "part.pythonMethod", args: directRequest }); const crossModule = invoke({ cmd: "part.pythonMethod", args: { ...directRequest, module: "BIM" } }); const unsupported = invoke({ cmd: "part.unknownMethod", args: {} }); 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-box", await facade.handlePartPythonMethod(payload, new AbortController().signal, kernel)), }; const bridge = new apiBridge.ApiMethodBridge(bridgeRouter, runtimeIndex); const bridgeSuccess = await bridge.call({ apiId: "py:Part:function:makeBox", args: [2, 3, 4] }); const bridgeInvalid = await bridge.call({ apiId: "py:Part:function:makeBox", args: [2, 0, 4] }); const diagnostics = { ready, success, repeat, invalid, cancelled, direct, directRepeat, crossModule, unsupported, bridgeSuccess, bridgeInvalid }; if (ready.kind !== "ready" || success.kind !== "result" || success.result.status !== "exposed" || success.result.native !== true || success.result.result.shapeType !== "Solid" || success.result.result.solid !== true || Math.abs(success.result.result.volume - 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" || direct.ok !== true || direct.result.shapeType !== "Solid" || Math.abs(direct.result.volume - 24) > 1e-9 || JSON.stringify(direct) !== JSON.stringify(directRepeat) || crossModule.ok !== false || crossModule.code !== "unmatched_source_record" || unsupported.ok !== false || unsupported.code !== "unsupported_command" || bridgeSuccess.kind !== "result" || bridgeSuccess.result.apiId !== "py:Part:function:makeBox" || bridgeInvalid.kind !== "error" || bridgeInvalid.error.code !== "invalid_arguments") { console.error(JSON.stringify(diagnostics)); throw new Error("Part.makeBox Python method gate assertion failed"); } 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-make-box-method-runtime/v1", taskId: "FW-P6-FPA-Y", module: "Part", tier: "tier-2-pure", apiId: "py:Part:function:makeBox", status: "pass", sourceEntry: "src/Mod/Part/App/AppPartPy.cpp", 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" }, fixture: create("part-box-success"), fixtureSha256: hash(create("part-box-success")), trace: diagnostics, replay: { workerEqual: true, nativeEqual: true, workerResultSha256: hash(repeat.result), nativeResultSha256: hash(direct) }, resources: { resourcesReleased: success.result.resourcesReleased === true, documentMutation: false }, 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")) }, source: { path: "freecad-source/src/WebAssembly/FreeCADCoreProbe/PartBoxProbe.cpp", officialBinding: "freecad-source/src/Mod/Part/App/AppPartPy.cpp" }, }, boundaries: ["Part.makeBox accepts only positive finite length, width and height in the typed Web method contract.", "All shape values are returned by the compiled FreeCAD Part Python binding through fc_web_dispatch_json; no JavaScript geometry substitute is used."], }; fs.writeFileSync(path.join(root, "docs/p6/part-make-box-method-runtime.json"), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/freecad-p6-part-make-box-method-gate.log"), `${JSON.stringify({ status: report.status, apiId: report.apiId, volume: success.result.result.volume, resultSha256: hash(report) })}\n`); console.log(JSON.stringify({ status: report.status, apiId: report.apiId, volume: success.result.result.volume, resultSha256: hash(report) }));