107 lines
5.7 KiB
JavaScript
107 lines
5.7 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 { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
|
import { fnv1a64 } from "./replay.mjs";
|
|
|
|
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
|
const PROGRAM_PATH = "linuxcnc/nc_files/3D_Chips.ngc";
|
|
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 XyzacSession(core, { sessionId: "xyzac-3d-chips" });
|
|
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: `chips-${seq}`, payload });
|
|
|
|
send("HELLO");
|
|
send("SESSION_ATTACH");
|
|
assert.equal(send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
|
const source = await readFile(new URL(`../../${PROGRAM_PATH}`, import.meta.url), "utf8");
|
|
const loaded = send("PROGRAM_LOAD", { source, path: PROGRAM_PATH });
|
|
assert.equal(loaded.kind, "COMMAND_RESULT");
|
|
assert.equal(loaded.payload.status, "verified");
|
|
assert.equal(loaded.payload.program.path, PROGRAM_PATH);
|
|
assert.equal(loaded.payload.program.source_length, 200509);
|
|
assert.equal(loaded.payload.program.line_count, 4711);
|
|
assert.equal(loaded.payload.program.source_hash, fnv1a64(source));
|
|
assert.equal(loaded.payload.preview.status, "verified");
|
|
assert.equal(loaded.payload.preview.source, "linuxcnc-wasm-sim_program");
|
|
assert.equal(loaded.payload.preview.line_count, 4711);
|
|
assert.equal(loaded.payload.preview.segments.length, 4684);
|
|
|
|
const power = send("POWER", { active: true });
|
|
assert.equal(power.kind, "COMMAND_RESULT");
|
|
assert.equal(Object.hasOwn(power.payload.panel_snapshot.visual, "toolpath"), false);
|
|
const home = send("HOME", { all: true });
|
|
assert.equal(home.kind, "COMMAND_RESULT");
|
|
assert.equal(Object.hasOwn(home.payload.panel_snapshot.visual, "toolpath"), false);
|
|
const run = send("PROGRAM_RUN");
|
|
assert.equal(run.kind, "COMMAND_RESULT");
|
|
assert.equal(Object.hasOwn(run.payload.panel_snapshot.visual, "toolpath"), false);
|
|
assert.equal(run.payload.xyzac_ledger.entries.some((entry) => Object.hasOwn(entry, "response")), false);
|
|
let stepped = null;
|
|
let executionTraceEntries = 0;
|
|
let runningQueueGaps = 0;
|
|
// The first long Z feed is planned at the configured LinuxCNC TP limit. Give
|
|
// the fixed-tick simulation enough servo time to complete it before checking
|
|
// that the interpreter has advanced beyond the historical line-82 boundary.
|
|
const cyclesToRun = Number(process.env.XYZAC_CHIPS_CYCLES ?? 3000);
|
|
assert.ok(Number.isSafeInteger(cyclesToRun) && cyclesToRun > 0);
|
|
for (let cycle = 0; cycle < cyclesToRun; cycle += 1) {
|
|
stepped = send("SIM_STEP", { cycles: 1, response: "motion_delta" });
|
|
assert.equal(stepped.kind, "COMMAND_RESULT");
|
|
executionTraceEntries += stepped.payload.execution_trace.length;
|
|
if (cycle > 0 && stepped.payload.program.program_status === "running" && Number(stepped.payload.wasm_motion.state.queue_depth) === 0) runningQueueGaps += 1;
|
|
}
|
|
assert.equal(stepped.kind, "COMMAND_RESULT");
|
|
assert.equal(stepped.payload.program.program_status, "running");
|
|
assert.ok(stepped.payload.program.current_line > 82);
|
|
assert.ok(executionTraceEntries > 0);
|
|
assert.ok(stepped.payload.program.execution_line_text.length > 0);
|
|
assert.equal(stepped.payload.motion_authority, "wasm");
|
|
assert.equal(Object.hasOwn(stepped.payload.xyzac_ledger.entries.at(-1), "response"), false);
|
|
assert.equal(session.timeline.snapshots.snapshots.size, 0);
|
|
assert.equal(session.ledger.audit({ maxEntries: 32 }).entries.length, 32);
|
|
assert.equal(runningQueueGaps, 0);
|
|
|
|
// Replacing a large preview must leave the worker responsive for the next
|
|
// control transaction and must publish only the new program's path.
|
|
const aborted = send("PROGRAM_ABORT");
|
|
assert.equal(aborted.kind, "COMMAND_RESULT");
|
|
const replacement = send("PROGRAM_LOAD", { source: "G21 G90\nG1 X1 Y2 F100\nM2\n", path: "replacement.ngc" });
|
|
assert.equal(replacement.kind, "COMMAND_RESULT");
|
|
assert.equal(replacement.payload.program.path, "replacement.ngc");
|
|
assert.equal(replacement.payload.panel_snapshot.visual.toolpath.length, 1);
|
|
const replacementPower = send("POWER", { active: false });
|
|
assert.equal(replacementPower.kind, "COMMAND_RESULT");
|
|
assert.equal(Object.hasOwn(replacementPower.payload.panel_snapshot.visual, "toolpath"), false);
|
|
|
|
console.log(JSON.stringify({
|
|
schema: "xyzac-3d-chips-program.v1",
|
|
status: "verified",
|
|
program_path: PROGRAM_PATH,
|
|
source_length: source.length,
|
|
source_hash: loaded.payload.program.source_hash,
|
|
line_count: loaded.payload.program.line_count,
|
|
preview_segments: loaded.payload.preview.segments.length,
|
|
cycles: cyclesToRun,
|
|
execution_line: stepped.payload.program.current_line,
|
|
execution_trace: executionTraceEntries,
|
|
running_queue_gaps: runningQueueGaps,
|
|
motion_authority: stepped.payload.motion_authority,
|
|
motion_delta_snapshots: session.timeline.snapshots.snapshots.size,
|
|
ledger_audit_window: session.ledger.audit({ maxEntries: 32 }).entries.length,
|
|
replacement_segments: replacement.payload.panel_snapshot.visual.toolpath.length,
|
|
}));
|