70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
|
|
import { describe, expect, it } from 'vitest'
|
||
|
|
import { openInputs } from '../app/lib/logic/check'
|
||
|
|
import { compile } from '../app/lib/logic/compile'
|
||
|
|
import { rgbHex, screenLayout, shapeOf } from '../app/lib/logic/geometry'
|
||
|
|
import { createProject, lookup } from '../app/lib/logic/ops'
|
||
|
|
import { Simulator } from '../app/lib/logic/sim'
|
||
|
|
import type { ComponentDef } from '../app/lib/logic/types'
|
||
|
|
|
||
|
|
/** A component with one RGB pixel, its red and blue inputs from two input pins; placed in a circuit with two switches. */
|
||
|
|
function setup() {
|
||
|
|
const p = createProject({ examples: false })
|
||
|
|
const inner: ComponentDef = {
|
||
|
|
id: 'mon',
|
||
|
|
name: 'Monitor',
|
||
|
|
symbol: 'box',
|
||
|
|
nodes: [
|
||
|
|
{ id: 'R', kind: 'input', x: 0, y: 0, label: 'R' },
|
||
|
|
{ id: 'B', kind: 'input', x: 0, y: 2, label: 'B' },
|
||
|
|
{ id: 'px', kind: 'pixel', x: 10, y: 0, rgb: true },
|
||
|
|
],
|
||
|
|
wires: [
|
||
|
|
{ id: 'w1', from: { node: 'R', pin: 'q' }, to: { node: 'px', pin: 'r' } },
|
||
|
|
{ id: 'w2', from: { node: 'B', pin: 'q' }, to: { node: 'px', pin: 'b' } },
|
||
|
|
],
|
||
|
|
}
|
||
|
|
const root: ComponentDef = {
|
||
|
|
id: 'root',
|
||
|
|
name: 'Root',
|
||
|
|
symbol: 'box',
|
||
|
|
nodes: [
|
||
|
|
{ id: 's1', kind: 'input', x: 0, y: 0 },
|
||
|
|
{ id: 's2', kind: 'input', x: 0, y: 2 },
|
||
|
|
{ id: 'm', kind: 'chip', chip: 'mon', x: 10, y: 0 },
|
||
|
|
],
|
||
|
|
wires: [
|
||
|
|
{ id: 'a', from: { node: 's1', pin: 'q' }, to: { node: 'm', pin: 'R' } },
|
||
|
|
{ id: 'b', from: { node: 's2', pin: 'q' }, to: { node: 'm', pin: 'B' } },
|
||
|
|
],
|
||
|
|
}
|
||
|
|
p.components.push(inner, root)
|
||
|
|
return { p, inner, root }
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('RGB pixels', () => {
|
||
|
|
it('have red, green and blue inputs and mix eight colours', () => {
|
||
|
|
const { p, inner } = setup()
|
||
|
|
expect(shapeOf(inner.nodes[2]!, lookup(p)).inputs.map(i => i.key)).toEqual(['r', 'g', 'b'])
|
||
|
|
expect(rgbHex(0, 0, 0)).toBe('')
|
||
|
|
expect(rgbHex(1, 0, 1)).toBe('#ff5cf0')
|
||
|
|
expect(rgbHex(1, 1, 1)).toBe('#f2f6fa')
|
||
|
|
// Green left open on purpose: no warning.
|
||
|
|
expect(openInputs(inner, inner.nodes[2]!, lookup(p))).toEqual([])
|
||
|
|
})
|
||
|
|
|
||
|
|
it('show their colour on the screen of the component they are in', () => {
|
||
|
|
const { p, inner, root } = setup()
|
||
|
|
expect(screenLayout(inner)!.pixels).toEqual([{ id: 'px', col: 0, row: 0, color: undefined, rgb: true }])
|
||
|
|
const s = new Simulator(compile(lookup(p), root.id))
|
||
|
|
s.setSource('s1', true)
|
||
|
|
s.setSource('s2', true)
|
||
|
|
s.run()
|
||
|
|
expect([s.face('m', 'px', 'r'), s.face('m', 'px', 'g'), s.face('m', 'px', 'b')]).toEqual([1, 0, 1])
|
||
|
|
})
|
||
|
|
|
||
|
|
it('a one-colour pixel keeps its single input', () => {
|
||
|
|
const { p } = setup()
|
||
|
|
expect(shapeOf({ id: 'x', kind: 'pixel', x: 0, y: 0 }, lookup(p)).inputs.map(i => i.key)).toEqual(['a'])
|
||
|
|
})
|
||
|
|
})
|