fix topology of cards
This commit is contained in:
@@ -1,105 +1,125 @@
|
||||
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 { bpHandles, cardHandles } from '../src/handles'
|
||||
import { highlightFor, rowSelId } from '../src/highlight'
|
||||
import { autoLayout, heightOf, WIDTH } from '../src/layout'
|
||||
import type { Graph } from '../src/types'
|
||||
import raw from './fixtures/graph.json'
|
||||
|
||||
const graph = raw as unknown as Graph
|
||||
const RU = 'node:n-ru'
|
||||
const FI = 'node:n-2'
|
||||
|
||||
describe('handles: карточки рисуют ровно те порты, к которым привязаны рёбра', () => {
|
||||
const nodes = new Map(graph.nodes.map((n) => [n.id, n]))
|
||||
const portsOf = (id: string) => {
|
||||
const n = nodes.get(id)!
|
||||
const defs = n.type === 'node' ? cardHandles(n.data) : bpHandles(n.type, n.data)
|
||||
return { source: new Set(defs.filter((h) => h.type === 'source').map((h) => h.id)), target: new Set(defs.filter((h) => h.type === 'target').map((h) => h.id)) }
|
||||
}
|
||||
it('у каждого ребра есть исходящий и входящий порт', () => {
|
||||
for (const e of graph.edges) {
|
||||
expect(portsOf(e.source).source.has(e.sourceHandle), `${e.id} (source)`).toBe(true)
|
||||
expect(portsOf(e.target).target.has(e.targetHandle), `${e.id} (target)`).toBe(true)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('autoLayout', () => {
|
||||
const pos = autoLayout(graph)
|
||||
const nodes = new Map(graph.nodes.map((n) => [n.id, n]))
|
||||
|
||||
it('даёт позицию каждому узлу', () => {
|
||||
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 cols = new Map<number, string[]>()
|
||||
for (const n of graph.nodes) cols.set(pos[n.id].x, [...(cols.get(pos[n.id].x) ?? []), n.id])
|
||||
for (const ids of cols.values()) {
|
||||
const sorted = ids.sort((a, b) => pos[a].y - pos[b].y)
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
const prev = nodes.get(sorted[i - 1])!
|
||||
expect(pos[sorted[i]].y, `${sorted[i - 1]} / ${sorted[i]}`).toBeGreaterThanOrEqual(pos[sorted[i - 1]].y + heightOf(prev))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
it('поток идёт слева направо: источник каждого ребра левее приёмника', () => {
|
||||
for (const e of graph.edges) {
|
||||
expect(pos[e.source].x + WIDTH[nodes.get(e.source)!.type], e.id).toBeLessThan(pos[e.target].x)
|
||||
}
|
||||
})
|
||||
|
||||
it('хост стоит напротив строки, на которую садится его нитка', () => {
|
||||
const h3 = graph.edges.find((e) => e.source === 'host:h3')!
|
||||
const hostY = pos['host:h3'].y + heightOf(nodes.get('host:h3')!) / 2
|
||||
const ruY = pos[RU].y
|
||||
expect(h3.targetHandle).toBe('rin:inb-bridge:8')
|
||||
// правило #8 лежит ниже шапки карточки; хост не должен висеть выше карточки
|
||||
expect(hostY).toBeGreaterThan(ruY)
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
describe('highlight', () => {
|
||||
it('хост: путь через обе ноды, проверенные строки приглушены', () => {
|
||||
const hl = highlightFor('host:h3', graph)!
|
||||
expect(hl.hostIds).toEqual(['host:h3'])
|
||||
expect(hl.nodes.has(RU) && hl.nodes.has(FI)).toBe(true)
|
||||
const ru = hl.rows.get(RU)!
|
||||
expect(ru.active.has('inb-bridge:8')).toBe(true)
|
||||
expect([...ru.pass].sort()).toEqual(['inb-bridge:6', 'inb-bridge:7'])
|
||||
expect(hl.rows.get(FI)!.active.has('inb:inb-def-ss')).toBe(true)
|
||||
})
|
||||
|
||||
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(highlightFor(rowSelId(RU, 'inb-bridge:8'), graph)!.hostIds).toEqual(['host:h3'])
|
||||
expect(highlightFor(rowSelId(RU, 'inb-bridge:13'), graph)!.hostIds.sort()).toEqual(['host:h8', 'host:hn'])
|
||||
})
|
||||
|
||||
it('хост другого профиля отсутствует', () => {
|
||||
expect(reach.has('host:hx')).toBe(false)
|
||||
it('нода-приёмник: и мост, и прямое подключение', () => {
|
||||
const ids = highlightFor(FI, graph)!.hostIds
|
||||
expect(ids).toContain('host:hd')
|
||||
expect(ids).toContain('host:h3')
|
||||
expect(ids).toContain('host:h7') // через балансер
|
||||
expect(ids).not.toContain('host:nonexistent')
|
||||
})
|
||||
|
||||
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(rowSelId(RU, 'inb-bridge:14'), graph)!.hostIds).toEqual([])
|
||||
})
|
||||
|
||||
it('клик по недостижимому правилу не подсвечивает ни одного хоста', () => {
|
||||
expect(highlightFor('rule:14', reach)!.hostIds).toEqual([])
|
||||
})
|
||||
|
||||
it('клик по DIRECT подсвечивает все хосты профиля', () => {
|
||||
expect(highlightFor('out:DIRECT', reach)!.hostIds).toHaveLength(4)
|
||||
it('без выбора подсветки нет', () => {
|
||||
expect(highlightFor(null, graph)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
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('нода рисуется карточкой, остальное простыми блоками', () => {
|
||||
expect(flow.nodes.filter((n) => n.type === 'card')).toHaveLength(graph.nodes.filter((n) => n.type === 'node').length)
|
||||
expect(flow.nodes.find((n) => n.id === 'host:h3')!.type).toBe('bp')
|
||||
})
|
||||
|
||||
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 })
|
||||
const f = toFlow(graph, { [RU]: { x: 1, y: 2 } }, autoLayout(graph))
|
||||
expect(f.nodes.find((n) => n.id === RU)!.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))
|
||||
it('подсветка: остальное приглушается, карточки получают строки, позиции не меняются', () => {
|
||||
const hl = highlightFor('host:h3', 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.find((n) => n.id === 'host:h3')!.class).toBe('active')
|
||||
expect(v.nodes.find((n) => n.id === 'host:h7')!.class).toBe('dim')
|
||||
expect(v.nodes.find((n) => n.id === RU)!.data.hl.active).toContain('inb-bridge:8')
|
||||
expect(v.edges.filter((e) => e.animated).length).toBe(hl!.edges.size)
|
||||
expect(v.nodes.map((n) => n.position)).toEqual(flow.nodes.map((n) => n.position))
|
||||
})
|
||||
|
||||
it('hideShadowed и пустая подсветка пробрасываются в карточки', () => {
|
||||
const v = applyView(flow.nodes, flow.edges, null, true)
|
||||
expect(v.nodes.find((n) => n.id === RU)!.data.hideShadowed).toBe(true)
|
||||
expect(v.nodes.find((n) => n.id === RU)!.data.hl).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user