logigate/tests/cables-ribbon.test.ts

120 lines
5.3 KiB
TypeScript
Raw Normal View History

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])
})
})
describe('automatic cable placement', () => {
const setup = (srcX: number, dstX: number, extra: ComponentDef['nodes'] = []): ComponentDef => ({
id: 'c',
name: 'C',
symbol: 'box',
nodes: [
...[0, 1, 2].map(i => ({ id: `s${i}`, kind: 'input' as const, x: srcX, y: i * 2 })),
...[0, 1, 2].map(i => ({ id: `l${i}`, kind: 'lamp' as const, x: dstX, y: i * 2 })),
...extra,
],
wires: [0, 1, 2].map(i => ({ id: `w${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: `l${i}`, pin: 'a' } })),
})
const get = lookup(createProject({ examples: false }))
it('starts right after the sources and ends right before the targets', () => {
const d = setup(0, 30)
const res = bundleWires(d, ['w0', 'w1', 'w2'], get)
if (!res.ok) throw new Error(res.error)
expect([res.bundle.x, res.split.x]).toEqual([4, 27])
// Centred on the rows the wires come from and go to.
expect([res.bundle.y, res.split.y]).toEqual([1, 1])
})
it('runs backwards when the wires do, the end still just before the targets', () => {
const d = setup(30, 0)
const res = bundleWires(d, ['w0', 'w1', 'w2'], get)
if (!res.ok) throw new Error(res.error)
expect(res.split.x).toBe(-3)
expect(res.bundle.x).toBe(34)
})
it('steps aside when another part sits where an end would go', () => {
const d = setup(0, 30, [{ id: 'block', kind: 'nand', x: 3, y: 0 }])
const res = bundleWires(d, ['w0', 'w1', 'w2'], get)
if (!res.ok) throw new Error(res.error)
const nand = { x0: 3, x1: 7, y0: -0.5, y1: 2.5 }
const body = { x0: res.bundle.x, x1: res.bundle.x + 1, y0: res.bundle.y - 0.5, y1: res.bundle.y + 2.5 }
const overlap = body.x0 < nand.x1 && body.x1 > nand.x0 && body.y0 < nand.y1 && body.y1 > nand.y0
expect(overlap).toBe(false)
})
})
describe('unlinking cable ends', () => {
it('keeps the cable and removes only the wires into the bundle or out of the split', async () => {
const { unlinkWires } = await import('../app/lib/logic/ops')
const d: ComponentDef = {
id: 'c',
name: 'C',
symbol: 'box',
nodes: [
...[0, 1].map(i => ({ id: `s${i}`, kind: 'input' as const, x: 0, y: i * 2 })),
...[0, 1].map(i => ({ id: `l${i}`, kind: 'lamp' as const, x: 30, y: i * 2 })),
],
wires: [0, 1].map(i => ({ id: `w${i}`, from: { node: `s${i}`, pin: 'q' }, to: { node: `l${i}`, pin: 'a' } })),
}
const res = bundleWires(d, ['w0', 'w1'], lookup(createProject({ examples: false })))
if (!res.ok) throw new Error(res.error)
expect(unlinkWires(d, new Set([res.bundle.id]))).toBe(2)
expect(d.wires.some(w => w.from.node === res.bundle.id && w.to.node === res.split.id)).toBe(true)
expect(unlinkWires(d, new Set([res.bundle.id, res.split.id]))).toBe(2)
expect(d.wires.map(w => `${w.from.node}>${w.to.node}`)).toEqual([`${res.bundle.id}>${res.split.id}`])
})
})