89 lines
3.6 KiB
TypeScript
89 lines
3.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { compile } from '../app/lib/logic/compile'
|
|
import { shapeOf } from '../app/lib/logic/geometry'
|
|
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()
|
|
})
|
|
})
|