import { describe, expect, it } from 'vitest' import { nandCounts, TRANSISTORS_PER_NAND } from '../app/lib/logic/analyze' import { arrangeNodes, duplicateNames, entryLabel, ioList, numberedNames, seriesLinks, sharedFamily, transistorBreakdown } from '../app/lib/logic/bulk' import { compile } from '../app/lib/logic/compile' import { createProject, lookup } from '../app/lib/logic/ops' import type { CircuitNode, ComponentDef } from '../app/lib/logic/types' import { NAND_BASE, wideGate } from '../app/lib/logic/wide' const node = (id: string, kind: CircuitNode['kind'], x: number, y: number, extra: Partial = {}): CircuitNode => ({ id, kind, x, y, ...extra }) describe('numbered names', () => { it('replaces # runs with a padded counter', () => { expect(numberedNames('D#', 3)).toEqual(['D0', 'D1', 'D2']) expect(numberedNames('Q##', 2, 9)).toEqual(['Q09', 'Q10']) expect(numberedNames('Bit # of #', 2, 1)).toEqual(['Bit 1 of 1', 'Bit 2 of 2']) }) it('appends the number when there is no #, unless it is a single part', () => { expect(numberedNames('IN', 3, 1)).toEqual(['IN1', 'IN2', 'IN3']) expect(numberedNames('Reset', 1)).toEqual(['Reset']) }) }) describe('the list of inputs and outputs', () => { const def: ComponentDef = { id: 'd', name: 'D', symbol: 'box', nodes: [ node('b', 'input', 0, 4, { label: 'B' }), node('a', 'input', 0, 0, { label: 'A' }), node('clk', 'clock', 0, 10), node('p', 'panel', 0, 14, { rows: [{ id: 'r1', type: 'switch', label: 'EN' }, { id: 'r2', type: 'button' }] }), node('l', 'lamp', 10, 4, { label: 'A' }), node('q', 'output', 10, 0, { label: 'Q' }), node('g', 'nand', 5, 0), ], wires: [], } it('lists pins first in pin order (panel rows are pins too), then the parts that stay inside', () => { const { inputs, outputs } = ioList(def) expect(inputs.map(e => `${e.node.id}${e.row ? `/${e.row.id}` : ''}:${e.pin}`)).toEqual(['a:true', 'b:true', 'p/r1:true', 'p/r2:true', 'clk:false']) expect(outputs.map(e => e.node.id)).toEqual(['q', 'l']) expect(inputs.map(entryLabel)).toEqual(['A', 'B', 'EN', '', '']) }) it('a panel kept inside lists its rows after the pins', () => { const inside = { ...def, nodes: def.nodes.map(n => (n.kind === 'panel' ? { ...n, inner: true } : n)) } const { inputs } = ioList(inside) expect(inputs.map(e => `${e.node.id}${e.row ? `/${e.row.id}` : ''}:${e.pin}`)).toEqual(['a:true', 'b:true', 'clk:false', 'p/r1:false', 'p/r2:false']) }) it('flags names used twice on the same side only', () => { const { inputs, outputs } = ioList(def) expect([...duplicateNames(inputs)]).toEqual([]) expect([...duplicateNames([...inputs, ...outputs])]).toEqual(['a']) }) }) describe('selections', () => { it('knows when parts can all be swapped to the same thing', () => { expect(sharedFamily([node('1', 'input', 0, 0), node('2', 'clock', 0, 0)])).toBe('source') expect(sharedFamily([node('1', 'nand', 0, 0), node('2', 'chip', 0, 0, { chip: 'x' })])).toBe('logic') expect(sharedFamily([node('1', 'lamp', 0, 0), node('2', 'output', 0, 0)])).toBe('sink') expect(sharedFamily([node('1', 'lamp', 0, 0), node('2', 'input', 0, 0)])).toBeNull() }) it('lines parts up and spreads them evenly', () => { const ns = [node('1', 'input', 3, 0), node('2', 'input', 1, 7), node('3', 'input', 2, 3)] arrangeNodes(ns, 'left') expect(ns.map(n => n.x)).toEqual([1, 1, 1]) arrangeNodes(ns, 'spread-y') expect(ns.map(n => n.y).sort((a, b) => a - b)).toEqual([0, 4, 7]) }) }) describe('transistors', () => { const p = createProject({ examples: true }) for (const base of [NAND_BASE, 'ex-and', 'ex-or', 'ex-xor', 'ex-nor']) for (const n of [3, 8, 16]) wideGate(p, base, n) const get = lookup(p) const counts = nandCounts(p.components) it('count 4 per NAND gate the simulator actually builds, in every component', () => { for (const d of p.components) expect(counts.get(d.id) ?? 0, d.name).toBe(compile(get, d.id).gateOut.length) }) it('match the textbook NAND builds', () => { const t = (id: string) => (counts.get(id) ?? 0) * TRANSISTORS_PER_NAND expect([t('ex-not'), t('ex-and'), t('ex-or'), t('ex-xor'), t('ex-ha'), t('ex-fa')]).toEqual([4, 8, 12, 16, 24, 60]) }) it('break a circuit down into its parts, adding up to the total', () => { const fa = get('ex-fa')! const lines = transistorBreakdown(fa, counts, get) expect(lines.map(l => [l.name, l.count, l.each, l.total])).toEqual([ ['Half Adder', 2, 24, 48], ['OR', 1, 12, 12], ]) expect(lines.reduce((s, l) => s + l.total, 0)).toBe((counts.get('ex-fa') ?? 0) * TRANSISTORS_PER_NAND) }) }) describe('wiring several parts at once', () => { const p = createProject({ examples: true }) const get = lookup(p) const add4 = get('ex-add4')! const outsOfAdder = (n: CircuitNode) => { const d = get(n.chip!)! return d.nodes.filter(x => x.kind === 'output').sort((a, b) => a.y - b.y).map(x => x.id) } const def: ComponentDef = { id: 't', name: 'T', symbol: 'box', nodes: [ node('add', 'chip', 0, 0, { chip: add4.id }), ...['Y0', 'Y1', 'Y2', 'Y3', 'Carry'].map((l, i) => node(`y${i}`, 'output', 20, i * 2, { label: l })), ], wires: [], } const sel = ['y0', 'y1', 'y2', 'y3', 'y4'] const S = outsOfAdder(def.nodes[0]!) it('drags one wire and connects every selected output to the next pin in turn', () => { const links = seriesLinks(def, get, sel, { node: 'y0', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! }) expect(links.map(l => `${l.from.pin}>${l.to.node}`)).toEqual(S.map((s, i) => `${s}>y${i}`)) }) it('counts from the dragged part, both ways, and stops at the last pin', () => { const links = seriesLinks(def, get, sel, { node: 'y2', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! }) expect(links.map(l => `${l.from.pin}>${l.to.node}`)).toEqual([`${S[0]}>y2`, `${S[1]}>y3`, `${S[2]}>y4`]) }) it('falls back to a single wire when the dragged part is not selected', () => { expect(seriesLinks(def, get, ['y1'], { node: 'y0', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! })).toHaveLength(1) }) }) describe('wiring several parts at once, mixed selection', () => { it('skips selected parts without a matching pin instead of using up a target pin', () => { const p = createProject({ examples: true }) const get = lookup(p) const def: ComponentDef = { id: 't', name: 'T', symbol: 'box', nodes: [ node('add', 'chip', 0, 0, { chip: 'ex-add4' }), node('y0', 'output', 20, 0), node('note', 'note', 0, 1, { label: 'hi' }), node('y1', 'output', 20, 2), ], wires: [], } const S = get('ex-add4')!.nodes.filter(x => x.kind === 'output').sort((a, b) => a.y - b.y).map(x => x.id) const links = seriesLinks(def, get, ['y0', 'note', 'y1'], { node: 'y0', pin: 'a', dir: 'in' }, { node: 'add', pin: S[0]! }) expect(links.map(l => `${l.from.pin}>${l.to.node}`)).toEqual([`${S[0]}>y0`, `${S[1]}>y1`]) }) })