Files
MikrotikManager/backend/src/modules/mcp/resilience-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

99 lines
5.3 KiB
TypeScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing"
import { assessFailover, routePaths, summarizePaths } from "./resilience-actions.js"
const first = { id: "a", wanInterface: "ether1", jumpHostId: 2, exitNodeId: 3,
downstreamInterface: "wg-a", gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*a" } }
const second = { ...first, id: "b", jumpHostId: 4, exitNodeId: 5, downstreamInterface: "wg-b" }
function topology(reserve: typeof second) {
const profile = routeAiProfileSchema.parse({ name: "failover", community: "65000:1", family: "ipv4", serverId: 1, candidates: [first, reserve] })
return summarizePaths(routePaths(profile))
}
test("different jump and exit nodes still share a WAN on the origin router", () => {
const result = topology(second)
assert.deepEqual(result.sharedDomains, [
{ kind: "source", serverId: 1, pathIds: ["a", "b"] },
{ kind: "wan", serverId: 1, interfaceName: "ether1", pathIds: ["a", "b"] },
])
assert.equal(result.physicalIndependenceVerified, false)
})
test("logically disjoint WAN, jump and exit nodes retain an unknown physical domain", () => {
const result = topology({ ...second, wanInterface: "ether2" })
assert.deepEqual(result.sharedDomains, [{ kind: "source", serverId: 1, pathIds: ["a", "b"] }])
assert.ok(result.unknownDomains.includes("carrier"))
assert.equal(result.physicalIndependenceVerified, false)
})
test("pinned profiles retain the other path for inspection and exclude it from eligible reserve", () => {
const profile = routeAiProfileSchema.parse({
name: "pinned", community: "65000:1", family: "ipv4", serverId: 1, pinnedCandidateId: "a",
candidates: [{ ...first, wanRemoteInterface: "wan-up", downstreamRemoteInterface: "wg-remote" }, second],
})
const result = summarizePaths(routePaths(profile))
assert.deepEqual(result.eligiblePathIds, ["a"])
assert.deepEqual(result.sharedDomains, [])
assert.deepEqual(result.paths[0].dependencies.slice(-2), [
{ kind: "wan", serverId: 2, interfaceName: "wan-up" },
{ kind: "tunnel", serverId: 3, interfaceName: "wg-remote" },
])
})
test("separate IX groups with a single eligible path do not form a reserve pair", () => {
const profile = routeAiProfileSchema.parse({ name: "failover", community: "65000:1", family: "ipv4", serverId: 1, candidates: [first, second] })
const paths = routePaths(profile).map((path, index) => ({ ...path, groupId: `${index + 2}/exit` }))
assert.deepEqual(summarizePaths(paths).alternativeGroups, [
{ groupId: "2/exit", eligiblePathIds: ["a"] },
{ groupId: "3/exit", eligiblePathIds: ["b"] },
])
})
const time = (offset: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, 0, offset)).toISOString()
const request = {
target: { kind: "route-ai" as const, profileId: "1551445f-7c71-4411-976f-ce30302dcefe" },
window: { from: time(0), outageAt: time(3000), restoreAt: time(10000), to: time(20000) },
controlProbe: { primaryCandidateId: "a", backupCandidateId: "b" },
targetFailoverMs: 1000, targetFailbackMs: 1000,
}
const probe = (candidateId: string, status: "passed" | "failed" | "unknown", begin: number, end: number) => ({
id: candidateId + begin, candidateId, status, startedAt: time(begin), observedAt: time(end),
})
const baseline = probe("a", "passed", 2500, 2700)
const failure = probe("a", "failed", 3100, 3300)
const recovered = probe("a", "passed", 10100, 10500)
test("report passes each probe deadline and leaves HTTPS, SSH and existing connections unverified", () => {
const result = assessFailover(request, [baseline, failure, probe("b", "failed", 3100, 3400), probe("b", "passed", 3500, 3800), recovered])
assert.equal(result.status, "passed")
assert.equal(result.failover.lowerBoundMs, 100)
assert.equal(result.failover.upperBoundMs, 800)
assert.equal(result.failover.firstGood?.id, "b3500")
assert.equal(result.failback.upperBoundMs, 500)
assert.equal(result.https, "inconclusive")
assert.equal(result.serviceFailover, "inconclusive")
assert.equal(result.existingConnections, "inconclusive")
assert.equal(result.managementSsh, "inconclusive")
assert.equal(result.returnPathVerified, false)
})
test("report fails when the latest definitive failed probe started after the deadline", () => {
const result = assessFailover(request, [baseline, failure, probe("b", "failed", 4500, 4700), probe("b", "passed", 4800, 5000), recovered])
assert.equal(result.status, "failed")
assert.equal(result.failover.lowerBoundMs, 1500)
assert.equal(result.failover.upperBoundMs, 2000)
assert.equal(result.failover.lastBad?.id, "b4500")
})
test("late first success, unknown evidence, flap, or truncated history cannot establish success", () => {
const history = [baseline, failure, recovered]
assert.equal(assessFailover(request, [...history, probe("b", "passed", 4500, 4700)]).status, "inconclusive")
assert.equal(assessFailover(request, [...history, probe("b", "unknown", 4500, 4700)]).status, "inconclusive")
const pass = probe("b", "passed", 3500, 3700)
assert.equal(assessFailover(request, [...history, pass, probe("b", "failed", 3800, 4000)]).status, "inconclusive")
assert.equal(assessFailover(request, [...history, pass], true).status, "inconclusive")
assert.equal(assessFailover(request, [baseline, probe("a", "unknown", 3100, 3300), recovered, pass]).status, "inconclusive")
})