8 lines
4.8 KiB
JavaScript
8 lines
4.8 KiB
JavaScript
#!/usr/bin/env node
|
|
import assert from "node:assert/strict"; 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 = path.resolve(path.dirname(new URL(import.meta.url).pathname), ".."); const facadePath = process.argv[2]; if (!facadePath) throw new Error("compiled Sandbox inspect facade path is required"); const facade = await import(pathToFileURL(path.resolve(facadePath))); const envelope = await import(pathToFileURL(path.join(path.dirname(path.resolve(facadePath)), "facade-envelope.js"))); const build = path.resolve(process.argv[3] ?? path.join(root, "freecad-source/build-web/configure/bin")); globalThis.__dirname = `${build}${path.sep}`; globalThis.require = createRequire(import.meta.url); const loader = await import(pathToFileURL(path.join(build, "FreeCADWebSandboxRuntimeProbe.js"))); const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebSandboxRuntimeProbe.wasm")) }); const queue = new envelope.FacadeSerialQueue(); const trace = []; let active = 0; let maxActive = 0;
|
|
async function dispatch(request, source = kernel) { trace.push({ direction: "request", requestId: request.requestId }); const response = await queue.dispatch(request, async (payload, signal) => { active += 1; maxActive = Math.max(maxActive, active); trace.push({ direction: "handler-start", requestId: request.requestId }); try { return await facade.handleSandboxInspect(payload, signal, source); } finally { active -= 1; trace.push({ direction: "handler-end", requestId: request.requestId }); } }); trace.push({ direction: "response", requestId: response.requestId, kind: response.kind, code: response.kind === "error" ? response.error.code : null }); return response; }
|
|
const success = await dispatch(facade.createSandboxInspectRequest("sdb-act02-e-success")); const expectedError = await dispatch(facade.createSandboxInspectRequest("sdb-act02-e-error", { ...facade.SANDBOX_INSPECT_FIXTURE, sourceEntry: "src/Mod/Sandbox/Missing.cpp" })); const duplicate = await dispatch(facade.createSandboxInspectRequest("sdb-act02-e-success")); const delayed = new Promise((resolve) => setTimeout(() => resolve(kernel), 0)); const cancelRequest = facade.createSandboxInspectRequest("sdb-act02-e-cancel"); const cancelPromise = dispatch(cancelRequest, delayed); const cancelAccepted = queue.cancel(envelope.createCancel("sdb-act02-e-cancel-message", cancelRequest.requestId, "sandbox-source-inventory")); const cancelled = await cancelPromise; const afterCancel = await dispatch(facade.createSandboxInspectRequest("sdb-act02-e-after-cancel")); assert.equal(success.kind, "result"); assert.equal(success.result.registeredTypeCount, 1); assert.equal(expectedError.error.code, "revision_mismatch"); assert.equal(duplicate.error.code, "duplicate_request"); assert.equal(cancelAccepted, true); assert.equal(cancelled.error.code, "cancelled"); assert.deepEqual(afterCancel.result, success.result); assert.equal(maxActive, 1);
|
|
const responses = { success, expectedError, duplicate, cancelled, afterCancel }; const report = { schema: "freecad-web-p6-sandbox-action-02-facade/v1", taskId: "FW-P6-SDB-ACT-02-E", module: "Sandbox", action: facade.SANDBOX_INSPECT_COMMAND, result: "pass", envelopeSchema: "freecad-web-facade/v1", typedSchema: facade.SANDBOX_INSPECT_SCHEMA, fixture: { ...facade.SANDBOX_INSPECT_FIXTURE, inputSha256: crypto.createHash("sha256").update(JSON.stringify(facade.SANDBOX_INSPECT_FIXTURE)).digest("hex") }, responses, trace, sequencing: { maxActive, requestCount: 5, responseCount: 5, cancelAccepted }, state: { unchangedAfterCancel: true }, schemaChecks: { request: true, result: success.kind === "result", expectedError: expectedError.kind === "error", duplicate: duplicate.kind === "error", cancel: cancelled.kind === "error", noNativePointers: !JSON.stringify(responses).match(/pointer|native|handle/i) }, artifacts: { kernel: "docs/p6/sandbox-action-02-kernel.json", recovery: "docs/p6/sandbox-action-02-recovery.json", workerLog: "docs/freecad-p6-sandbox-action-02-e-facade.log" }, commands: ["npx tsc --target ES2022 --module ESNext --moduleResolution Bundler --strict --skipLibCheck --outDir <tmp> --declaration false --noEmit false src/facade-envelope.ts src/sandbox-inspect-facade.ts", "node tools/run_p6_sandbox_action02_facade_smoke.mjs <tmp>/sandbox-inspect-facade.js freecad-source/build-web/configure/bin"] };
|
|
fs.writeFileSync(path.join(root, "docs/p6/sandbox-action-02-facade.json"), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/freecad-p6-sandbox-action-02-e-facade.log"), `${trace.map((event, index) => `${String(index + 1).padStart(2, "0")} ${JSON.stringify(event)}`).join("\n")}\nresult=pass\nmaxActive=${maxActive}\n`); console.log(JSON.stringify({ result: report.result, maxActive, requests: 5, cancelAccepted }));
|