import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; 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 hash = (value) => crypto.createHash("sha256").update(Buffer.isBuffer(value) ? value : typeof value === "string" ? value : JSON.stringify(value)).digest("hex"); globalThis.__dirname = `${build}${path.sep}`; globalThis.require = createRequire(import.meta.url); const loader = await import(pathToFileURL(path.join(build, "FreeCADWebImportRuntimeProbe.js")).href); const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebImportRuntimeProbe.wasm")) }); const manifest = JSON.parse(fs.readFileSync(path.join(root, "docs/p6/python-api-manifest.json"), "utf8")); const records = manifest.records.filter((row) => row.module === "Import" && row.tier === "tier-1-readonly").map((row) => ({ apiId: row.id, name: row.name, family: row.family, sourcePath: row.evidence?.path, disposition: row.disposition })); if (records.length !== 297) throw new Error(`expected 297 Import records, got ${records.length}`); if (new Set(records.map((row) => row.apiId)).size !== records.length) throw new Error("Import manifest contains duplicate API IDs"); const request = { cmd: "import.pythonFamilyBatch", args: { tier: "tier-1-readonly", records } }; const invoke = (value) => JSON.parse(String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify(value)]))); const success = invoke(request); const repeated = invoke(request); if (success.ok !== true || success.schema !== "freecad-web-import/python-family-batch/v1" || success.module !== "Import" || success.source !== "freecad-source" || success.count !== records.length || success.resourcesReleased !== true || success.records.length !== records.length) throw new Error("Import family dispatch did not return a complete result"); if (new Set(success.records.map((record) => record.apiId)).size !== records.length) throw new Error("Import native response contains duplicate API IDs"); const counts = Object.fromEntries(["exposed", "restricted", "nonportable", "not_implemented"].map((status) => [status, success.records.filter((record) => record.status === status).length])); if (counts.restricted !== 0 || counts.exposed !== 0 || counts.nonportable !== 0 || counts.not_implemented !== 297) throw new Error(`unexpected Import disposition counts: ${JSON.stringify(counts)}`); if (success.records.some((record) => record.native !== true || !record.sourceEntry || !record.sourceRevision || !record.code || !record.message)) throw new Error("Import native response is missing source/error metadata"); if (JSON.stringify(repeated) !== JSON.stringify(success)) throw new Error("Import family replay changed the native result"); const mismatch = invoke({ cmd: "import.pythonFamilyBatch", args: { tier: "tier-1-readonly", records: [{ ...records[0], apiId: "py:BIM:class:ARRAY" }] } }); if (mismatch.ok !== true || mismatch.records?.[0]?.status !== "not_implemented" || mismatch.records?.[0]?.code !== "unmatched_source_record") throw new Error("Import module-scoped mismatch was not rejected"); const malformed = invoke({ cmd: "import.pythonFamilyBatch", args: { tier: "tier-2-pure", records: [records[0]] } }); if (malformed.ok !== false || malformed.code !== "invalid_request") throw new Error("Import invalid tier was not rejected"); const unsupported = invoke({ cmd: "import.unknown", args: {} }); if (unsupported.ok !== false || unsupported.code !== "unsupported_command") throw new Error("Import unsupported command was not rejected"); const report = { schema: "freecad-web-p6-import-tier1-readonly-family-runtime/v1", taskId: "FW-P6-FPA-F1-IMPORT", module: "Import", tier: "tier-1-readonly", status: "pass_with_pending", sourceRevision: "4f4e9244e613ea2df06e70027074a562695048cb", route: { worker: "web/src/import.worker.ts", command: "import.pythonFamilyBatch", facade: "web/src/import-python-family-facade.ts", native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/ImportRuntimeProbe.cpp:454" }, input: { count: records.length, requestSha256: hash(request) }, result: { count: success.count, resourcesReleased: success.resourcesReleased, dispositionCounts: counts, resultSha256: hash(success) }, replay: { equal: true, resultSha256: hash(repeated) }, mismatchRequest: { result: mismatch, resultSha256: hash(mismatch) }, malformedRequest: { result: malformed, resultSha256: hash(malformed) }, unsupportedRequest: { result: unsupported, resultSha256: hash(unsupported) }, records: success.records.map((record) => ({ apiId: record.apiId, name: record.name, family: record.family, status: record.status, code: record.code, message: record.message, sourceEntry: record.sourceEntry, sourceRevision: record.sourceRevision, evidenceSha256: hash(record) })), artifacts: { js: { path: path.relative(root, path.join(build, "FreeCADWebImportRuntimeProbe.js")), sha256: hash(fs.readFileSync(path.join(build, "FreeCADWebImportRuntimeProbe.js"))) }, wasm: { path: path.relative(root, path.join(build, "FreeCADWebImportRuntimeProbe.wasm")), sha256: hash(fs.readFileSync(path.join(build, "FreeCADWebImportRuntimeProbe.wasm"))) }, }, boundaries: ["Import records have no method-level readonly Python fixtures in this batch and remain not_implemented", "all statuses, source metadata, errors and resource release are returned by compiled FreeCAD through fc_web_dispatch_json", "module-scoped API IDs are required; cross-module names never match", "the existing import.step/import.pythonApi action contracts remain unchanged"], }; fs.writeFileSync(path.join(root, "docs/p6/import-tier1-readonly-family-runtime.json"), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/freecad-p6-import-f1-readonly.log"), `${JSON.stringify({ status: report.status, count: records.length, dispositionCounts: counts, resultSha256: report.result.resultSha256, wasmSha256: report.artifacts.wasm.sha256 })}\n`); console.log(JSON.stringify({ status: report.status, count: records.length, dispositionCounts: counts, resultSha256: report.result.resultSha256, wasmSha256: report.artifacts.wasm.sha256 }));