Files
MikrotikManager/backend/src/modules/mcp/network-diagnostic-actions.test.ts
T
Denozordec b896e469a7
Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 10m31s
Docker images / frontend-image (push) Successful in 2m18s
Docker images / updater-image (push) Successful in 39s
Docker images / netflow-agent-image (push) Successful in 31s
Docker images / backend-image (push) Successful in 2m11s
Docker images / publish-release (push) Successful in 11s
Docker images / notify-webhook (push) Successful in 2s
feat(mcp): добавить диагностику инцидентов и проверку отказоустойчивости сети
2026-11-27 17:21:20 +07:00

85 lines
5.0 KiB
TypeScript

import assert from "node:assert/strict"
import test from "node:test"
import { projectDnsState, projectFibTableState, projectRouteLookup } from "./network-diagnostic-actions.js"
const route = (prefix: string, immediateGateway: string, table = "tenant") => ({
"routing-table": table, "dst-address": prefix, active: "true",
gateway: immediateGateway, "immediate-gw": immediateGateway,
})
test("requires positive FIB evidence in the explicitly selected table", () => {
const main = { name: "main", fib: true, invalid: false }
assert.equal(projectFibTableState([main], "main"), "ready")
assert.equal(projectFibTableState([main], "tenant"), "table-not-found")
assert.equal(projectFibTableState([main, main], "main"), "partial")
assert.equal(projectFibTableState([{ ...main, fib: "" }], "main"), "partial")
assert.equal(projectFibTableState([{ ...main, fib: "false" }], "main"), "table-not-fib")
assert.equal(projectFibTableState([{ ...main, invalid: true }], "main"), "table-inactive")
})
test("looks up IPv4 by longest active prefix and preserves ECMP path multiplicity", () => {
const prefix = "203.0.113.0/24"
const result = projectRouteLookup([
route("0.0.0.0/0", "192.0.2.1%wan"),
route("203.0.113.10/32", "192.0.2.2%main", "main"),
{ ...route("203.0.113.10/32", "192.0.2.3%disabled"), disabled: "true" },
{ ...route("203.0.113.10/32", "192.0.2.4%inactive"), active: "false" },
route(prefix, "192.0.2.5%uplink,192.0.2.6%uplink"),
route(prefix, "192.0.2.5%uplink"),
], "tenant", "203.0.113.10")
assert.equal(result.status, "matched")
assert.deepEqual(result.routes.map(item => item.prefix), [prefix, prefix])
assert.deepEqual(result.nextHops, ["192.0.2.5%uplink", "192.0.2.6%uplink", "192.0.2.5%uplink"])
assert.deepEqual(result.nextHopDetails.map(hop => hop.interface), ["uplink", "uplink", "uplink"])
})
test("matches IPv6 blackhole and distinguishes absent and partial observations", () => {
const blackhole = { "routing-table": "tenant", "dst-address": "2001:db8:1::/48", active: "true", blackhole: "yes" }
const result = projectRouteLookup([route("::/0", "fe80::1%uplink"), blackhole], "tenant", "2001:db8:1:2::15")
assert.equal(result.status, "matched")
assert.equal(result.routes[0].disposition, "blackhole")
assert.deepEqual(result.nextHops, [])
assert.equal(projectRouteLookup([blackhole], "tenant", "2001:db8:2::1").status, "no-route")
assert.equal(projectRouteLookup([{ ...blackhole, active: undefined }], "tenant", "2001:db8:1::1").status, "partial")
assert.equal(projectRouteLookup([route("::/0", "")], "tenant", "2001:db8:1::1").status, "partial")
assert.equal(projectRouteLookup([route("::/0", "fe80::1%uplink"), { ...blackhole, "dst-address": "2001:db8:1::/129" }], "tenant", "2001:db8:1::1").status, "partial")
assert.equal(projectRouteLookup([{ ...blackhole, blackhole: "" }], "tenant", "2001:db8:1::1").routes[0].disposition, "blackhole")
assert.equal(projectRouteLookup([route("::/0", "bridge wan")], "tenant", "2001:db8:1::1").nextHopDetails[0].interface, "bridge wan")
})
test("bounds ECMP output and exposes omissions as partial", () => {
const rows = Array.from({ length: 129 }, () => route("0.0.0.0/0", "192.0.2.1%uplink"))
const result = projectRouteLookup(rows, "tenant", "203.0.113.10")
assert.equal(result.status, "partial")
assert.equal(result.nextHops.length, 128)
})
test("retains duplicate native array nexthops across active default rows", () => {
const defaults = Array.from({ length: 2 }, () => ({
"routing-table": "main", "dst-address": "0.0.0.0/0", active: true,
gateway: ["192.0.2.1"], "immediate-gw": ["192.0.2.1%ether1"],
}))
const result = projectRouteLookup(defaults, "main", "203.0.113.10")
assert.equal(result.status, "matched")
assert.deepEqual(result.nextHops, ["192.0.2.1%ether1", "192.0.2.1%ether1"])
assert.deepEqual(result.routes.map(item => item.gateways), [["192.0.2.1"], ["192.0.2.1"]])
assert.equal(projectRouteLookup([{ ...defaults[0], "immediate-gw": ["192.0.2.1%ether1", 1] }], "main", "203.0.113.10").status, "partial")
})
test("DNS reports ordered upstream addresses and projects DoH to a boolean", () => {
assert.deepEqual(projectDnsState({
"allow-remote-requests": "yes", servers: "192.0.2.53@tenant,2001:db8::53", "dynamic-servers": "",
"use-doh-server": "https://example.invalid/dns-query?secret=private", "cache-used": "private",
}), {
allowRemoteRequests: true, staticServers: ["192.0.2.53@tenant", "2001:db8::53"],
dynamicServers: [], dohEnabled: true, incomplete: false,
})
assert.deepEqual(projectDnsState({ "allow-remote-requests": false, servers: "", "use-doh-server": "" }), {
allowRemoteRequests: false, staticServers: [], dynamicServers: null, dohEnabled: false, incomplete: true,
})
assert.deepEqual(projectDnsState([{ "allow-remote-requests": false, servers: ["192.0.2.53", "2001:db8::53"], "dynamic-servers": [], "use-doh-server": "" }]), {
allowRemoteRequests: false, staticServers: ["192.0.2.53", "2001:db8::53"], dynamicServers: [], dohEnabled: false, incomplete: false,
})
assert.throws(() => projectDnsState([{}, {}]))
})