Files
linuxcncWebWASM/web/worker/test_xyzac_step_flow.mjs

101 lines
4.1 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/5axis/table-rotary-tilting/xyzac-trt.ini";
const fetchImpl = async (url) => {
try {
const body = await readFile(String(url).replace(/^\//, ""), "utf8");
return { ok: true, text: async () => body };
} catch {
return { ok: false, text: async () => "" };
}
};
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
assert.equal(bundle.status, "ready");
const core = await createModule({ wasmBinary: await readFile("./build-wasm/linuxcnc-sim-core.wasm") });
const session = new SimSession(core, { sessionId: "xyzac-step-flow" });
let seq = 0;
const send = (kind, payload = {}) => session.accept({ schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId, seq: ++seq, operation_id: `step-flow-${seq}`, kind, payload });
const payload = (kind, body = {}) => {
const response = send(kind, body);
assert.notEqual(response.kind, "ERROR", `${kind}: ${response.payload?.gate_reason ?? response.payload?.message_key ?? response.payload?.code}`);
return response.payload;
};
const step = () => payload("PROGRAM_STEP");
const drain = () => {
let cycles = 0;
while (session.wasmMotion.isStepping() && cycles < 1000) {
payload("SIM_STEP", { cycles: 1, response: "motion_delta" });
cycles += 1;
}
assert.equal(session.wasmMotion.isStepping(), false, "single-step motion must reach an execution-id boundary");
return cycles;
};
payload("HELLO");
payload("SESSION_ATTACH");
payload("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
payload("PROGRAM_LOAD", { path: "step-flow.ngc", source: "G21 G90\nG1 X1 F600\nG1 X2 F600\nM2\n" });
payload("POWER", { active: true });
payload("HOME", { all: true });
payload("SIM_STEP", { cycles: 1 });
let result = step();
assert.equal(result.step_phase, "start_pause");
assert.equal(result.step_source_line, 0);
assert.equal(result.step_waiting_for_motion, false);
assert.equal(result.task.state, "PAUSED");
result = step();
assert.equal(result.step_phase, "source_line_pause");
assert.equal(result.step_source_line, 1);
assert.equal(result.program_steps.length, 1);
result = step();
assert.equal(result.step_phase, "source_line_motion");
assert.equal(result.step_waiting_for_motion, true);
assert.equal(result.step_source_line, 2);
assert.equal(result.program_steps.length, 1);
const firstTarget = result.step_target_id ?? result.wasm_motion.state.active_id;
assert.ok(Number.isSafeInteger(result.step_target_id) && result.step_target_id > 0);
assert.equal(result.motion.stepping, true);
const duplicate = step();
assert.equal(duplicate.step_phase, "motion_step_pending");
assert.equal(duplicate.step_target_id, firstTarget);
assert.equal(duplicate.program_steps.length, 0);
drain();
assert.ok(Math.abs(session.wasmMotion.read().state.position.x - 1) < 1e-8);
result = step();
assert.equal(result.step_phase, "source_line_motion");
assert.equal(result.step_source_line, 3);
drain();
assert.ok(Math.abs(session.wasmMotion.read().state.position.x - 2) < 1e-8);
result = step();
assert.equal(result.step_phase, "terminal");
assert.equal(result.program.program_status, "completed");
assert.equal(result.task.state, "STOPPED");
result = step();
assert.equal(result.step_phase, "start_pause");
assert.equal(result.program.program_status, "paused");
assert.equal(result.task.state, "PAUSED");
payload("PROGRAM_ABORT");
result = step();
assert.equal(result.step_phase, "start_pause");
assert.equal(result.program.program_status, "paused");
console.log(JSON.stringify({
schema: "xyzac-step-flow.v2",
status: "verified",
source: ["axis.py::task_step", "emctaskmain.cc::EMC_TASK_PLAN_STEP_TYPE", "sim_linuxcnc_tp_step_line"],
phases: ["start_pause", "source_line_pause", "source_line_motion", "motion_step_pending", "terminal"],
duplicate_step_idempotent: true,
completed_and_aborted_rerun: true,
}));