import { describe, expect, it } from 'vitest' import { dropLinks, dropPins, fitLinks, fitSpots, fitStart } from '../app/lib/logic/fit' import { chipPins, shapeOf } from '../app/lib/logic/geometry' import { createProject, lookup } from '../app/lib/logic/ops' import type { CircuitNode, ComponentDef, Project } from '../app/lib/logic/types' /** An Adder 4 with nothing wired, two lamps and two switches next to it. */ function bench(): { p: Project; def: ComponentDef; ins: string[]; outs: string[] } { const p = createProject({ examples: true }) const pins = chipPins(lookup(p)('ex-add4')!) const def: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes: [ { id: 'add', kind: 'chip', chip: 'ex-add4', x: 20, y: 0 }, { id: 'l0', kind: 'lamp', x: 40, y: 2 }, { id: 'l1', kind: 'lamp', x: 40, y: 5 }, { id: 's0', kind: 'input', x: 5, y: 0 }, { id: 's1', kind: 'input', x: 5, y: 3 }, ], wires: [], } p.components.push(def) return { p, def, ins: pins.inputs.map(x => x.key), outs: pins.outputs.map(x => x.key) } } describe('where a selection fits', () => { it('two lamps fit on any two outputs in a row, nearest first', () => { const { p, def, outs } = bench() const spots = fitSpots(def, lookup(p), ['l0', 'l1']) expect(spots).toHaveLength(1) const [spot] = spots expect(spot!.side).toBe('out') expect(spot!.pins.map(x => x.label)).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout']) // Pointing between S2 and S3 picks that pair; it wires top to bottom. const start = fitStart(spot!, (spot!.pins[2]!.y + spot!.pins[3]!.y) / 2) expect(start).toBe(2) expect(fitLinks(spot!, start)).toEqual([ { from: { node: 'add', pin: outs[2] }, to: { node: 'l0', pin: 'a' } }, { from: { node: 'add', pin: outs[3] }, to: { node: 'l1', pin: 'a' } }, ]) }) it('a wired pin splits the run', () => { const { p, def, outs } = bench() def.nodes.push({ id: 'x', kind: 'lamp', x: 40, y: 20 }) def.wires.push({ id: 'w', from: { node: 'add', pin: outs[2]! }, to: { node: 'x', pin: 'a' } }) const spots = fitSpots(def, lookup(p), ['l0', 'l1']) expect(spots.map(s => s.pins.map(x => x.label))).toEqual(expect.arrayContaining([['S0', 'S1'], ['S3', 'Cout']])) }) it('two switches fit on free inputs in a row', () => { const { p, def, ins } = bench() def.wires.push({ id: 'w', from: { node: 's0', pin: 'q' }, to: { node: 'add', pin: ins[4]! } }) def.nodes.push({ id: 's2', kind: 'input', x: 5, y: 6 }) const spots = fitSpots(def, lookup(p), ['s1', 's2']) const runs = spots.filter(s => s.part === 'add').map(s => s.pins.map(x => x.label)) expect(runs).toEqual(expect.arrayContaining([['A0', 'A1', 'A2', 'A3'], ['B1', 'B2', 'B3', 'Cin']])) expect(spots.every(s => s.side === 'in')).toBe(true) }) it('parts with no single free pin to wire fit nowhere', () => { const { p, def } = bench() def.nodes.push({ id: 'n0', kind: 'nand', x: 5, y: 10 }, { id: 'n1', kind: 'nand', x: 5, y: 14 }) // Two free inputs each: no input side; their one free output each fits onto inputs. const spots = fitSpots(def, lookup(p), ['n0', 'n1']) expect(spots.length).toBeGreaterThan(0) expect(spots.every(s => s.side === 'in')).toBe(true) expect(fitSpots(def, lookup(p), ['l0'])).toEqual([]) }) }) describe('dropping a part onto pins', () => { const pinsOf = (p: Project, def: ComponentDef, nodes: CircuitNode[]) => dropPins(def, nodes, n => shapeOf(n, lookup(p))) it('an output dropped a square away from a free output joins it, lined up exactly', () => { const { p, def, outs } = bench() const add = def.nodes.find(n => n.id === 'add')! const s1 = shapeOf(add, lookup(p)).outputs[1]! const lamp = def.nodes.find(n => n.id === 'l0')! lamp.x = add.x + s1.dx + 1 lamp.y = add.y + s1.dy const res = dropLinks(pinsOf(p, def, [lamp]), pinsOf(p, def, def.nodes.filter(n => n !== lamp)))! expect(res.shift).toEqual([-1, 0]) expect(res.links).toEqual([{ from: { node: 'add', pin: outs[1] }, to: { node: 'l0', pin: 'a' } }]) // A group only joins where pins land exactly. expect(dropLinks(pinsOf(p, def, [lamp]), pinsOf(p, def, def.nodes.filter(n => n !== lamp)), 0)).toBeNull() }) it('an input that has a wire takes no second one', () => { const { p, def, ins } = bench() const add = def.nodes.find(n => n.id === 'add')! def.wires.push({ id: 'w', from: { node: 's0', pin: 'q' }, to: { node: 'add', pin: ins[0]! } }) const a0 = shapeOf(add, lookup(p)).inputs[0]! const sw = def.nodes.find(n => n.id === 's1')! const q = shapeOf(sw, lookup(p)).outputs[0]! sw.x = add.x + a0.dx - q.dx sw.y = add.y + a0.dy - q.dy const res = dropLinks(pinsOf(p, def, [sw]), pinsOf(p, def, def.nodes.filter(n => n !== sw))) expect(res?.links.some(l => l.to.pin === ins[0])).not.toBe(true) }) })