75 lines
5.5 KiB
JavaScript
75 lines
5.5 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];
|
|
const build = path.resolve(process.argv[3] ?? path.join(root, "freecad-source/build-web/configure/bin"));
|
|
if (!facadePath) throw new Error("compiled Material facade module 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")));
|
|
globalThis.__dirname = `${build}${path.sep}`;
|
|
globalThis.require = createRequire(import.meta.url);
|
|
const loader = await import(pathToFileURL(path.join(build, "FreeCADWebMaterialKernelProbe.js")));
|
|
const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebMaterialKernelProbe.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, command: request.command });
|
|
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.handleMaterialCreateRecompute(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.createMaterialRequest("mat-e-success"));
|
|
const expectedError = await dispatch(facade.createMaterialRequest("mat-e-error", { ...facade.MATERIAL_FIXTURE, uuid: "material-wrong-fixture" }));
|
|
const duplicate = await dispatch(facade.createMaterialRequest("mat-e-success"));
|
|
const delayedKernel = new Promise((resolve) => setTimeout(() => resolve(kernel), 0));
|
|
const cancelRequest = facade.createMaterialRequest("mat-e-cancel");
|
|
const cancelPromise = dispatch(cancelRequest, delayedKernel);
|
|
const cancelAccepted = queue.cancel(envelope.createCancel("mat-e-cancel-message", cancelRequest.requestId, "material-doc"));
|
|
const cancelled = await cancelPromise;
|
|
const afterCancel = await dispatch(facade.createMaterialRequest("mat-e-after-cancel"));
|
|
assert.equal(facade.isMaterialRequest(facade.createMaterialRequest("schema")), true);
|
|
assert.equal(success.kind, "result");
|
|
assert.equal(success.result.objectType, "App::FeaturePython");
|
|
assert.equal(success.result.propertyType, "Materials::PropertyMaterial");
|
|
assert.equal(success.result.uuid, facade.MATERIAL_FIXTURE_UUID);
|
|
assert.equal(success.result.name, facade.MATERIAL_FIXTURE_NAME);
|
|
assert.equal(success.result.resourcesReleased, true);
|
|
assert.equal(expectedError.kind, "error");
|
|
assert.equal(expectedError.error.code, "invalid_fixture");
|
|
assert.equal(expectedError.error.diagnostic?.field, "uuid");
|
|
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-material-facade/v1",
|
|
taskId: "FW-P6-MAT-E",
|
|
module: "Material",
|
|
action: facade.MATERIAL_COMMAND,
|
|
result: "pass",
|
|
envelopeSchema: "freecad-web-facade/v1",
|
|
typedSchema: facade.MATERIAL_ACTION_SCHEMA,
|
|
fixture: { ...facade.MATERIAL_FIXTURE, inputSha256: crypto.createHash("sha256").update(JSON.stringify(facade.MATERIAL_FIXTURE)).digest("hex") },
|
|
responses,
|
|
trace,
|
|
sequencing: { maxActive, requestCount: 5, responseCount: 5, cancelAccepted },
|
|
state: { unchangedAfterCancel: true, successReplayEqual: JSON.stringify(success.result) === JSON.stringify(afterCancel.result) },
|
|
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/i) },
|
|
artifacts: { kernel: "docs/p6/material-kernel.json", recovery: "docs/p6/material-recovery.json", wasm: "freecad-source/build-web/configure/bin/FreeCADWebMaterialKernelProbe.wasm", workerLog: "docs/freecad-p6-material-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/material-facade.ts", "node tools/run_p6_material_facade_smoke.mjs <tmp>/material-facade.js freecad-source/build-web/configure/bin"],
|
|
boundaries: ["Only Material App::FeaturePython create/recompute facade dispatch is covered; GUI and external Material managers are excluded."],
|
|
};
|
|
fs.writeFileSync(path.join(root, "docs/p6/material-facade.json"), `${JSON.stringify(report, null, 2)}\n`);
|
|
fs.writeFileSync(path.join(root, "docs/freecad-p6-material-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 }));
|