import { describe, expect, it } from 'vitest' import { bundleWires } from '../app/lib/logic/cables' import { compile } from '../app/lib/logic/compile' import { createProject, lookup } from '../app/lib/logic/ops' import { probeAt, probeBits, probeName, probeOfWire, probeValue } from '../app/lib/logic/probes' import { toProject } from '../app/lib/logic/project' import { Simulator } from '../app/lib/logic/sim' import type { ComponentDef } from '../app/lib/logic/types' /** Four switches into a cable, the cable to four lamps. */ function setup() { const p = createProject({ examples: false }) const def: ComponentDef = { id: 'root', name: 'Root', symbol: 'box', nodes: [ ...[0, 1, 2, 3].map(i => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 2, label: `S${i}` })), ...[0, 1, 2, 3].map(i => ({ id: `l${i}`, kind: 'lamp' as const, x: 30, y: 10 + i * 2 })), ], wires: [0, 1, 2, 3].map(i => ({ id: `w${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: `l${i}`, pin: 'a' } })), } p.components.push(def) const res = bundleWires(def, ['w0', 'w1', 'w2', 'w3'], lookup(p)) if (!res.ok) throw new Error(res.error) return { p, def } } describe('analyzer signals', () => { it('a cable reads as one number, wire 0 the lowest bit; a wire as 0 or 1', () => { const { p, def } = setup() const s = new Simulator(compile(lookup(p), def.id)) s.setSource('s0', true) s.setSource('s2', true) s.run() const cable = def.wires.find(w => def.nodes.find(n => n.id === w.from.node)?.kind === 'bundle')! const cp = probeOfWire(cable) expect(probeBits(def, cp)).toBe(4) expect(probeValue(def, cp, (n, pin) => s.pin(n, pin))).toBe(5) const lamp = def.nodes.find(n => n.id === 'l2')! const lp = probeAt(def, lamp, 'a', 'in', lookup(p))! expect(probeBits(def, lp)).toBe(1) expect(probeValue(def, lp, (n, pin) => s.pin(n, pin))).toBe(1) expect(probeName(def, { node: 's1', pin: 'q' }, lookup(p))).toBe('S1') expect(probeName(def, cp, lookup(p))).toBe('Cable 4') }) it('watched signals are saved with the circuit and survive loading', () => { const { p, def } = setup() def.probes = [{ node: 's0', pin: 'q' }, { node: 'x', pin: 'q', label: 'mine' }] const back = toProject(JSON.parse(JSON.stringify(p)))! expect(back.components.find(c => c.id === 'root')!.probes).toEqual(def.probes) }) })