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 compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText; const module = { exports: {} }; vm.runInNewContext("(function(exports,require,module){" + compiled + "})", { AbortController })(module.exports, load, module); modules.set(name, module.exports); return module.exports; } const points = load("./points-facade.js"); const payload = { ...points.POINTS_FIXTURE, document: "CloudA", object: "Current", points: [[-2, 1, 3], [4, 6, -1]] }; const result = { ok: true, schema: points.POINTS_SCHEMA, module: "Points", action: "create/recompute", document: "CloudA", objectName: "Current", pointCount: 2, objectCount: 1, min: [-2, 1, -1], max: [4, 6, 3], resourcesReleased: true }; test("variable point counts and named document parameters reach the native dispatcher", async () => { let received; const kernel = { ccall(_name, _type, _types, args) { received = JSON.parse(args[0]); return JSON.stringify(result); } }; const actual = await points.handlePoints(payload, new AbortController().signal, kernel); assert.deepEqual(received.args, payload); assert.equal(actual.pointCount, 2); assert.equal(actual.document, "CloudA"); }); test("invalid coordinates or names never call native code", async () => { const kernel = { ccall() { assert.fail("invalid payload reached native code"); } }; for (const invalid of [{ points: [] }, { points: [[1, 2]] }, { points: [[1, Infinity, 3]] }, { points: [[1, "2", 3]] }, { document: "bad/name" }, { object: "" }]) { await assert.rejects(points.handlePoints({ ...payload, ...invalid }, new AbortController().signal, kernel), /nonempty finite point set/); } }); test("native ownership, count and invalid bounds cannot be reported as success", async () => { for (const changed of [{ document: "Other" }, { objectName: "Other" }, { pointCount: 3 }, { min: [7, 8, 9] }, { objectCount: 0 }]) { await assert.rejects(points.handlePoints(payload, new AbortController().signal, { ccall: () => JSON.stringify({ ...result, ...changed }) }), /do not match/); } }); test("cancellation remains distinct from a native protocol error", async () => { const controller = new AbortController(); controller.abort(); await assert.rejects(points.handlePoints(payload, controller.signal, { ccall() { assert.fail("cancelled request ran"); } }), /cancelled/); });