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-snapshot" }); 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"); const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }); const projection = loaded.payload.panel_snapshot; assert.equal(projection.schema, "axis-panel-snapshot.v1"); assert.equal(projection.context_id, CONTEXT_ID); assert.equal(projection.session_id, session.sessionId); assert.equal(projection.motion_authority, "wasm"); assert.equal(projection.task_authority, "wasm_task_canonical"); assert.equal(projection.axis_state.state, "IDLE"); assert.equal(projection.gates.run.enabled, false); assert.equal(projection.gates.run.reason, "axis_taskfile_missing"); assert.equal(projection.configuration.state, "TASK_READY"); assert.equal(projection.home_limit.state.source, "wasm_home_limit"); assert.equal(projection.visual.source, "wasm_canonical_motion"); assert.deepEqual(projection.visual.pose, { x: 0, y: 0, z: 0 }); const loadedProgram = send("PROGRAM_LOAD", "program", { path: "panel.ngc", source: "G21 G90\nG0 X1 Y2 Z-1\nM2\n" }); assert.equal(loadedProgram.payload.panel_snapshot.program.program_status, "loaded"); assert.ok(loadedProgram.payload.panel_snapshot.visual.toolpath.length > 0); const beforeHash = loadedProgram.payload.panel_snapshot.state_hash; send("HOME", "home-z", { axis: "z" }); send("HOME", "home-x", { axis: "x" }); send("HOME", "home-y", { axis: "y" }); const jog = send("JOG_START", "jog", { axis: "x", direction: 1, speed: 2 }); const stepped = send("SIM_STEP", "jog-step", { cycles: 2 }); assert.notEqual(stepped.payload.panel_snapshot.state_hash, beforeHash); assert.equal(stepped.payload.panel_snapshot.visual.source, "wasm_canonical_motion"); assert.equal(stepped.payload.panel_snapshot.simulation_tick, stepped.simulation_tick); assert.equal(stepped.payload.panel_snapshot.state_hash, session.panelSnapshot({ input: { kind: "SIM_STEP", payload: { cycles: 2 } }, tick: session.tick }).state_hash); assert.ok(jog.payload.panel_snapshot.home_limit.state.homed === true); console.log(JSON.stringify({ schema: "panel-snapshot-projection.v1", status: "verified", case: CASE, fields: ["task", "program", "motion", "wasm_motion", "home_limit", "hal", "devices", "device_io", "configuration", "axis_state", "visual", "state_hash"], authority: "wasm", startup: projection.configuration.state, visual_source: projection.visual.source, state_hash: stepped.payload.panel_snapshot.state_hash, }));