80 lines
4.1 KiB
JavaScript
80 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 { 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 core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
|
const session = new SimSession(core, { sessionId: "xyzac-live-path-lifecycle" });
|
|
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 });
|
|
const epoch = (response) => response.payload.panel_snapshot.visual.live_path_epoch;
|
|
|
|
send("HELLO");
|
|
send("SESSION_ATTACH");
|
|
send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
|
const source = ["G21", "G90", ...Array.from({ length: 80 }, (_, index) => `G1 X${index % 2 ? 0 : 5} Y${index % 2 ? 0 : 3} Z${index % 2 ? 10 : 8} F300`), "M2", ""].join("\n");
|
|
const loaded = send("PROGRAM_LOAD", { source, path: "live-path.ngc" });
|
|
assert.equal(loaded.kind, "COMMAND_RESULT");
|
|
const loadEpoch = epoch(loaded);
|
|
const previewCount = loaded.payload.panel_snapshot.visual.toolpath.length;
|
|
assert.equal(loadEpoch, 0);
|
|
assert.ok(previewCount > 0);
|
|
send("POWER", { active: true });
|
|
send("HOME", { all: true });
|
|
|
|
const run = send("PROGRAM_RUN");
|
|
const runEpoch = epoch(run);
|
|
assert.equal(runEpoch, loadEpoch);
|
|
send("SIM_STEP", { cycles: 5, response: "motion_delta" });
|
|
const pause = send("PROGRAM_PAUSE");
|
|
assert.equal(epoch(pause), runEpoch);
|
|
assert.equal(Object.hasOwn(pause.payload.panel_snapshot.visual, "toolpath"), false);
|
|
const resume = send("PROGRAM_RESUME");
|
|
assert.equal(epoch(resume), runEpoch);
|
|
assert.equal(Object.hasOwn(resume.payload.panel_snapshot.visual, "toolpath"), false);
|
|
|
|
let completed = resume;
|
|
for (let index = 0; index < 5000; index += 1) {
|
|
completed = send("SIM_STEP", { cycles: 20, response: "motion_delta" });
|
|
if (completed.payload.program?.program_status === "completed" && Number(completed.payload.wasm_motion?.state?.queue_depth ?? -1) === 0) break;
|
|
}
|
|
assert.equal(completed.payload.program.program_status, "completed");
|
|
assert.equal(epoch(completed), runEpoch);
|
|
assert.equal(Object.hasOwn(completed.payload.panel_snapshot.visual, "toolpath"), false);
|
|
|
|
const rerun = send("PROGRAM_RUN");
|
|
assert.equal(epoch(rerun), runEpoch);
|
|
const abort = send("PROGRAM_ABORT");
|
|
assert.equal(epoch(abort), runEpoch);
|
|
|
|
const replacement = send("PROGRAM_LOAD", { source: "G21\nG90\nG1 X1 Y1 Z1 F300\nM2\n", path: "replacement-live-path.ngc" });
|
|
assert.equal(epoch(replacement), runEpoch);
|
|
assert.equal(replacement.payload.panel_snapshot.visual.toolpath.length, 1);
|
|
const restart = send("PROGRAM_RESTART");
|
|
assert.equal(epoch(restart), runEpoch);
|
|
const clear = send("VISUAL_CLEAR");
|
|
assert.equal(epoch(clear), runEpoch + 1);
|
|
assert.equal(clear.payload.panel_snapshot.visual.toolpath.length, 1);
|
|
assert.equal(session.previewState.segments.length, 1);
|
|
|
|
console.log(JSON.stringify({
|
|
schema: "xyzac-live-path-lifecycle.v1",
|
|
status: "verified",
|
|
source: "axis.py:LivePlotter/clear_live_plot/task_pause/task_resume/task_stop",
|
|
epochs: { load: loadEpoch, run: runEpoch, pause: epoch(pause), resume: epoch(resume), completed: epoch(completed), rerun: epoch(rerun), abort: epoch(abort), replacement: epoch(replacement), restart: epoch(restart), clear: epoch(clear) },
|
|
static_preview: { initial_segments: previewCount, clear_plot_segments: clear.payload.panel_snapshot.visual.toolpath.length },
|
|
}));
|