86 lines
3.7 KiB
TypeScript
86 lines
3.7 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { shapeOf } from '../app/lib/logic/geometry'
|
|
import { latchedTo, latchInto, latchLayout, latchTarget, syncLatches } from '../app/lib/logic/latch'
|
|
import { createProject, lookup } from '../app/lib/logic/ops'
|
|
import type { ComponentDef, Project } from '../app/lib/logic/types'
|
|
|
|
/** A 4-bit counter's outputs (the example component) feeding a hex display placed further right. */
|
|
function setup(order = [0, 1, 2, 3]): { p: Project; def: ComponentDef } {
|
|
const p = createProject({ examples: true })
|
|
const counter = p.components.find(d => d.id === 'ex-counter4') ?? p.components.find(d => /counter/i.test(d.name))!
|
|
const def: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes: [{ id: 'c', kind: 'chip', chip: counter.id, x: 0, y: 0 }, { id: 'h', kind: 'hex', x: 20, y: 3 }], wires: [] }
|
|
const outs = shapeOf(def.nodes[0]!, lookup(p)).outputs.slice(0, 4)
|
|
outs.forEach((o, i) => def.wires.push({ id: `w${i}`, from: { node: 'c', pin: o.key }, to: { node: 'h', pin: `b${order[i]}` } }))
|
|
p.components.push(def)
|
|
return { p, def }
|
|
}
|
|
|
|
describe('latching a part onto another', () => {
|
|
it('a display wired to four outputs in order latches onto them: its inputs on their pins', () => {
|
|
const { p, def } = setup()
|
|
const get = lookup(p)
|
|
const hex = def.nodes[1]!
|
|
expect(latchTarget(def, hex, get)?.id).toBe('c')
|
|
expect(latchInto(def, hex, 'c', get)).toBe(true)
|
|
expect(hex.latch).toBe('c')
|
|
const chip = def.nodes[0]!
|
|
const outs = shapeOf(chip, get).outputs.slice(0, 4)
|
|
const ins = shapeOf(hex, get).inputs
|
|
outs.forEach((o, i) => {
|
|
expect(hex.x + ins[i]!.dx).toBe(chip.x + o.dx)
|
|
expect(hex.y + ins[i]!.dy).toBe(chip.y + o.dy)
|
|
})
|
|
})
|
|
|
|
it('wired in another order it does not fit, so it does not latch', () => {
|
|
const { p, def } = setup([1, 0, 2, 3])
|
|
expect(latchTarget(def, def.nodes[1]!, lookup(p))).toBeUndefined()
|
|
})
|
|
|
|
it('a pin that would land on a pin it is not wired to keeps it from latching', () => {
|
|
const { p, def } = setup()
|
|
// Only three of the four outputs are wired: the fourth input would sit on an output without a wire.
|
|
def.wires = def.wires.filter(w => w.id !== 'w3')
|
|
const get = lookup(p)
|
|
expect(latchLayout(def, def.nodes[1]!, def.nodes[0]!, get)).toBeNull()
|
|
})
|
|
|
|
it('parts whose shared pins already touch latch by themselves, the smaller onto the larger', () => {
|
|
const { p, def } = setup()
|
|
const get = lookup(p)
|
|
const hex = def.nodes[1]!
|
|
const lay = latchLayout(def, hex, def.nodes[0]!, get)!
|
|
hex.x = lay.x
|
|
hex.y = lay.y
|
|
syncLatches(p)
|
|
expect(hex.latch).toBe('c')
|
|
expect(def.nodes[0]!.latch).toBeUndefined()
|
|
expect(latchedTo(def, new Set(['c'])).map(n => n.id)).toEqual(['h'])
|
|
})
|
|
|
|
it('a latched part stays on its part\'s pins when that part moves', () => {
|
|
const { p, def } = setup()
|
|
const get = lookup(p)
|
|
const hex = def.nodes[1]!
|
|
latchInto(def, hex, 'c', get)
|
|
const dx = hex.x - def.nodes[0]!.x
|
|
def.nodes[0]!.x += 7
|
|
syncLatches(p)
|
|
expect(hex.x - def.nodes[0]!.x).toBe(dx)
|
|
})
|
|
|
|
it('a switch feeding a component input from the left latches onto it', () => {
|
|
const { p, def } = setup()
|
|
const get = lookup(p)
|
|
const chip = def.nodes[0]!
|
|
const pin = shapeOf(chip, get).inputs[0]!
|
|
def.nodes.push({ id: 's', kind: 'input', x: -10, y: 5 })
|
|
def.wires.push({ id: 'ws', from: { node: 's', pin: 'q' }, to: { node: 'c', pin: pin.key } })
|
|
const sw = def.nodes[2]!
|
|
expect(latchTarget(def, sw, get)?.id).toBe('c')
|
|
latchInto(def, sw, 'c', get)
|
|
const out = shapeOf(sw, get).outputs[0]!
|
|
expect([sw.x + out.dx, sw.y + out.dy]).toEqual([chip.x + pin.dx, chip.y + pin.dy])
|
|
})
|
|
})
|