#!/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 { createRequire } from "node:module"; 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 out = fs.mkdtempSync(path.join(os.tmpdir(), "freecad-p6-part-geometry-value-")); 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)); execFileSync(path.join(root, "web/node_modules/.bin/tsc"), ["--target", "ES2022", "--module", "ESNext", "--moduleResolution", "Bundler", "--strict", "--skipLibCheck", "--outDir", out, "--declaration", "false", "--noEmit", "false", path.join(root, "web/src/facade-envelope.ts"), path.join(root, "web/src/part-python-family-facade.ts")], { cwd: root, stdio: "inherit" }); const batch = JSON.parse(fs.readFileSync(path.join(root, "docs/current/module-batch-manifest.json"), "utf8")); if (batch.batchId !== "6c8e7a46c2d0c77448a330a5" || batch.selected.module !== "Part" || batch.selected.tier !== "tier-1-readonly" || batch.selected.family !== "geometry-value" || batch.records.length !== 20) throw new Error("active Part geometry-value batch does not match the focused gate"); const records = batch.records.map(([apiId, name, disposition, sourcePath]) => ({ apiId, name, family: "geometry-value", kind: apiId.includes(":method:") ? "method" : "class", sourcePath, disposition })); const loader = await import(pathToFileURL(path.join(build, "FreeCADWebPartBoxProbe.js"))); globalThis.__dirname = `${build}${path.sep}`; globalThis.require = createRequire(import.meta.url); const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebPartBoxProbe.wasm")), locateFile: file => path.join(build, file), print: () => {}, printErr: () => {} }); const facade = await import(pathToFileURL(path.join(out, "part-python-family-facade.js"))); const envelope = await import(pathToFileURL(path.join(out, "facade-envelope.js"))); const request = (id, values = records) => facade.createPartPythonFamilyRequest(id, values, "tier-1-readonly"); const queue = new envelope.FacadeSerialQueue(); const success = await facade.dispatchPartPythonFamily(queue, request("part-geometry-value"), kernel); const repeat = await facade.dispatchPartPythonFamily(queue, request("part-geometry-value-repeat"), kernel); const cross = await facade.dispatchPartPythonFamily(queue, request("part-geometry-value-cross", [{ ...records[0], apiId: "py:BIM:class:TopoShapeCompSolid" }]), kernel); const cancelRequest = request("part-geometry-value-cancel"); const cancelledPromise = facade.dispatchPartPythonFamily(queue, cancelRequest, new Promise(resolve => setTimeout(() => resolve(kernel), 25))); const cancelAccepted = queue.cancel(envelope.createCancel("cancel-control", cancelRequest.requestId)); const cancelled = await cancelledPromise; const invoke = value => JSON.parse(String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify(value)]))); const malformed = invoke({ cmd: facade.PART_PYTHON_FAMILY_COMMAND, args: { tier: "bad", records } }); const unsupported = invoke({ cmd: "part.unknown", args: {} }); const worker = new Worker(path.join(root, "tools/part-python-family-facade-smoke-worker.mjs"), { workerData: { buildDirectory: build, facadePath: pathToFileURL(path.join(out, "part-python-family-facade.js")).href, envelopePath: pathToFileURL(path.join(out, "facade-envelope.js")).href } }); const receive = () => new Promise((resolve, reject) => { worker.once("message", resolve); worker.once("error", reject); }); const ready = await receive(); worker.postMessage(request("part-geometry-value-worker")); const workerSuccess = await receive(); worker.postMessage(request("part-geometry-value-worker")); const duplicate = await receive(); await worker.terminate(); if (success.kind !== "result" || success.result.count !== 20 || success.result.resourcesReleased !== true) throw new Error(`Part geometry-value result failed: ${JSON.stringify(success)}`); const counts = Object.fromEntries(["exposed", "restricted", "nonportable", "not_implemented"].map(status => [status, success.result.records.filter(row => row.status === status).length])); if (counts.exposed !== 10 || counts.restricted !== 9 || counts.not_implemented !== 1 || success.result.records.some(row => row.native !== true || row.resourcesReleased !== true)) throw new Error(`unexpected Part geometry-value dispositions: ${JSON.stringify(counts)}`); if (JSON.stringify(success.result) !== JSON.stringify(repeat.result)) throw new Error("Part geometry-value replay changed"); if (cross.kind !== "result" || cross.result.records[0]?.code !== "unmatched_source_record" || malformed.ok !== false || unsupported.code !== "unsupported_command") throw new Error("Part geometry-value dispatcher boundaries failed"); if (!cancelAccepted || cancelled.kind !== "error" || cancelled.error.code !== "cancelled") throw new Error("Part geometry-value cancellation failed"); if (ready.kind !== "ready" || workerSuccess.kind !== "result" || duplicate.kind !== "error" || duplicate.error.code !== "duplicate_request") throw new Error("Part geometry-value Worker sequencing failed"); const manifestPath = path.join(root, "docs/p6/python-api-manifest.json"); const apiManifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); for (const row of apiManifest.records) { const native = success.result.records.find(record => record.apiId === row.id); if (!native) continue; row.disposition = native.status; row.dispositionEvidence = ["docs/p6/part-geometry-value-batch-runtime.json"]; row.dispositionReason = native.message; } fs.writeFileSync(manifestPath, `${JSON.stringify(apiManifest, null, 2)}\n`); const modules = JSON.parse(fs.readFileSync(path.join(root, "web/public/modules/manifest.json"), "utf8")); const artifact = (modules.modules?.Part?.artifacts ?? modules.artifacts ?? []).find(item => item.runtimeUrl?.endsWith("FreeCADWebPartBoxProbe.js")) ?? {}; const trace = { success, repeat, cross, cancelled, malformed, unsupported, worker: { ready, workerSuccess, duplicate } }; const report = { schema: "freecad-web-p6-part-geometry-value-batch-runtime/v1", taskId: "FW-P6-FPA-Y", batchId: batch.batchId, module: "Part", tier: "tier-1-readonly", family: "geometry-value", status: "pass_with_boundaries", buildProfile: "fast-dev", dispositionCounts: counts, records: success.result.records.map(row => ({ apiId: row.apiId, name: row.name, status: row.status, code: row.code, message: row.message, result: row.result, sourceEntry: row.sourceEntry, sourceRevision: row.sourceRevision, evidenceSha256: hash(row) })), route: { command: facade.PART_PYTHON_FAMILY_COMMAND, worker: "web/src/part.worker.ts", facade: "web/src/part-python-family-facade.ts", router: "web/src/module-router.ts", native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/PartBoxProbe.cpp:fc_web_dispatch_json" }, artifact: { module: "Part", worker: artifact.worker ?? "/src/part.worker.ts", revision: artifact.revision ?? artifact.hash ?? fileHash(path.join(build, "FreeCADWebPartBoxProbe.wasm")), runtimeUrl: artifact.runtimeUrl ?? "/kernel/FreeCADWebPartBoxProbe.js", wasmUrl: artifact.wasmUrl ?? "/kernel/FreeCADWebPartBoxProbe.wasm", dataUrl: artifact.dataUrl ?? null, assetBaseUrl: artifact.assetBaseUrl ?? "/kernel/", runtimeSha256: fileHash(path.join(build, "FreeCADWebPartBoxProbe.js")), wasmSha256: fileHash(path.join(build, "FreeCADWebPartBoxProbe.wasm")) }, replay: { equal: true, resultSha256: hash(repeat.result) }, traceSha256: hash(trace), resources: { resourcesReleased: true, documentMutation: false }, boundaries: ["Five Python source helpers/enums and four parttests classes remain explicit runtime boundaries.", "Ten records were constructed or invoked by compiled FreeCAD Part/OCCT code through fc_web_dispatch_json; one constructor retains its native_fixture_error.", "The shared Part Worker is loaded on demand; the current increment was not preloaded."] }; fs.writeFileSync(path.join(root, "docs/p6/part-geometry-value-batch-runtime.json"), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/freecad-p6-part-geometry-value-batch-gate.log"), `${JSON.stringify({ status: report.status, batchId: report.batchId, dispositionCounts: counts, traceSha256: report.traceSha256 })}\n`); console.log(JSON.stringify({ status: report.status, records: report.records.length, dispositionCounts: counts, traceSha256: report.traceSha256 })); process.exit(0);