logigate/tests/features.test.ts

206 lines
8.2 KiB
TypeScript
Raw Permalink Normal View History

import { describe, expect, it } from 'vitest'
import { compile } from '../app/lib/logic/compile'
import { chipPins, routeWire, shapeOf, type Pt } from '../app/lib/logic/geometry'
import { createProject, lookup, mergeComponents } from '../app/lib/logic/ops'
import { parseProject, toProject } from '../app/lib/logic/project'
import { Simulator } from '../app/lib/logic/sim'
import type { ComponentDef, Project } from '../app/lib/logic/types'
import { branchCorners, branchTo, moveCorner, moveSegment, segmentDrag, splitDrag } from '../app/lib/logic/wires'
/** A component whose input drives a labelled lamp, placed once in a root circuit. */
function lampProject(): Project {
const p = createProject({ examples: false })
const part: ComponentDef = {
id: 'part',
name: 'Part',
symbol: 'not',
nodes: [
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
{ id: 'q', kind: 'output', x: 10, y: 0, label: 'Q' },
{ id: 'l', kind: 'lamp', x: 6, y: 4, label: 'Busy' },
],
wires: [
{ id: 'w1', from: { node: 'a', pin: 'q' }, to: { node: 'q', pin: 'a' } },
{ id: 'w2', from: { node: 'a', pin: 'q' }, to: { node: 'l', pin: 'a' } },
],
}
const root: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 's', kind: 'input', x: 0, y: 0, label: 'S' },
{ id: 'c', kind: 'chip', chip: 'part', x: 6, y: 0 },
],
wires: [{ id: 'w', from: { node: 's', pin: 'q' }, to: { node: 'c', pin: 'a' } }],
}
p.components.push(part, root)
return p
}
describe('lamps on the component box', () => {
it('draws a component with lamps as a box with the lamp and its label below the pins', () => {
const p = lampProject()
const shape = shapeOf(p.components.find(d => d.id === 'root')!.nodes[1]!, lookup(p))
expect(shape.gate).toBeUndefined()
expect(shape.face).toHaveLength(1)
const lamp = shape.face![0]!
expect(lamp.label).toBe('Busy')
expect(lamp.y).toBeGreaterThan(shape.nameY!)
expect(lamp.y + lamp.h).toBeLessThanOrEqual(shape.body.y + shape.body.h)
})
it('can be turned off per component', () => {
const p = lampProject()
p.components.find(d => d.id === 'part')!.face = false
const shape = shapeOf(p.components.find(d => d.id === 'root')!.nodes[1]!, lookup(p))
expect(shape.face ?? []).toHaveLength(0)
expect(shape.gate).toBe('not')
})
it('lights the lamp from the instance’s own state', () => {
const p = lampProject()
const sim = new Simulator(compile(lookup(p), 'root'))
sim.run()
expect(sim.face('c', 'l', 'a')).toBe(0)
sim.setSource('s', true)
sim.run()
expect(sim.face('c', 'l', 'a')).toBe(1)
})
it('arranges lamps in rows the way they sit inside', () => {
const p = lampProject()
const part = p.components.find(d => d.id === 'part')!
part.nodes.push({ id: 'l2', kind: 'lamp', x: 9, y: 4 }, { id: 'l3', kind: 'lamp', x: 6, y: 8 })
const face = shapeOf(p.components.find(d => d.id === 'root')!.nodes[1]!, lookup(p)).face!
const [l1, l2, l3] = ['l', 'l2', 'l3'].map(id => face.find(f => f.id === id)!)
expect(l1!.y).toBe(l2!.y)
expect(l2!.x).toBeGreaterThan(l1!.x)
expect(l3!.y).toBeGreaterThan(l1!.y)
})
})
describe('wire editing', () => {
// Output pin at (0,0), input pin at (10,4): default route has a vertical step at x=5.
const route = routeWire([0, 0], [10, 4])
it('moves an inner segment', () => {
expect(route).toEqual([[0, 0], [5, 0], [5, 4], [10, 4]])
const prep = segmentDrag(route, 1)!
expect(prep.axis).toBe(0)
const pts = moveSegment(prep.base, prep.k, prep.axis, 2)
expect(routeWire([0, 0], [10, 4], pts)).toEqual([[0, 0], [7, 0], [7, 4], [10, 4]])
})
it('moves a segment that starts at a pin, keeping a stub at the pin', () => {
const prep = segmentDrag(route, 0)!
const pts = moveSegment(prep.base, prep.k, prep.axis, 2)
const r = routeWire([0, 0], [10, 4], pts)
expect(r[0]).toEqual([0, 0])
expect(r[1]).toEqual([1, 0])
expect(r).toContainEqual([1, 2])
expect(r.at(-1)).toEqual([10, 4])
})
it('moves a straight wire sideways with stubs at both pins', () => {
const straight = routeWire([0, 0], [8, 0])
const prep = segmentDrag(straight, 0)!
const r = routeWire([0, 0], [8, 0], moveSegment(prep.base, prep.k, prep.axis, -3))
expect(r).toEqual([[0, 0], [1, 0], [1, -3], [7, -3], [7, 0], [8, 0]])
})
it('moves a corner and keeps the wire square', () => {
const r = routeWire([0, 0], [10, 4], moveCorner(route, 1, [3, 2]))
for (let i = 0; i < r.length - 1; i++) {
const [a, b] = [r[i]!, r[i + 1]!]
expect(a[0] === b[0] || a[1] === b[1]).toBe(true)
}
expect(r).toContainEqual([3, 2])
})
it('pulls a new bend out of a segment', () => {
const prep = splitDrag(route, 2, [8, 4])!
const r = routeWire([0, 0], [10, 4], moveSegment(prep.base, prep.k, prep.axis, 2))
expect(r).toContainEqual([8, 4])
expect(r).toContainEqual([8, 6])
expect(r.at(-1)).toEqual([10, 4])
})
it('branches a new wire off an existing one', () => {
const corners = branchCorners(route, 1, [5.2, 2.4])
expect(corners).toEqual([[5, 0], [5, 2]] as Pt[])
const r = routeWire([0, 0], [12, 8], branchTo(corners, [0, 0], [12, 8]))
expect(r).toEqual([[0, 0], [5, 0], [5, 2], [9, 2], [9, 8], [12, 8]])
})
})
describe('project files', () => {
it('fills in defaults and keeps the name', () => {
const p = toProject({ name: 'Mine', components: [{ id: 'x', name: 'X', nodes: [], wires: [] }] })
expect(p.name).toBe('Mine')
expect(p.openId).toBe('x')
expect(p.settings).toEqual({ symbols: 'iec', flow: true })
})
it('rejects files that are not projects', () => {
expect(() => parseProject('{"hello":1}')).toThrow(/Not a Logigate project/)
expect(() => parseProject('nope')).toThrow(/JSON/)
})
it('merges components: skips identical ones, re-ids clashes and their references', () => {
const p = createProject({ examples: true })
const other = createProject({ examples: true })
expect(mergeComponents(p, other.components.filter(d => d.id !== 'main'))).toBe(0)
const changedNot = { ...structuredClone(other.components.find(d => d.id === 'ex-not')!), description: 'changed' }
const usesNot = structuredClone(other.components.find(d => d.id === 'ex-and')!)
usesNot.description = 'changed too'
expect(mergeComponents(p, [changedNot, usesNot])).toBe(2)
const added = p.components.slice(-2)
expect(added[0]!.id).not.toBe('ex-not')
expect(added[0]!.name).toBe('NOT 2')
expect(added[1]!.nodes.find(n => n.kind === 'chip')!.chip).toBe(added[0]!.id)
expect(chipPins(added[1]!).inputs).toHaveLength(2)
})
})
describe('route clean-up', () => {
it('collapses a bump that was dragged flat, including the duplicate it leaves', async () => {
const { simplify } = await import('../app/lib/logic/geometry')
expect(simplify([[2, 0], [4, 0], [4, 3], [4, 3], [4, 0], [5, 0]])).toEqual([[2, 0], [5, 0]])
})
})
describe('example counter face', () => {
it('shows the count on the Counter 4 box in the demo', () => {
const p = createProject({ examples: true })
const get = lookup(p)
const demo = get('ex-demo-counter')!
const chip = demo.nodes.find(n => n.kind === 'chip')!
const hex = get('ex-cnt4')!.nodes.find(n => n.kind === 'hex')!
expect(hex.label).toBe('count')
expect(shapeOf(chip, get).face!.map(f => f.kind)).toEqual(['hex'])
const sim = new Simulator(compile(get, demo.id))
const shown = () => [0, 1, 2, 3].reduce((n, i) => n + (sim.face(chip.id, hex.id, `b${i}`) << i), 0)
sim.run()
for (let i = 1; i <= 5; i++) {
sim.setClock(1)
sim.run()
sim.setClock(0)
sim.run()
expect(shown()).toBe(i)
}
})
})
describe('box selection of wires', () => {
it('finds wires that pass through a box, not only those inside it', async () => {
const { routeTouchesBox } = await import('../app/lib/logic/wires')
const route: Pt[] = [[0, 0], [10, 0], [10, 8], [20, 8]]
const box = { x0: 4, y0: -1, x1: 6, y1: 1 }
expect(routeTouchesBox(route, box)).toBe(true)
expect(routeTouchesBox(route, { x0: 12, y0: 1, x1: 18, y1: 6 })).toBe(false)
expect(routeTouchesBox(route, { x0: 9, y0: 3, x1: 11, y1: 4 })).toBe(true)
})
})