66 lines
3.3 KiB
JavaScript
66 lines
3.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFile } from "node:fs/promises";
|
|
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
|
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
|
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
|
|
|
|
const CASE = "linuxcnc/configs/sim/axis/vismach/3axis-tutorial/3axis-tutorial.ini";
|
|
const fetchImpl = async (url) => {
|
|
try {
|
|
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
|
return { ok: true, text: async () => body.toString("utf8") };
|
|
} catch {
|
|
return { ok: false, text: async () => "" };
|
|
}
|
|
};
|
|
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
|
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
|
const session = new SimSession(core, { sessionId: "panel-gates" });
|
|
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
|
let seq = 0;
|
|
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
|
|
|
send("HELLO", "hello");
|
|
send("SESSION_ATTACH", "attach");
|
|
send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
|
const noProgram = send("PROGRAM_RUN", "run-without-program");
|
|
assert.equal(noProgram.kind, "ERROR");
|
|
assert.equal(noProgram.payload.code, "axis_gate");
|
|
assert.equal(noProgram.payload.gate_reason, "axis_taskfile_missing");
|
|
|
|
send("PROGRAM_LOAD", "load", { path: "gate.ngc", source: "G21 G90\nG1 X1 F10\nM2\n" });
|
|
const running = send("PROGRAM_RUN", "run");
|
|
assert.equal(running.payload.task.state, "RUNNING");
|
|
const homeWhileRunning = send("HOME", "home-while-running", { axis: "x" });
|
|
assert.equal(homeWhileRunning.kind, "ERROR");
|
|
assert.equal(homeWhileRunning.payload.code, "axis_gate");
|
|
assert.equal(homeWhileRunning.payload.gate_reason, "axis_manual_mode_required");
|
|
const mdiWhileRunning = send("MDI_EXECUTE", "mdi-while-running", { block: "G0 X2" });
|
|
assert.equal(mdiWhileRunning.kind, "ERROR");
|
|
assert.equal(mdiWhileRunning.payload.message_key, "mdi_while_program_active");
|
|
send("PROGRAM_ABORT", "abort");
|
|
send("SET_MODE", "manual", { mode: "MANUAL" });
|
|
send("HOME", "home-x", { axis: "x" });
|
|
send("HOME", "home-y", { axis: "y" });
|
|
send("HOME", "home-z", { axis: "z" });
|
|
const jog = send("JOG_START", "jog", { axis: "x", direction: 1, speed: 1 });
|
|
assert.equal(jog.payload.status, "verified");
|
|
send("JOG_STOP", "jog-stop");
|
|
const estop = send("ESTOP", "estop", { active: true });
|
|
assert.equal(estop.kind, "ERROR");
|
|
const blockedRun = send("PROGRAM_RUN", "run-estop");
|
|
assert.equal(blockedRun.kind, "ERROR");
|
|
assert.ok(["axis_estop_active", "axis_fault_active"].includes(blockedRun.payload.gate_reason));
|
|
send("ESTOP", "estop-reset", { active: false });
|
|
send("IO_RECOVER", "recover");
|
|
const reset = send("RESET", "reset");
|
|
assert.equal(reset.payload.status, "verified");
|
|
|
|
console.log(JSON.stringify({
|
|
schema: "panel-gate-matrix.v1",
|
|
status: "verified",
|
|
normal: ["PROGRAM_LOAD", "PROGRAM_RUN", "SET_MODE", "HOME", "JOG_START", "JOG_STOP", "RESET"],
|
|
blocked: [noProgram.payload.gate_reason, homeWhileRunning.payload.gate_reason, blockedRun.payload.gate_reason, mdiWhileRunning.payload.message_key],
|
|
authority: "worker_axis_gate",
|
|
}));
|