logigate/tests/ports.test.ts

253 lines
12 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
import { checkCircuit, doublyFedLines } from '../app/lib/logic/check'
import { compile } from '../app/lib/logic/compile'
import { pinKindOf, shapeOf } from '../app/lib/logic/geometry'
import { pinName } from '../app/lib/logic/labels'
import { createProject, lineForWire, lookup, pruneWires, splitPort } from '../app/lib/logic/ops'
import { attachPanel } from '../app/lib/logic/panels'
import { toProject } from '../app/lib/logic/project'
import { Simulator } from '../app/lib/logic/sim'
import type { CircuitNode, ComponentDef, Project, Wire } from '../app/lib/logic/types'
const w = (id: string, from: [string, string], to: [string, string]): Wire => ({ id, from: { node: from[0], pin: from[1] }, to: { node: to[0], pin: to[1] } })
/**
* The "Mini HDMI" of the connector tests: D0 and D1 out → in, HPD back.
* Source sends A and B out of its out port and reports HPD; Sink shows the
* data as X and Y and drives HPD from H. Root places one of each, unwired.
*/
function project(): Project {
const p = createProject({ examples: false })
p.connectors = [{ id: 'hdmi', name: 'Mini HDMI', color: 'blue', lines: [{ name: 'D0' }, { name: 'D1' }, { name: 'HPD', back: true }] }]
const source: ComponentDef = {
id: 'source',
name: 'Source',
symbol: 'box',
nodes: [
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
{ id: 'b', kind: 'input', x: 0, y: 2, label: 'B' },
{ id: 'tx', kind: 'portout', conn: 'hdmi', x: 20, y: 0, label: 'OUT' },
{ id: 'hpd', kind: 'output', x: 10, y: 8, label: 'HPD' },
],
wires: [w('w1', ['a', 'q'], ['tx', 'l0']), w('w2', ['b', 'q'], ['tx', 'l1']), w('w3', ['tx', 'l2'], ['hpd', 'a'])],
}
const sink: ComponentDef = {
id: 'sink',
name: 'Sink',
symbol: 'box',
nodes: [
{ id: 'rx', kind: 'portin', conn: 'hdmi', x: 0, y: 0, label: 'IN' },
{ id: 'x', kind: 'output', x: 20, y: 0, label: 'X' },
{ id: 'y', kind: 'output', x: 20, y: 2, label: 'Y' },
{ id: 'h', kind: 'input', x: 10, y: 8, label: 'H' },
],
wires: [w('w1', ['rx', 'l0'], ['x', 'a']), w('w2', ['rx', 'l1'], ['y', 'a']), w('w3', ['h', 'q'], ['rx', 'l2'])],
}
const root: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 'sa', kind: 'input', x: 0, y: 0 },
{ id: 'sb', kind: 'input', x: 0, y: 2 },
{ id: 'sh', kind: 'input', x: 0, y: 8 },
{ id: 'src', kind: 'chip', chip: 'source', x: 6, y: 0 },
{ id: 'snk', kind: 'chip', chip: 'sink', x: 30, y: 0 },
{ id: 'lamp', kind: 'lamp', x: 50, y: 0 },
],
wires: [w('r1', ['sa', 'q'], ['src', 'a']), w('r2', ['sb', 'q'], ['src', 'b']), w('r3', ['sh', 'q'], ['snk', 'h'])],
}
p.components.push(source, sink, root)
return p
}
const node = (p: Project, def: string, id: string) => p.components.find(d => d.id === def)!.nodes.find(n => n.id === id)!
const root = (p: Project) => p.components.find(d => d.id === 'root')!
function run(p: Project, on: Record<string, boolean>) {
const s = new Simulator(compile(lookup(p), 'root'))
for (const [k, v] of Object.entries(on)) s.setSource(k, v)
s.run()
return s
}
describe('ports on a component box', () => {
it('sit at the very top of their side, above the single pins', () => {
const p = project()
// More single pins than before, so a port at the bottom would show.
node(p, 'sink', 'h').y = -4
p.components.find(d => d.id === 'sink')!.nodes.push({ id: 'k', kind: 'input', x: 10, y: 10, label: 'K' })
const get = lookup(p)
const snk = shapeOf({ id: 'n', kind: 'chip', chip: 'sink', x: 0, y: 0 }, get)
expect(snk.inputs.map(i => [i.key, i.dy])).toEqual([['rx', 0], ['h', 1], ['k', 2]])
const src = shapeOf({ id: 'n', kind: 'chip', chip: 'source', x: 0, y: 0 }, get)
expect(src.outputs.map(o => [o.key, o.dx, o.dy])).toEqual([['tx', src.body.w, 0], ['hpd', src.body.w, 1]])
})
it('also at the top of a box drawn by hand', () => {
const p = project()
const sink = p.components.find(d => d.id === 'sink')!
sink.design = { w: 6, h: 3, pinAt: { h: 0, x: 2 } }
const s = shapeOf({ id: 'n', kind: 'chip', chip: 'sink', x: 0, y: 0 }, lookup(p))
expect(s.inputs.map(i => [i.key, i.dy])).toEqual([['rx', 0], ['h', 1]])
expect(s.outputs.map(o => [o.key, o.dx, o.dy])).toEqual([['x', 6, 2], ['y', 6, 3]])
})
it('split, a port shows a pin per line right below its socket, each facing out of its side', () => {
const p = project()
const get = lookup(p)
const snk = shapeOf({ id: 'n', kind: 'chip', chip: 'sink', x: 0, y: 0, splitPorts: ['rx'] }, get)
// In port on the left: D0 and D1 come in (inputs), HPD goes back out (an output on the left).
expect(snk.inputs.map(i => [i.key, i.label, i.dx, i.dy, i.face])).toEqual([
['rx', 'IN', 0, 0, undefined],
['rx.l0', 'D0', 0, 1, -1],
['rx.l1', 'D1', 0, 2, -1],
['h', 'H', 0, 4, undefined],
])
expect(snk.outputs.find(o => o.key === 'rx.l2')).toMatchObject({ label: 'HPD', dx: 0, dy: 3, face: -1, port: 'rx' })
// Out port on the right: D0 and D1 go out, HPD comes back in (an input on the right).
const src = shapeOf({ id: 'n', kind: 'chip', chip: 'source', x: 0, y: 0, splitPorts: ['tx'] }, get)
const right = src.body.w
expect(src.outputs.map(o => [o.key, o.dx, o.dy, o.face])).toEqual([
['tx', right, 0, undefined],
['tx.l0', right, 1, 1],
['tx.l1', right, 2, 1],
['hpd', right, 4, undefined],
])
expect(src.inputs.find(i => i.key === 'tx.l2')).toMatchObject({ label: 'HPD', dx: right, dy: 3, face: 1 })
// Lines take single wires; the socket still takes the cable.
expect(pinKindOf(snk.inputs[1]!)).toBe('')
expect(pinKindOf(snk.inputs[0]!)).toBe('c:hdmi')
})
it('split on a box drawn by hand, the lines follow the socket and push the pins below down', () => {
const p = project()
p.components.find(d => d.id === 'sink')!.design = { w: 6, h: 3, pinAt: { h: 1 } }
const s = shapeOf({ id: 'n', kind: 'chip', chip: 'sink', x: 0, y: 0, splitPorts: ['rx'] }, lookup(p))
expect(s.inputs.map(i => [i.key, i.dy])).toEqual([['rx', 0], ['rx.l0', 1], ['rx.l1', 2], ['h', 4]])
expect(s.outputs.find(o => o.key === 'rx.l2')).toMatchObject({ dx: 0, dy: 3 })
})
it('a line wired from outside drives the inside, and a back line brings the inside out', () => {
const p = project()
node(p, 'root', 'snk').splitPorts = ['rx']
root(p).wires.push(w('d0', ['sa', 'q'], ['snk', 'rx.l0']), w('hp', ['snk', 'rx.l2'], ['lamp', 'a']))
for (const [a, h] of [[0, 0], [1, 0], [0, 1], [1, 1]] as const) {
const s = run(p, { sa: !!a, sh: !!h })
expect([s.pin('snk', 'x'), s.pin('snk', 'rx.l2'), s.pin('lamp', 'q')]).toEqual([a, h, h])
}
})
it('an out port split the same way: its lines leave on the right, the back line comes in there', () => {
const p = project()
node(p, 'root', 'src').splitPorts = ['tx']
root(p).wires.push(w('d1', ['src', 'tx.l1'], ['lamp', 'a']), w('hp', ['sh', 'q'], ['src', 'tx.l2']))
for (const [b, h] of [[0, 1], [1, 0], [1, 1]] as const) {
const s = run(p, { sb: !!b, sh: !!h })
expect([s.pin('lamp', 'q'), s.pin('src', 'hpd')]).toEqual([b, h])
}
})
it('a cable on the socket and wires on the lines work together', () => {
const p = project()
node(p, 'root', 'snk').splitPorts = ['rx']
root(p).wires.push(w('cable', ['src', 'tx'], ['snk', 'rx']), w('hp', ['snk', 'rx.l2'], ['lamp', 'a']))
const s = run(p, { sa: true, sb: false, sh: true })
// The cable still carries every line; the line pins show what it carries.
expect([s.pin('snk', 'x'), s.pin('snk', 'y'), s.pin('src', 'hpd')]).toEqual([1, 0, 1])
expect([s.pin('snk', 'rx.l0'), s.pin('snk', 'rx.l1'), s.pin('lamp', 'q')]).toEqual([1, 0, 1])
expect(doublyFedLines(root(p), node(p, 'root', 'snk'), lookup(p))).toEqual([])
// A wire into a line the cable feeds as well: both drive it, and the check says so.
root(p).wires.push(w('d0', ['sb', 'q'], ['snk', 'rx.l0']))
expect(doublyFedLines(root(p), node(p, 'root', 'snk'), lookup(p))).toEqual(['rx.l0'])
expect(checkCircuit(root(p), lookup(p)).some(i => i.level === 'warn' && i.text.includes('D0'))).toBe(true)
})
it('passes split lines through a component that offers an inner part’s port as its own', () => {
const p = project()
p.components.push({
id: 'box',
name: 'Box',
symbol: 'box',
nodes: [
{ id: 'i', kind: 'input', x: 0, y: 0, label: 'I' },
{ id: 'inner', kind: 'chip', chip: 'source', x: 6, y: 0 },
{ id: 'p2', kind: 'portout', conn: 'hdmi', x: 30, y: 0 },
],
wires: [w('b1', ['i', 'q'], ['inner', 'a']), w('b2', ['inner', 'tx'], ['p2', 'bus'])],
})
root(p).nodes.push({ id: 'bx', kind: 'chip', chip: 'box', x: 6, y: 20, splitPorts: ['p2'] })
root(p).wires.push(w('r9', ['sa', 'q'], ['bx', 'i']), w('l0', ['bx', 'p2.l0'], ['lamp', 'a']))
expect(run(p, { sa: true }).pin('lamp', 'q')).toBe(1)
expect(run(p, { sa: false }).pin('lamp', 'q')).toBe(0)
})
it('joining a port again removes the wires on its lines and keeps the cable', () => {
const p = project()
const r = root(p)
expect(splitPort(p, r, 'snk', 'rx', true)).toBe(0)
expect(node(p, 'root', 'snk').splitPorts).toEqual(['rx'])
r.wires.push(w('cable', ['src', 'tx'], ['snk', 'rx']), w('d0', ['sa', 'q'], ['snk', 'rx.l0']), w('hp', ['snk', 'rx.l2'], ['lamp', 'a']))
expect(pruneWires(p)).toBe(false)
expect(splitPort(p, r, 'snk', 'rx', false)).toBe(2)
expect(node(p, 'root', 'snk').splitPorts).toBeUndefined()
expect(r.wires.map(x => x.id)).toEqual(['r1', 'r2', 'r3', 'cable'])
// No port of that name: nothing changes.
expect(splitPort(p, r, 'snk', 'nope', true)).toBe(0)
expect(node(p, 'root', 'snk').splitPorts).toBeUndefined()
})
it('a single wire dropped on a socket takes the first free line that runs its way', () => {
const p = project()
const r = root(p)
// Into the sink's in port: D0, then D1; from it: HPD.
expect(lineForWire(p, r, 'snk', 'rx', true)).toBe('rx.l0')
expect(lineForWire(p, r, 'snk', 'rx', false)).toBe('rx.l2')
r.wires.push(w('d0', ['sa', 'q'], ['snk', 'rx.l0']))
expect(lineForWire(p, r, 'snk', 'rx', true)).toBe('rx.l1')
r.wires.push(w('d1', ['sb', 'q'], ['snk', 'rx.l1']))
expect(lineForWire(p, r, 'snk', 'rx', true)).toBeNull()
// A line coming out can feed another wire.
r.wires.push(w('hp', ['snk', 'rx.l2'], ['lamp', 'a']))
expect(lineForWire(p, r, 'snk', 'rx', false)).toBe('rx.l2')
// The source's out port: its back line goes in, D0 comes out first.
expect(lineForWire(p, r, 'src', 'tx', true)).toBe('tx.l2')
expect(lineForWire(p, r, 'src', 'tx', false)).toBe('tx.l0')
expect(lineForWire(p, r, 'src', 'nope', true)).toBeNull()
})
it('cleans up wires on lines whose port is no longer split', () => {
const p = project()
node(p, 'root', 'snk').splitPorts = ['rx']
root(p).wires.push(w('d0', ['sa', 'q'], ['snk', 'rx.l0']))
expect(pruneWires(p)).toBe(false)
delete node(p, 'root', 'snk').splitPorts
expect(pruneWires(p)).toBe(true)
expect(root(p).wires.some(x => x.id === 'd0')).toBe(false)
})
it('a typed cable never plugs into a line, a single wire never into a socket', () => {
const p = project()
node(p, 'root', 'snk').splitPorts = ['rx']
root(p).wires.push(w('bad1', ['src', 'tx'], ['snk', 'rx.l0']), w('bad2', ['sa', 'q'], ['snk', 'rx']))
expect(pruneWires(p)).toBe(true)
expect(root(p).wires.map(x => x.id)).toEqual(['r1', 'r2', 'r3'])
})
it('names a line pin after its line, keeps the setting through loading and leaves line pins off docked panels', () => {
const p = project()
const snk = node(p, 'root', 'snk')
snk.splitPorts = ['rx']
expect(pinName(snk, 'rx.l1', lookup(p))).toBe('D1')
const data = JSON.parse(JSON.stringify(p))
data.components.find((d: ComponentDef) => d.id === 'root').nodes.find((n: CircuitNode) => n.id === 'src').splitPorts = 'tx'
const back = toProject(data)
expect(node(back, 'root', 'snk').splitPorts).toEqual(['rx'])
expect(node(back, 'root', 'src').splitPorts).toBeUndefined()
// A docked input panel gets a switch for H only: the socket and the lines are the cable's.
root(p).wires = root(p).wires.filter(x => x.id !== 'r3')
const panel = attachPanel(p, root(p), 'snk')!
expect(panel.rows!.map(r => r.id)).toEqual(['h'])
})
})