Files
linuxcncWebWASM/web/worker/test_trt_switch_stationary.mjs

68 lines
5.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile, writeFile } from 'node:fs/promises';
import createModule from '../../build-wasm/linuxcnc-sim-core.js';
import { SimSession, CONTEXT_ID } from './sim-session.mjs';
import { loadSimulationCase, toWorkerConfiguration } from '../workspace/sim-case-loader.mjs';
import { writeTrtGeometry } from '../workspace/trt-geometry.mjs';
import { MACHINE_DEFINITIONS } from '../workspace/machine-definitions.mjs';
const root = new URL('../../', import.meta.url), output = new URL('coverage/glb-machine/switching/', root);
const fetchImpl = async (url) => { try { const body = await readFile(new URL(String(url).replace(/^\//, ''), root), 'utf8'); return { ok: true, text: async () => body }; } catch { return { ok: false }; } };
const report = { report_id: 'trt-stationary-switch-worker-v1', status: 'running', machines: [] };
const closeJoints = (actual, expected, label) => actual.forEach((v, i) => assert.ok(Math.abs(v - expected[i]) < 1e-8, `${label} J${i}: ${v} != ${expected[i]}`));
try {
for (const definition of MACHINE_DEFINITIONS) {
const core = await createModule({ wasmBinary: await readFile(new URL('build-wasm/linuxcnc-sim-core.wasm', root)) });
const session = new SimSession(core, { sessionId: `switch-${definition.id}` }); let seq = 0;
const send = (kind, payload = {}, expected = 'COMMAND_RESULT') => {
const r = session.accept({ schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId, kind, seq: ++seq, operation_id: `switch-${seq}`, payload });
assert.equal(r.kind, expected, `${kind}: ${JSON.stringify(r.payload)}`); return r;
};
send('HELLO', {}, 'CAPABILITIES'); send('SESSION_ATTACH', {}, 'SESSION_READY');
const offsets = definition.coordinates === 'XYZAC' ? { x_rot_point: 24, y_rot_point: -12, z_rot_point: 8, y_offset: 18, z_offset: -45 } : { x_rot_point: -18, y_rot_point: 14, z_rot_point: 12, x_offset: 22, z_offset: -40 };
const bundle = writeTrtGeometry(await loadSimulationCase({ path: definition.entrypoint, fetchImpl }), offsets);
send('LOAD_WORKSPACE', { root_path: '/workspace', configuration: toWorkerConfiguration(bundle) });
send('POWER', { active: true }); send('HOME', { all: true });
for (let i = 0; i < 500 && !session.wasmHomeLimit.read().state.homed; i++) send('SIM_STEP', { cycles: 1 });
assert.ok(session.wasmHomeLimit.read().state.homed);
const drain = () => { for (let i = 0; i < 5000 && session.wasmMotion.read().state.queue_depth; i++) send('SIM_STEP', { cycles: 1 }); assert.equal(session.wasmMotion.read().state.queue_depth, 0); };
const rows = [];
for (const [x, y, z, tilt, c] of [[0, 0, 44.5858, 4.8675, 179.88], [190, -95, 110, 40, 175]]) {
send('KINEMATICS_SELECT', { type: 0 });
send('MDI_EXECUTE', { block: `G21 G90 G0 X${x} Y${y} Z${z} ${definition.coordinates[3]}${tilt} C${c}` }); drain();
const baseline = session.wasmMotion.read().state.joint_commanded;
for (const kind of ['KINEMATICS_SELECT', 'MDI_EXECUTE']) for (const type of [1, 0, 1, 1, 0, 2, 1, 0]) {
const before = session.wasmMotion.read().state;
const result = send(kind, kind === 'KINEMATICS_SELECT' ? { type } : { block: `M${type === 1 ? 428 : type === 0 ? 429 : 430}` });
const current = session.wasmMotion.read().state;
closeJoints(current.joint_commanded, baseline, `${kind} mode=${type}`);
closeJoints(result.payload.panel_snapshot.visual.joints, baseline, 'visual');
closeJoints(result.payload.kinematics.joints, baseline, 'kinematics');
assert.equal(current.active_tag, type); assert.equal(current.queue_depth, 0);
assert.deepEqual(session.xyzacProgram.synchronizedPosition, current.position, 'interpreter must share new frame');
for (let i = 0; i < 3; i++) { send('SIM_STEP', { cycles: 1 }); closeJoints(session.wasmMotion.read().state.joint_commanded, baseline, 'idle tick'); }
rows.push({ kind, type, baseline, joints: current.joint_commanded, previous_pose: before.position, pose: current.position });
}
}
send('KINEMATICS_SELECT', { type: 0 });
send('MDI_EXECUTE', { block: `G21 G90 G0 X10 Y5 Z20 ${definition.coordinates[3]}20 C60` }); drain();
send('MDI_EXECUTE', { block: 'G10 L2 P1 X3 Y-2 Z4' });
send('KINEMATICS_SELECT', { type: 1 });
const beforeIncrement = session.wasmMotion.read().state.position;
send('MDI_EXECUTE', { block: 'G91 G1 X1 F600' }); drain();
const afterIncrement = session.wasmMotion.read().state.position;
assert.ok(Math.abs(afterIncrement.x - beforeIncrement.x - 1) < 1e-8);
for (const key of ['y', 'z', 'a', 'b', 'c']) assert.ok(Math.abs(afterIncrement[key] - beforeIncrement[key]) < 1e-8);
send('MDI_EXECUTE', { block: 'G91 G1 X10 F60' });
const activeBefore = session.wasmMotion.read().state;
send('KINEMATICS_SELECT', { type: 0 }, 'ERROR');
assert.equal(session.xyzacKinematics.activeMode, 1); closeJoints(session.wasmMotion.read().state.joint_commanded, activeBefore.joint_commanded, 'blocked switch');
drain();
const beforeInvalid = session.wasmMotion.read().state;
send('KINEMATICS_SELECT', { type: 9 }, 'ERROR'); closeJoints(session.wasmMotion.read().state.joint_commanded, beforeInvalid.joint_commanded, 'invalid switch');
report.machines.push({ coordinates: definition.coordinates, rows, beforeIncrement, afterIncrement, busy_and_invalid_rejected: true });
}
report.status = 'verified';
} catch (error) { report.status = 'failed'; report.error = String(error.stack ?? error); throw error; }
finally { await writeFile(new URL('worker.report.json', output), JSON.stringify(report, null, 2) + '\n'); }
console.log(JSON.stringify({ report_id: report.report_id, status: report.status, switches: report.machines.reduce((n, m) => n + m.rows.length, 0) }));