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 { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs"; const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.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 XyzacSession(core, { sessionId: "xyzac-workflow" }); const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId }; let seq = 0; const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload }); send("HELLO", "hello"); send("SESSION_ATTACH", "attach"); assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified"); assert.equal(send("POWER", "power-on", { active: true }).payload.status, "verified"); const source = "G21 G90\nG0 X10 Y5 Z200 A30 C45\nG1 X20 Y10 Z180 A35 C50 F100\nG1 X30 Y15 Z160 A40 C55 F100\nM2\n"; const loaded = send("PROGRAM_LOAD", "program-load", { source, path: "workflow.ngc" }); assert.equal(loaded.kind, "COMMAND_RESULT"); const runWithout = send("PROGRAM_RUN", "run"); assert.equal(runWithout.payload.task.state, "RUNNING"); const first = send("SIM_STEP", "step-1", { cycles: 1 }); assert.equal(first.payload.motion_authority, "wasm"); const tickAfterFirst = first.simulation_tick; const paused = send("PROGRAM_PAUSE", "pause"); assert.equal(paused.payload.task.state, "PAUSED"); const cursorPaused = paused.payload.program.current_line; const pausedSnapshot = send("SNAPSHOT_REQUEST", "paused-snapshot"); assert.equal(pausedSnapshot.payload.task.state, "PAUSED"); assert.equal(pausedSnapshot.payload.panel_snapshot.program.current_line, cursorPaused); const resumed = send("PROGRAM_RESUME", "resume"); assert.equal(resumed.payload.task.state, "RUNNING"); const second = send("SIM_STEP", "step-2", { cycles: 1 }); assert.ok(second.simulation_tick > tickAfterFirst); const activeBeforeAbort = second.payload.wasm_motion.state.active_id; const aborted = send("PROGRAM_ABORT", "abort"); assert.equal(aborted.payload.task.state, "STOPPED"); assert.equal(aborted.payload.panel_snapshot.wasm_motion.state.queue_depth, 0); assert.equal(aborted.payload.panel_snapshot.wasm_motion.state.active_id, 0); const restart = send("PROGRAM_RESTART", "restart"); assert.equal(restart.payload.task.program_status, "loaded"); assert.equal(restart.payload.program.path, "workflow.ngc"); assert.equal(restart.payload.program.source_hash, loaded.payload.program.source_hash); const seek = send("PROGRAM_SEEK", "seek-line", { line: 3 }); assert.equal(seek.payload.task.program_status, "paused"); assert.equal(seek.payload.program.current_line, 3); const runLine = send("PROGRAM_RESUME", "run-from-line"); assert.equal(runLine.payload.task.state, "RUNNING"); const lineStep = send("SIM_STEP", "run-line-step", { cycles: 1 }); assert.equal(lineStep.payload.motion_authority, "wasm"); const checkpoint = send("CHECKPOINT", "checkpoint"); assert.equal(checkpoint.kind, "CHECKPOINT_READY"); const recovered = send("RECOVER", "recover", { checkpoint: checkpoint.payload.checkpoint }); assert.equal(recovered.payload.status, "recovered"); assert.equal(recovered.payload.state.program.source_text, source); assert.equal(session.ledger.reconcile("step-1").status, "committed"); console.log(JSON.stringify({ schema: "xyzac-workflow-rtcp-05.v1", status: "verified", case: CASE, tick_after_first: tickAfterFirst, active_before_abort: activeBeforeAbort, pause_cursor: cursorPaused, restart_source_hash: restart.payload.program.source_hash, run_from_line: lineStep.payload.program.current_line, recovery: recovered.payload.status, final_state_hash: lineStep.payload.panel_snapshot.state_hash }));