2026-10-21 09:51:45 +00:00
|
|
|
import { describe, expect, it } from 'vitest'
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
import { bundleWires, cablePanel, nameTags, plugInto, plugLayout, syncCables } from '../app/lib/logic/cables'
|
2026-10-21 09:51:45 +00:00
|
|
|
import { compile, rowSource } from '../app/lib/logic/compile'
|
|
|
|
|
import { chipPins, panelOffsets, pinPos, shapeOf } from '../app/lib/logic/geometry'
|
|
|
|
|
import { copySelection, createProject, lookup, pasteClip } from '../app/lib/logic/ops'
|
|
|
|
|
import { circuitRoutes } from '../app/lib/logic/routing'
|
|
|
|
|
import { Simulator } from '../app/lib/logic/sim'
|
|
|
|
|
import type { CircuitNode, ComponentDef, Project } from '../app/lib/logic/types'
|
|
|
|
|
|
|
|
|
|
/** Switches A0..A3 and B0..B3 wired to an Adder 4, its outputs to lamps. */
|
|
|
|
|
function adder(): { p: Project; def: ComponentDef; ins: string[]; outs: string[] } {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const pins = chipPins(lookup(p)('ex-add4')!)
|
|
|
|
|
const ins = pins.inputs.map(x => x.key)
|
|
|
|
|
const outs = pins.outputs.map(x => x.key)
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 'root',
|
|
|
|
|
name: 'Root',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'add', kind: 'chip', chip: 'ex-add4', x: 20, y: 0 },
|
|
|
|
|
...pins.inputs.map((x, i) => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 3, label: x.label })),
|
|
|
|
|
...pins.outputs.map((_, i) => ({ id: `l${i}`, kind: 'lamp' as const, x: 50, y: 20 + i * 2 })),
|
|
|
|
|
],
|
|
|
|
|
wires: [
|
|
|
|
|
...ins.map((k, i) => ({ id: `wi${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: 'add', pin: k } })),
|
|
|
|
|
...outs.map((k, i) => ({ id: `wo${i}`, from: { node: 'add', pin: k }, to: { node: `l${i}`, pin: 'a' } })),
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(def)
|
|
|
|
|
return { p, def, ins, outs }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sums(p: Project, def: ComponentDef): string[] {
|
|
|
|
|
const out: string[] = []
|
|
|
|
|
for (const [a, b] of [[3, 5], [15, 1], [9, 9], [0, 0]] as const) {
|
|
|
|
|
const s = new Simulator(compile(lookup(p), def.id))
|
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
|
s.setSource(`s${i}`, ((a >> i) & 1) === 1)
|
|
|
|
|
s.setSource(`s${i + 4}`, ((b >> i) & 1) === 1)
|
|
|
|
|
}
|
|
|
|
|
s.run()
|
|
|
|
|
out.push([0, 1, 2, 3, 4].map(i => s.pin(`l${i}`, 'a')).join(''))
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const node = (def: ComponentDef, id: string) => def.nodes.find(n => n.id === id)!
|
|
|
|
|
|
|
|
|
|
/** Where each wire on the single-wire side of a cable end meets the part it is plugged into. */
|
|
|
|
|
function touches(p: Project, def: ComponentDef, end: CircuitNode): boolean {
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const wires = end.kind === 'bundle' ? def.wires.filter(w => w.to.node === end.id) : def.wires.filter(w => w.from.node === end.id)
|
|
|
|
|
return wires.every(w => {
|
|
|
|
|
const a = pinPos(node(def, w.from.node), shapeOf(node(def, w.from.node), get), w.from.pin, 'out')!
|
|
|
|
|
const b = pinPos(node(def, w.to.node), shapeOf(node(def, w.to.node), get), w.to.pin, 'in')!
|
|
|
|
|
return a.x === b.x && a.y === b.y
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('plugged cable ends', () => {
|
|
|
|
|
it('bundling wires into one part plugs the split straight onto its pins', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const before = sums(p, def)
|
|
|
|
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
expect(res.split.plug).toBe('add')
|
|
|
|
|
expect(res.bundle.plug).toBeUndefined()
|
|
|
|
|
expect(touches(p, def, res.split)).toBe(true)
|
|
|
|
|
expect(res.split.x).toBe(19)
|
|
|
|
|
expect(sums(p, def)).toEqual(before)
|
|
|
|
|
// The plugged wires have no length, so there is nothing to draw.
|
|
|
|
|
const routes = circuitRoutes(def, new Map(def.nodes.map(n => [n.id, shapeOf(n, lookup(p))])))
|
|
|
|
|
const plugged = new Set(def.wires.filter(w => w.from.node === res.split.id).map(w => w.id))
|
|
|
|
|
for (const r of routes.filter(r => plugged.has(r.id))) expect(r.grid).toHaveLength(2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a bundle plugs onto the outputs, and the free split names its wires after them', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
expect(res.bundle.plug).toBe('add')
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.split.names).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout'])
|
2026-10-21 09:59:17 +00:00
|
|
|
// Names never change the shape: the pins stay where they were.
|
2026-10-21 09:51:45 +00:00
|
|
|
const s = shapeOf(res.split, lookup(p))
|
2026-10-21 09:59:17 +00:00
|
|
|
expect(s.body).toEqual({ x: 0, y: -0.5, w: 1, h: 5 })
|
2026-10-21 09:51:45 +00:00
|
|
|
expect(s.outputs.every(o => o.dx === 1)).toBe(true)
|
|
|
|
|
// Plugged in, the bundle leaves the names to the component.
|
2026-10-21 09:59:17 +00:00
|
|
|
expect(nameTags(def, res.bundle, lookup(p))).toEqual([])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a free end shows only the names the part at the other end does not show already', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], get)
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
syncCables(p)
|
|
|
|
|
// Lamps without a name show the wire's name, dimmed.
|
|
|
|
|
expect(nameTags(def, res.split, get)).toEqual(['', '', '', '', ''])
|
|
|
|
|
// A lamp with another name, and a NAND gate, don't.
|
|
|
|
|
node(def, 'l1').label = 'Bit 1'
|
|
|
|
|
def.nodes.push({ id: 'g', kind: 'nand', x: 60, y: 30 })
|
|
|
|
|
def.wires.find(w => w.from.node === res.split.id && w.from.pin === 'b4')!.to = { node: 'g', pin: 'a' }
|
|
|
|
|
expect(nameTags(def, res.split, get)).toEqual(['', 'S1', '', '', 'Cout'])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a bundle fed by switches of the same names shows none', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const ins = chipPins(get('ex-add4')!).inputs
|
|
|
|
|
def.nodes.push({ id: 'b', kind: 'bundle', bits: 4, x: 8, y: 2 })
|
|
|
|
|
def.wires = def.wires.filter(w => !['wi4', 'wi5', 'wi6', 'wi7'].includes(w.id))
|
|
|
|
|
for (const i of [0, 1, 2, 3]) def.wires.push({ id: `f${i}`, from: { node: `s${i + 4}`, pin: 'q' }, to: { node: 'b', pin: `b${i}` } })
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(node(def, 'b').names).toEqual(ins.slice(4, 8).map(x => x.label))
|
|
|
|
|
expect(nameTags(def, node(def, 'b'), get)).toEqual(['', '', '', ''])
|
2026-10-21 09:51:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('names come from what drives the wires, else from what they drive', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
for (const i of [0, 1, 2, 3]) delete node(def, `s${i}`).label
|
|
|
|
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.bundle.names).toEqual(['A0', 'A1', 'A2', 'A3'])
|
|
|
|
|
node(def, 's1').label = 'X'
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.bundle.names).toEqual(['A0', 'X', 'A2', 'A3'])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('follows its part, and comes loose when the pins no longer line up', () => {
|
|
|
|
|
const { p, def, ins } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
const chip = node(def, 'add')
|
|
|
|
|
chip.x += 7
|
|
|
|
|
chip.y += 3
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(touches(p, def, res.split)).toBe(true)
|
|
|
|
|
// Rewire one wire to a pin further down: no longer in the cable's order on one edge.
|
|
|
|
|
def.wires.find(w => w.from.node === res.split.id && w.from.pin === 'b0')!.to.pin = ins[8]!
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.split.plug).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a free end dropped with its pins on a part plugs in', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
const { x, y } = res.bundle
|
|
|
|
|
delete res.bundle.plug
|
|
|
|
|
res.bundle.x += 3
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.bundle.plug).toBeUndefined()
|
|
|
|
|
res.bundle.x = x
|
|
|
|
|
res.bundle.y = y
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.bundle.plug).toBe('add')
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-21 10:16:26 +00:00
|
|
|
it('one wire landing on a part does not plug the whole end in', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wo0', 'wo1', 'wo2', 'wo3', 'wo4'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
// Only the last wire runs on, to a lamp sitting right on its pin.
|
|
|
|
|
def.wires = def.wires.filter(w => w.from.node !== res.split.id || w.from.pin === 'b4')
|
|
|
|
|
const lamp = node(def, 'l4')
|
|
|
|
|
const pin = shapeOf(res.split, lookup(p)).outputs[4]!
|
|
|
|
|
lamp.x = res.split.x + pin.dx
|
|
|
|
|
lamp.y = res.split.y + pin.dy
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(touches(p, def, res.split)).toBe(true)
|
|
|
|
|
expect(res.split.plug).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-21 09:51:45 +00:00
|
|
|
it('takes the spacing of pins further apart', () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const get = lookup(p)
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 'root',
|
|
|
|
|
name: 'Root',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'g', kind: 'nand', x: 10, y: 0 },
|
|
|
|
|
{ id: 'b', kind: 'split', bits: 2, x: 0, y: 0 },
|
|
|
|
|
],
|
|
|
|
|
wires: [
|
|
|
|
|
{ id: 'w0', from: { node: 'b', pin: 'b0' }, to: { node: 'g', pin: 'a' } },
|
|
|
|
|
{ id: 'w1', from: { node: 'b', pin: 'b1' }, to: { node: 'g', pin: 'b' } },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(def)
|
|
|
|
|
const split = node(def, 'b')
|
|
|
|
|
expect(plugInto(def, split, 'g', get)).toBe(true)
|
|
|
|
|
const gate = shapeOf(node(def, 'g'), get).inputs
|
|
|
|
|
expect(split.pitch).toBe(gate[1]!.dy - gate[0]!.dy)
|
|
|
|
|
expect(touches(p, def, split)).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('input panels for cables', () => {
|
|
|
|
|
it('a bundle gets a panel with a switch per wire, plugged in and named', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
// Take the switches away: the bundle has nothing driving it.
|
|
|
|
|
def.nodes = def.nodes.filter(n => !['s0', 's1', 's2', 's3'].includes(n.id))
|
|
|
|
|
def.wires = def.wires.filter(w => !['s0', 's1', 's2', 's3'].includes(w.from.node))
|
|
|
|
|
syncCables(p)
|
|
|
|
|
const panel = cablePanel(def, res.bundle.id, lookup(p))!
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(panel.rows!.map(r => r.name)).toEqual(['A0', 'A1', 'A2', 'A3'])
|
|
|
|
|
expect(res.bundle.plug).toBe(panel.id)
|
|
|
|
|
expect(touches(p, def, res.bundle)).toBe(true)
|
|
|
|
|
// Flipping row 2 sets A2.
|
|
|
|
|
const s = new Simulator(compile(lookup(p), def.id))
|
|
|
|
|
s.setSource(rowSource(panel.id, panel.rows![2]!.id), true)
|
|
|
|
|
s.run()
|
|
|
|
|
expect([0, 1, 2, 3].map(i => s.pin(`l${i}`, 'a')).join('')).toBe('0010')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a bundle plugged into a panel takes the spacing of its rows', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
def.nodes = def.nodes.filter(n => !['s0', 's1', 's2', 's3'].includes(n.id))
|
|
|
|
|
def.wires = def.wires.filter(w => !['s0', 's1', 's2', 's3'].includes(w.from.node))
|
|
|
|
|
const panel = cablePanel(def, res.bundle.id, lookup(p))!
|
|
|
|
|
// A gap of two rows above the third row.
|
|
|
|
|
const at = panelOffsets(panel.rows!)
|
|
|
|
|
panel.rows!.forEach((r, i) => (r.dy = at[i]! + (i >= 2 ? 2 : 0)))
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(res.bundle.offsets).toEqual([0, 1, 4, 5])
|
|
|
|
|
expect(touches(p, def, res.bundle)).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('copying plugged ends', () => {
|
|
|
|
|
it('a copied end plugs into the copy of its part, or into nothing', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const res = bundleWires(def, ['wi0', 'wi1', 'wi2', 'wi3'], lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
const both = pasteClip(def, copySelection(def, new Set(['add', res.split.id])), 0, 30)
|
|
|
|
|
const copy = both.map(id => node(def, id)).find(n => n.kind === 'split')!
|
|
|
|
|
const chip = both.map(id => node(def, id)).find(n => n.kind === 'chip')!
|
|
|
|
|
expect(copy.plug).toBe(chip.id)
|
|
|
|
|
const alone = pasteClip(def, copySelection(def, new Set([res.split.id])), 0, 60)
|
|
|
|
|
expect(node(def, alone[0]!).plug).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('cables saved before plugs existed', () => {
|
|
|
|
|
it('stay where they are and compute the same; only names are added', () => {
|
|
|
|
|
const { p, def } = adder()
|
|
|
|
|
const before = sums(p, def)
|
|
|
|
|
def.nodes.push({ id: 'b', kind: 'bundle', bits: 4, x: 8, y: 2 }, { id: 'sp', kind: 'split', bits: 4, x: 16, y: 2 })
|
|
|
|
|
def.wires = def.wires.filter(w => !['wi0', 'wi1', 'wi2', 'wi3'].includes(w.id))
|
|
|
|
|
const ins = chipPins(lookup(p)('ex-add4')!).inputs
|
|
|
|
|
for (const i of [0, 1, 2, 3]) {
|
|
|
|
|
def.wires.push({ id: `f${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: 'b', pin: `b${i}` } })
|
|
|
|
|
def.wires.push({ id: `t${i}`, from: { node: 'sp', pin: `b${i}` }, to: { node: 'add', pin: ins[i]!.key } })
|
|
|
|
|
}
|
|
|
|
|
def.wires.push({ id: 'c', from: { node: 'b', pin: 'q' }, to: { node: 'sp', pin: 'a' } })
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect([node(def, 'b').x, node(def, 'b').y, node(def, 'sp').x, node(def, 'sp').y]).toEqual([8, 2, 16, 2])
|
|
|
|
|
expect(node(def, 'b').plug ?? node(def, 'sp').plug).toBeUndefined()
|
|
|
|
|
expect(node(def, 'sp').names).toEqual(['A0', 'A1', 'A2', 'A3'])
|
|
|
|
|
expect(sums(p, def)).toEqual(before)
|
|
|
|
|
})
|
|
|
|
|
})
|
BF input panels are component inputs, cable ends plug in more reliably; A merge panels, hex pass-through, design a component's box
- Inside a component, the switch and button rows of its input panels are now input pins (in order with the other inputs, named after the rows); "Rows are component inputs" can be turned off per panel to keep the old fixed switches
- Select input panels, Merge: one panel with all rows, spacing and wires kept; circuits that place this one keep their wires
- Cable ends plug in more often: bundling orders the wires by the part a side runs to, an end with only some wires on a part still plugs, an end placed by hand next to the pins plugs, and dragging an end near its part shows green rings and plugs it on drop
- Hex displays pass their four bits on (outputs on the right), so they can sit in the middle of a bus
- Design the box: move lamps, displays, the screen and the name on a component's box, draw lines, boxes, circles and text in six colours, size it, leave things off; every placed copy shows it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:49:29 +00:00
|
|
|
|
|
|
|
|
describe('plugging in more often', () => {
|
|
|
|
|
it('outputs of one part bundled to scattered targets: the bundle plugs into the part', () => {
|
|
|
|
|
const { p, def, outs } = adder()
|
|
|
|
|
// Lamps in reverse order: wire order follows the Adder's pins, not the lamps.
|
|
|
|
|
for (const i of [0, 1, 2, 3, 4]) node(def, `l${i}`).y = 40 - i * 3
|
|
|
|
|
const res = bundleWires(def, outs.map((_, i) => `wo${i}`), lookup(p))
|
|
|
|
|
if (!res.ok) throw new Error(res.error)
|
|
|
|
|
expect(res.bundle.plug).toBe('add')
|
|
|
|
|
expect(touches(p, def, res.bundle)).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('an end with only some of its wires on a part still plugs in', () => {
|
|
|
|
|
const { p, def, ins } = adder()
|
|
|
|
|
def.nodes.push({ id: 'sp', kind: 'split', bits: 5, x: 0, y: 30 })
|
|
|
|
|
for (const i of [0, 1, 2, 3]) def.wires.push({ id: `t${i}`, from: { node: 'sp', pin: `b${i}` }, to: { node: 'add', pin: ins[i]! } })
|
|
|
|
|
const sp = node(def, 'sp')
|
|
|
|
|
const lay = plugLayout(def, sp, node(def, 'add'), lookup(p))!
|
|
|
|
|
sp.x = lay.x
|
|
|
|
|
sp.y = lay.y
|
|
|
|
|
syncCables(p)
|
|
|
|
|
expect(sp.plug).toBe('add')
|
|
|
|
|
})
|
|
|
|
|
})
|