logigate/tests/face.test.ts

162 lines
6.4 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
import { compile } from '../app/lib/logic/compile'
import { faceCandidates, screenLayouts, shapeOf } from '../app/lib/logic/geometry'
import { matchScore } from '../app/lib/match'
import { createProject, lookup } from '../app/lib/logic/ops'
import { toProject } from '../app/lib/logic/project'
import { Simulator } from '../app/lib/logic/sim'
import type { ComponentDef } from '../app/lib/logic/types'
function shown(): { p: ReturnType<typeof createProject>; inner: ComponentDef } {
const p = createProject({ examples: false })
const inner: ComponentDef = {
id: 'inner',
name: 'Show',
symbol: 'box',
nodes: [
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
{ id: 'b', kind: 'input', x: 0, y: 1, label: 'B' },
{ id: 'l', kind: 'lamp', x: 6, y: 0, label: 'L' },
{ id: 'h', kind: 'hex', x: 6, y: 4 },
{ id: 'q', kind: 'output', x: 12, y: 0, label: 'Q' },
],
wires: [{ id: 'w', from: { node: 'a', pin: 'q' }, to: { node: 'l', pin: 'a' } }],
}
p.components.push(inner)
return { p, inner }
}
describe('hex display', () => {
it('passes its four bits on, so it can sit in the middle of a bus', () => {
const p = createProject({ examples: false })
const def: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 's', kind: 'input', x: 0, y: 0 },
{ id: 'h', kind: 'hex', x: 4, y: 0 },
{ id: 'l', kind: 'lamp', x: 10, y: 0 },
],
wires: [
{ id: 'a', from: { node: 's', pin: 'q' }, to: { node: 'h', pin: 'b0' } },
{ id: 'b', from: { node: 'h', pin: 'q0' }, to: { node: 'l', pin: 'a' } },
],
}
p.components.push(def)
expect(shapeOf(def.nodes[1]!, lookup(p)).outputs.map(o => o.key)).toEqual(['q0', 'q1', 'q2', 'q3'])
const sim = new Simulator(compile(lookup(p), 'root'))
sim.setSource('s', true)
sim.run()
expect(sim.pin('l', 'a')).toBe(1)
})
})
describe('a box drawn by hand', () => {
it('takes its size, places things where they were put, leaves hidden ones off, carries drawings', () => {
const { p, inner } = shown()
const chip = { id: 'c', kind: 'chip' as const, chip: 'inner', x: 0, y: 0 }
const auto = shapeOf(chip, lookup(p))
expect(auto.face?.map(f => f.id)).toEqual(['l', 'h'])
inner.design = {
w: 9,
h: 7,
place: { l: { x: 5, y: 1 }, name: { x: 3, y: 5 } },
hide: ['h'],
shapes: [{ id: 's1', kind: 'rect', x: 1, y: 1, w: 2, h: 2, ink: 'red' }],
}
const s = shapeOf(chip, lookup(p))
expect(s.body.w).toBe(9)
expect(s.body.h).toBe(7)
// Outputs move to the new right edge; inputs stay on the left.
expect(s.outputs.every(o => o.dx === 9)).toBe(true)
expect(s.face).toEqual([expect.objectContaining({ id: 'l', x: 5, y: s.body.y + 1 })])
expect(s.nameAt).toEqual({ x: 3, y: s.body.y + 5 })
expect(s.drawing?.[0]).toMatchObject({ id: 's1', y: s.body.y + 1 })
// Never shorter than its pins need.
inner.design.h = 1
expect(shapeOf(chip, lookup(p)).body.h).toBeGreaterThanOrEqual(2)
})
it('a broken design from a file is dropped, a broken drawing too', () => {
const { p, inner } = shown()
inner.design = { w: 6, h: 4, shapes: [{ id: 'x', kind: 'rect', x: 0, y: 0, w: 1, h: 1 }, { id: 'y', kind: 'laser' as 'rect', x: 0, y: 0, w: 1, h: 1 }] }
const back = toProject(JSON.parse(JSON.stringify(p)))
expect(back.components.find(d => d.id === 'inner')!.design!.shapes!.map(s => s.id)).toEqual(['x'])
;(inner as { design: unknown }).design = { w: 'big' }
expect(toProject(JSON.parse(JSON.stringify(p))).components.find(d => d.id === 'inner')!.design).toBeUndefined()
})
})
describe('the box, finer', () => {
it('pixels make one screen per screen name', () => {
const def: ComponentDef = {
id: 'd',
name: 'D',
symbol: 'box',
nodes: [
{ id: 'p1', kind: 'pixel', x: 0, y: 0 },
{ id: 'p2', kind: 'pixel', x: 1, y: 0 },
{ id: 'q1', kind: 'pixel', x: 0, y: 5, screen: 'Status' },
],
wires: [],
}
expect(screenLayouts(def).map(s => [s.key, s.pixels!.length])).toEqual([
['screen', 2],
['screen:Status', 1],
])
})
it('shows displays of components inside when asked, with their values, and spaces pins', () => {
const { p, inner } = shown()
const outer: ComponentDef = {
id: 'outer',
name: 'Outer',
symbol: 'box',
nodes: [
{ id: 'x', kind: 'input', x: 0, y: 0, label: 'X' },
{ id: 'y', kind: 'input', x: 0, y: 1, label: 'Y' },
{ id: 'u', kind: 'chip', chip: 'inner', x: 5, y: 0 },
],
wires: [{ id: 'w', from: { node: 'x', pin: 'q' }, to: { node: 'u', pin: 'a' } }],
}
const root: ComponentDef = {
id: 'root',
name: 'Root',
symbol: 'box',
nodes: [
{ id: 's', kind: 'input', x: -4, y: 0 },
{ id: 'o', kind: 'chip', chip: 'outer', x: 0, y: 0 },
],
wires: [{ id: 'v', from: { node: 's', pin: 'q' }, to: { node: 'o', pin: 'x' } }],
}
p.components.push(outer, root)
const get = lookup(p)
const cands = faceCandidates(outer, get)
expect(cands.filter(c => c.nested).map(c => c.key)).toEqual(['u/l', 'u/h'])
const chip = { id: 'o', kind: 'chip' as const, chip: 'outer', x: 0, y: 0 }
expect(shapeOf(chip, get).face ?? []).toEqual([])
outer.design = { w: 8, h: 6, show: ['u/l'], scale: { 'u/l': 2 }, rotate: { 'u/l': 90 }, pinAt: { y: 3 } }
const s = shapeOf(chip, get)
expect(s.face).toEqual([expect.objectContaining({ id: 'u/l', key: 'u/l', scale: 2, rotate: 90 })])
// Y was put three rows down, under X: a gap.
expect(s.inputs.map(i => i.dy)).toEqual([0, 3])
// The lamp deep inside lights on the outer box when its signal is on.
const sim = new Simulator(compile(get, 'root'))
sim.setSource('s', true)
sim.run()
expect(sim.face('o', 'u/l', 'a')).toBe(1)
void inner
})
})
describe('finding parts', () => {
it('matches names by start, word, inside, and letters in order', () => {
expect(matchScore('Adder 4', 'adder 4')).toBeGreaterThan(matchScore('Adder 4', 'add'))
expect(matchScore('Adder 4', 'add')).toBeGreaterThan(matchScore('Full Adder', 'add'))
expect(matchScore('Full Adder', 'add')).toBeGreaterThan(matchScore('Ladder', 'add'))
expect(matchScore('Adder 4', 'adr4')).toBeGreaterThan(0)
expect(matchScore('Lamp', 'xyz')).toBe(0)
})
})