import { describe, expect, it } from 'vitest' import { compile } from '../app/lib/logic/compile' import { faceCandidates, pixelKey, screenLayouts, shapeOf, splitScreens } 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; 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) }) }) describe('the box designer, more', () => { /** A 2×2 screen of pixels and two inputs, in a component placed once in another. */ function screened() { const p = createProject({ examples: false }) const inner: ComponentDef = { id: 'inner', name: 'Scr', symbol: 'box', nodes: [ { id: 'a', kind: 'input', x: 0, y: 0, label: 'A' }, { id: 'b', kind: 'input', x: 0, y: 1, label: 'B' }, { id: 'p1', kind: 'pixel', x: 6, y: 0 }, { id: 'p2', kind: 'pixel', x: 8, y: 0 }, { id: 'p3', kind: 'pixel', x: 6, y: 2 }, { id: 'p4', kind: 'pixel', x: 8, y: 2 }, ], wires: [], } p.components.push(inner) return { p, inner, get: lookup(p), node: { id: 'c', kind: 'chip' as const, chip: 'inner', x: 0, y: 0 } } } it('splitting a screen into pixels moves nothing, also sized and turned', () => { const [screen] = screenLayouts(screened().inner) for (const rotate of [undefined, 90, 180, 270]) { const it = { ...screen!, x: 1, y: 1, scale: 2, ...(rotate ? { rotate } : {}) } const pixels = splitScreens([it], new Set([it.key])) expect(pixels).toHaveLength(4) // The four pixels cover exactly the screen's turned square: 2 × 2 pixels of 1 × 2 = a 4 × 4 square at 1, 1. const xs = pixels.map(px => px.x).sort((a, b) => a - b) const ys = pixels.map(px => px.y).sort((a, b) => a - b) expect([xs[0], xs[3], ys[0], ys[3]]).toEqual([1, 3, 1, 3]) expect(pixels.every(px => px.scale === 2 && px.w === 1 && px.key === pixelKey(px.id) && px.screenOf === it.key)).toBe(true) } // Turned a quarter, the top left pixel goes to the top right. const turned = splitScreens([{ ...screen!, x: 0, y: 0, rotate: 90 }], new Set([screen!.key])) expect(turned.find(px => px.id === 'p1')).toMatchObject({ x: 1, y: 0 }) }) it('pins can change order on their edge', () => { const { inner, get, node } = screened() const before = shapeOf(node, get).inputs.map(p => [p.key, p.dy]) inner.design = { w: 6, h: 4, pinOrder: ['b', 'a'] } const after = shapeOf(node, get).inputs expect(after.find(p => p.key === 'b')!.dy).toBe(before[0]![1]) expect(after.find(p => p.key === 'a')!.dy).toBe(before[1]![1]) }) it('a fitted box grows and shrinks with what is on it; a set one keeps its size', () => { const { inner, get, node } = screened() inner.design = { w: 20, h: 20, fit: true, place: { screen: { x: 1, y: 1 } } } const small = shapeOf(node, get).body expect(small.w).toBeLessThan(20) inner.design.place!.screen = { x: 12, y: 1 } expect(shapeOf(node, get).body.w).toBeGreaterThan(small.w + 8) delete inner.design.fit inner.design.place!.screen = { x: 1, y: 1 } expect(shapeOf(node, get).body.w).toBe(20) }) it('a display of a part inside shows in a free spot inside the box, not below it', () => { const { p } = screened() const outer: ComponentDef = { id: 'outer', name: 'Outer', symbol: 'box', nodes: [ { id: 'x', kind: 'chip', chip: 'inner', x: 0, y: 0 }, ...['i0', 'i1', 'i2', 'i3', 'i4', 'i5'].map((id, k) => ({ id, kind: 'input' as const, x: -6, y: k, label: id })), ], wires: [], design: { w: 10, h: 7, fit: true, show: ['x/screen'] }, } p.components.push(outer) const shape = shapeOf({ id: 'o', kind: 'chip', chip: 'outer', x: 0, y: 0 }, lookup(p)) const scr = shape.face!.find(f => f.key === 'x/screen')! expect(scr.y + scr.h).toBeLessThanOrEqual(shape.body.y + shape.body.h) // The six inputs make it six rows high; the screen fits beside their names. expect(shape.body.h).toBeLessThan(7) expect(scr.from).toBe('Scr') }) })