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 { MACHINE_DEFINITIONS } from "../workspace/machine-definitions.mjs"; import { CONTEXT_ID, SimSession } from "./sim-session.mjs"; const root = "/home/mes006/linuxcncWebWASM"; const fetchImpl = async (url) => { const relative = String(url).replace(/^\.?\//, ""); for (const path of [`${root}/${relative}`, `${root}/web/react/public/${relative}`]) { try { const body = await readFile(path); return { ok: true, text: async () => body.toString("utf8") }; } catch { /* try next root */ } } return { ok: false, text: async () => "" }; }; const wasmBinary = await readFile(`${root}/build-wasm/linuxcnc-sim-core.wasm`); const rows = []; for (const machine of MACHINE_DEFINITIONS.filter((entry) => entry.runtime === "generic-wasm")) { const core = await createModule({ wasmBinary }); const session = new SimSession(core, { sessionId: machine.id, planner: "queue" }); let seq = 0; const send = (kind, payload = {}) => session.accept({ schema_version: 1, context_id: CONTEXT_ID, session_id: machine.id, kind, seq: ++seq, operation_id: `${machine.id}-${seq}`, payload }); send("HELLO"); send("SESSION_ATTACH"); const bundle = await loadSimulationCase({ path: machine.entrypoint, fetchImpl }); assert.equal(bundle.status, "ready", machine.id); const configuration = toWorkerConfiguration(bundle); const workspace = send("LOAD_WORKSPACE", { root_path: "/workspace", configuration }); assert.equal(workspace.kind, "COMMAND_RESULT", machine.id); const loaded = send("PROGRAM_LOAD", { source: bundle.files[machine.program], path: machine.program }); assert.equal(loaded.kind, "COMMAND_RESULT", `${machine.id}: ${loaded.payload?.code ?? "load"}`); assert.equal(send("PROGRAM_RUN").kind, "COMMAND_RESULT", machine.id); let last = null; for (let tick = 0; tick < 12000; tick += 1) { last = send("SIM_STEP", { cycles: 1 }); assert.notEqual(last.kind, "ERROR", `${machine.id}: ${last.payload?.code ?? "step"}`); if (last.payload?.program?.program_status === "completed" && Number(last.payload?.wasm_motion?.state?.queue_depth ?? 0) === 0) break; } assert.equal(last?.payload?.program?.program_status, "completed", machine.id); const joints = last.payload.wasm_motion.state.joint_commanded; assert.equal(joints.length, machine.joint_count, machine.id); assert.ok(joints.every(Number.isFinite), machine.id); rows.push({ id: machine.id, kinematics: machine.kinematics, coordinates: machine.coordinates, program: machine.program, joints }); } console.log(JSON.stringify({ schema: "generic-wasm-machine-cases.v1", status: "verified", case_count: rows.length, rows }));