Files
MikrotikManager/backend/src/modules/mcp/ospf-legacy-source.test.ts
T

70 lines
6.8 KiB
TypeScript

import assert from "node:assert/strict"
import { createHmac } from "node:crypto"
import { LegacyOspfSourceCapture, type LegacyOspfPrefix } from "./ospf-legacy-source.js"
import type { LegacyOspfPreparation } from "../../services/ospf-legacy-optimization.js"
const key = Buffer.alloc(32, 7)
const digest = (value: string) => createHmac("sha256", key).update(value).digest("hex")
const plan: LegacyOspfPreparation = { serverId: 1, serverName: "fixture", pingWeight: 60, interfaces: [
{ id: "*1", interface: "gre-a", currentCost: 30, optimalCost: 10, score: 80, pingMs: 10, dlMbps: 100, ulMbps: 90 },
{ id: "*2", interface: "gre-b", currentCost: 40, optimalCost: 20, score: 70, pingMs: 20, dlMbps: 90, ulMbps: 80 },
] }
const templates = [{ ".id": "*1", interfaces: "gre-a", cost: "30", disabled: "false", opaque: "native-kept" }, { ".id": "*2", interfaces: "gre-b", cost: "40", disabled: "false" }]
const neighbors = [{ ".id": "*n1", "router-id": "10.0.0.2", interface: "gre-a", state: "Full", uptime: "1m" }, { ".id": "*n2", "router-id": "10.0.0.3", interface: "gre-a", state: "Full", uptime: "2m" }]
const source = [{ id: 1, password: "NEVER_PUBLISH_SQL_PASSWORD", polledAt: new Date("2026-10-23T00:00:00Z") }]
function collect(prefix?: LegacyOspfPrefix, edits: { native?: typeof templates; neighbors?: typeof neighbors; source?: unknown; ranked?: LegacyOspfPreparation; routeMetric?: string } = {}) {
const capture = new LegacyOspfSourceCapture(digest, prefix)
capture.native(1, "/routing/ospf/interface-template", null, edits.native ?? templates)
capture.native(1, "/routing/ospf/neighbor", null, edits.neighbors ?? neighbors)
capture.native(1, "/ip/route", null, [{ ".id": "*r1", metric: edits.routeMetric ?? "10" }])
for(const path of ["/routing/ospf/area", "/routing/ospf/instance", "/routing/bfd/session", "/ipv6/route"])capture.native(1,path,null,[])
capture.native(1, "/routing/ospf/interface-template/get", { ".id": "*1", "value-name": "disabled" }, { ret: "false" })
capture.source("enabled-servers", edits.source ?? source)
capture.source("latency:1", [{latencyMs:10,polledAt:new Date("2026-10-23T00:00:00Z")}])
capture.source("traffic:1", [])
capture.source("speed-probes:1", [])
return capture.finish(edits.ranked ?? plan)
}
const before = collect()
assert.equal(collect(undefined, { routeMetric: "99", neighbors: neighbors.map(row => ({ ...row, uptime: "5m" })) }).proof, before.proof)
assert.equal(collect(undefined, { native: [...templates].reverse() }).proof, before.proof)
assert.notEqual(collect(undefined, { neighbors: [...neighbors].reverse() }).proof, before.proof, "Equal-state native order is a ranking input")
assert.notEqual(collect(undefined, { source: [{ ...source[0], password: "changed" }] }).proof, before.proof)
assert.equal(collect(undefined, { source: [{ ...source[0], password: "changed" }] }).nativeProof, before.nativeProof)
assert.notEqual(collect(undefined, { native: templates.map(row => ({ ...row, opaque: "changed" })) }).proof, before.proof)
assert.ok(!JSON.stringify(before).includes(source[0].password))
const progressed = { ...plan, interfaces: plan.interfaces.map((row, index) => ({ ...row, currentCost: index === 0 ? row.optimalCost : row.currentCost })) }
const prefix = { baseline: plan, completedCount: 1 }
const nativeProgressed = templates.map((row, index) => ({ ...row, cost: index === 0 ? "10" : row.cost }))
assert.equal(collect(prefix, { native: nativeProgressed, ranked: progressed }).proof, before.proof)
assert.throws(() => collect(prefix), /не подтверждены/)
assert.throws(() => collect(prefix, { native: templates.map(row => ({ ...row, cost: String(plan.interfaces.find(item => item.id === row[".id"])!.optimalCost) })), ranked: progressed }), /не подтверждены/, "Future writes must not be accepted as completed prefix")
assert.throws(() => collect(prefix, { native: nativeProgressed, ranked: { ...progressed, interfaces: progressed.interfaces.slice(0, 1) } }), /не подтверждены/)
assert.notEqual(collect(prefix, { native: nativeProgressed.map(row => ({ ...row, opaque: "collateral" })), ranked: progressed }).proof, before.proof)
assert.throws(() => new LegacyOspfSourceCapture(digest, { baseline: plan, completedCount: 3 }))
const inverseBaseline={...plan,interfaces:plan.interfaces.map(row=>({...row,currentCost:row.optimalCost}))}
const inverseNative=templates.map((row,index)=>({...row,cost:String(plan.interfaces[index].optimalCost)}))
const inverseBefore=collect(undefined,{native:inverseNative,ranked:inverseBaseline})
const inverseTargets=Object.fromEntries(plan.interfaces.map(row=>[row.id,row.currentCost]))
const inverseRank={...inverseBaseline,interfaces:inverseBaseline.interfaces.map((row,index)=>({...row,currentCost:index===0?plan.interfaces[0].currentCost:row.currentCost}))}
const inverseStep=collect({baseline:inverseBaseline,completedCount:1,targets:inverseTargets},{native:inverseNative.map((row,index)=>({...row,cost:index===0?"30":row.cost})),ranked:inverseRank})
assert.equal(inverseStep.proof,inverseBefore.proof);assert.equal(inverseStep.nativeProof,inverseBefore.nativeProof)
assert.throws(()=>new LegacyOspfSourceCapture(digest,{baseline:inverseBaseline,completedCount:0,targets:{"*1":30}}))
for (const [path, payload] of [["/routing/ospf/interface-template", [templates[0], templates[0]]], ["/routing/ospf/interface-template", null], ["/unexpected", []], ["/ip/route", Array.from({ length: 10001 }, (_, index) => ({ ".id": String(index) }))]] as const) {
assert.throws(() => new LegacyOspfSourceCapture(digest).native(1, path, null, payload))
}
const duplicate = new LegacyOspfSourceCapture(digest)
duplicate.source("latency:1", [{ latencyMs: 10 }])
assert.throws(() => duplicate.source("latency:1", [{ latencyMs: 11 }]), /не подтверждены/, "Intra-capture drift must fail closed")
assert.throws(() => new LegacyOspfSourceCapture(digest).source("latency:1", [{}, {}]))
assert.throws(() => new LegacyOspfSourceCapture(digest).source("traffic:1", [{ data: "x".repeat(1048577) }]))
assert.throws(() => new LegacyOspfSourceCapture(digest).source("traffic:1", Array.from({length:2001},()=>({}))))
assert.throws(() => new LegacyOspfSourceCapture(digest).native(1, "/routing/ospf/interface-template/set", { cost: "10" }, {}))
const closed = new LegacyOspfSourceCapture(digest)
assert.throws(()=>closed.finish(plan), /не подтверждены/, "Missing source closure must fail")
for(const path of ["/routing/ospf/area", "/routing/ospf/instance", "/routing/bfd/session", "/ipv6/route", "/ip/route", "/routing/ospf/neighbor", "/routing/ospf/interface-template"])closed.native(1,path,null,[])
closed.finish({...plan,interfaces:[]})
assert.throws(() => closed.source("latency:1", []))
assert.throws(() => closed.finish(plan))
console.log("Legacy OSPF source proof: keyed SQL/native/rank capture, actual prefix costs, opaque configuration and neighbor tie order, bounded inputs, intra-read drift and no raw secrets")