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"; import { fnv1a64 } from "./replay.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 SimSession(core, { sessionId: "xyzac-program-replace" }); 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: `replace-${seq}`, payload }); send("HELLO"); send("SESSION_ATTACH"); assert.equal(send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified"); const first = send("PROGRAM_LOAD", { path: "first.ngc", source: "G21\nG1 X10 Y5 Z2 F100\nM2\n" }); assert.equal(first.payload.preview.segments.length, 1); assert.equal(first.payload.panel_snapshot.visual.toolpath.length, 1); const failed = send("PROGRAM_LOAD", { path: "invalid.ngc", source: "G999\n" }); assert.equal(failed.kind, "ERROR"); assert.equal(failed.payload.panel_snapshot.visual.toolpath.length, 0); assert.equal(session.previewState, null); const second = send("PROGRAM_LOAD", { path: "second.ngc", source: "G21\nG1 X1 Y2 Z3 F100\nM2\n" }); assert.equal(second.payload.program.path, "second.ngc"); assert.notEqual(second.payload.program.source_hash, first.payload.program.source_hash); assert.equal(second.payload.panel_snapshot.visual.toolpath.length, 1); const epoch = () => session.livePathEpoch; const before = epoch(); const source = "G21 G90\nG1 X2 Y2 Z3 F100\nM2\n"; const replaced = send("PROGRAM_LOAD", { source, path: "web-replacement.ngc", clear_live_plot_on_replace: true }); assert.equal(replaced.kind, "COMMAND_RESULT"); assert.equal(replaced.payload.visual_clear, undefined); assert.equal(epoch(), before, "changed file retains the native trace, even from an old caller"); assert.equal(replaced.payload.program.source_hash, fnv1a64(source)); assert.equal(session.xyzacProgram.snapshot().source_hash, fnv1a64(source)); assert.equal(send("PROGRAM_LOAD", { source, path: "web-replacement.ngc", clear_live_plot_on_replace: true }).kind, "COMMAND_RESULT"); assert.equal(epoch(), before, "unchanged reload preserves live history"); const failedReplacement = send("PROGRAM_LOAD", { source: "G999\n", path: "bad-web.ngc", clear_live_plot_on_replace: true }); assert.equal(failedReplacement.kind, "ERROR"); assert.equal(epoch(), before, "failed parse does not clear history"); const native = send("PROGRAM_LOAD", { source, path: "native.ngc" }); assert.equal(native.kind, "COMMAND_RESULT"); assert.equal(epoch(), before, "default source PROGRAM_LOAD preserves history"); send("POWER", { active: true }); send("HOME", { all: true }); send("PROGRAM_RUN"); const blocked = send("PROGRAM_LOAD", { source, path: "blocked.ngc", clear_live_plot_on_replace: true }); assert.equal(blocked.kind, "ERROR"); assert.equal(epoch(), before, "gate rejection does not clear history"); send("PROGRAM_ABORT"); const edited = send("PROGRAM_LOAD", { source: source.replace("X2", "X3"), path: "native.ngc", clear_live_plot_on_replace: true }); assert.equal(edited.kind, "COMMAND_RESULT"); assert.equal(epoch(), before, "edited text retains the native live trace"); assert.equal(edited.payload.program.source_hash, fnv1a64(source.replace("X2", "X3"))); const cleared = send("VISUAL_CLEAR"); assert.equal(cleared.kind, "COMMAND_RESULT"); assert.equal(epoch(), before + 1); assert.deepEqual(cleared.payload.before, cleared.payload.after, "clear must not change CNC state"); assert.deepEqual(cleared.payload.panel_snapshot.visual.toolpath, edited.payload.panel_snapshot.visual.toolpath); console.log(JSON.stringify({ schema: "xyzac-program-replacement.v1", status: "verified", first_segments: first.payload.panel_snapshot.visual.toolpath.length, failed_segments: failed.payload.panel_snapshot.visual.toolpath.length, second_segments: second.payload.panel_snapshot.visual.toolpath.length, old_preview_retained_after_failure: false, }));