// Drift check: machine-kinematics-specs.json must match the limits declared in // the LinuxCNC INIs. A null limit means the source declares none; inventing a // number here would put a fabricated value in front of the operator. import assert from "node:assert/strict"; import { readFileSync, existsSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { MACHINE_DEFINITIONS } from "./machine-definitions.mjs"; // This file lives in web/workspace/, so the repo root is two levels up. const root = resolve(new URL("../..", import.meta.url).pathname); const specs = JSON.parse(readFileSync(join(root, "web/workspace/machine-kinematics-specs.json"), "utf8")); assert.equal(specs.schema, "linuxcnc.machine-kinematics-specs/1"); function readCase(entrypoint) { const path = join(root, entrypoint); if (!existsSync(path)) return null; let text = readFileSync(path, "utf8"); for (const match of text.matchAll(/^\s*(?:#)?INCLUDE\s+(\S+)/gim)) { const include = join(dirname(path), match[1]); if (existsSync(include)) text += `\n${readFileSync(include, "utf8")}`; } return text; } // `m` alone makes `$` match at the first line end, which truncated every body. // The lookahead uses a "no more input" test instead of `$` so the section runs // to the next header or to the end of the file. const section = (text, name) => { const match = text.match(new RegExp(`^\\[${name}\\]\\s*(?:\\r?\\n)([\\s\\S]*?)(?=^\\[|(?![\\s\\S]))`, "im")); return match ? match[1] : null; }; const value = (body, key) => body?.match(new RegExp(`^\\s*${key}\\s*=\\s*(\\S+)`, "im"))?.[1] ?? null; const generic = MACHINE_DEFINITIONS.filter((machine) => machine.runtime === "generic-wasm"); assert.equal(specs.cases.length, generic.length, "每个通用案例都应有规格"); for (const machine of generic) { const spec = specs.cases.find((item) => item.machineId === machine.id); assert.ok(spec, `${machine.id} 缺少规格`); assert.equal(spec.jointCount, machine.joint_count, `${machine.id} 槽位数`); assert.equal(spec.coordinates, machine.coordinates, `${machine.id} 坐标`); assert.equal(spec.jointOrder.length, machine.joint_count, `${machine.id} 槽位键数量`); assert.equal(spec.limitsByJoint.length, machine.joint_count, `${machine.id} 限位数量`); const text = readCase(machine.entrypoint); assert.ok(text, `${machine.id} 的 INI 不存在:${machine.entrypoint}`); for (let index = 0; index < machine.joint_count; index += 1) { const body = section(text, `JOINT_${index}`); const lower = value(body, "MIN_LIMIT"), upper = value(body, "MAX_LIMIT"); const expected = lower !== null && upper !== null && Number.isFinite(Number(lower)) && Number.isFinite(Number(upper)) ? [Number(lower), Number(upper)] : null; assert.deepEqual(spec.limitsByJoint[index], expected, `${machine.id} J${index} 限位与 INI 不一致(spec=${JSON.stringify(spec.limitsByJoint[index])} ini=${JSON.stringify(expected)})`); } } console.log(JSON.stringify({ schema: "machine-kinematics-specs.v1", status: "verified", cases: specs.cases.length, checks: ["槽位数与运行目录一致", "逐关节限位与 INI 完全一致", "INI 未声明的限位保持 null"] }));