import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import createModule from "../../build-wasm/linuxcnc-sim-core.js"; import { MotionState } from "./motion-state.mjs"; import { SimSession, CONTEXT_ID } from "./sim-session.mjs"; const twin = new MotionState(); assert.equal(twin.startJog({ axis: "x", direction: 1, speed: 10 }).code, "motion_unhomed"); assert.equal(twin.home().status, "verified"); assert.equal(twin.setOverride(0.5, 1).status, "verified"); assert.equal(twin.startJog({ axis: "x", direction: 1, speed: 10 }).status, "verified"); twin.step(1); assert.equal(twin.state.axes.x, 0.005); assert.equal(twin.setLimit("x", true).state.fault, "limit"); assert.equal(twin.setFollowingError(0.02).state.fault, "following_error"); const checkpoint = twin.export(); const restoredTwin = new MotionState(); assert.deepEqual(restoredTwin.restore(checkpoint), checkpoint); const partialHome = new MotionState(); assert.deepEqual(partialHome.home("z").state.homed_axes, [false, false, true]); assert.equal(partialHome.state.homed, false); assert.deepEqual(partialHome.home("x").state.homed_axes, [true, false, true]); assert.equal(partialHome.home("y").state.homed, true); const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) }); const session = new SimSession(core, { sessionId: "motion-state" }); const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: "motion-state" }; const send = (kind, seq, operation_id, payload = {}) => session.accept({ ...base, kind, seq, operation_id, payload }); send("HELLO", 1); send("SESSION_ATTACH", 2, "attach"); send("LOAD_WORKSPACE", 3, "workspace", { root_path: "/workspace" }); assert.equal(send("HOME", 4, "home").payload.state.homed, true); assert.deepEqual(session.motion.export().homed_axes, [true, true, true]); send("JOG_START", 5, "jog", { axis: "x", direction: 1, speed: 10 }); const stepped = send("SIM_STEP", 6, "step", { cycles: 2 }); assert.equal(stepped.payload.snapshot.state_hash, stepped.payload.state_hash); assert.equal(stepped.payload.motion.axes.x, 0.02); const limit = send("LIMIT_SET", 7, "limit", { axis: "x", active: true }); assert.equal(limit.payload.state.fault, "limit"); const blocked = send("JOG_START", 8, "jog-limit", { axis: "x", direction: 1, speed: 10 }); assert.equal(blocked.kind, "ERROR"); assert.equal(blocked.payload.code, "motion_faulted"); console.log(`motion_state=passed jog_x=${stepped.payload.motion.axes.x} fault=${limit.payload.state.fault} hash=${limit.payload.state.state_hash}`);