77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, resolve } from "node:path";
|
|
import assert from "node:assert/strict";
|
|
|
|
import createLinuxCncInterpModule from "../../../build/wasm/core/linuxcnc_interp.js";
|
|
|
|
function allocCString(mod, value) {
|
|
const bytes = mod.lengthBytesUTF8(value) + 1;
|
|
const ptr = mod._malloc(bytes);
|
|
mod.stringToUTF8(value, ptr, bytes);
|
|
return ptr;
|
|
}
|
|
|
|
function runProgram(mod, programText) {
|
|
const programPtr = allocCString(mod, programText);
|
|
let resultPtr = 0;
|
|
try {
|
|
resultPtr = mod._lcinterp_run_program(programPtr);
|
|
assert.notEqual(resultPtr, 0);
|
|
return mod.UTF8ToString(resultPtr);
|
|
} finally {
|
|
if (resultPtr) {
|
|
mod._lcinterp_free_string(resultPtr);
|
|
}
|
|
mod._free(programPtr);
|
|
}
|
|
}
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
const rootDir = resolve(__dirname, "../../..");
|
|
const wasmPath = resolve(rootDir, "build/wasm/core/linuxcnc_interp.wasm");
|
|
|
|
const interp = await createLinuxCncInterpModule({
|
|
wasmBinary: readFileSync(wasmPath),
|
|
print() {},
|
|
printErr(message) {
|
|
console.error(message);
|
|
},
|
|
});
|
|
|
|
const fixtureNames = [
|
|
"minimal_linear",
|
|
"arc_semantics",
|
|
"length_units",
|
|
"modal_incremental",
|
|
"plane_selection",
|
|
"coordinate_offsets",
|
|
];
|
|
|
|
for (const fixtureName of fixtureNames) {
|
|
const programText = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/gcode/${fixtureName}.ngc`),
|
|
"utf8",
|
|
);
|
|
const expectedEvents = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/canon/${fixtureName}.events`),
|
|
"utf8",
|
|
).trimEnd();
|
|
|
|
const output = runProgram(interp, programText);
|
|
const actualEventSet = new Set(
|
|
output.split("\n").filter((line) => line.startsWith("canon_event=")),
|
|
);
|
|
const expectedEventLines = expectedEvents.split("\n").filter(Boolean);
|
|
|
|
for (const expectedEvent of expectedEventLines) {
|
|
assert.equal(
|
|
actualEventSet.has(expectedEvent),
|
|
true,
|
|
`${fixtureName}: ${expectedEvent}`,
|
|
);
|
|
}
|
|
}
|
|
console.log("interp_wasm_node_smoke=ok");
|