feat(git-graph): 泳道布局与连线几何
`layoutGraph` 把「子先于父」的提交前缀排成泳道:首父尽量继承父提交的车道与 颜色,新分支与断开的 tip 单调分配色号,空闲车道复用但活着的车道永不平移 (跨分页每一行逐字不变,宽度与悬空边也一样)。 连线几何单独放在 `edgePath` 里,一个弯只拐一次,并且拐在哪儿决定了每条线的 颜色是否统一: - **开启新线的边**(合并提交的第二父)从节点水平出线、圆角拐进自己的新车道; - **并入既有线的边**(分叉收尾)沿**自己**的车道一路向下,再在行边界横切进 目标车道——反过来画就会把这条分支的颜色涂到它并入的主线上(实测报过 「红线上仍然有一段绿」); - 横切段的中心放在行边界上方半个描边宽(`STROKE_INSET`):行是后绘制的兄弟 元素,溢出行外的半条描边会被下一行的背景吃掉,实测 8× 像素扫描只有 0.75px (正常 1.5px),内缩之后下边缘正好落在边界上,与下一行的竖线无缝相接。 布局还给每条出边记了 `join` 标记(这条边是并入还是开启),颜色与几何都由它 决定;同一条出边不会在目标车道里留下垂直线段。 测试:`tests/graph-layout.spec.ts` 覆盖空图 / 孤立根、线性历史的稳定颜色、 合并只与共同祖先汇合、octopus 与重复父提交、断开 tip 不接顺路车道、任意 分页边界逐行稳定、冻结输入与调用间无共享状态、随机 DAG 的连续性走查,以及 上面三条几何契约(含「同一行同一车道不得出现两种颜色」的全组合断言)。
This commit is contained in:
1 parent
1508487442
commit
ff373c81ed
2 files changed
+478
No files matched your search
@@ -0,0 +1,196 @@
|
||||
export interface GraphNode {
|
||||
readonly hashFull: string
|
||||
readonly parents: readonly string[]
|
||||
}
|
||||
|
||||
/** Corner radius of a lane-change bend, clamped per edge by its real estate. */
|
||||
const CORNER_RADIUS = 4
|
||||
|
||||
/**
|
||||
* Half of the rail stroke (`graph.module.css` paints `stroke-width: 1.5`).
|
||||
*
|
||||
* A join edge's crossing runs along the row's BOTTOM EDGE, and everything the
|
||||
* SVG paints outside its box is covered by the NEXT row's background (siblings
|
||||
* paint in order — `overflow: visible` alone cannot win that fight). Centring
|
||||
* the crossing half a stroke above the boundary keeps the whole line inside the
|
||||
* row while its lower edge still lands exactly ON the boundary, where the next
|
||||
* row's vertical picks it up — measured at 8× zoom, the crossing renders the
|
||||
* full 1.5px instead of the 0.75px half a stroke that spilled.
|
||||
*/
|
||||
const STROKE_INSET = 0.75
|
||||
|
||||
/**
|
||||
* The SVG path of one segment — a "rectangular" edge with ONE rounded elbow,
|
||||
* never a bare diagonal.
|
||||
*
|
||||
* WHERE the elbow sits is what keeps every line one colour. An edge that
|
||||
* OPENS a line (a merge's second parent) leaves the node horizontally and
|
||||
* rounds down into its own new lane. An edge that JOINS a line (a branch
|
||||
* ending at its fork) must instead run down its OWN lane and then turn into
|
||||
* the surviving lane along the row boundary: rounding into the target lane
|
||||
* and dropping there would paint a piece of the leaving branch's colour
|
||||
* straight onto the surviving line — the exact "there is still a green
|
||||
* stretch on the red main line" defect.
|
||||
*
|
||||
* A full-row crossing (neither end at a node; the current layout never emits
|
||||
* one) bends twice at mid-height. Coordinates stay in the row-fraction space
|
||||
* of {@link GraphSegment}; callers scale by the row height.
|
||||
*/
|
||||
export function edgePath(segment: Pick<GraphSegment, 'fromLane' | 'toLane' | 'fromY' | 'toY' | 'join'>, laneX: (lane: number) => number, rowHeight: number): string {
|
||||
const x1 = laneX(segment.fromLane)
|
||||
const x2 = laneX(segment.toLane)
|
||||
const y1 = segment.fromY * rowHeight
|
||||
const y2 = segment.toY * rowHeight
|
||||
if (x1 === x2 || y1 === y2) return `M ${x1} ${y1} L ${x2} ${y2}`
|
||||
const dx = Math.sign(x2 - x1)
|
||||
const dy = Math.sign(y2 - y1)
|
||||
const r = Math.min(CORNER_RADIUS, Math.abs(x2 - x1) / 2, Math.abs(y2 - y1) / 2)
|
||||
if (segment.join || segment.toY === 0.5) {
|
||||
// The leaving line keeps its own lane all the way down, then crosses over
|
||||
// along the row boundary — no vertical inside the lane it joins, and (see
|
||||
// STROKE_INSET) no half-stroke for the next row's background to eat.
|
||||
const yb = y2 - STROKE_INSET
|
||||
// The elbow's radius is also bounded by the run left ABOVE the inset, so a
|
||||
// degenerate (very short) row can never curve past the node.
|
||||
const rj = Math.min(r, Math.abs(yb - y1) / 2)
|
||||
return `M ${x1} ${y1} L ${x1} ${yb - dy * rj} Q ${x1} ${yb} ${x1 + dx * rj} ${yb} L ${x2} ${yb}`
|
||||
}
|
||||
if (segment.fromY === 0.5) {
|
||||
return `M ${x1} ${y1} L ${x2 - dx * r} ${y1} Q ${x2} ${y1} ${x2} ${y1 + dy * r} L ${x2} ${y2}`
|
||||
}
|
||||
const ym = (y1 + y2) / 2
|
||||
return `M ${x1} ${y1} L ${x1} ${ym - dy * r} Q ${x1} ${ym} ${x1 + dx * r} ${ym} L ${x2 - dx * r} ${ym} Q ${x2} ${ym} ${x2} ${ym + dy * r} L ${x2} ${y2}`
|
||||
}
|
||||
|
||||
|
||||
/** Coordinates are lane indices horizontally and fractions of one row vertically. */
|
||||
export interface GraphSegment {
|
||||
readonly fromLane: number
|
||||
readonly toLane: number
|
||||
readonly fromY: 0 | 0.5
|
||||
readonly toY: 0.5 | 1
|
||||
readonly colorIndex: number
|
||||
/** True when this edge joins a line another child already claimed (a branch
|
||||
* line ENDING at its fork point). A join stub wears the ENDING line's
|
||||
* colour — the node's own — while the surviving line keeps its colour; the
|
||||
* reverse convention would paint the first stretch of every fork in the
|
||||
* colour of the line it joins. An edge that OPENS a parent's new line wears
|
||||
* that new line's colour instead. */
|
||||
readonly join: boolean
|
||||
/** The ancestor this line is travelling towards; not necessarily in this page. */
|
||||
readonly targetHash: string
|
||||
}
|
||||
|
||||
export interface GraphLayoutRow {
|
||||
readonly hashFull: string
|
||||
readonly lane: number
|
||||
readonly colorIndex: number
|
||||
/** Width needed by this row, including passing lines (not the whole graph). */
|
||||
readonly laneCount: number
|
||||
readonly segments: readonly GraphSegment[]
|
||||
}
|
||||
|
||||
interface Reservation {
|
||||
hash: string
|
||||
lane: number
|
||||
colorIndex: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Lay out a unique, child-before-parent commit prefix, without looking ahead.
|
||||
* Render segments before the node, whose center is (lane, 0.5). Scale x by the
|
||||
* lane spacing (plus padding), y by row height. A crossing is not a junction;
|
||||
* only an endpoint at a commit center represents a connection to that commit.
|
||||
*
|
||||
* Recompute on the entire concatenated prefix when fetching another page: every
|
||||
* previously returned row, including its width and dangling edges, is unchanged.
|
||||
* Missing parents deliberately remain reserved through the bottom edge. Do not
|
||||
* truncate them based on whether a parent is currently loaded.
|
||||
*
|
||||
* First-parent continuations inherit their lane/color when possible. Other new
|
||||
* branches and disconnected tips get monotonically allocated color indices;
|
||||
* callers may reduce these modulo a theme palette. Vacant lanes are reused but
|
||||
* live lanes never shift, avoiding accidental connections and pagination jitter.
|
||||
*
|
||||
* Cost is O(sum of frontier widths + edges), with O(frontier width) working
|
||||
* space, excluding output. Width includes vacant slots between live lanes.
|
||||
*/
|
||||
export function layoutGraph(nodes: readonly GraphNode[]): GraphLayoutRow[] {
|
||||
const lanes: (Reservation | undefined)[] = []
|
||||
const targets = new Map<string, Reservation>()
|
||||
const rows: GraphLayoutRow[] = []
|
||||
let nextColor = 0
|
||||
|
||||
for (const node of nodes) {
|
||||
// One forward scan per row, even for an octopus with many new parents.
|
||||
let freeCursor = 0
|
||||
/** The next free lane, never the lane this row's node sits on: the node's
|
||||
* consumed lane LOOKS free, but the row still draws strokes there (the
|
||||
* node's own outgoing edges and its dot), so handing it to a new line
|
||||
* would paint that line's colour over the node's line — the reported
|
||||
* "there is still a green stretch on the red main line". `-1` means no
|
||||
* exclusion (the node has not picked its lane yet). */
|
||||
const freeLane = (exclude: number) => {
|
||||
while (lanes[freeCursor] !== undefined || freeCursor === exclude) freeCursor++
|
||||
return freeCursor++
|
||||
}
|
||||
const incoming = targets.get(node.hashFull)
|
||||
const lane = incoming?.lane ?? freeLane(-1)
|
||||
const colorIndex = incoming?.colorIndex ?? nextColor++
|
||||
const segments: GraphSegment[] = []
|
||||
let laneCount = Math.max(lanes.length, lane + 1)
|
||||
|
||||
for (const reservation of lanes) {
|
||||
if (!reservation) continue
|
||||
segments.push({
|
||||
fromLane: reservation.lane,
|
||||
toLane: reservation.lane,
|
||||
fromY: 0,
|
||||
toY: reservation === incoming ? 0.5 : 1,
|
||||
colorIndex: reservation.colorIndex,
|
||||
join: false,
|
||||
targetHash: reservation.hash,
|
||||
})
|
||||
}
|
||||
if (incoming) {
|
||||
targets.delete(node.hashFull)
|
||||
lanes[lane] = undefined
|
||||
}
|
||||
|
||||
const parents = new Set(node.parents)
|
||||
let firstParent = true
|
||||
for (const hash of parents) {
|
||||
let reservation = targets.get(hash)
|
||||
const opening = reservation === undefined
|
||||
if (!reservation) {
|
||||
// The consumed node's lane is available even if freeCursor passed it.
|
||||
const parentLane = firstParent ? lane : freeLane(lane)
|
||||
reservation = {
|
||||
hash,
|
||||
lane: parentLane,
|
||||
colorIndex: firstParent ? colorIndex : nextColor++,
|
||||
}
|
||||
lanes[parentLane] = reservation
|
||||
targets.set(hash, reservation)
|
||||
}
|
||||
segments.push({
|
||||
fromLane: lane,
|
||||
toLane: reservation.lane,
|
||||
fromY: 0.5,
|
||||
toY: 1,
|
||||
// An edge OPENING the parent's new line wears that line's colour; an
|
||||
// edge JOINING an already-claimed parent is the tail of THIS node's
|
||||
// line and wears the node's own colour all the way into the fork.
|
||||
colorIndex: opening ? reservation.colorIndex : colorIndex,
|
||||
join: !opening,
|
||||
targetHash: hash,
|
||||
})
|
||||
laneCount = Math.max(laneCount, reservation.lane + 1)
|
||||
firstParent = false
|
||||
}
|
||||
|
||||
rows.push({ hashFull: node.hashFull, lane, colorIndex, laneCount, segments })
|
||||
while (lanes.length && lanes[lanes.length - 1] === undefined) lanes.pop()
|
||||
}
|
||||
return rows
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { edgePath, layoutGraph, type GraphNode, type GraphLayoutRow } from '../src/client/changes/graph-layout'
|
||||
|
||||
const node = (hashFull: string, ...parents: string[]): GraphNode => ({ hashFull, parents })
|
||||
const outgoing = (row: GraphLayoutRow) => row.segments.filter(segment => segment.fromY === 0.5)
|
||||
|
||||
/** Check the geometry itself, not only the lane assignment. */
|
||||
function assertConnections(nodes: readonly GraphNode[]) {
|
||||
const rows = layoutGraph(nodes)
|
||||
let frontier = new Map<number, { hash: string; color: number }>()
|
||||
rows.forEach((row, index) => {
|
||||
const top = row.segments.filter(segment => segment.fromY === 0)
|
||||
expect(top.length).toBe(frontier.size)
|
||||
for (const segment of top) {
|
||||
expect(frontier.get(segment.fromLane)).toEqual({ hash: segment.targetHash, color: segment.colorIndex })
|
||||
expect(segment.toLane).toBe(segment.fromLane)
|
||||
if (segment.toY === 0.5) {
|
||||
expect(segment.targetHash).toBe(row.hashFull)
|
||||
expect(segment.toLane).toBe(row.lane)
|
||||
} else {
|
||||
expect(segment.targetHash).not.toBe(row.hashFull)
|
||||
expect(segment.fromLane).not.toBe(row.lane)
|
||||
}
|
||||
}
|
||||
expect(outgoing(row).map(segment => segment.targetHash)).toEqual([...new Set(nodes[index]!.parents)])
|
||||
frontier = new Map()
|
||||
for (const segment of row.segments) {
|
||||
expect(segment.fromLane).toBeLessThan(row.laneCount)
|
||||
expect(segment.toLane).toBeLessThan(row.laneCount)
|
||||
if (segment.fromY === 0.5) expect(segment.fromLane).toBe(row.lane)
|
||||
if (segment.toY !== 1) continue
|
||||
const target = { hash: segment.targetHash, color: segment.colorIndex }
|
||||
if (segment.join) {
|
||||
// A join stub only TOUCHES the already-claimed lane: the frontier
|
||||
// entry belongs to the surviving line (its own colour), while the
|
||||
// stub wears the colour of the line that ENDS here — the node's.
|
||||
expect(frontier.has(segment.toLane)).toBe(true)
|
||||
expect(frontier.get(segment.toLane)!.hash).toBe(segment.targetHash)
|
||||
expect(segment.colorIndex).toBe(row.colorIndex)
|
||||
continue
|
||||
}
|
||||
if (frontier.has(segment.toLane)) expect(frontier.get(segment.toLane)).toEqual(target)
|
||||
frontier.set(segment.toLane, target)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
describe('layoutGraph', () => {
|
||||
it('handles an empty graph and an isolated root without phantom edges', () => {
|
||||
expect(layoutGraph([])).toEqual([])
|
||||
expect(layoutGraph([node('root')])).toEqual([
|
||||
{ hashFull: 'root', lane: 0, colorIndex: 0, laneCount: 1, segments: [] },
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps a linear history straight with a stable color and bounded endpoints', () => {
|
||||
const nodes = [node('a', 'b'), node('b', 'c'), node('c')]
|
||||
const rows = layoutGraph(nodes)
|
||||
expect(rows.map(row => [row.lane, row.colorIndex, row.laneCount])).toEqual([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
|
||||
expect(rows.map(row => row.segments.map(s => [s.fromY, s.toY]))).toEqual([
|
||||
[[0.5, 1]], [[0, 0.5], [0.5, 1]], [[0, 0.5]],
|
||||
])
|
||||
assertConnections(nodes)
|
||||
})
|
||||
|
||||
it('splits a merge into two parents and rejoins only their shared ancestor', () => {
|
||||
const nodes = [node('merge', 'left', 'right'), node('left', 'base'), node('right', 'base'), node('base')]
|
||||
const rows = layoutGraph(nodes)
|
||||
expect(rows.map(row => row.lane)).toEqual([0, 0, 1, 0])
|
||||
expect(outgoing(rows[0]!).map(s => [s.toLane, s.colorIndex])).toEqual([[0, 0], [1, 1]])
|
||||
expect(outgoing(rows[2]!)).toEqual([
|
||||
// The right line ends by JOINING the claimed base line: the stub wears
|
||||
// right's own colour (1), not the surviving line's (0).
|
||||
{ fromLane: 1, toLane: 0, fromY: 0.5, toY: 1, colorIndex: 1, join: true, targetHash: 'base' },
|
||||
])
|
||||
assertConnections(nodes)
|
||||
})
|
||||
|
||||
it('handles octopus merges, duplicate parent entries, and occupied first-parent lanes', () => {
|
||||
const nodes = [node('m', 'a', 'b', 'c', 'd', 'b'), node('a', 'base'), node('b', 'base', 'extra'), node('c', 'base'), node('d', 'extra'), node('extra', 'base'), node('base')]
|
||||
const rows = layoutGraph(nodes)
|
||||
expect(outgoing(rows[0]!).map(s => s.toLane)).toEqual([0, 1, 2, 3])
|
||||
// b's first parent joins a claimed lane (b's own colour); its second parent
|
||||
// opens a NEW line and may not reuse b's own lane, so it lands past the
|
||||
// live lanes — the row grows one lane wider rather than bleeding colour.
|
||||
expect(outgoing(rows[2]!).map(s => [s.toLane, s.colorIndex, s.join])).toEqual([[0, 1, true], [4, 4, false]])
|
||||
expect(rows[2]!.laneCount).toBe(5)
|
||||
expect(rows[0]!.laneCount).toBe(4)
|
||||
assertConnections(nodes)
|
||||
})
|
||||
|
||||
it('does not connect disconnected tips to passing lanes or reuse their colors', () => {
|
||||
const nodes = [node('a', 'base'), node('unrelated', 'other'), node('other'), node('base'), node('root')]
|
||||
const rows = layoutGraph(nodes)
|
||||
expect(rows.map(row => row.lane)).toEqual([0, 1, 1, 0, 0])
|
||||
expect(rows.map(row => row.colorIndex)).toEqual([0, 1, 1, 0, 2])
|
||||
expect(rows[4]!.segments).toEqual([])
|
||||
assertConnections(nodes)
|
||||
})
|
||||
|
||||
it('preserves every row at every pagination boundary, including dangling parents', () => {
|
||||
const nodes = [node('m', 'a', 'b', 'missing'), node('a', 'base'), node('tip', 'c'), node('b', 'base'), node('c', 'base'), node('base'), node('unrelated')]
|
||||
const full = layoutGraph(nodes)
|
||||
for (let count = 0; count <= nodes.length; count++) {
|
||||
expect(layoutGraph(nodes.slice(0, count))).toEqual(full.slice(0, count))
|
||||
}
|
||||
expect(full.at(-1)?.segments).toContainEqual({ fromLane: 2, toLane: 2, fromY: 0, toY: 1, colorIndex: 2, join: false, targetHash: 'missing' })
|
||||
assertConnections(nodes)
|
||||
})
|
||||
|
||||
it('paints a fork-join stub in the ENDING branch’s colour, not the surviving line’s', () => {
|
||||
// M1 comes before F1, so M1's line claims the shared fork C; F1's branch
|
||||
// ends by joining it. The stub must wear F1's colour all the way in —
|
||||
// painting it in the surviving line's colour is exactly the flaw this
|
||||
// test exists to pin (the "fork starts in the wrong colour" report).
|
||||
const rows = layoutGraph([node('M2', 'M1'), node('M1', 'C'), node('F1', 'C'), node('C')])
|
||||
const stub = outgoing(rows[2]!)[0]!
|
||||
expect(stub.join).toBe(true)
|
||||
expect(stub.colorIndex).toBe(rows[2]!.colorIndex)
|
||||
expect(stub.toLane).toBe(rows[3]!.lane)
|
||||
expect(stub.toLane).toBe(rows[0]!.lane)
|
||||
expect(rows[3]!.colorIndex).toBe(rows[0]!.colorIndex)
|
||||
})
|
||||
|
||||
it('never starts a new branch line in the node’s OWN lane (the bleed that paints green on red)', () => {
|
||||
// x claims p1 first, so m's first parent is an already-claimed line (a
|
||||
// join) while m's own lane is consumed and merely LOOKS free. m's SECOND
|
||||
// parent must not be handed that lane: its straight drop would be drawn in
|
||||
// the new colour right on top of m's own line.
|
||||
const nodes = [node('x', 'p1'), node('m', 'p1', 'p2'), node('p1'), node('p2')]
|
||||
const rows = layoutGraph(nodes)
|
||||
const mRow = rows[1]!
|
||||
const edges = outgoing(mRow)
|
||||
// First parent: joins p1's claimed lane, wearing m's own colour.
|
||||
expect(edges[0]).toMatchObject({ toLane: rows[2]!.lane, join: true, colorIndex: mRow.colorIndex })
|
||||
// Second parent: opens a NEW line, on a lane that is not m's.
|
||||
expect(edges[1]!.join).toBe(false)
|
||||
expect(edges[1]!.colorIndex).toBe(rows[3]!.colorIndex)
|
||||
expect(edges[1]!.toLane).not.toBe(mRow.lane)
|
||||
// The clash itself: no two same-lane strokes of different colours.
|
||||
const laneColors = new Map<number, number>()
|
||||
const verticals = (segment: (typeof mRow.segments)[number]): number[] =>
|
||||
segment.fromLane === segment.toLane || segment.fromY === 0 || segment.join
|
||||
? [segment.fromLane] : [segment.toLane]
|
||||
for (const segment of mRow.segments) {
|
||||
for (const lane of verticals(segment)) {
|
||||
const previous = laneColors.get(lane)
|
||||
expect(previous === undefined || previous === segment.colorIndex,
|
||||
`lane ${lane}: ${previous} then ${segment.colorIndex}`).toBe(true)
|
||||
laneColors.set(lane, segment.colorIndex)
|
||||
}
|
||||
}
|
||||
assertConnections(nodes)
|
||||
})
|
||||
|
||||
it('paints a merge’s second-parent edge in the NEW line’s colour', () => {
|
||||
// M opens a fresh line for its second parent B: the edge belongs to that
|
||||
// line and wears its colour from the dot on, matching the reference
|
||||
// picture where the whole merge curve carries the merged branch's colour.
|
||||
const rows = layoutGraph([node('M', 'A', 'B'), node('A'), node('B')])
|
||||
const edges = outgoing(rows[0]!)
|
||||
expect(edges.map(edge => [edge.toLane, edge.colorIndex, edge.join])).toEqual([
|
||||
[rows[0]!.lane, rows[0]!.colorIndex, false],
|
||||
[rows[2]!.lane, rows[2]!.colorIndex, false],
|
||||
])
|
||||
expect(rows[2]!.colorIndex).not.toBe(rows[0]!.colorIndex)
|
||||
})
|
||||
|
||||
it('does not mutate frozen inputs and has no state shared between calls', () => {
|
||||
const nodes = Object.freeze([
|
||||
Object.freeze({ hashFull: 'a', parents: Object.freeze(['b', 'c']) }),
|
||||
Object.freeze({ hashFull: 'b', parents: Object.freeze([]) }),
|
||||
])
|
||||
const first = layoutGraph(nodes)
|
||||
layoutGraph([node('unrelated')])
|
||||
expect(layoutGraph(nodes)).toEqual(first)
|
||||
})
|
||||
|
||||
it('maintains edge continuity for deterministic generated topological DAGs', () => {
|
||||
for (let seed = 1; seed <= 12; seed++) {
|
||||
let state = seed
|
||||
const next = () => (state = (Math.imul(state, 1664525) + 1013904223) >>> 0)
|
||||
const nodes = Array.from({ length: 40 }, (_, i) => {
|
||||
const parents = Array.from({ length: next() % 5 }, () => String(i + 1 + next() % (44 - i)))
|
||||
return node(String(i), ...parents)
|
||||
})
|
||||
assertConnections(nodes)
|
||||
expect(layoutGraph(nodes.slice(0, 20))).toEqual(layoutGraph(nodes).slice(0, 20))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('edgePath', () => {
|
||||
// The lens's real geometry: lane x = 10 + lane * 12, row height 28.
|
||||
const laneX = (lane: number): number => 10 + lane * 12
|
||||
const H = 28
|
||||
/** A segment fixture: `join` defaults to false (an OPENING edge). */
|
||||
const seg = (fromLane: number, toLane: number, fromY: 0 | 0.5, toY: 0.5 | 1, join = false) =>
|
||||
({ fromLane, toLane, fromY, toY, join })
|
||||
/** Every x a path draws a VERTICAL run at (an `L`/`Q` whose two endpoints
|
||||
* share an x, plus the vertical stub a curve starts from). */
|
||||
const verticalXs = (d: string): number[] => {
|
||||
const tokens = d.match(/[MLQ]|-?\d+(\.\d+)?/g) ?? []
|
||||
const xs: number[] = []
|
||||
let i = 0
|
||||
let cx = 0
|
||||
let cy = 0
|
||||
while (i < tokens.length) {
|
||||
const cmd = tokens[i]!
|
||||
if (cmd === 'M' || cmd === 'L') {
|
||||
const x = Number(tokens[i + 1])
|
||||
const y = Number(tokens[i + 2])
|
||||
if (cmd === 'L' && x === cx) xs.push(x)
|
||||
cx = x; cy = y; i += 3
|
||||
} else if (cmd === 'Q') {
|
||||
const qx = Number(tokens[i + 1])
|
||||
const qy = Number(tokens[i + 2])
|
||||
const x = Number(tokens[i + 3])
|
||||
const y = Number(tokens[i + 4])
|
||||
if (qx === cx && cy === qy) xs.push(cx)
|
||||
cx = x; cy = y; i += 5
|
||||
} else { i += 1 }
|
||||
}
|
||||
return xs
|
||||
}
|
||||
|
||||
it('draws a same-lane run as one straight vertical', () => {
|
||||
expect(edgePath(seg(1, 1, 0, 1), laneX, H)).toBe('M 22 0 L 22 28')
|
||||
expect(edgePath(seg(0, 0, 0, 0.5), laneX, H)).toBe('M 10 0 L 10 14')
|
||||
})
|
||||
|
||||
it('rounds an OPENING lane change with the elbow at the node', () => {
|
||||
// A merge's second parent: the new line leaves the node, turns once, and
|
||||
// drops down its OWN lane.
|
||||
const d = edgePath(seg(0, 1, 0.5, 1, false), laneX, H)
|
||||
expect(d).toBe('M 10 14 L 18 14 Q 22 14 22 18 L 22 28')
|
||||
expect(d.match(/Q/g)).toHaveLength(1)
|
||||
expect(verticalXs(d)).toEqual([22])
|
||||
})
|
||||
|
||||
it('mirrors the opening elbow for a right-to-left lane change', () => {
|
||||
expect(edgePath(seg(2, 0, 0.5, 1, false), laneX, H)).toBe('M 34 14 L 14 14 Q 10 14 10 18 L 10 28')
|
||||
})
|
||||
|
||||
it('runs a JOIN edge down its OWN lane and crosses over at the row boundary', () => {
|
||||
// The fork's ending branch: green down lane 1, then a left turn along the
|
||||
// bottom edge. It must NOT drop vertically inside lane 0 — that put a
|
||||
// green stretch on the red main line (the reported defect).
|
||||
// The crossing rides half a stroke (0.75px) above the boundary so the next
|
||||
// row's background cannot eat its lower half; its LOWER EDGE still lands
|
||||
// exactly on the boundary (27.25 + 1.5/2 = 28).
|
||||
const d = edgePath(seg(1, 0, 0.5, 1, true), laneX, H)
|
||||
expect(d).toBe('M 22 14 L 22 23.25 Q 22 27.25 18 27.25 L 10 27.25')
|
||||
expect(verticalXs(d)).toEqual([22])
|
||||
expect(verticalXs(d)).not.toContain(10)
|
||||
})
|
||||
|
||||
it('never draws a vertical inside the lane a join edge joins, at any radius/spacing', () => {
|
||||
for (const from of [0, 1, 2, 3]) {
|
||||
for (const to of [0, 1, 2, 3]) {
|
||||
if (from === to) continue
|
||||
const d = edgePath(seg(from, to, 0.5, 1, true), laneX, H)
|
||||
expect(verticalXs(d), `join ${from}->${to}: ${d}`).not.toContain(laneX(to))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('clamps the corner radius so a tight turn never overshoots', () => {
|
||||
// One lane over (12px) is fine at r=4; a hypothetical half-height row
|
||||
// clamps to half the vertical run instead.
|
||||
expect(edgePath(seg(0, 1, 0.5, 1, false), laneX, 8)).toBe('M 10 4 L 20 4 Q 22 4 22 6 L 22 8')
|
||||
// A join's radius is bounded by the run left above the inset too, so the
|
||||
// elbow can never curve back past the node on a degenerate short row.
|
||||
expect(edgePath(seg(1, 0, 0.5, 1, true), laneX, 8)).toBe('M 22 4 L 22 5.625 Q 22 7.25 20.375 7.25 L 10 7.25')
|
||||
})
|
||||
|
||||
it('bends a full-row crossing twice at mid-height (defensive shape)', () => {
|
||||
const d = edgePath(seg(0, 2, 0, 1, false), laneX, H)
|
||||
expect(d).toBe('M 10 0 L 10 10 Q 10 14 14 14 L 30 14 Q 34 14 34 18 L 34 28')
|
||||
expect(d.match(/Q/g)).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user