74 lines
3.2 KiB
TypeScript
74 lines
3.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { checkCircuit, unusedComponents } from '../app/lib/logic/check'
|
|
import { createProject, lookup } from '../app/lib/logic/ops'
|
|
import { searchProject } from '../app/lib/logic/search'
|
|
import { paperColor } from '../app/lib/sheet'
|
|
import type { ComponentDef } from '../app/lib/logic/types'
|
|
|
|
function project() {
|
|
const p = createProject({ examples: true })
|
|
const def: ComponentDef = {
|
|
id: 'root',
|
|
name: 'Bench',
|
|
symbol: 'box',
|
|
nodes: [
|
|
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
|
|
{ id: 'a2', kind: 'input', x: 0, y: 4, label: 'A' },
|
|
{ id: 'g', kind: 'nand', x: 6, y: 0 },
|
|
{ id: 'add', kind: 'chip', chip: 'ex-add4', x: 20, y: 0, label: 'U1' },
|
|
{ id: 't', kind: 'tag', x: 0, y: 10, label: 'CLK' },
|
|
{ id: 't2', kind: 'tag', x: 0, y: 12, label: 'CLK' },
|
|
{ id: 'r', kind: 'ref', x: 10, y: 10, label: 'RESET' },
|
|
{ id: 'note', kind: 'note', x: 0, y: 20, label: 'Carry goes to the display' },
|
|
],
|
|
wires: [{ id: 'w', from: { node: 'a', pin: 'q' }, to: { node: 'g', pin: 'a' } }],
|
|
}
|
|
p.components.push(def)
|
|
return { p, def }
|
|
}
|
|
|
|
describe('checking a circuit', () => {
|
|
it('finds open inputs, unused parts, duplicate pins and broken signals', () => {
|
|
const { p, def } = project()
|
|
const issues = checkCircuit(def, lookup(p))
|
|
const text = issues.map(i => i.text)
|
|
expect(text).toContain('NAND gate: B not connected (reads 0)')
|
|
expect(text.some(t => t.startsWith('Adder 4 U1: no input connected'))).toBe(true)
|
|
expect(text).toContain('2 input pins are called A')
|
|
expect(text).toContain('Signal "CLK" is sent by 2 parts: they drive it against each other')
|
|
expect(text).toContain('Nothing sends signal "RESET" (reads 0)')
|
|
expect(text).toContain('NAND gate: its output goes nowhere')
|
|
// Worst first.
|
|
expect(issues[0]!.level).toBe('error')
|
|
expect(issues.at(-1)!.level).toBe('info')
|
|
})
|
|
|
|
it('lists components no other circuit places, leaving the examples out', () => {
|
|
const { p } = project()
|
|
expect(unusedComponents(p).map(d => d.id)).toContain('root')
|
|
expect(unusedComponents(p).some(d => d.id.startsWith('ex-'))).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('search', () => {
|
|
it('finds parts by name, kind and note text, and components with where they are used', () => {
|
|
const { p } = project()
|
|
const res = searchProject(p, 'adder 4')
|
|
expect(res.components[0]!.def.id).toBe('ex-add4')
|
|
expect(res.components[0]!.usedIn.map(u => u.def.id)).toContain('root')
|
|
expect(searchProject(p, 'u1').parts[0]).toMatchObject({ node: 'add', def: 'root' })
|
|
expect(searchProject(p, 'display').parts.some(h => h.node === 'note')).toBe(true)
|
|
expect(searchProject(p, 'clk').parts.filter(h => h.def === 'root').map(h => h.node).sort()).toEqual(['t', 't2'])
|
|
expect(searchProject(p, ' ').parts).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('sheets', () => {
|
|
it('turns screen colours into paper colours of the same hue', () => {
|
|
expect(paperColor('rgb(14, 17, 22)')).toBe('rgb(233, 236, 241)')
|
|
expect(paperColor('rgb(219, 226, 234)')).toMatch(/^rgb\(\d+, \d+, \d+\)$/)
|
|
expect(paperColor('none')).toBe('none')
|
|
expect(paperColor('rgba(61, 220, 132, 0.2)')).toMatch(/, 0.2\)$/)
|
|
})
|
|
})
|