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/5axis/table-rotary-tilting/xyzac-trt.ini"; const PROGRAM = "G21 G90\nG0 X0 Y0 Z10 A0 C0\nG1 X10 Y5 Z5 A30 C45 F100\nM2\n"; 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: "xyzac-button-constraints" }); const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId }; let seq = 0; const send = (kind, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: `button-${seq}`, payload }); send("HELLO"); send("SESSION_ATTACH"); const loaded = send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }); assert.equal(loaded.payload.panel_snapshot.axis_state.machine_state, "OFF"); assert.equal(send("PROGRAM_LOAD", { source: PROGRAM, path: "button.ngc" }).payload.status, "verified"); assert.equal(send("RESET").payload.gate_reason, "xyzac_machine_off"); const restarted = send("PROGRAM_RESTART"); assert.equal(restarted.payload.program.path, "button.ngc"); for (const [kind, payload, reason] of [["PROGRAM_RUN", {}, "xyzac_machine_off"], ["HOME", { all: true }, "xyzac_machine_off"], ["JOG_START", { axis: "x", direction: 1, speed: 1 }, "xyzac_machine_off"], ["MDI_EXECUTE", { block: "G0 X1" }, "xyzac_machine_off"], ["KINEMATICS_SELECT", { type: 1 }, "xyzac_machine_off"], ["POWER", { active: false }, "xyzac_power_already_off"]]) { const blocked = send(kind, payload); assert.equal(blocked.kind, "ERROR", kind); assert.equal(blocked.payload.gate_reason, reason, kind); } const powered = send("POWER", { active: true }); assert.equal(powered.payload.panel_snapshot.axis_state.machine_state, "ON"); assert.equal(send("HOME", { all: true }).payload.wasm_home_limit.state.homed, true); const readyGates = send("SNAPSHOT_REQUEST").payload.panel_snapshot.gates; assert.equal(readyGates.spindle.enabled, true); assert.equal(readyGates.coolant.enabled, true); assert.equal(readyGates.tool.enabled, true); assert.equal(readyGates.probe.enabled, true); assert.equal(send("KINEMATICS_SELECT", { type: 1 }).payload.kinematics.active_type, 1); const alternateKinematicsGates = send("SNAPSHOT_REQUEST").payload.panel_snapshot.gates; // Switchkins leaves MDI selected; AXIS spindle() ensures MANUAL itself. assert.equal(alternateKinematicsGates.spindle.enabled, true); assert.equal(alternateKinematicsGates.spindle.reason, "linuxcnc_wasm_authority"); assert.equal(alternateKinematicsGates.tool.reason, "linuxcnc_wasm_mode_unavailable"); assert.equal(send("RESET").payload.command, "RESET"); assert.equal(send("MDI_EXECUTE", { block: "G0 X1" }).payload.task.mode, "MDI"); assert.equal(send("JOG_STOP").kind, "ERROR", "jog stop must not abort a running MDI move"); assert.equal(send("SIM_STEP", { cycles: 100 }).kind, "COMMAND_RESULT"); assert.equal(send("JOG_STOP").payload.command, "JOG_STOP"); assert.equal(send("SET_MODE", { mode: "MANUAL" }).payload.mode, "MANUAL"); assert.equal(send("POWER", { active: false }).payload.panel_snapshot.axis_state.machine_state, "OFF"); assert.equal(send("PROGRAM_RUN").payload.gate_reason, "xyzac_machine_off"); assert.equal(send("POWER", { active: true }).payload.panel_snapshot.axis_state.machine_state, "ON"); assert.equal(send("PROGRAM_RUN").payload.task.state, "RUNNING"); for (const [kind, payload, reason] of [["HOME", { all: true }, "xyzac_interp_running"], ["MDI_EXECUTE", { block: "G0 X1" }, "mdi_while_program_active"], ["KINEMATICS_SELECT", { type: 0 }, "xyzac_interp_running"], ["SET_MODE", { mode: "MANUAL" }, "linuxcnc_wasm_auto_interp_active"]]) { const blocked = send(kind, payload); assert.equal(blocked.kind, "ERROR", kind); assert.equal(blocked.payload.gate_reason, reason, kind); } assert.equal(send("PROGRAM_PAUSE").payload.task.state, "PAUSED"); const abortedWhilePaused = send("PROGRAM_ABORT"); assert.equal(abortedWhilePaused.payload.task.state, "STOPPED"); assert.equal(abortedWhilePaused.payload.task.program_status, "aborted"); assert.equal(abortedWhilePaused.payload.wasm_motion.state.queue_depth, 0); assert.equal(send("PROGRAM_RESTART").payload.task.program_status, "loaded"); assert.equal(send("PROGRAM_RUN").payload.task.state, "RUNNING"); assert.equal(send("PROGRAM_PAUSE").payload.task.state, "PAUSED"); assert.equal(send("PROGRAM_RESUME").payload.task.state, "RUNNING"); assert.equal(send("POWER", { active: false }).payload.task.state, "STOPPED"); assert.equal(send("POWER", { active: true }).payload.panel_snapshot.axis_state.machine_state, "ON"); assert.equal(send("PROGRAM_ABORT").payload.task.state, "STOPPED"); assert.equal(send("ESTOP", { active: true }).payload.panel_snapshot.axis_state.machine_state, "ESTOP"); const reset = send("ESTOP", { active: false }); assert.equal(reset.payload.panel_snapshot.axis_state.source_task_state, "STATE_ESTOP_RESET"); assert.equal(send("POWER", { active: true }).payload.panel_snapshot.axis_state.machine_state, "ON"); console.log(JSON.stringify({ schema: "xyzac-button-constraints.v1", status: "verified", case: CASE, sequence: ["ESTOP", "ESTOP_RESET", "POWER_ON", "HOME", "IDLE", "RUNNING", "PAUSED", "RESUME", "ABORT", "ESTOP"], authority: "linuxcnc-wasm-worker", gates: { off_blocks_motion: true, running_blocks_manual: true, running_allows_power_off: true, power_off_aborts_running: true, pause_resume_exclusive: true, mdi_requires_home: true }, state_hash: reset.payload.panel_snapshot.state_hash, }));