import assert from "node:assert/strict" import { setTimeout as delay } from "node:timers/promises" import { MikrotikClient } from "./mikrotik.js" import { dispatchTerminalCommand, executeTerminalDiagnostic, parseTerminalCommand, terminalDiagnosticSchema } from "./terminal-diagnostics.js" import { withTerminalDiagnosticClient } from "./terminal-diagnostic-client.js" import { createTerminalDiagnosticFixture, terminalDiagnosticCases } from "../test/terminal-diagnostic-fixture.js" const fixture = await createTerminalDiagnosticFixture(), endpoint = { host: "127.0.0.1", port: fixture.port, useSsl: false, verifySsl: false, username: "fixture", password: "NEVER_TERMINAL_CREDENTIAL" } let checks = 0, allowed = true const authorize = async () => { checks++; if (!allowed) throw Error("NEVER_TERMINAL_AUTHORITY_DETAIL") } const client = new MikrotikClient(endpoint) try { for (const [raw, expected] of terminalDiagnosticCases) { const parsed = parseTerminalCommand(raw); assert.equal(parsed.diagnostic, expected) const before = fixture.requests.length const bounded = await withTerminalDiagnosticClient(endpoint, { authorize }, native => executeTerminalDiagnostic(native, parsed)).catch(error => { throw new Error(`Terminal parity branch ${expected}`, { cause: error }) }) assert.equal(bounded, await dispatchTerminalCommand(client, raw), raw) if (expected === "help") assert.equal(fixture.requests.length, before) } assert.ok(checks > fixture.requests.length) assert.deepEqual(fixture.requests.filter(row => row.path === "/tool/ping").at(-1)?.body, { address: "192.0.2.1", count: "20", interval: "0.2s" }) for (const bad of [{ diagnostic: "execute", command: "/system reboot" }, { diagnostic: "interfaces", target: "host" }, { diagnostic: "logs", count: 2 }, { diagnostic: "ping" }, { diagnostic: "ping", target: "192.0.2.1", count: 21 }, { diagnostic: "logs", limit: 201 }]) assert.equal(terminalDiagnosticSchema.safeParse(bad).success, false) for (const bad of ["/system reboot", ":execute /system resource print", "ip address print;system reboot", "log print limit=0", "ping host count=1junk"]) assert.throws(() => parseTerminalCommand(bad)) const oldInterfaces = fixture.payloads["/interface"] fixture.payloads["/interface"] = Array.from({ length: 1001 }, () => ({ name: "x" })) await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize }, native => native.getInterfaces())) fixture.payloads["/interface"] = [{ name: "x".repeat(1048577) }] await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize }, native => native.getInterfaces()), error => error instanceof Error && !error.message.includes(endpoint.password)) fixture.payloads["/interface"] = oldInterfaces fixture.setBeforeResponse(async (_request, _response, path) => { if (path === "/routing/ospf/interface-template") allowed = false }) const beforeRevoke = fixture.requests.length await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize }, native => native.getOspfInterfaceTemplates())) assert.equal(fixture.requests.length, beforeRevoke + 1, "Revocation after native response prevents flag getter/print dispatch") allowed = true let disconnected!: () => void const closed = new Promise(resolve => { disconnected = resolve }) fixture.setBeforeResponse(async (request, response) => { response.writeHead(200); response.write("["); request.socket.once("close", disconnected); return true }) const cancellation = new AbortController() const cancelled = withTerminalDiagnosticClient(endpoint, { authorize, signal: cancellation.signal }, native => native.getInterfaces()) const rejected = assert.rejects(cancelled) await delay(20); cancellation.abort(); await rejected; await closed const timed = withTerminalDiagnosticClient(endpoint, { authorize, timeoutMs: 25 }, native => native.getInterfaces()) await assert.rejects(timed) const beforeAlreadyCancelled = fixture.requests.length, stopped = new AbortController(); stopped.abort() await assert.rejects(withTerminalDiagnosticClient(endpoint, { authorize, signal: stopped.signal }, native => native.getInterfaces())) assert.equal(fixture.requests.length, beforeAlreadyCancelled) console.log("Typed terminal: all REST branches/aliases exact formatted parity; inherited OSPF/resource semantics, clamps, no CLI, bounded rows/bytes, cancellation/deadline and late authority fences") } finally { await fixture.close() }