import { describe, expect, it } from 'vitest' import { bundleWires, offsetPath, ribbon } from '../app/lib/logic/cables' import { compile } from '../app/lib/logic/compile' import { lookup, createProject } from '../app/lib/logic/ops' import { Simulator } from '../app/lib/logic/sim' import type { ComponentDef } from '../app/lib/logic/types' describe('ribbon cables', () => { it('moves a square path sideways and keeps the corners square', () => { // Right, then down, then right again. const p = offsetPath([[0, 0], [4, 0], [4, 3], [8, 3]], 0.5) expect(p).toEqual([[0, 0.5], [3.5, 0.5], [3.5, 3.5], [8, 3.5]]) }) it('draws wire 0 on top when the cable runs to the right', () => { const lanes = ribbon([[0, 0], [10, 0]], 4) expect(lanes.map(l => l[0]![1])).toEqual([...lanes.map(l => l[0]![1])].sort((a, b) => a - b)) expect(lanes[0]![0]![1]).toBeLessThan(0) }) }) describe('placing a cable', () => { const def = (): ComponentDef => ({ id: 'c', name: 'C', symbol: 'box', nodes: [ ...[0, 1, 2].map(i => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 2, on: i !== 1 })), ...[0, 1, 2].map(i => ({ id: `l${i}`, kind: 'lamp' as const, x: 30, y: 10 + i * 2 })), ], wires: [0, 1, 2].map(i => ({ id: `w${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: `l${i}`, pin: 'a' }, ...(i === 0 ? { color: 'red' as const } : {}) })), }) it('puts the start and end where you click, centred on the cable', () => { const d = def() const res = bundleWires(d, ['w0', 'w1', 'w2'], lookup(createProject({ examples: false })), { start: [8, 4], end: [22, 12] }) if (!res.ok) throw new Error(res.error) expect([res.bundle.x, res.bundle.y]).toEqual([8, 3]) expect([res.split.x, res.split.y]).toEqual([22, 11]) // Each wire keeps its colour on both sides of the cable; the sleeve starts neutral. expect(d.wires.filter(w => w.color === 'red')).toHaveLength(2) expect(d.wires.find(w => w.from.node === res.bundle.id)!.color).toBeUndefined() }) it('still carries every signal to its lamp', () => { const p = createProject({ examples: false }) const d = def() p.components.push(d) bundleWires(d, ['w0', 'w1', 'w2'], lookup(p), { start: [8, 4], end: [22, 12] }) const sim = new Simulator(compile(lookup(p), 'c')) for (const n of d.nodes) if (n.kind === 'input') sim.setSource(n.id, !!n.on) sim.run() expect([0, 1, 2].map(i => sim.pin(`l${i}`, 'a'))).toEqual([1, 0, 1]) }) })