fix(ui): учитывать область в стоимости соседа OSPF

This commit is contained in:
Denozordec committed 2026-09-30 18:09:10 +07:00
1 parent 2199950681
commit b1080b0a05
6 files changed
+54 -9

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
import assert from "node:assert/strict"
import { test } from "node:test"
import { buildOspfCostIndex, ospfCostKey } from "./ospf-cost"
const iface = { serverId: 1, interface: "ether1", areaId: "0.0.0.0", cost: 7 }
test("neighbor cost is scoped to device, interface and area", () => {
const index = buildOspfCostIndex([iface, { ...iface, areaId: "0.0.0.1", cost: 42 }, { ...iface, serverId: 2, cost: 9 }])
assert.equal(index.get(ospfCostKey(1,"ether1","0.0.0.0")), 7)
assert.equal(index.get(ospfCostKey(1,"ether1","0.0.0.1")), 42)
assert.equal(index.get(ospfCostKey(2,"ether1","0.0.0.0")), 9)
assert.equal(index.get(ospfCostKey(3,"ether1","0.0.0.0")), undefined)
})
test("conflicting or invalid template costs do not choose a fabricated value", () => {
const key = ospfCostKey(1,"ether1","0.0.0.0")
assert.equal(buildOspfCostIndex([iface, iface]).get(key), 7)
assert.equal(buildOspfCostIndex([iface, { ...iface, cost: 42 }, iface]).get(key), null)
for (const cost of [0, -1, 65536, 1.5]) assert.equal(buildOspfCostIndex([{ ...iface, cost }]).get(key), null)
})
+11
View File
@@ -0,0 +1,11 @@
type InterfaceCost = { serverId: number; interface: string; areaId: string; cost: number }
export const ospfCostKey = (serverId: number, iface: string, areaId: string) => JSON.stringify([serverId, iface, areaId])
export function buildOspfCostIndex(interfaces: InterfaceCost[]): Map<string, number | null> {
const result = new Map<string, number | null>()
for (const iface of interfaces) {
const key = ospfCostKey(iface.serverId, iface.interface, iface.areaId)
const cost = Number.isInteger(iface.cost) && iface.cost >= 1 && iface.cost <= 65535 ? iface.cost : null
result.set(key, result.has(key) && result.get(key) !== cost ? null : cost)
}
return result
}