init commit

This commit is contained in:
Udo
2026-12-01 10:02:27 +05:00
commit 200083e3ef
36 changed files with 5611 additions and 0 deletions
+1235
View File
File diff suppressed because it is too large Load Diff
+105
View File
@@ -0,0 +1,105 @@
import { describe, expect, it } from 'vitest'
import { applyView, toFlow } from '../src/flow'
import { autoLayout, COL_X, NODE_HEIGHT } from '../src/layout'
import { buildReach, highlightFor } from '../src/reach'
import type { Graph } from '../src/types'
import raw from './fixtures/graph.json'
const graph = raw as unknown as Graph
describe('autoLayout', () => {
const pos = autoLayout(graph)
it('даёт позицию каждому узлу', () => {
for (const n of graph.nodes) expect(pos[n.id], n.id).toBeDefined()
})
it('узлы одной колонки не перекрываются', () => {
const byCol = new Map<number, { y: number; h: number; id: string }[]>()
for (const n of graph.nodes) {
const x = COL_X[n.type]
byCol.set(x, [...(byCol.get(x) ?? []), { y: pos[n.id].y, h: NODE_HEIGHT[n.type], id: n.id }])
}
for (const items of byCol.values()) {
items.sort((a, b) => a.y - b.y)
for (let i = 1; i < items.length; i++) {
expect(items[i].y, `${items[i - 1].id} / ${items[i].id}`).toBeGreaterThanOrEqual(items[i - 1].y + items[i - 1].h)
}
}
})
it('правила идут сверху вниз по порядку', () => {
const ys = graph.nodes.filter((n) => n.type === 'rule').sort((a, b) => a.data.index - b.data.index).map((n) => pos[n.id].y)
expect([...ys].sort((a, b) => a - b)).toEqual(ys)
})
})
describe('reach / highlight', () => {
const reach = buildReach(graph)
it('хост с route 3 доходит до BRIDGE_3 и FI-01, пропуская правила 6 и 7', () => {
const r = reach.get('host:h3')!
expect(r.nodes.has('out:BRIDGE_3')).toBe(true)
expect(r.nodes.has('node:FI-01')).toBe(true)
expect(r.nodes.has('out:BRIDGE_4')).toBe(false)
expect([...r.pass].sort()).toEqual(['rule:6', 'rule:7'])
expect(r.nodes.has('rule:8')).toBe(true)
expect(r.pass.has('rule:8')).toBe(false)
})
it('хост с route 8 упирается в catch-all и никогда не видит SoundCloud-правила', () => {
const r = reach.get('host:h8')!
expect(r.nodes.has('rule:13')).toBe(true)
expect(r.nodes.has('rule:14')).toBe(false)
expect(r.nodes.has('bal:BRIDGE_AUTO')).toBe(true)
})
it('хост другого профиля отсутствует', () => {
expect(reach.has('host:hx')).toBe(false)
})
it('клик по ноде подсвечивает хосты, которые на неё выходят', () => {
const hl = highlightFor('node:FI-01', reach)!
expect(hl.hostIds.sort()).toEqual(['host:h3', 'host:h7', 'host:h8', 'host:hn'])
expect(hl.active.has('node:FI-01')).toBe(true)
})
it('клик по недостижимому правилу не подсвечивает ни одного хоста', () => {
expect(highlightFor('rule:14', reach)!.hostIds).toEqual([])
})
it('клик по DIRECT подсвечивает все хосты профиля', () => {
expect(highlightFor('out:DIRECT', reach)!.hostIds).toHaveLength(4)
})
})
describe('toFlow / applyView', () => {
const flow = toFlow(graph, {}, autoLayout(graph))
it('правило знает свою цель, цепочка идёт через нижний/верхний порт', () => {
expect(flow.nodes.find((n) => n.id === 'rule:8')!.data.target).toBe('BRIDGE_3')
expect(flow.nodes.find((n) => n.id === 'rule:13')!.data.targetKind).toBe('balancer')
const ft = flow.edges.find((e) => e.id.includes('fallthrough'))!
expect([ft.sourceHandle, ft.targetHandle]).toEqual(['next', 'prev'])
})
it('сохранённая позиция важнее автоматической', () => {
const f = toFlow(graph, { 'rule:0': { x: 1, y: 2 } }, autoLayout(graph))
expect(f.nodes.find((n) => n.id === 'rule:0')!.position).toEqual({ x: 1, y: 2 })
})
it('hideShadowed скрывает недостижимые правила и их рёбра', () => {
const v = applyView(flow.nodes, flow.edges, null, true)
expect(v.nodes.filter((n) => n.hidden).length).toBe(8)
expect(v.edges.filter((e) => e.hidden).length).toBeGreaterThan(8)
})
it('подсветка: остальное приглушается, позиции не меняются', () => {
const hl = highlightFor('host:h3', buildReach(graph))
const v = applyView(flow.nodes, flow.edges, hl, false)
expect(v.nodes.find((n) => n.id === 'host:h3')!.class).toContain('active')
expect(v.nodes.find((n) => n.id === 'host:h7')!.class).toContain('dim')
expect(v.nodes.find((n) => n.id === 'rule:6')!.class).toContain('pass')
expect(v.nodes.map((n) => n.position)).toEqual(flow.nodes.map((n) => n.position))
})
})