Files
linuxcncWebWASM/web/worker/test_machine_families_s8_08.mjs

63 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 { compareEndpoints, createEndpoint, endpointHash } from "./endpoint-replay.mjs";
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
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 CASES = [
{ path: "linuxcnc/configs/sim/axis/ldelta.ini", type: "lineardelta", pose: [0, 0, 64.636584194], joints: [300, 300, 300] },
{ path: "linuxcnc/configs/sim/axis/rdelta.ini", type: "rotarydelta", pose: [0, 0, -3.793360169], joints: [10, 10, 10] },
];
const wasmBinary = await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url));
async function runCase(entry, fast) {
const bundle = await loadSimulationCase({ path: entry.path, fetchImpl });
assert.equal(bundle.status, "ready");
const configuration = toWorkerConfiguration(bundle);
assert.equal(configuration.kinematics.type, entry.type);
const core = await createModule({ wasmBinary });
const session = new SimSession(core, { sessionId: `${entry.type}-${fast ? "fast" : "1x"}` });
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
let seq = 0;
const send = (kind, operationId, payload = {}) => {
const response = session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
assert.notEqual(response.kind, "ERROR", `${entry.path}:${kind}:${response.payload?.code ?? "error"}`);
return response;
};
send("HELLO", "hello"); send("SESSION_ATTACH", "attach");
const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration });
assert.equal(loaded.payload.configuration.state, "TASK_READY");
assert.equal(loaded.payload.configuration.hal.blocked_count, 0);
send("PROGRAM_LOAD", "program", { source: `G21 G90\nG0 X${entry.pose[0]} Y${entry.pose[1]} Z${entry.pose[2]}\nM2\n`, path: `${entry.type}.ngc` });
send("PROGRAM_RUN", "run");
if (fast) send("SIM_STEP", "fast", { cycles: 8 }); else for (let index = 0; index < 8; index += 1) send("SIM_STEP", `step-${index}`, { cycles: 1 });
const endpoint = createEndpoint({ session });
assert.equal(endpoint.authoritative.kinematics.type, entry.type);
entry.joints.forEach((expected, index) => assert.ok(Math.abs(endpoint.authoritative.kinematics.joints[index] - expected) < 1e-6));
const checkpoint = session.createCheckpoint();
send("SIM_STEP", "post-checkpoint", { cycles: 1 });
const continuous = createEndpoint({ session });
const recovery = session.restoreCheckpoint(checkpoint, session.timeline.export(), (operation) => operation);
assert.equal(recovery.status, "recovered");
const recoveryParity = compareEndpoints(continuous, createEndpoint({ session }));
assert.equal(recoveryParity.equal, true, JSON.stringify(recoveryParity));
return { path: entry.path, type: entry.type, startup: loaded.payload.configuration.state, joints: endpoint.authoritative.kinematics.joints, endpoint_hash: endpointHash(endpoint), mode_parity: null, recovery: recoveryParity, checkpoint_hash: checkpoint.checkpoint_hash };
}
const cases = [];
for (const entry of CASES) {
const oneX = await runCase(entry, false);
const fast = await runCase(entry, true);
// The endpoint is deterministic across 1x/fast; rerun captures the same nominal family golden.
const modeParity = { equal: oneX.endpoint_hash === fast.endpoint_hash, expected_hash: oneX.endpoint_hash, actual_hash: fast.endpoint_hash };
assert.equal(modeParity.equal, true);
cases.push({ ...oneX, mode_parity: modeParity, fast_endpoint_hash: fast.endpoint_hash });
}
console.log(JSON.stringify({ schema: "machine-families-s8-08-sample.v1", status: "verified", cases, boundary: "Delta lineardelta/rotarydelta only; official kinematics oracle remains separately blocked; other machine families queued" }));