logigate/tests/drag.test.ts

245 lines
11 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
import { LANE } from '../app/lib/logic/cables'
import { cableBits, GRID, shapeOf, type Pt, type Shape } from '../app/lib/logic/geometry'
import { latchedTo } from '../app/lib/logic/latch'
import { moveParts, moveStart } from '../app/lib/logic/move'
import { createProject, lookup } from '../app/lib/logic/ops'
import { circuitRoutes, type Routed } from '../app/lib/logic/routing'
import type { CircuitNode, ComponentDef, Wire } from '../app/lib/logic/types'
interface Circuit {
def: ComponentDef
shapes: Map<string, Shape>
}
function circuit(nodes: CircuitNode[], wires: Wire[]): Circuit {
const p = createProject({ examples: false })
const def: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes, wires }
p.components.push(def)
const get = lookup(p)
return { def, shapes: new Map(nodes.map(n => [n.id, shapeOf(n, get)])) }
}
const part = (id: string, kind: CircuitNode['kind'], x: number, y: number, more: Partial<CircuitNode> = {}): CircuitNode => ({ id, kind, x, y, ...more })
const wire = (id: string, from: string, to: string, pts?: Pt[]): Wire => {
const [fn, fp] = from.split('.')
const [tn, tp] = to.split('.')
return { id, from: { node: fn!, pin: fp! }, to: { node: tn!, pin: tp! }, ...(pts ? { pts } : {}) }
}
/** Drawn width of a cable in pixels, as the canvas draws it: a ribbon's sleeve with its outline. */
const widthOf = (c: Circuit) => (id: string) => {
const w = c.def.wires.find(x => x.id === id)
const from = w && c.def.nodes.find(n => n.id === w.from.node)
return from?.kind === 'bundle' ? cableBits(from) * LANE * GRID + 6 : undefined
}
/** Moves parts the way the canvas does: in steps from where they started, as a drag goes. */
function drag(c: Circuit, ids: string[], steps: Pt[], picked: string[] = []) {
const start = moveStart(c.def, new Set(ids), picked)
for (const [dx, dy] of steps) moveParts(c.def, start, dx, dy, c.shapes, { widthOf: widthOf(c) })
}
const routes = (c: Circuit) => circuitRoutes(c.def, c.shapes, { widthOf: widthOf(c) })
/**
* Lines drawn over or under a part, as "wire>part": a line counts as wide
* as it is drawn, and only its ends may touch a part, on their own pins.
*/
function underParts(c: Circuit, rs: Routed[]): string[] {
const out: string[] = []
for (const r of rs) {
const p = r.draw
for (let i = 0; i < p.length - 1; i++) {
let [ax, ay] = p[i]!
let [bx, by] = p[i + 1]!
// Pins that touch (a latched part): nothing is drawn.
if (ax === bx && ay === by) continue
const dx = Math.sign(bx - ax)
const dy = Math.sign(by - ay)
// The ends on the wire's own pins touch their parts.
if (i === 0) [ax, ay] = [ax + dx * r.h, ay + dy * r.h]
if (i === p.length - 2) [bx, by] = [bx - dx * r.h, by - dy * r.h]
if ((bx - ax) * dx < 0 || (by - ay) * dy < 0) continue
const m = r.h - 1e-6
for (const n of c.def.nodes) {
if (n.kind === 'note') continue
const b = c.shapes.get(n.id)!.body
const x0 = n.x + b.x - m
const x1 = n.x + b.x + b.w + m
const y0 = n.y + b.y - m
const y1 = n.y + b.y + b.h + m
const lx = Math.min(ax, bx)
const hx = Math.max(ax, bx)
const ly = Math.min(ay, by)
const hy = Math.max(ay, by)
if (ly < y1 && hy > y0 && lx < x1 && hx > x0) out.push(`${r.id}>${n.id}`)
}
}
}
return out
}
/** Lines of different signals drawn on or against each other (or routed so, `grid`), as "wire/wire". */
function touching(rs: Routed[], grid = false): string[] {
const segs: { id: string; net: string; h: number; horizontal: boolean; c: number; lo: number; hi: number }[] = []
for (const r of rs) {
const p = grid ? r.grid : r.draw
for (let i = 0; i < p.length - 1; i++) {
const [ax, ay] = p[i]!
const [bx, by] = p[i + 1]!
if (ax === bx && ay === by) continue
const horizontal = ay === by
segs.push({ id: r.id, net: r.net, h: r.h, horizontal, c: horizontal ? ay : ax, lo: horizontal ? Math.min(ax, bx) : Math.min(ay, by), hi: horizontal ? Math.max(ax, bx) : Math.max(ay, by) })
}
}
const out: string[] = []
for (let i = 0; i < segs.length; i++) {
for (let j = i + 1; j < segs.length; j++) {
const s = segs[i]!
const t = segs[j]!
if (s.net === t.net || s.horizontal !== t.horizontal || Math.abs(s.c - t.c) >= s.h + t.h - 1e-9) continue
if (Math.min(s.hi, t.hi) - Math.max(s.lo, t.lo) > 1e-9) out.push(`${s.id}/${t.id}`)
}
}
return out
}
const shifted = (route: Pt[], dx: number, dy: number) => route.map(([x, y]) => [x + dx, y + dy])
const routeOf = (rs: Routed[], id: string) => rs.find(r => r.id === id)!.grid
describe('dragging parts', () => {
it('a wire to a part that moves a little keeps its hand-placed corners, stretched to the pin', () => {
const c = circuit([part('s', 'input', 0, 0), part('l', 'output', 12, 6)], [wire('w', 's.q', 'l.a', [[5, 0], [5, 6]])])
drag(c, ['l'], [[0, -2]])
expect(c.def.wires[0]!.pts).toEqual([[5, 0], [5, 4]])
expect(routeOf(routes(c), 'w')).toEqual([[2, 0], [5, 0], [5, 4], [12, 4]])
})
it('a wire whose part moves past its hand-placed corners comes into the pin from the left, not through the part', () => {
const c = circuit([part('s', 'input', 0, 0), part('g', 'nand', 14, 0)], [wire('w', 's.q', 'g.a', [[6, 0], [6, 4], [11, 4], [11, 0]])])
drag(c, ['g'], [[-2, 0], [-5, 0]])
const rs = routes(c)
expect(underParts(c, rs)).toEqual([])
expect(routeOf(rs, 'w').at(-1)).toEqual([9, 0])
})
it('parts moved together keep the wires between them; wires to parts that stay go around what is in the way', () => {
const c = circuit(
[
part('i1', 'input', 0, 0),
part('i2', 'input', 0, 4),
part('g1', 'nand', 10, 0),
part('g2', 'nand', 20, 3),
part('o', 'output', 32, 4),
// A part that stays, beside where the moved parts go.
part('x', 'nand', 3, 7),
],
[
wire('w1', 'i1.q', 'g1.a'),
wire('w2', 'i2.q', 'g1.b', [[5, 4], [5, 2]]),
wire('w3', 'g1.q', 'g2.a', [[17, 1], [17, 3]]),
wire('w4', 'g1.q', 'g2.b'),
wire('w5', 'g2.q', 'o.a'),
],
)
const before = routes(c)
drag(c, ['g1', 'g2'], [[0, 3], [1, 6], [0, 8]])
const rs = routes(c)
expect(underParts(c, rs)).toEqual([])
expect(touching(rs)).toEqual([])
// Between the two moved parts: the same shape, moved along.
expect(routeOf(rs, 'w3')).toEqual(shifted(routeOf(before, 'w3'), 0, 8))
expect(routeOf(rs, 'w4')).toEqual(shifted(routeOf(before, 'w4'), 0, 8))
expect(c.def.wires.find(w => w.id === 'w3')!.pts).toEqual([[17, 9], [17, 11]])
})
it('a wire whose stretched corners would run along another signal lets go of them', () => {
const c = circuit(
[part('s1', 'input', -2, 0), part('l1', 'output', 30, 0), part('s2', 'input', 0, 12), part('l2', 'output', 30, 9)],
[wire('w1', 's1.q', 'l1.a', [[10, 0], [10, 6], [20, 6], [20, 0]]), wire('w2', 's2.q', 'l2.a', [[4, 12], [4, 9], [16, 9]])],
)
drag(c, ['l2'], [[0, -3]])
const rs = routes(c)
expect(touching(rs, true)).toEqual([])
expect(underParts(c, rs)).toEqual([])
// The other wire is left as it was.
expect(c.def.wires[0]!.pts).toEqual([[10, 0], [10, 6], [20, 6], [20, 0]])
})
it('everything moved at once keeps every corner, even of wires that already shared a line', () => {
const c = circuit(
[part('s1', 'input', -2, 0), part('l1', 'output', 10, 0), part('s2', 'input', -2, 4), part('l2', 'output', 10, 4)],
[wire('a', 's1.q', 'l1.a', [[2, 0], [2, 4], [8, 4], [8, 0]]), wire('b', 's2.q', 'l2.a', [[1, 4], [9, 4]])],
)
drag(c, ['s1', 'l1', 's2', 'l2'], [[3, 5]], ['a', 'b'])
expect(c.def.wires.map(w => w.pts)).toEqual([shifted([[2, 0], [2, 4], [8, 4], [8, 0]], 3, 5), shifted([[1, 4], [9, 4]], 3, 5)])
})
it('a wire moved on its own goes where it is put', () => {
const c = circuit([part('s', 'input', 0, 0), part('l', 'output', 12, 6)], [wire('w', 's.q', 'l.a', [[5, 0], [5, 6]])])
drag(c, [], [[1, 0]], ['w'])
expect(c.def.wires[0]!.pts).toEqual([[6, 0], [6, 6]])
})
it('a part moved onto a wire that stays: the wire lets go of the corners under it', () => {
const c = circuit(
[part('a', 'input', 0, 0), part('b', 'output', 30, 0), part('p', 'nand', 12, -10)],
[wire('w', 'a.q', 'b.a', [[8, 0], [8, 6], [20, 6], [20, 0]])],
)
drag(c, ['p'], [[0, 15]])
const rs = routes(c)
expect(underParts(c, rs)).toEqual([])
expect(touching(rs)).toEqual([])
})
it('what shows while dragging is what stays: corners let go on the way come back', () => {
const make = () =>
circuit([part('a', 'input', 0, 0), part('b', 'output', 30, 0), part('p', 'nand', 12, -10)], [wire('w', 'a.q', 'b.a', [[8, 0], [8, 6], [20, 6], [20, 0]])])
const c = make()
// Down over the wire's dip and on below it.
drag(c, ['p'], [[0, 5], [0, 15], [0, 16], [0, 20]])
const once = make()
drag(once, ['p'], [[0, 20]])
expect(c.def).toEqual(once.def)
expect(c.def.wires[0]!.pts).toEqual([[8, 0], [8, 6], [20, 6], [20, 0]])
expect(underParts(c, routes(c))).toEqual([])
})
it('a cable keeps clear of parts by its full width', () => {
// An 8-wire cable with a hand-placed hump; a part sits below where its end goes.
const c = circuit(
[part('bu', 'bundle', 0, 0, { bits: 8 }), part('sp', 'split', 30, 0, { bits: 8 }), part('x', 'nand', 18, 12)],
[wire('c', 'bu.q', 'sp.a', [[5, 3], [5, 10], [14, 10], [14, 3]])],
)
drag(c, ['sp'], [[0, 8]])
const rs = routes(c)
expect(rs[0]!.h).toBeGreaterThan(0.5)
expect(underParts(c, rs)).toEqual([])
// A plain wire there passes the part with room to spare and keeps its corners.
const plain = circuit(
[part('s', 'input', -1, 3), part('l', 'output', 30, 3), part('x', 'nand', 18, 12)],
[wire('w', 's.q', 'l.a', [[5, 3], [5, 10], [14, 10], [14, 3]])],
)
drag(plain, ['l'], [[0, 8]])
expect(plain.def.wires[0]!.pts?.length).toBeGreaterThan(1)
expect(underParts(plain, routes(plain))).toEqual([])
})
it('a part latched onto another moves with it, and its wires to parts that stay settle the same way', () => {
const c = circuit(
[part('g', 'nand', 10, 0), part('o', 'output', 14, 1, { latch: 'g' }), part('l', 'lamp', 30, 1), part('x', 'nand', 18, 3)],
[wire('in', 'g.q', 'o.a'), wire('w', 'o.q', 'l.a', [[20, 1], [20, -4], [26, -4], [26, 1]])],
)
const ids = new Set(['g'])
for (const n of latchedTo(c.def, ids)) ids.add(n.id)
drag(c, [...ids], [[0, 6]])
const o = c.def.nodes.find(n => n.id === 'o')!
expect([o.x, o.y]).toEqual([14, 7])
const rs = routes(c)
expect(routeOf(rs, 'in')).toEqual([[14, 7], [14, 7]])
expect(underParts(c, rs)).toEqual([])
expect(touching(rs)).toEqual([])
})
})