import { describe, expect, it } from 'vitest' import { runSteps, truthTable } from '../app/lib/logic/analyze' import { CHALLENGES, challengeSteps } from '../app/lib/logic/challenges' import { compile } from '../app/lib/logic/compile' import { createProject, lookup, makeUnique, swapNode, unlinkWires, unpackChip } from '../app/lib/logic/ops' import { Simulator } from '../app/lib/logic/sim' const rows = (p: ReturnType, id: string) => { const t = truthTable(lookup(p), id) if ('error' in t) throw new Error(t.error) return t.rows.map(r => [...r.ins, ...r.outs].join('')) } describe('swapping a placed part', () => { it('keeps the wires when a NAND becomes an AND', () => { const p = createProject({ examples: true }) const xor = p.components.find(d => d.id === 'ex-xor')! const last = xor.nodes.filter(n => n.kind === 'nand').at(-1)! const wires = xor.wires.length expect(swapNode(p, xor, last.id, { kind: 'chip', chip: 'ex-and' })).toBe(0) expect(xor.wires).toHaveLength(wires) // The final NAND became an AND, so the output is inverted: XNOR. const xnor = CHALLENGES.find(c => c.id === 'xnor')! expect(runSteps(lookup(p), 'ex-xor', challengeSteps(xnor)).pass).toBe(true) }) it('removes only the wires on pins the new part lacks', () => { const p = createProject({ examples: true }) const main = p.components.find(d => d.id === 'main')! const nand = main.nodes.find(n => n.kind === 'nand')! expect(swapNode(p, main, nand.id, { kind: 'chip', chip: 'ex-not' })).toBe(1) expect(main.wires).toHaveLength(2) }) it('turns a switch into a clock and back, keeping its wire', () => { const p = createProject({ examples: true }) const main = p.components.find(d => d.id === 'main')! const input = main.nodes.find(n => n.kind === 'input')! input.on = true swapNode(p, main, input.id, { kind: 'clock' }) expect(input.kind).toBe('clock') expect(input.on).toBeUndefined() expect(main.wires.some(w => w.from.node === input.id)).toBe(true) }) }) describe('unlinking and unpacking', () => { it('unlinks every wire of a part', () => { const p = createProject({ examples: true }) const main = p.components.find(d => d.id === 'main')! const nand = main.nodes.find(n => n.kind === 'nand')! expect(unlinkWires(main, new Set([nand.id]))).toBe(3) expect(main.wires).toHaveLength(0) }) it('unpacks a component into its parts without changing what the circuit does', () => { const p = createProject({ examples: true }) const before = rows(p, 'ex-fa') const fa = p.components.find(d => d.id === 'ex-fa')! const ha = fa.nodes.find(n => n.chip === 'ex-ha')! const res = unpackChip(p, 'ex-fa', ha.id) if (!res.ok) throw new Error(res.error) expect(fa.nodes.some(n => n.id === ha.id)).toBe(false) expect(res.nodes.length).toBe(2) expect(rows(p, 'ex-fa')).toEqual(before) }) it('unpacks a component whose input runs straight to its output', () => { const p = createProject({ examples: false }) p.components.push( { id: 'wire', name: 'Wire', symbol: 'box', nodes: [ { id: 'i', kind: 'input', x: 0, y: 0, label: 'A' }, { id: 'o', kind: 'output', x: 6, y: 0, label: 'Q' }, ], wires: [{ id: 'w', from: { node: 'i', pin: 'q' }, to: { node: 'o', pin: 'a' } }], }, { id: 'top', name: 'Top', symbol: 'box', nodes: [ { id: 's', kind: 'input', x: 0, y: 0, label: 'S' }, { id: 'c', kind: 'chip', chip: 'wire', x: 4, y: 0 }, { id: 'q', kind: 'output', x: 12, y: 0, label: 'Q' }, ], wires: [ { id: 'w1', from: { node: 's', pin: 'q' }, to: { node: 'c', pin: 'i' } }, { id: 'w2', from: { node: 'c', pin: 'o' }, to: { node: 'q', pin: 'a' } }, ], }, ) expect(unpackChip(p, 'top', 'c').ok).toBe(true) const top = p.components.find(d => d.id === 'top')! expect(top.wires.map(w => `${w.from.node}>${w.to.node}`)).toEqual(['s>q']) }) it('gives one placed component its own copy of the definition', () => { const p = createProject({ examples: true }) const fa = p.components.find(d => d.id === 'ex-fa')! const [first, second] = fa.nodes.filter(n => n.chip === 'ex-ha') const copy = makeUnique(p, fa, first!.id)! expect(first!.chip).toBe(copy.id) expect(second!.chip).toBe('ex-ha') expect(rows(p, 'ex-fa')).toEqual(rows(createProject({ examples: true }), 'ex-fa')) }) }) describe('clocks with their own frequency', () => { it('compiles each clock with its frequency and keeps levels per clock', () => { const p = createProject({ examples: false }) p.components.push({ id: 'clk', name: 'Clocks', symbol: 'box', nodes: [ { id: 'c1', kind: 'clock', x: 0, y: 0 }, { id: 'c2', kind: 'clock', x: 0, y: 4, hz: 10 }, { id: 'l1', kind: 'lamp', x: 6, y: 0 }, { id: 'l2', kind: 'lamp', x: 6, y: 4 }, ], wires: [ { id: 'w1', from: { node: 'c1', pin: 'q' }, to: { node: 'l1', pin: 'a' } }, { id: 'w2', from: { node: 'c2', pin: 'q' }, to: { node: 'l2', pin: 'a' } }, ], }) const nl = compile(lookup(p), 'clk') expect([...nl.clockHz]).toEqual([0, 10]) const sim = new Simulator(nl) sim.toggleClock(1) sim.run() expect([sim.pin('l1', 'a'), sim.pin('l2', 'a')]).toEqual([0, 1]) const again = new Simulator(compile(lookup(p), 'clk'), sim.carry()) again.run() expect([again.pin('l1', 'a'), again.pin('l2', 'a')]).toEqual([0, 1]) }) })