53 lines
2.3 KiB
TypeScript
53 lines
2.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { trimCable } from '../app/lib/logic/cables'
|
|
import { cableOffsets } from '../app/lib/logic/geometry'
|
|
import type { ComponentDef } from '../app/lib/logic/types'
|
|
|
|
/** Sixteen outputs of a part into a 16-wire bundle, its cable to a split, and wires on some of them only. */
|
|
function cable(used: number[], fromSplit: number[] = used): ComponentDef {
|
|
const def: ComponentDef = {
|
|
id: 'd',
|
|
name: 'D',
|
|
symbol: 'box',
|
|
nodes: [
|
|
{ id: 'b', kind: 'bundle', x: 10, y: 0, bits: 16 },
|
|
{ id: 's', kind: 'split', x: 30, y: 0, bits: 16, names: Array.from({ length: 16 }, (_, i) => `Y${i}`) },
|
|
],
|
|
wires: [{ id: 'c', from: { node: 'b', pin: 'q' }, to: { node: 's', pin: 'a' } }],
|
|
}
|
|
for (const i of used) def.wires.push({ id: `in${i}`, from: { node: 'src', pin: `Y${i}` }, to: { node: 'b', pin: `b${i}` } })
|
|
for (const i of fromSplit) def.wires.push({ id: `out${i}`, from: { node: 's', pin: `b${i}` }, to: { node: `lamp${i}`, pin: 'a' } })
|
|
return def
|
|
}
|
|
|
|
describe('dropping the unused wires of a cable', () => {
|
|
it('keeps the wires in use, in order, each on the row it had', () => {
|
|
const def = cable([0, 1, 2, 3, 4])
|
|
expect(trimCable(def, 'b')).toEqual({ before: 16, after: 5 })
|
|
const [b, s] = def.nodes
|
|
expect(b!.bits).toBe(5)
|
|
expect(s!.bits).toBe(5)
|
|
expect(b!.y).toBe(0)
|
|
expect(cableOffsets(b!)).toEqual([0, 1, 2, 3, 4])
|
|
expect(s!.names).toEqual(['Y0', 'Y1', 'Y2', 'Y3', 'Y4'])
|
|
expect(def.wires.filter(w => w.to.node === 'b').map(w => w.to.pin)).toEqual(['b0', 'b1', 'b2', 'b3', 'b4'])
|
|
})
|
|
|
|
it('wires in use with gaps between them close up, keeping their rows', () => {
|
|
const def = cable([1, 3, 6])
|
|
expect(trimCable(def, 's')).toEqual({ before: 16, after: 3 })
|
|
const b = def.nodes[0]!
|
|
// Wire 1 was on row 1: the end moves down one, its wires keep their rows.
|
|
expect(b.y).toBe(1)
|
|
expect(cableOffsets(b)).toEqual([0, 2, 5])
|
|
expect(def.wires.find(w => w.id === 'in6')!.to.pin).toBe('b2')
|
|
expect(def.wires.find(w => w.id === 'out3')!.from.pin).toBe('b1')
|
|
})
|
|
|
|
it('a wire used only at the split still counts; nothing to drop leaves it alone', () => {
|
|
const def = cable([0, 1], [0, 1, 2])
|
|
expect(trimCable(def, 'b')!.after).toBe(3)
|
|
expect(trimCable(def, 'b')).toBeNull()
|
|
})
|
|
})
|