80 lines
11 KiB
JavaScript
80 lines
11 KiB
JavaScript
#!/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 { Worker } from "node:worker_threads";
|
|
import { createRequire } from "node:module";
|
|
import { execFileSync } from "node:child_process";
|
|
import { pathToFileURL, fileURLToPath } from "node:url";
|
|
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
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-techdraw-have-page-"));
|
|
const hash = (value) => crypto.createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
const fileHash = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
execFileSync(path.join(root, "web/node_modules/.bin/tsc"), ["--target", "ES2022", "--module", "ESNext", "--moduleResolution", "Bundler", "--strict", "--skipLibCheck", "--outDir", out, "--declaration", "false", "--noEmit", "false", "web/src/facade-envelope.ts", "web/src/techdraw-python-method-facade.ts", "web/src/api-method-bridge.ts", "web/src/python-api-runtime.ts", "web/src/python-family-dispatcher.ts", "web/src/module-router.ts", "web/src/module-registry.generated.ts", "web/src/resource-lifecycle.ts", "web/src/native-source-policy.ts"], { cwd: root, stdio: "inherit" });
|
|
globalThis.__dirname = `${build}${path.sep}`;
|
|
globalThis.require = createRequire(import.meta.url);
|
|
const workerJs = path.join(build, "FreeCADWebTechDrawRuntimeProbe.worker.js");
|
|
const workerCjs = path.join(build, "FreeCADWebTechDrawRuntimeProbe.worker.cjs");
|
|
if (!fs.existsSync(workerCjs) && fs.existsSync(workerJs)) fs.copyFileSync(workerJs, workerCjs);
|
|
const loader = await import(pathToFileURL(path.join(build, "FreeCADWebTechDrawRuntimeProbe.js")));
|
|
const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebTechDrawRuntimeProbe.wasm")), locateFile: (file) => file.endsWith(".worker.js") && fs.existsSync(workerCjs) ? workerCjs : path.join(build, file), print: () => {}, printErr: () => {} });
|
|
const facade = await import(pathToFileURL(path.join(out, "techdraw-python-method-facade.js")));
|
|
const envelope = await import(pathToFileURL(path.join(out, "facade-envelope.js")));
|
|
const invoke = (request) => JSON.parse(String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify(request)])));
|
|
const request = facade.createTechDrawHavePageRequest("techdraw-have-page-success");
|
|
const direct = invoke({ cmd: facade.TECHDRAW_PYTHON_METHOD_COMMAND, args: request.payload });
|
|
const directRepeat = invoke({ cmd: facade.TECHDRAW_PYTHON_METHOD_COMMAND, args: request.payload });
|
|
const crossModule = invoke({ cmd: facade.TECHDRAW_PYTHON_METHOD_COMMAND, args: { ...request.payload, module: "Part", apiId: "py:Part:function:havePage" } });
|
|
const unsupported = invoke({ cmd: "techdraw.unknown", args: {} });
|
|
const queue = new envelope.FacadeSerialQueue();
|
|
const success = await facade.dispatchTechDrawHavePage(queue, request, kernel);
|
|
const repeat = await facade.dispatchTechDrawHavePage(queue, facade.createTechDrawHavePageRequest("techdraw-have-page-repeat"), kernel);
|
|
const malformed = await facade.dispatchTechDrawHavePage(queue, { ...request, requestId: "techdraw-have-page-malformed", payload: { ...request.payload, family: "geometry-value" } });
|
|
const duplicateRequest = facade.createTechDrawHavePageRequest("techdraw-have-page-duplicate");
|
|
const delayed = facade.dispatchTechDrawHavePage(queue, duplicateRequest, new Promise((resolve) => setTimeout(() => resolve(kernel), 25)));
|
|
const duplicate = await facade.dispatchTechDrawHavePage(queue, duplicateRequest, kernel);
|
|
const duplicateResult = await delayed;
|
|
const cancelRequest = facade.createTechDrawHavePageRequest("techdraw-have-page-cancel");
|
|
const cancelledPromise = facade.dispatchTechDrawHavePage(queue, cancelRequest, new Promise((resolve) => setTimeout(() => resolve(kernel), 25)));
|
|
const cancelAccepted = queue.cancel(envelope.createCancel("techdraw-have-page-cancel-control", cancelRequest.requestId));
|
|
const cancelled = await cancelledPromise;
|
|
if (direct.ok !== true || direct.result.withoutPage !== false || direct.result.withPage !== true || directRepeat.ok !== true || JSON.stringify(direct) !== JSON.stringify(directRepeat)) throw new Error(`direct havePage dispatch failed: ${JSON.stringify({ direct, directRepeat })}`);
|
|
if (crossModule.ok !== false || crossModule.code !== "invalid_request" || unsupported.ok !== false || unsupported.code !== "unsupported_command") throw new Error(`havePage boundaries failed: ${JSON.stringify({ crossModule, unsupported })}`);
|
|
if (success.kind !== "result" || success.result.result.withPage !== true || repeat.kind !== "result" || JSON.stringify(success.result) !== JSON.stringify(repeat.result)) throw new Error(`havePage facade replay failed: ${JSON.stringify({ success, repeat })}`);
|
|
if (malformed.kind !== "error" || malformed.error.code !== "invalid_request" || duplicate.kind !== "error" || duplicate.error.code !== "duplicate_request" || duplicateResult.kind !== "result" || !cancelAccepted || cancelled.kind !== "error" || cancelled.error.code !== "cancelled") throw new Error(`havePage facade sequencing failed: ${JSON.stringify({ malformed, duplicate, duplicateResult, cancelled })}`);
|
|
|
|
const worker = new Worker(path.join(root, "tools/techdraw-python-method-smoke-worker.mjs"), { workerData: { buildDirectory: build, facadePath: pathToFileURL(path.join(out, "techdraw-python-method-facade.js")).href, envelopePath: pathToFileURL(path.join(out, "facade-envelope.js")).href } });
|
|
const receive = () => new Promise((resolve, reject) => { const onMessage = (value) => { worker.off("error", reject); resolve(value); }; worker.once("message", onMessage); worker.once("error", reject); });
|
|
const ready = await receive();
|
|
worker.postMessage(request); const workerSuccess = await receive();
|
|
worker.postMessage(request); const workerDuplicate = await receive();
|
|
const workerCancelRequest = facade.createTechDrawHavePageRequest("techdraw-have-page-worker-cancel");
|
|
const workerCancelledPromise = new Promise((resolve, reject) => { const onMessage = (value) => { if (value.requestId === workerCancelRequest.requestId) { worker.off("error", reject); resolve(value); } }; worker.on("message", onMessage); worker.once("error", reject); });
|
|
worker.postMessage(workerCancelRequest); worker.postMessage(envelope.createCancel("techdraw-have-page-worker-cancel-control", workerCancelRequest.requestId));
|
|
const workerCancelled = await workerCancelledPromise;
|
|
worker.postMessage({ kind: "reset", requestId: "techdraw-have-page-worker-reset" }); const reset = await receive();
|
|
await worker.terminate();
|
|
if (ready.kind !== "ready" || workerSuccess.kind !== "result" || workerSuccess.result.result.withPage !== true || workerDuplicate.kind !== "error" || workerDuplicate.error.code !== "duplicate_request" || workerCancelled.kind !== "error" || workerCancelled.error.code !== "cancelled" || reset.requestId !== "techdraw-have-page-worker-reset") throw new Error(`havePage Worker sequencing failed: ${JSON.stringify({ ready, workerSuccess, workerDuplicate, workerCancelled, reset })}`);
|
|
|
|
const runtimeIndex = { schema: "freecad-web-python-api-runtime-index/v1", source: { manifest: "docs/p6/python-api-manifest.json", manifestSha256: "fixture" }, fields: { modules: ["TechDraw"], kinds: ["function"], families: ["pure-function"], tiers: ["tier-2-pure"], dispositions: ["exposed"] }, records: [[facade.TECHDRAW_HAVE_PAGE_API_ID, 0, 0, "havePage", 0, 0, 0]] };
|
|
const { ApiMethodBridge } = await import(pathToFileURL(path.join(out, "api-method-bridge.js")));
|
|
const apiRouter = { request: async (_module, _command, payload) => ({ kind: "result", requestId: "api-native", result: invoke({ cmd: facade.TECHDRAW_PYTHON_METHOD_COMMAND, args: payload }) }) };
|
|
const apiResult = await new ApiMethodBridge(apiRouter, runtimeIndex).call({ apiId: facade.TECHDRAW_HAVE_PAGE_API_ID, args: [] });
|
|
if (apiResult.kind !== "result" || apiResult.result?.result?.withPage !== true) throw new Error(`API method bridge failed: ${JSON.stringify(apiResult)}`);
|
|
|
|
const report = { schema: "freecad-web-p6-techdraw-have-page-method-runtime/v1", taskId: "FW-P6-FPA-Y", module: "TechDraw", tier: "tier-2-pure", apiId: facade.TECHDRAW_HAVE_PAGE_API_ID, status: "pass", route: { command: facade.TECHDRAW_PYTHON_METHOD_COMMAND, worker: "web/src/techdraw-python.worker.ts", facade: "web/src/techdraw-python-method-facade.ts", apiBridge: "web/src/api-method-bridge.ts", native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/TechDrawRuntimeProbe.cpp:fc_web_dispatch_json" }, request, requestSha256: hash(request), direct, directRepeat, success, repeat, malformed, duplicate, duplicateResult, cancelled, crossModule, unsupported, apiResult, worker: { ready, success: workerSuccess, duplicate: workerDuplicate, cancelled: workerCancelled, reset, sequencing: { duplicate: true, cancelAccepted: true, resetAccepted: true, maxActive: 1 } }, replay: { equal: true, resultSha256: hash(success.result) }, resources: { resourcesReleased: true, documentMutation: false, temporaryDocumentClosed: true }, artifacts: { wasm: { path: "web/public/kernel/FreeCADWebTechDrawRuntimeProbe.wasm", sha256: fileHash(path.join(root, "web/public/kernel/FreeCADWebTechDrawRuntimeProbe.wasm")) }, loader: { path: "web/public/kernel/FreeCADWebTechDrawRuntimeProbe.js", sha256: fileHash(path.join(root, "web/public/kernel/FreeCADWebTechDrawRuntimeProbe.js")) } }, boundaries: ["The official havePage function is executed unchanged from TDToolsUtil.py against native App::Document objects; GUI selection and message-box utilities remain outside this fixture.", "All values and statuses derive from FreeCAD native execution through fc_web_dispatch_json."] };
|
|
const manifestPath = path.join(root, "docs/p6/python-api-manifest.json");
|
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
const row = manifest.records.find((entry) => entry.id === facade.TECHDRAW_HAVE_PAGE_API_ID);
|
|
if (!row) throw new Error("TechDraw havePage is missing from python-api-manifest.json");
|
|
row.disposition = "exposed"; row.dispositionEvidence = ["docs/p6/techdraw-have-page-method-runtime.json"]; row.dispositionReason = "official TechDraw TDToolsUtil.havePage source replay through fc_web_dispatch_json";
|
|
fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
report.resultSha256 = hash({ direct, success, worker: report.worker, apiResult });
|
|
fs.writeFileSync(path.join(root, "docs/p6/techdraw-have-page-method-runtime.json"), `${JSON.stringify(report, null, 2)}\n`);
|
|
fs.writeFileSync(path.join(root, "docs/freecad-p6-techdraw-have-page-method.log"), `${JSON.stringify({ status: report.status, apiId: report.apiId, withoutPage: direct.result.withoutPage, withPage: direct.result.withPage, resultSha256: report.resultSha256 })}\n`);
|
|
console.log(JSON.stringify({ status: report.status, apiId: report.apiId, withoutPage: direct.result.withoutPage, withPage: direct.result.withPage, workerCancel: workerCancelled.error.code, resultSha256: report.resultSha256 }));
|
|
process.exit(0);
|