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 { describe, expect, it } from 'vitest'
|
|
|
|
|
import { compile } from '../app/lib/logic/compile'
|
|
|
|
|
import { chipPins, panelOffsets } from '../app/lib/logic/geometry'
|
|
|
|
|
import { createProject, lookup } from '../app/lib/logic/ops'
|
2026-10-21 12:38:59 +00:00
|
|
|
import { attachPanel, combineIntoPanel, mergePanels } from '../app/lib/logic/panels'
|
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 { Simulator } from '../app/lib/logic/sim'
|
|
|
|
|
import type { ComponentDef } from '../app/lib/logic/types'
|
|
|
|
|
|
|
|
|
|
describe('merging input panels', () => {
|
|
|
|
|
it('rows of the lower panel follow below, with their wires, as one panel', () => {
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 'd',
|
|
|
|
|
name: 'D',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'p2', kind: 'panel', x: 0, y: 10, rows: [{ id: 'r1', type: 'switch', label: 'B0' }, { id: 'r2', type: 'button', label: 'B1', dy: 2 }] },
|
|
|
|
|
{ id: 'p1', kind: 'panel', x: 0, y: 0, attach: 'x', rows: [{ id: 'r1', type: 'switch', label: 'A0' }] },
|
|
|
|
|
{ id: 'l', kind: 'lamp', x: 10, y: 0 },
|
|
|
|
|
{ id: 'm', kind: 'lamp', x: 10, y: 4 },
|
|
|
|
|
],
|
|
|
|
|
wires: [
|
|
|
|
|
{ id: 'w1', from: { node: 'p1', pin: 'r1' }, to: { node: 'l', pin: 'a' } },
|
|
|
|
|
{ id: 'w2', from: { node: 'p2', pin: 'r2' }, to: { node: 'm', pin: 'a' } },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
const res = mergePanels(def, ['p1', 'p2'])!
|
|
|
|
|
expect(res.panel.id).toBe('p1')
|
|
|
|
|
expect(def.nodes.some(n => n.id === 'p2')).toBe(false)
|
|
|
|
|
expect(res.panel.attach).toBeUndefined()
|
|
|
|
|
const rows = res.panel.rows!
|
|
|
|
|
expect(rows.map(r => r.label)).toEqual(['A0', 'B0', 'B1'])
|
|
|
|
|
// Spacing kept: B1 was two rows under B0.
|
|
|
|
|
expect(panelOffsets(rows)).toEqual([0, 1, 3])
|
|
|
|
|
// The wire from B1 now leaves the merged panel; the clashing row id got a new one.
|
|
|
|
|
const w2 = def.wires.find(w => w.id === 'w2')!
|
|
|
|
|
expect(w2.from.node).toBe('p1')
|
|
|
|
|
expect(rows.find(r => r.label === 'B1')!.id).toBe(w2.from.pin)
|
|
|
|
|
expect(new Set(rows.map(r => r.id)).size).toBe(3)
|
|
|
|
|
expect(res.renamed.get('p2:r2')).toBe(`p1:${w2.from.pin}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('a merged panel inside a component gives one pin per row, in order', () => {
|
|
|
|
|
const p = createProject({ examples: false })
|
|
|
|
|
const inner: ComponentDef = {
|
|
|
|
|
id: 'inner',
|
|
|
|
|
name: 'Inner',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'a', kind: 'panel', x: 0, y: 0, rows: [{ id: 'r', type: 'switch', label: 'X' }] },
|
|
|
|
|
{ id: 'b', kind: 'panel', x: 0, y: 5, rows: [{ id: 'r', type: 'switch', label: 'Y' }] },
|
|
|
|
|
{ id: 'q', kind: 'output', x: 10, y: 5, label: 'Q' },
|
|
|
|
|
],
|
|
|
|
|
wires: [{ id: 'w', from: { node: 'b', pin: 'r' }, to: { node: 'q', pin: 'a' } }],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(inner)
|
|
|
|
|
mergePanels(inner, ['a', 'b'])
|
|
|
|
|
const pins = chipPins(inner).inputs
|
|
|
|
|
expect(pins.map(x => x.label)).toEqual(['X', 'Y'])
|
|
|
|
|
const root: ComponentDef = {
|
|
|
|
|
id: 'root',
|
|
|
|
|
name: 'Root',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 's', kind: 'input', x: -5, y: 0 },
|
|
|
|
|
{ id: 'u', kind: 'chip', chip: 'inner', x: 0, y: 0 },
|
|
|
|
|
],
|
|
|
|
|
wires: [{ id: 'x', from: { node: 's', pin: 'q' }, to: { node: 'u', pin: pins[1]!.key } }],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(root)
|
|
|
|
|
const sim = new Simulator(compile(lookup(p), 'root'))
|
|
|
|
|
sim.setSource('s', true)
|
|
|
|
|
sim.run()
|
|
|
|
|
expect(sim.pin('u', 'q')).toBe(1)
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-10-21 12:38:59 +00:00
|
|
|
|
|
|
|
|
describe('output panels', () => {
|
|
|
|
|
it('show their signals, pass them on, and are the outputs of their component', () => {
|
|
|
|
|
const p = createProject({ examples: false })
|
|
|
|
|
const inner: ComponentDef = {
|
|
|
|
|
id: 'inner',
|
|
|
|
|
name: 'Inner',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
|
|
|
|
|
{ id: 'o', kind: 'outpanel', x: 6, y: 0, rows: [{ id: 'r1', type: 'switch', label: 'Y' }, { id: 'r2', type: 'switch', label: 'Z' }] },
|
|
|
|
|
{ id: 'l', kind: 'lamp', x: 14, y: 0 },
|
|
|
|
|
],
|
|
|
|
|
wires: [
|
|
|
|
|
{ id: 'w', from: { node: 'a', pin: 'q' }, to: { node: 'o', pin: 'r1' } },
|
|
|
|
|
{ id: 'x', from: { node: 'o', pin: 'r1' }, to: { node: 'l', pin: 'a' } },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(inner)
|
|
|
|
|
expect(chipPins(inner).outputs).toEqual([
|
|
|
|
|
{ key: 'o:r1', label: 'Y' },
|
|
|
|
|
{ key: 'o:r2', label: 'Z' },
|
|
|
|
|
])
|
|
|
|
|
const root: ComponentDef = {
|
|
|
|
|
id: 'root',
|
|
|
|
|
name: 'Root',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 's', kind: 'input', x: -5, y: 0 },
|
|
|
|
|
{ id: 'u', kind: 'chip', chip: 'inner', x: 0, y: 0 },
|
|
|
|
|
{ id: 'm', kind: 'lamp', x: 10, y: 0 },
|
|
|
|
|
],
|
|
|
|
|
wires: [
|
|
|
|
|
{ id: 'a', from: { node: 's', pin: 'q' }, to: { node: 'u', pin: 'a' } },
|
|
|
|
|
{ id: 'b', from: { node: 'u', pin: 'o:r1' }, to: { node: 'm', pin: 'a' } },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(root)
|
|
|
|
|
const sim = new Simulator(compile(lookup(p), 'root'))
|
|
|
|
|
sim.setSource('s', true)
|
|
|
|
|
sim.run()
|
|
|
|
|
expect(sim.pin('m', 'a')).toBe(1)
|
|
|
|
|
// In its own circuit: the row lights and passes the signal on.
|
|
|
|
|
const own = new Simulator(compile(lookup(p), 'inner'))
|
|
|
|
|
own.setSource('a', true)
|
|
|
|
|
own.run()
|
|
|
|
|
expect([own.pin('o', 'r1'), own.pin('l', 'a')]).toEqual([1, 1])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('docks to the right of a component with a lamp per output, and follows new outputs', () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const root: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes: [{ id: 'u', kind: 'chip', chip: 'ex-add4', x: 0, y: 0 }], wires: [] }
|
|
|
|
|
p.components.push(root)
|
|
|
|
|
const panel = attachPanel(p, root, 'u', 'out')!
|
|
|
|
|
expect(panel.kind).toBe('outpanel')
|
|
|
|
|
expect(panel.rows!.map(r => r.name)).toEqual(['S0', 'S1', 'S2', 'S3', 'Cout'])
|
|
|
|
|
expect(panel.x).toBeGreaterThan(0)
|
|
|
|
|
expect(root.wires.every(w => w.from.node === 'u' && w.to.node === panel.id)).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('combining parts into a panel', () => {
|
|
|
|
|
it('switches and a button become one input panel; their pins, names, states and wires stay', () => {
|
|
|
|
|
const p = createProject({ examples: false })
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 'inner',
|
|
|
|
|
name: 'Inner',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A', on: true, key: 'q' },
|
|
|
|
|
{ id: 'b', kind: 'input', x: 0, y: 3, label: 'B' },
|
|
|
|
|
{ id: 'go', kind: 'button', x: 0, y: 4, label: 'GO' },
|
|
|
|
|
{ id: 'o', kind: 'output', x: 10, y: 0, label: 'Q' },
|
|
|
|
|
],
|
|
|
|
|
wires: [{ id: 'w', from: { node: 'a', pin: 'q' }, to: { node: 'o', pin: 'a' } }],
|
|
|
|
|
}
|
|
|
|
|
p.components.push(def)
|
|
|
|
|
const res = combineIntoPanel(def, ['a', 'b', 'go'], lookup(p))!
|
|
|
|
|
const panel = res.panel
|
|
|
|
|
expect(panel.kind).toBe('panel')
|
|
|
|
|
expect(panel.inner).toBeUndefined()
|
|
|
|
|
expect(panel.rows!.map(r => [r.label, r.type, r.on ?? false, r.key ?? ''])).toEqual([
|
|
|
|
|
['A', 'switch', true, 'q'],
|
|
|
|
|
['B', 'switch', false, ''],
|
|
|
|
|
['GO', 'button', false, ''],
|
|
|
|
|
])
|
|
|
|
|
// Spread-out parts keep their distance; B and GO were one row apart.
|
|
|
|
|
expect(panelOffsets(panel.rows!)).toEqual([0, 3, 4])
|
|
|
|
|
expect(def.wires[0]!.from).toEqual({ node: panel.id, pin: 'a' })
|
|
|
|
|
expect(res.renamed.get("a")).toBe(`${panel.id}:a`)
|
|
|
|
|
expect(def.nodes.some(n => ['a', 'b', 'go'].includes(n.id))).toBe(false)
|
|
|
|
|
// Still drives Q when used: pins A, B, GO.
|
|
|
|
|
expect(chipPins(def).inputs.map(x => x.label)).toEqual(['A', 'B', 'GO'])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('lamps become an output panel that stays inside, as lamps did', () => {
|
|
|
|
|
const def: ComponentDef = {
|
|
|
|
|
id: 'd',
|
|
|
|
|
name: 'D',
|
|
|
|
|
symbol: 'box',
|
|
|
|
|
nodes: [
|
|
|
|
|
{ id: 's', kind: 'input', x: 0, y: 0 },
|
|
|
|
|
{ id: 'l1', kind: 'lamp', x: 6, y: 0, label: 'L1', color: 'red' },
|
|
|
|
|
{ id: 'l2', kind: 'lamp', x: 6, y: 1 },
|
|
|
|
|
],
|
|
|
|
|
wires: [{ id: 'w', from: { node: 's', pin: 'q' }, to: { node: 'l1', pin: 'a' } }],
|
|
|
|
|
}
|
|
|
|
|
const p = createProject({ examples: false })
|
|
|
|
|
p.components.push(def)
|
|
|
|
|
const res = combineIntoPanel(def, ['l1', 'l2'], lookup(p))!
|
|
|
|
|
expect(res.panel.kind).toBe('outpanel')
|
|
|
|
|
expect(res.panel.inner).toBe(true)
|
|
|
|
|
expect(res.panel.rows![0]).toMatchObject({ label: 'L1', color: 'red' })
|
|
|
|
|
expect(def.wires[0]!.to).toEqual({ node: res.panel.id, pin: 'l1' })
|
|
|
|
|
expect(combineIntoPanel(def, ['s'], lookup(p))).toBeNull()
|
|
|
|
|
})
|
|
|
|
|
})
|