logigate/tests/connectors.test.ts

154 lines
6.8 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
import { compile } from '../app/lib/logic/compile'
import { routeWire, shapeOf } from '../app/lib/logic/geometry'
import { createProject, lookup, pruneWires } from '../app/lib/logic/ops'
import { toProject } from '../app/lib/logic/project'
import { Simulator } from '../app/lib/logic/sim'
import type { 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] } })
/**
* A made-up "Mini HDMI": two data lines out → in, and a hot-plug line back.
* Source sends A and B out through its out port and reports the hot-plug
* line as HPD; Sink shows the data as X and Y and drives hot-plug from H.
*/
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 },
],
wires: [
w('r1', ['sa', 'q'], ['src', 'a']),
w('r2', ['sb', 'q'], ['src', 'b']),
w('r3', ['sh', 'q'], ['snk', 'h']),
w('cable', ['src', 'tx'], ['snk', 'rx']),
],
}
p.components.push(source, sink, root)
return p
}
function run(p: Project, rootId: string, on: Record<string, boolean>) {
const s = new Simulator(compile(lookup(p), rootId))
for (const [k, v] of Object.entries(on)) s.setSource(k, v)
s.run()
return s
}
describe('connector ports', () => {
it('puts each port on the component box as one cable pin of its type', () => {
const p = project()
const get = lookup(p)
const src = shapeOf({ id: 'n', kind: 'chip', chip: 'source', x: 0, y: 0 }, get)
const port = src.outputs.find(o => o.key === 'tx')!
expect(port).toMatchObject({ bus: true, conn: 'hdmi', label: 'OUT' })
expect(src.inputs.map(i => i.key)).toEqual(['a', 'b'])
const snk = shapeOf({ id: 'n', kind: 'chip', chip: 'sink', x: 0, y: 0 }, get)
expect(snk.inputs.find(i => i.key === 'rx')).toMatchObject({ bus: true, conn: 'hdmi' })
})
it('a port part has a pin per line on its inner side, facing into the circuit', () => {
const get = lookup(project())
const out = shapeOf({ id: 'n', kind: 'portout', conn: 'hdmi', x: 0, y: 0 }, get)
expect(out.inputs.map(i => i.key)).toEqual(['bus', 'l0', 'l1'])
expect(out.outputs).toMatchObject([{ key: 'l2', face: -1, dx: 0 }])
const inn = shapeOf({ id: 'n', kind: 'portin', conn: 'hdmi', x: 0, y: 0 }, get)
expect(inn.outputs.map(o => o.key)).toEqual(['bus', 'l0', 'l1'])
expect(inn.inputs).toMatchObject([{ key: 'l2', face: 1 }])
})
it('one cable carries the forward lines one way and the back line the other', () => {
const p = project()
for (const [a, b, h] of [[0, 0, 0], [1, 0, 1], [0, 1, 0], [1, 1, 1]] as const) {
const s = run(p, 'root', { sa: !!a, sb: !!b, sh: !!h })
expect([s.pin('snk', 'x'), s.pin('snk', 'y'), s.pin('src', 'hpd')]).toEqual([a, b, h])
}
})
it('passes a port straight through a component that wraps another', () => {
const p = project()
// "Box" holds a Source and offers its port as its own.
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'])],
})
const root = p.components.find(d => d.id === 'root')!
root.nodes.push({ id: 'bx', kind: 'chip', chip: 'box', x: 6, y: 20 }, { id: 'snk2', kind: 'chip', chip: 'sink', x: 30, y: 20 })
root.wires.push(w('r9', ['sa', 'q'], ['bx', 'i']), w('cable2', ['bx', 'p2'], ['snk2', 'rx']))
expect(run(p, 'root', { sa: true }).pin('snk2', 'x')).toBe(1)
expect(run(p, 'root', { sa: false }).pin('snk2', 'x')).toBe(0)
})
it('drops a cable between ports of different types, keeps the rest', () => {
const p = project()
p.connectors!.push({ id: 'rj45', name: 'RJ45', lines: [{ name: 'TX' }, { name: 'RX', back: true }] })
expect(pruneWires(p)).toBe(false)
p.components.find(d => d.id === 'sink')!.nodes.find(n => n.id === 'rx')!.conn = 'rj45'
expect(pruneWires(p)).toBe(true)
expect(p.components.find(d => d.id === 'root')!.wires.some(x => x.id === 'cable')).toBe(false)
expect(p.components.find(d => d.id === 'root')!.wires).toHaveLength(3)
})
it('keeps connector types through validation and cleans bad ones', () => {
const p = project()
const data = JSON.parse(JSON.stringify(p))
data.connectors.push({ id: 'x', name: 'Bad', color: 'chartreuse', lines: [{ name: 5 }, { name: 'OK', back: 'yes' }] }, { name: 'no id' })
const back = toProject(data)
expect(back.connectors).toEqual([
{ id: 'hdmi', name: 'Mini HDMI', color: 'blue', lines: [{ name: 'D0' }, { name: 'D1' }, { name: 'HPD', back: true }] },
{ id: 'x', name: 'Bad', lines: [{ name: 'OK' }] },
])
})
it('routes wires to pins facing the other way around the right side', () => {
// Out of a normal output, into an input that faces right (a back line of an in port).
expect(routeWire([10, 0], [2, 4], undefined, 1, 1)).toEqual([[10, 0], [11, 0], [11, 4], [2, 4]])
// Out of a pin facing left, into a normal input: mirrored usual route.
const r = routeWire([20, 0], [2, 4], undefined, -1, -1)
expect(r[0]).toEqual([20, 0])
expect(r.at(-1)).toEqual([2, 4])
expect(r[1]![0]).toBeLessThan(20)
})
})