75 lines
3.5 KiB
JavaScript
75 lines
3.5 KiB
JavaScript
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: "wasm-motion-authority" });
|
|
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.payload.motion_authority, "wasm");
|
|
|
|
let jsAuthorityCalls = 0;
|
|
for (const method of ["syncProgram", "step", "applyMotion"]) {
|
|
session.authoritative[method] = () => {
|
|
jsAuthorityCalls += 1;
|
|
throw new Error(`js_motion_authority_called:${method}`);
|
|
};
|
|
}
|
|
|
|
send("PROGRAM_LOAD", "program", { path: "authority.ngc", source: "G21 G90\nG0 X0 Y0 Z0\nG1 X40 Y20 F120\nG1 Z-2 F60\nM2\n" });
|
|
assert.equal(send("PROGRAM_RUN", "run").payload.task.state, "RUNNING");
|
|
const moving = send("SIM_STEP", "moving", { cycles: 3 });
|
|
assert.equal(moving.payload.wasm_motion.state.source, "wasm_motion_queue");
|
|
assert.deepEqual(moving.payload.motion.axes, moving.payload.wasm_motion.state.position);
|
|
assert.ok(moving.payload.wasm_motion.state.queue_depth > 0);
|
|
|
|
const pausedPosition = { ...moving.payload.wasm_motion.state.position };
|
|
const paused = send("PROGRAM_PAUSE", "pause");
|
|
assert.equal(paused.payload.task.state, "PAUSED");
|
|
assert.deepEqual(paused.payload.wasm_motion.state.position, pausedPosition);
|
|
assert.deepEqual(paused.payload.motion.axes, pausedPosition);
|
|
assert.equal(paused.payload.wasm_motion.state.velocity, 0);
|
|
assert.equal(send("PROGRAM_RESUME", "resume").payload.task.state, "RUNNING");
|
|
const resumed = send("SIM_STEP", "resumed-step", { cycles: 2 });
|
|
assert.notDeepEqual(resumed.payload.wasm_motion.state.position, pausedPosition);
|
|
|
|
send("PROGRAM_RESTART", "restart");
|
|
const stepped = send("PROGRAM_STEP", "single-step");
|
|
assert.equal(stepped.payload.task.state, "PAUSED");
|
|
assert.ok(stepped.payload.wasm_motion.state.queue_depth >= 0);
|
|
|
|
const aborted = send("PROGRAM_ABORT", "abort");
|
|
assert.equal(aborted.payload.task.program_status, "aborted");
|
|
assert.equal(aborted.payload.wasm_motion.state.queue_depth, 0);
|
|
assert.deepEqual(aborted.payload.motion.axes, aborted.payload.wasm_motion.state.position);
|
|
assert.equal(jsAuthorityCalls, 0);
|
|
|
|
console.log(JSON.stringify({
|
|
schema: "wasm-motion-authority-t3.v1",
|
|
status: "verified",
|
|
case: CASE,
|
|
motion_authority: loaded.payload.motion_authority,
|
|
paused_position: pausedPosition,
|
|
resumed_position: resumed.payload.wasm_motion.state.position,
|
|
abort_queue_depth: aborted.payload.wasm_motion.state.queue_depth,
|
|
js_authority_calls: jsAuthorityCalls,
|
|
}));
|