Files
linuxcncWebWASM/web/worker/test_xyzac_3d_chips.mjs

72 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 { 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);
assert.equal(send("POWER", { active: true }).kind, "COMMAND_RESULT");
assert.equal(send("HOME", { all: true }).kind, "COMMAND_RESULT");
const run = send("PROGRAM_RUN");
assert.equal(run.kind, "COMMAND_RESULT");
let stepped = null;
for (let cycle = 0; cycle < 300; cycle += 1) {
stepped = send("SIM_STEP", { cycles: 1, response: "motion_delta" });
assert.equal(stepped.kind, "COMMAND_RESULT");
}
assert.equal(stepped.kind, "COMMAND_RESULT");
assert.equal(stepped.payload.program.program_status, "running");
assert.ok(stepped.payload.program.current_line > 82);
assert.ok(stepped.payload.execution_trace.length > 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);
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,
execution_line: stepped.payload.program.current_line,
execution_trace: stepped.payload.execution_trace.length,
motion_authority: stepped.payload.motion_authority,
}));