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 { XYZAC_TRT_SOURCE_SLICES } from "../workspace/xyzac-trt-source-slices.mjs"; import { SimSession, CONTEXT_ID } from "./sim-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 }); assert.equal(bundle.status, "ready"); assert.deepEqual(bundle.missing, []); assert.ok(bundle.runtime_fixtures.some((fixture) => fixture.generated_hal?.endsWith("xyzac-trt-headless_cmds.hal"))); 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-trt-case" }); 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) }); assert.equal(loaded.kind, "COMMAND_RESULT"); assert.equal(loaded.payload.status, "verified"); assert.equal(loaded.payload.motion_authority, "wasm"); assert.equal(loaded.payload.configuration.state, "TASK_READY"); assert.equal(loaded.payload.panel_snapshot.kinematics.joint_count, 5); assert.equal(send("POWER", "power-on", { active: true }).payload.status, "verified"); assert.equal(send("HOME", "home-all", { all: true }).kind, "COMMAND_RESULT"); for (let tick = 0; tick < 500 && !session.wasmHomeLimit.read().state.homed; tick++) send("SIM_STEP", `home-step-${tick}`, { cycles: 1 }); assert.equal(session.wasmHomeLimit.read().state.homed, true); const switchRows = []; for (const [code, expected] of [["M429", 0], ["M428", 1], ["M430", 2]]) { const result = send("MDI_EXECUTE", `mdi-${code}`, { block: code }); assert.equal(result.kind, "COMMAND_RESULT"); assert.equal(result.payload.status, "verified"); assert.equal(result.payload.kinematics.active_type, expected); assert.equal(result.payload.remap.hal_pin, "motion.switchkins-type"); assert.equal(result.payload.remap.sync, "M66 E0 L0"); switchRows.push({ code, tick: result.simulation_tick, active_type: expected, state_hash: result.payload.panel_snapshot.state_hash }); } // Direct panel selection must preserve physical joints and keep the new // coordinate pose synchronized with TP for every mode transition. const move = send("MDI_EXECUTE", "seed-position", { block: "G0 X10 Y5 Z20 A30 C45" }); assert.equal(move.kind, "COMMAND_RESULT"); for (let cycle = 0; cycle < 1000 && session.wasmMotion.read().state.queue_depth > 0; cycle += 1) send("SIM_STEP", `seed-step-${cycle}`, { cycles: 1 }); assert.equal(session.wasmMotion.read().state.queue_depth, 0); const directSwitches = []; for (const expected of [0, 1, 2, 0, 1, 2, 0]) { const physicalJoints = session.wasmMotion.read().state.joint_commanded; const result = send("KINEMATICS_SELECT", `direct-${expected}-${seq}`, { type: expected }); assert.equal(result.kind, "COMMAND_RESULT"); assert.equal(result.payload.kinematics.active_type, expected); const pose = result.payload.kinematics.pose; const tpPose = result.payload.wasm_motion.state.position; for (const axis of ["x", "y", "z", "a", "b", "c", "u", "v", "w"]) assert.ok(Math.abs(Number(pose[axis]) - Number(tpPose[axis])) <= 1e-9, `${axis}: ${pose[axis]} != ${tpPose[axis]}`); result.payload.wasm_motion.state.joint_commanded.forEach((value, i) => assert.ok(Math.abs(value - physicalJoints[i]) < 1e-8, `switch must preserve physical joint ${i}`)); directSwitches.push({ active_type: expected, state_hash: result.payload.kinematics.state_hash }); } const pose = session.xyzacKinematics.forward([100, 50, 100, 30, 45]); assert.equal(pose.status, "verified"); assert.equal(pose.joint_count, 5); assert.ok(Number.isFinite(pose.pose.x) && Number.isFinite(pose.pose.y) && Number.isFinite(pose.pose.z)); const sourceSliceIds = new Set(XYZAC_TRT_SOURCE_SLICES.slices.map((slice) => slice.slice_id)); const controls = [ ...XYZAC_TRT_SOURCE_SLICES.axis_controls.menus, ...XYZAC_TRT_SOURCE_SLICES.axis_controls.toolbar, ...XYZAC_TRT_SOURCE_SLICES.axis_controls.keyboard_bindings, ]; assert.ok(controls.length >= 110); assert.ok(controls.every((control) => sourceSliceIds.has(control.slice_id))); console.log(JSON.stringify({ schema: "xyzac-trt-wasm-worker-trace.v1", status: "verified", case: CASE, configuration: loaded.payload.configuration.state, motion_authority: loaded.payload.motion_authority, control_count: controls.length, switchkins: switchRows, direct_switchkins: directSwitches, pose: pose.pose, wasm_state_hash: pose.wasm_state_hash, state_hash: pose.state_hash, first_divergent_tick: null, }));