Files
MikrotikManager/backend/src/services/recursive-route-identity.test.ts
T

22 lines
2.3 KiB
TypeScript

import assert from "node:assert/strict"
import { planRecursiveIdentities as plan, type RecursiveIdentityRow } from "./recursive-route-identity.js"
const row: RecursiveIdentityRow = { id: "1", dstAddress: "192.0.2.1/32", gateway: "198.51.100.1", distance: 0, scope: 0, targetScope: 0, routingTable: "main", checkGateway: "none", country: "Метка", comment: "Маршрут", disabled: false }
const second = { ...row, id: "2", gateway: "198.51.100.2", country: "Другая" }
const refs = new Set(["1"])
assert.deepEqual(plan([row], [{ ...row, id: "*A", dstAddress: "192.0.2.1", country: "" }], refs, "observed").rows[0], { ...row, dstAddress: "192.0.2.1" })
assert.deepEqual(plan([row, second], [{ ...second, id: "*B", country: "" }, { ...row, id: "*A", country: "" }], refs, "observed").rows.map(r => [r.id, r.country]), [["2", "Другая"], ["1", "Метка"]])
for (const change of [{ distance: 17 }, { gateway: "198.51.100.99" }, { disabled: true }, { scope: null, targetScope: null }]) {
assert.equal(plan([row], [{ ...row, ...change, id: "*new" }], refs, "observed").rows[0].id, "1")
}
assert.throws(() => plan([row], [], refs, "observed"), /BGP/)
assert.throws(() => plan([{ ...row, dstAddress: "192.0.2.1/24" }], [{ ...row, dstAddress: "192.0.2.2/24" }], refs, "apply"), /BGP/)
for (const change of [{ dstAddress: "192.0.2.2/32" }, { routingTable: "other" }]) assert.throws(() => plan([row], [{ ...row, ...change }], refs, "apply"), /BGP/)
assert.equal(plan([row], [{ ...row, country: "" }], refs, "apply").rows[0].country, "")
assert.throws(() => plan([row], [{ ...row, id: "999" }], refs, "apply"), /сервере/)
assert.equal(plan([row], [{ ...row, id: "999" }], refs, "copy").rows[0].id, "1")
assert.throws(() => plan([row, second], [{ ...row, id: "*A", gateway: "198.51.100.3" }, { ...second, id: "*B", gateway: "198.51.100.4" }], refs, "observed"), /Неоднозначное/)
assert.throws(() => plan([row, { ...row, id: "2", country: "Другая" }], [{ ...row, id: "*A", country: "" }], refs, "observed"), /Неоднозначные/)
assert.deepEqual(plan([row], [], new Set(), "apply").deletedIds, ["1"])
assert.equal(plan([], [{ ...row, id: "*A" }], new Set(), "observed").rows[0].id, "")
console.log("recursive identities: stable IDs, host aliases, reorder, next-hop changes, country ownership, scoped IDs and referenced/ambiguous removal guards passed")