import assert from "node:assert/strict" import { test } from "node:test" import { buildWanRuntime } from "./wan-runtime-model.js" const uplinks = [{ id: "wan", iface: "ether1", name: "WAN", isp: "Fixture", ip: "192.0.2.10" }] const route = { ".id": "*1", "dst-address": "0.0.0.0/0", "routing-table": "main", active: "true", distance: "1", gateway: "192.0.2.1", "immediate-gw": "192.0.2.1%ether1" } const source = { dhcp: [{ interface: "ether1", address: "192.0.2.10/24", gateway: "192.0.2.1", status: "bound" }], addresses: [{ interface: "ether1", address: "192.0.2.10/24" }], routes: [route] } test("confirmed native IPv4/main default and bound DHCP, without a forwarding claim", () => { const result = buildWanRuntime(uplinks, source) assert.equal(result.defaultRouteStatus, "confirmed"); assert.equal(result.defaultInterface, "ether1") assert.equal(result.uplinks[0].leasedIp, "192.0.2.10"); assert.equal(result.uplinks[0].addressSource, "dhcp-bound") assert.equal(result.uplinks[0].isDefault, true); assert.equal(result.uplinks[0].defaultKnown, true) assert.equal(result.forwardingVerified, false) }) test("ECMP entries and a multi-gateway native row do not select the first WAN", () => { for (const routes of [[route, { ...route, ".id": "*2", gateway: "198.51.100.1", "immediate-gw": "198.51.100.1%ether2" }], [{ ...route, gateway: "192.0.2.1,198.51.100.1" }]]) { const result = buildWanRuntime(uplinks, { ...source, routes }) assert.equal(result.defaultRouteStatus, "ambiguous"); assert.equal(result.defaultGateway, null) assert.equal(result.uplinks[0].isDefault, false); assert.equal(result.uplinks[0].defaultKnown, false) } }) test("missing table, activity or distance and contradictory active flags remain unknown", () => { for (const fields of [{ active: undefined }, { "routing-table": undefined }, { distance: "1garbage" }, { disabled: "true" }, { inactive: "true" }]) { const value = Object.fromEntries(Object.entries({ ...route, ...fields }).filter(([, value]) => value !== undefined)) const result = buildWanRuntime(uplinks, { ...source, routes: [value] }) assert.equal(result.defaultRouteStatus, "unknown"); assert.equal(result.defaultInterface, null) } const missingAlternative = { ".id": "*2", "dst-address": "0.0.0.0/0", "routing-table": "main" } assert.equal(buildWanRuntime(uplinks, { ...source, routes: [route, missingAlternative] }).defaultRouteStatus, "unknown") }) test("known inactive/foreign table routes and empty route arrays are not a main default", () => { for (const routes of [[], [{ ...route, active: "false" }], [{ ...route, "routing-table": "isolated" }]]) { const result = buildWanRuntime(uplinks, { ...source, routes }) assert.equal(result.defaultRouteStatus, "none"); assert.equal(result.uplinks[0].defaultKnown, true) } }) test("an unbound lease is not used; multiple native addresses and conflicting interfaces remain ambiguous", () => { const result = buildWanRuntime(uplinks, { ...source, dhcp: [{ ...source.dhcp[0], status: "searching", address: "203.0.113.77/24" }], addresses: [...source.addresses, { interface: "ether1", address: "192.0.2.11/24" }] }) assert.equal(result.uplinks[0].leasedIp, null); assert.equal(result.uplinks[0].addressSource, "ambiguous") const conflict = buildWanRuntime(uplinks, { ...source, routes: [{ ...route, interface: "ether2" }] }) assert.equal(conflict.defaultInterface, null); assert.equal(conflict.uplinks[0].defaultKnown, false) }) test("malformed source shapes fail; invalid IPv4 and prefix suffixes are not addresses", () => { for (const key of ["dhcp", "addresses", "routes"]) assert.throws(() => buildWanRuntime(uplinks, { ...source, [key]: {} })) for (const address of ["192.0.2.1/24garbage", "192.0.2.1/33", "192.0.2.1/24/1", "999.0.0.1", "2001:db8::1/64"]) { const result = buildWanRuntime(uplinks, { ...source, dhcp: [], addresses: [{ interface: "ether1", address }] }) assert.equal(result.uplinks[0].leasedIp, null) } })