import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { DESTINATIONS, LIMITS, REFUSALS, ALLOWED_PORTS, PROTOCOL_VERSION } from "../src/spec.js"; const source = readFileSync( fileURLToPath(new URL("../../../devplacepy/services/nodes/spec.py", import.meta.url)), "utf8", ); function evaluate(expression) { const clean = expression.replaceAll("_", "").trim(); assert.match(clean, /^[\d\s*]+$/, expression); return clean.split("*").reduce((product, part) => product * Number(part), 1); } test("baked allowlist matches the server specification", () => { const suffixes = [...source.matchAll(/DestinationSpec\("([^"]+)"/g)].map((match) => match[1]); assert.deepEqual(DESTINATIONS.map((item) => item.suffix), suffixes); }); test("baked ports, protocol and refusal codes match the server specification", () => { const ports = /ALLOWED_PORTS = \(([^)]*)\)/.exec(source)[1].split(",").filter(Boolean).map(Number); assert.deepEqual([...ALLOWED_PORTS], ports); assert.equal(PROTOCOL_VERSION, Number(/PROTOCOL_VERSION = (\d+)/.exec(source)[1])); const codes = [...source.matchAll(/RefusalSpec\("([a-z_]+)"/g)].map((match) => match[1]); assert.deepEqual(Object.values(REFUSALS).sort(), codes.sort()); }); test("baked limits match the server LimitSpec defaults", () => { for (const [key, value] of Object.entries(LIMITS)) { const pattern = new RegExp(`^\\s+${key}: int = ([\\d_ *]+)$`, "m"); const match = pattern.exec(source); assert.ok(match, `${key} missing from spec.py`); assert.equal(value, evaluate(match[1]), key); } });