import assert from "node:assert/strict"
import {
  bareIpv4,
  ipInCidr,
  isNatMasqueradeExporter,
  normalizeExporterIp,
  pickServerIdForExporter,
} from "./traffic-flow-map-exporter.js"

assert.equal(normalizeExporterIp("::ffff:172.18.0.2"), "172.18.0.2")
assert.equal(bareIpv4("10.255.254.3/32"), "10.255.254.3")
assert.equal(ipInCidr("10.255.254.3", "10.255.254.0/24"), true)
assert.equal(ipInCidr("172.18.0.2", "10.255.254.0/24"), false)
assert.equal(isNatMasqueradeExporter("172.18.0.2", "10.255.254.0/24"), true)
assert.equal(isNatMasqueradeExporter("10.255.254.3", "10.255.254.0/24"), false)
assert.equal(isNatMasqueradeExporter("10.0.0.12", "10.255.254.0/24"), true)

const byTunnel = new Map([["10.255.254.3", 7]])
assert.equal(pickServerIdForExporter({
  exporterIp: "10.255.254.3",
  overlayPrefix: "10.255.254.0/24",
  byTunnelIp: byTunnel,
  peers: [],
  hostIps: new Map(),
}), 7)

assert.equal(pickServerIdForExporter({
  exporterIp: "172.18.0.2",
  overlayPrefix: "10.255.254.0/24",
  byTunnelIp: byTunnel,
  peers: [{ serverId: 7, address: "10.255.254.3", allowedIps: ["10.255.254.3/32"] }],
  hostIps: new Map(),
}), 7)

assert.equal(pickServerIdForExporter({
  exporterIp: "172.18.0.2",
  overlayPrefix: "10.255.254.0/24",
  byTunnelIp: new Map([["10.255.254.3", 7], ["10.255.254.4", 8]]),
  peers: [
    { serverId: 7, address: "10.255.254.3", allowedIps: ["10.255.254.3/32"] },
    { serverId: 8, address: "10.255.254.4", allowedIps: ["10.255.254.4/32"] },
  ],
  hostIps: new Map(),
}), null)

assert.equal(pickServerIdForExporter({
  exporterIp: "94.142.140.141",
  overlayPrefix: "10.255.254.0/24",
  byTunnelIp: byTunnel,
  peers: [],
  hostIps: new Map([["94.142.140.141", 7]]),
}), 7)

console.log("traffic-flow-map-exporter.test.ts: ok")
