Files
FreeCADWasmDev/web/tools/test-part-fixed-box.mjs
wangdequan 9a7c4f0335 Save FreeCAD Web workspace sources and user validation evidence
IFC native binding remains incomplete; preserve accurate test results.
2026-09-15 22:01:58 +08:00

52 lines
3.8 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
import vm from "node:vm";
import test from "node:test";
import ts from "typescript";
const modules = new Map();
function load(name) {
if (modules.has(name)) return modules.get(name);
const source = fs.readFileSync(new URL(`../src/${name.replace(/^\.\//, "").replace(/\.js$/, ".ts")}`, import.meta.url), "utf8");
const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText;
const module = { exports: {} };
vm.runInNewContext("(function(exports,require,module){" + code + "})", { AbortController })(module.exports, load, module);
modules.set(name, module.exports); return module.exports;
}
const { handlePartDocument, PART_DOCUMENT_SCHEMA } = load("./part-document-facade.js");
const { executePartFixedBoxDesktop } = load("./part-desktop-adapter.js");
const input = { schema: PART_DOCUMENT_SCHEMA, module: "Part", action: "createFixedBox", document: "Current", boxCommand: "Part_Box3" };
const object = { name: "PartBox", type: "Part::Box", dimensions: [100, 100, 100], position: [50, 50, 50], volume: 1000000 };
const state = { document: "Current", objectCount: 1, objects: [object], createdObject: "PartBox", boxCommand: "Part_Box3" };
const call = (payload, value = state) => handlePartDocument(payload, new AbortController().signal, { ccall: () => JSON.stringify({ ok: true, result: value }) });
test("fixed box facade preserves its native command and validates the created feature", async () => {
const result = await call(input); assert.equal(result.createdObject, "PartBox");
for (const change of [{ document: "Other" }, { objectCount: 0 }, { boxCommand: "Part_Box2" }, { createdObject: "Missing" },
...[{ type: "Part::Feature" }, { dimensions: [1, 2, 3] }, { position: [0, 0, 0] }, { volume: null }].map(value => ({ objects: [{ ...object, ...value }] }))])
await assert.rejects(call(input, { ...state, ...change }));
});
test("invalid presets and overrides fail before touching native state", async () => {
const kernel = { ccall() { assert.fail("invalid request reached native code"); } };
for (const change of [{ boxCommand: "Other" }, { dimensions: [100, 100, 100] }, { position: [50, 50, 50] }, { object: "../Box" }, { object: null }, { object: 12 }, { document: "other/doc" }])
await assert.rejects(handlePartDocument({ ...input, ...change }, new AbortController().signal, kernel));
const controller = new AbortController(); const pending = handlePartDocument(input, controller.signal, Promise.resolve(kernel)); controller.abort();
await assert.rejects(pending, /cancelled/i);
});
test("desktop adapter selects the native created object and does not create a replacement document", async () => {
const requests = [], selections = [];
const router = { request: async (...args) => { requests.push(args); return { kind: "result", result: state }; } };
await executePartFixedBoxDesktop(router, "Part_Box3", {}, "Current", (...args) => selections.push(args));
assert.equal(requests.length, 1); assert.equal(requests[0][1], "part.document");
assert.equal(requests[0][2].action, "createFixedBox"); assert.equal(requests[0][2].boxCommand, "Part_Box3");
assert.equal(selections[0][1][0].object, "PartBox");
for (const args of [{ activeTask: true }, { taskActive: true }, { action: "cancel" }, { dimensions: [1, 2, 3] }, { origin: [0, 0, 0] }])
assert.equal((await executePartFixedBoxDesktop(router, "Part_Box3", args, "Current")).kind, "error");
assert.equal(executePartFixedBoxDesktop(router, "Part_Boolean", {}, "Current"), undefined);
assert.equal(requests.length, 1);
router.request = async () => ({ kind: "error", error: { code: "document_operation_failed", message: "missing document" } });
assert.equal((await executePartFixedBoxDesktop(router, "Part_Box3", {}, "Missing")).kind, "error");
});