39 lines
2.6 KiB
JavaScript
39 lines
2.6 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 code = ts.transpileModule(fs.readFileSync(new URL(`../src/${name.replace(/^\.\//, "").replace(/\.js$/, ".ts")}`, import.meta.url), "utf8"), { 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 facade = load("./start-facade.js");
|
|
const payload = facade.createStartImportSvgRequest("test", "/tmp/input.svg", '<svg xmlns="http://www.w3.org/2000/svg"/>').payload;
|
|
const native = { command: "Start_ImportSVG", status: "imported", document: "Current", object: "Rectangle", label: "Rectangle", recomputed: true, objectCount: 4, objects: [{ object: "Rectangle", valid: true, isNull: false, length: 16 }] };
|
|
test("valid geometry metadata is transported unchanged", async () => {
|
|
let request;
|
|
const result = await facade.handleStartImportSvg(payload, new AbortController().signal, { ccall(_name, _type, _types, args) { request = JSON.parse(args[0]); return JSON.stringify(native); } });
|
|
assert.equal(request.args.content, payload.content);
|
|
assert.equal(result.objects[0].length, 16);
|
|
assert.equal(result.objectCount, 4);
|
|
});
|
|
test("empty, nonfinite or mismatched native geometry cannot pass", async () => {
|
|
for (const change of [{ objects: [] }, { objectCount: 0 }, { object: "Missing" }, { objects: [{ ...native.objects[0], length: null }] },
|
|
{ objects: [{ ...native.objects[0], valid: false }] }, { objects: [native.objects[0], native.objects[0]] }]) {
|
|
await assert.rejects(facade.handleStartImportSvg(payload, new AbortController().signal, { ccall: () => JSON.stringify({ ...native, ...change }) }));
|
|
}
|
|
});
|
|
test("invalid input and cancellation during runtime loading never reach native code", async () => {
|
|
const kernel = { ccall() { assert.fail("invalid or cancelled request reached native"); } };
|
|
for (const change of [{ content: false }, { content: 0 }, { path: "/tmp/input.txt" }])
|
|
await assert.rejects(facade.handleStartImportSvg({ ...payload, ...change }, new AbortController().signal, kernel));
|
|
const controller = new AbortController();
|
|
const pending = facade.handleStartImportSvg(payload, controller.signal, Promise.resolve(kernel));
|
|
controller.abort(); await assert.rejects(pending, error => error.code === "cancelled");
|
|
});
|