import { strict as assert } from "node:assert" import { test } from "node:test" import type { FlowMapService, FlowMapServicePath } from "@mmapp/contracts/traffic-flow" import { groupMapCategories, mapCategoryId, smallMapServices } from "./network-map-destinations" import { readNetworkMapUrl } from "./network-map-url-state" const service: FlowMapService = { id: "s", label: "Example", category: "Видео", bytes: 100, bps: 80, share: 0.4 } const path: FlowMapServicePath = { clientId: "a", clientName: "A", viaId: "1", viaName: "Вход", enId: "", enName: "", serviceId: "s", bytes: 100, bps: 80 } const edge = { fromId: "1", toId: "s", bytes: 100, bps: 80, bpsFwd: 30, bpsRev: 50 } test("category drilldown preserves partial evidence, the source window and route certainty", () => { const serviceEvidence = [{ basis: "network" as const, bytes: 60 }, { basis: "unknown" as const, bytes: 40 }] const second = { ...service, id: "second", bytes: 25, bps: 20, share: 0.1 } const layer = groupMapCategories([{ ...service, serviceEvidence }, second], [ { ...edge, observedOnly: true }, { ...edge, toId: second.id, bytes: 25, bps: 20, bpsFwd: 15, bpsRev: 5 }, ], [{ ...path, serviceEvidence }, { ...path, serviceId: second.id, enId: "1", bytes: 25, bps: 20 }]) const video = mapCategoryId("Видео"), unknown = mapCategoryId("Не определена") assert.deepEqual(layer.services.map(s => [s.id, s.bytes, s.bps]), [[video, 85, 68], [unknown, 40, 32]]) assert.ok(Math.abs(layer.services[0].share - 0.34) < Number.EPSILON) assert.deepEqual(layer.members.get(video)?.map(s => [s.id, s.bytes]), [["s", 60], ["second", 25]]) assert.deepEqual(layer.paths.get(unknown)?.map(p => [p.enId, p.serviceId, p.bytes]), [["", "s", 40]]) assert.deepEqual(layer.edges.filter(e => e.toId === video).map(e => [!!e.observedOnly, e.bytes, e.bpsFwd]), [[true, 60, 0], [false, 25, 15]]) assert.equal(readNetworkMapUrl(new URLSearchParams("dest=categories&user=a")).dest, "categories") }) test("services at exactly ten percent collapse only for a known share in the current scope", () => { const items = [0.09, 0.10, 0.1001, 0.01].map((share, i) => ({...service, id: String(i), share})) assert.deepEqual(smallMapServices(items, true, "0", "3").map(s => s.id), ["1"]) assert.deepEqual(smallMapServices(items, false, null, null), []) assert.deepEqual(smallMapServices([{ ...items[0], share: 0.9 }], true, null, null), []) })