logigate/tests/panels.test.ts

234 lines
9.2 KiB
TypeScript

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'
import { attachPanel, combineIntoPanel, insertPanelRow, mergePanels, movePanelRow } from '../app/lib/logic/panels'
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'])
// Packed: B1 was two rows under B0, now right below it.
expect(panelOffsets(rows)).toEqual([0, 1, 2])
// 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)
})
})
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, ''],
])
// Packed without gaps, though A and B were three rows apart.
expect(panelOffsets(panel.rows!)).toEqual([0, 1, 2])
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()
})
})
describe('editing the rows of a free panel', () => {
const panel = () => ({
id: 'p',
kind: 'panel' as const,
x: 0,
y: 0,
rows: [
{ id: 'a', type: 'switch' as const },
{ id: 'b', type: 'button' as const, dy: 3 },
{ id: 'c', type: 'button' as const },
],
})
it('a row added above another takes its place; the rest move down, gaps kept', () => {
const p = panel()
expect(panelOffsets(p.rows)).toEqual([0, 3, 4])
const r = insertPanelRow(p, 1)
expect(p.rows.map(x => x.id)).toEqual(['a', r.id, 'b', 'c'])
expect(panelOffsets(p.rows)).toEqual([0, 3, 4, 5])
expect(r.type).toBe('button')
})
it('rows added above the first and below the last', () => {
const p = panel()
const top = insertPanelRow(p, 0)
expect(panelOffsets(p.rows)).toEqual([0, 1, 4, 5])
expect(top.type).toBe('switch')
const end = insertPanelRow(p, 99)
expect(p.rows.at(-1)!.id).toBe(end.id)
expect(panelOffsets(p.rows)).toEqual([0, 1, 4, 5, 6])
})
it('a dragged row trades places: the places and gaps stay', () => {
const p = panel()
expect(movePanelRow(p, 2, 0)).toBe(true)
expect(p.rows.map(x => x.id)).toEqual(['c', 'a', 'b'])
expect(panelOffsets(p.rows)).toEqual([0, 3, 4])
expect(movePanelRow(p, 1, 1)).toBe(false)
})
})