138 lines
4.0 KiB
JavaScript
138 lines
4.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 { createLinuxCncInterpSdk } from "../../../runtime/sdk/src/index.js";
|
|
import {
|
|
INTERP_ERROR_FIXTURES,
|
|
INTERP_FILE_FIXTURES,
|
|
INTERP_INI_FIXTURE,
|
|
INTERP_MDI_FIXTURES,
|
|
INTERP_POSITION_PARAMS_FILE_FIXTURE,
|
|
} from "../../fixtures/interp-fixture-matrix.mjs";
|
|
|
|
function verifyExpectedOutput(fixtureName, output, expectedText) {
|
|
const expectedLines = expectedText.split("\n").filter(Boolean);
|
|
|
|
for (const expectedLine of expectedLines) {
|
|
if (expectedLine.startsWith("absent=")) {
|
|
const forbidden = expectedLine.slice("absent=".length);
|
|
assert.equal(
|
|
output.includes(forbidden),
|
|
false,
|
|
`${fixtureName}: unexpected ${forbidden}`,
|
|
);
|
|
continue;
|
|
}
|
|
|
|
assert.equal(
|
|
output.includes(expectedLine),
|
|
true,
|
|
`${fixtureName}: ${expectedLine}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
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 createLinuxCncInterpSdk({
|
|
wasmBinary: readFileSync(wasmPath),
|
|
print() {},
|
|
printErr(message) {
|
|
console.error(message);
|
|
},
|
|
});
|
|
|
|
for (const fixtureName of INTERP_MDI_FIXTURES) {
|
|
const programText = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/gcode/${fixtureName}.ngc`),
|
|
"utf8",
|
|
);
|
|
const expectedEvents = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/canon/${fixtureName}.events`),
|
|
"utf8",
|
|
).trimEnd();
|
|
|
|
verifyExpectedOutput(fixtureName, interp.runProgram(programText), expectedEvents);
|
|
}
|
|
|
|
const namedParamIniPath = "/work/namedparams.ini";
|
|
interp.writeTextFile(
|
|
namedParamIniPath,
|
|
readFileSync(resolve(rootDir, "tests/fixtures/ini/namedparams.ini"), "utf8"),
|
|
);
|
|
const namedParamProgramText = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/gcode/${INTERP_INI_FIXTURE}.ngc`),
|
|
"utf8",
|
|
);
|
|
const namedParamExpected = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/canon/${INTERP_INI_FIXTURE}.events`),
|
|
"utf8",
|
|
).trimEnd();
|
|
verifyExpectedOutput(
|
|
INTERP_INI_FIXTURE,
|
|
interp.runProgramWithIni(namedParamProgramText, namedParamIniPath),
|
|
namedParamExpected,
|
|
);
|
|
|
|
for (const fixtureName of INTERP_ERROR_FIXTURES) {
|
|
const programText = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/gcode_errors/${fixtureName}.ngc`),
|
|
"utf8",
|
|
);
|
|
const expectedOutput = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/canon_errors/${fixtureName}.expected`),
|
|
"utf8",
|
|
).trimEnd();
|
|
|
|
verifyExpectedOutput(fixtureName, interp.runProgram(programText), expectedOutput);
|
|
}
|
|
|
|
for (const fixtureName of INTERP_FILE_FIXTURES) {
|
|
const programPath = `/work/${fixtureName}.ngc`;
|
|
const programText = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/gcode/${fixtureName}.ngc`),
|
|
"utf8",
|
|
);
|
|
const expectedEvents = readFileSync(
|
|
resolve(rootDir, `tests/fixtures/canon/${fixtureName}.events`),
|
|
"utf8",
|
|
).trimEnd();
|
|
|
|
interp.writeTextFile(programPath, programText);
|
|
verifyExpectedOutput(fixtureName, interp.runFile(programPath), expectedEvents);
|
|
}
|
|
|
|
const namedParamFilePath = `/work/${INTERP_INI_FIXTURE}.ngc`;
|
|
interp.writeTextFile(namedParamFilePath, namedParamProgramText);
|
|
verifyExpectedOutput(
|
|
"namedparam_semantics_file",
|
|
interp.runFileWithIni(namedParamFilePath, namedParamIniPath),
|
|
namedParamExpected,
|
|
);
|
|
|
|
const positionParamsFilePath = "/work/position_params.ngc";
|
|
interp.writeTextFile(
|
|
positionParamsFilePath,
|
|
readFileSync(
|
|
resolve(rootDir, `tests/fixtures/gcode/${INTERP_POSITION_PARAMS_FILE_FIXTURE}.ngc`),
|
|
"utf8",
|
|
),
|
|
);
|
|
verifyExpectedOutput(
|
|
`${INTERP_POSITION_PARAMS_FILE_FIXTURE}_file`,
|
|
interp.runFile(positionParamsFilePath),
|
|
readFileSync(
|
|
resolve(
|
|
rootDir,
|
|
`tests/fixtures/canon_file/${INTERP_POSITION_PARAMS_FILE_FIXTURE}.events`,
|
|
),
|
|
"utf8",
|
|
).trimEnd(),
|
|
);
|
|
console.log("interp_wasm_node_smoke=ok");
|