67 lines
3.6 KiB
JavaScript
67 lines
3.6 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 { CONTEXT_ID, SimSession } 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");
|
|
const wasmBinary = await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url));
|
|
const core = await createModule({ wasmBinary });
|
|
const session = new SimSession(core, { sessionId: "xyzac-program-rerun" });
|
|
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: `${kind.toLowerCase()}-${seq}`, payload });
|
|
|
|
send("HELLO");
|
|
send("SESSION_ATTACH");
|
|
assert.equal(send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.configuration.state, "TASK_READY");
|
|
const source = "G21\nG90\nG0 X0 Y0 Z10 A0 C0\nG1 X10 Y5 Z5 A15 C20 F600\nG1 X0 Y0 Z10 A0 C0 F600\nM2\n";
|
|
assert.equal(send("PROGRAM_LOAD", { source, path: "rerun.ngc" }).payload.status, "verified");
|
|
assert.equal(send("POWER", { active: true }).payload.status, "verified");
|
|
assert.equal(send("HOME", { all: true }).payload.status, "verified");
|
|
|
|
assert.equal(send("PROGRAM_RUN").payload.task.state, "RUNNING");
|
|
let completed;
|
|
for (let index = 0; index < 1000; index += 1) {
|
|
completed = send("SIM_STEP", { cycles: 20, response: "motion_delta" });
|
|
if (completed.payload.program.program_status === "completed" && completed.payload.wasm_motion.state.queue_depth === 0) break;
|
|
}
|
|
assert.equal(completed.payload.program.program_status, "completed");
|
|
assert.equal(completed.payload.task.state, "STOPPED");
|
|
const rerunCompleted = send("PROGRAM_RUN");
|
|
assert.equal(rerunCompleted.kind, "COMMAND_RESULT");
|
|
assert.equal(rerunCompleted.payload.status, "verified");
|
|
assert.equal(rerunCompleted.payload.reopened_taskfile, true);
|
|
assert.equal(rerunCompleted.payload.start_line, 0);
|
|
assert.equal(rerunCompleted.payload.program.current_line, 0);
|
|
assert.equal(rerunCompleted.payload.task.state, "RUNNING");
|
|
const rerunStep = send("SIM_STEP", { cycles: 2, response: "motion_delta" });
|
|
assert.equal(rerunStep.kind, "COMMAND_RESULT", JSON.stringify(rerunStep));
|
|
assert.equal(rerunStep.payload.program?.program_status, "running", JSON.stringify(rerunStep));
|
|
|
|
assert.equal(send("PROGRAM_ABORT").payload.task.state, "STOPPED");
|
|
const rerunAborted = send("PROGRAM_RUN");
|
|
assert.equal(rerunAborted.kind, "COMMAND_RESULT");
|
|
assert.equal(rerunAborted.payload.status, "verified");
|
|
assert.equal(rerunAborted.payload.reopened_taskfile, true);
|
|
assert.equal(rerunAborted.payload.program.current_line, 0);
|
|
assert.equal(rerunAborted.payload.task.state, "RUNNING");
|
|
|
|
console.log(JSON.stringify({
|
|
schema: "xyzac-program-rerun.v1",
|
|
status: "verified",
|
|
source: "linuxcnc-emctaskmain-auto-run",
|
|
completed_rerun: { reopened_taskfile: rerunCompleted.payload.reopened_taskfile, start_line: rerunCompleted.payload.start_line },
|
|
aborted_rerun: { reopened_taskfile: rerunAborted.payload.reopened_taskfile, start_line: rerunAborted.payload.start_line },
|
|
}));
|