Files
MikrotikManager/tests/unit/network-map-labels.test.ts
T
Denozordec ed7b1e0350
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m50s
Docker images / backend-image (push) Successful in 2m5s
Docker images / updater-image (push) Successful in 40s
Docker images / netflow-agent-image (push) Successful in 41s
Docker images / frontend-image (push) Successful in 2m13s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
fix(map): восстановить учёт трафика сервисов и привязку к выходным узлам
2026-11-27 02:34:26 +07:00

72 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from "node:test"
import assert from "node:assert/strict"
import { MAP_CARD_H, MAP_CARD_W, MAP_PILL_H, mapLinkPath, mapLinkPoint, mapRibbons, placeMapLinkLabels, type MapBox } from "../../lib/network-map-layout"
import { mapObservationRoute } from "../../lib/network-map-observation-route"
const card = (x: number, y: number): MapBox => ({ x: x - MAP_CARD_W / 2, y: y - MAP_CARD_H / 2, w: MAP_CARD_W, h: MAP_CARD_H })
const hit = (a: MapBox, b: MapBox) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h
const pill = (p: { x: number; y: number; w: number }): MapBox => ({ x: p.x - p.w / 2, y: p.y - MAP_PILL_H / 2, w: p.w, h: MAP_PILL_H })
test("кривая связи проходит через концы и середину", () => {
const l = mapLinkPath({ x: 0, y: 0 }, 10, { x: 200, y: 100 }, 10)
assert.deepEqual(mapLinkPoint(l, 0), { x: 10, y: 0 })
assert.deepEqual(mapLinkPoint(l, 1), { x: 190, y: 100 })
assert.deepEqual(mapLinkPoint(l, 0.5), { x: 100, y: 50 })
})
test("наблюдение IPFIX огибает промежуточный узел в обоих направлениях", () => {
const blocker = card(400, 100)
for (const [sourceX, serviceX] of [[100, 800], [800, 100]]) {
const link = mapLinkPath({x: sourceX, y: 100}, 80, {x: serviceX, y: 100}, 140)
const route = mapObservationRoute(link, [blocker], [])
assert.ok(route)
assert.ok(route.d.startsWith(`M ${link.x1} 100 `) && route.d.endsWith(`H ${link.x2}`))
assert.ok(route.track < blocker.y || route.track > blocker.y + blocker.h)
}
})
test("подписи не наезжают на карточки и друг на друга, длинная — укорачивается или пропадает", () => {
// Три JH слева сходятся в один EN справа; у каждой пары — длинный и короткий вариант подписи.
const jh = [100, 180, 260].map((y) => ({ x: 542, y }))
const en = { x: 780, y: 180 }
const obstacles = [...jh.map((p) => card(p.x, p.y)), card(en.x, en.y)]
const labels = jh.map((p, i) => ({
id: "jh" + i,
link: mapLinkPath(p, MAP_CARD_W / 2, en, MAP_CARD_W / 2),
texts: ["24 мс · 86 Мбит/с", "24 мс"],
}))
// Горизонтальная связь в узком зазоре: длинной подписи места нет нигде.
labels.push({ id: "flat", link: mapLinkPath({ x: 542, y: 400 }, MAP_CARD_W / 2, { x: 780, y: 400 }, MAP_CARD_W / 2), texts: ["очень длинная подпись связи"] })
obstacles.push(card(542, 400), card(780, 400))
const placed = placeMapLinkLabels(labels, obstacles)
assert.ok(placed.length >= 3, "подписи трёх сходящихся связей размещены")
assert.equal(placed.find((p) => p.id === "flat"), undefined, "не влезающая подпись не рисуется")
for (const [i, a] of placed.entries()) {
for (const o of obstacles) assert.equal(hit(pill(a), o), false, a.id + " поверх карточки")
for (const b of placed.slice(i + 1)) assert.equal(hit(pill(a), pill(b)), false, a.id + " поверх " + b.id)
}
})
test("ленты NetFlow уложены стопкой у узла и толще у большей скорости", () => {
const exit = { x: 780, y: 300 }
const flows = [
{ id: "a", from: exit, fromHalfW: 80, to: { x: 1080, y: 200 }, toHalfW: 140, bps: 300 },
{ id: "b", from: exit, fromHalfW: 80, to: { x: 1080, y: 400 }, toHalfW: 140, bps: 100 },
]
const [a, b] = mapRibbons(flows, 40)
assert.equal(a.w, 30)
assert.equal(b.w, 10)
// У выхода ленты лежат вплотную по центру карточки (стопка 40 px при пике 400 — сумма у выхода).
assert.match(a.d, /^M 860 295\.0 /)
assert.match(b.d, /^M 860 315\.0 /)
assert.match(a.d, / 940 200\.0$/)
})
test("совпадающие середины двух связей разводятся по своим кривым", () => {
const a = mapLinkPath({ x: 0, y: 0 }, 0, { x: 300, y: 200 }, 0)
const b = mapLinkPath({ x: 0, y: 200 }, 0, { x: 300, y: 0 }, 0)
const placed = placeMapLinkLabels([{ id: "a", link: a, texts: ["12 мс"] }, { id: "b", link: b, texts: ["14 мс"] }], [])
assert.equal(placed.length, 2)
assert.equal(hit(pill(placed[0]), pill(placed[1])), false)
})