195 lines
8.8 KiB
TypeScript
195 lines
8.8 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { CHALLENGES } from '../app/lib/logic/challenges'
|
|
import { createProject, startPuzzle } from '../app/lib/logic/ops'
|
|
import { challengeOpen, defUsable, kindUnlocked, NEEDS, unlockedBy } from '../app/lib/logic/progress'
|
|
import { toProject } from '../app/lib/logic/project'
|
|
import { cleanPuzzle, describeCircuit, isTable, makePuzzle, scoreSolution, weekKey, weeklyPuzzle } from '../app/lib/logic/puzzles'
|
|
import type { ComponentDef } from '../app/lib/logic/types'
|
|
|
|
/** XOR from four NANDs. */
|
|
function xor(id = 'x'): ComponentDef {
|
|
return {
|
|
id,
|
|
name: 'My XOR',
|
|
symbol: 'box',
|
|
nodes: [
|
|
{ id: 'a', kind: 'input', x: 0, y: 0, label: 'A' },
|
|
{ id: 'b', kind: 'input', x: 0, y: 4, label: 'B' },
|
|
{ id: 'n1', kind: 'nand', x: 4, y: 2 },
|
|
{ id: 'n2', kind: 'nand', x: 8, y: 0 },
|
|
{ id: 'n3', kind: 'nand', x: 8, y: 4 },
|
|
{ id: 'n4', kind: 'nand', x: 12, y: 2 },
|
|
{ id: 'q', kind: 'output', x: 16, y: 2, label: 'Q' },
|
|
],
|
|
wires: [
|
|
{ id: 'w1', from: { node: 'a', pin: 'q' }, to: { node: 'n1', pin: 'a' } },
|
|
{ id: 'w2', from: { node: 'b', pin: 'q' }, to: { node: 'n1', pin: 'b' } },
|
|
{ id: 'w3', from: { node: 'a', pin: 'q' }, to: { node: 'n2', pin: 'a' } },
|
|
{ id: 'w4', from: { node: 'n1', pin: 'q' }, to: { node: 'n2', pin: 'b' } },
|
|
{ id: 'w5', from: { node: 'n1', pin: 'q' }, to: { node: 'n3', pin: 'a' } },
|
|
{ id: 'w6', from: { node: 'b', pin: 'q' }, to: { node: 'n3', pin: 'b' } },
|
|
{ id: 'w7', from: { node: 'n2', pin: 'q' }, to: { node: 'n4', pin: 'a' } },
|
|
{ id: 'w8', from: { node: 'n3', pin: 'q' }, to: { node: 'n4', pin: 'b' } },
|
|
{ id: 'w9', from: { node: 'n4', pin: 'q' }, to: { node: 'q', pin: 'a' } },
|
|
],
|
|
}
|
|
}
|
|
|
|
describe('campaign', () => {
|
|
it('starts with NAND and earns the rest', () => {
|
|
const p = createProject({ examples: true, campaign: true })
|
|
expect(p.mode).toBe('campaign')
|
|
expect(p.components).toHaveLength(1)
|
|
expect(kindUnlocked(p, 'nand')).toBe(true)
|
|
expect(kindUnlocked(p, 'clock')).toBe(false)
|
|
expect(challengeOpen(p, 'not')).toBe(true)
|
|
expect(challengeOpen(p, 'and')).toBe(false)
|
|
expect(unlockedBy(p, 'not')).toEqual({ challenges: ['and', 'or', 'sr'], kinds: ['high', 'low'] })
|
|
p.solved.push('not')
|
|
expect(challengeOpen(p, 'and')).toBe(true)
|
|
expect(kindUnlocked(p, 'high')).toBe(true)
|
|
})
|
|
|
|
it('a challenge component becomes a part once solved, and sandbox projects have everything', () => {
|
|
const p = createProject({ examples: false, campaign: true })
|
|
const def: ComponentDef = { ...xor(), challenge: 'xor' }
|
|
expect(defUsable(p, def)).toBe(false)
|
|
p.solved.push('xor')
|
|
expect(defUsable(p, def)).toBe(true)
|
|
const sandbox = createProject({ examples: false })
|
|
expect(kindUnlocked(sandbox, 'clock') && challengeOpen(sandbox, 'dff') && defUsable(sandbox, { ...def, challenge: 'dff' })).toBe(true)
|
|
})
|
|
|
|
it('every challenge can be reached, and the chain has no loops', () => {
|
|
for (const c of CHALLENGES) expect(NEEDS[c.id], c.id).toBeDefined()
|
|
const p = createProject({ examples: false, campaign: true })
|
|
for (let round = 0; round < CHALLENGES.length; round++) {
|
|
for (const c of CHALLENGES) if (challengeOpen(p, c.id) && !p.solved.includes(c.id)) p.solved.push(c.id)
|
|
}
|
|
expect(p.solved).toHaveLength(CHALLENGES.length)
|
|
})
|
|
|
|
it('keeps the mode through validation', () => {
|
|
const p = createProject({ examples: false, campaign: true })
|
|
expect(toProject(JSON.parse(JSON.stringify(p))).mode).toBe('campaign')
|
|
expect(toProject({ ...JSON.parse(JSON.stringify(p)), mode: 'cheat' }).mode).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('puzzles', () => {
|
|
it('describes a circuit by its truth table, without the circuit', () => {
|
|
const spec = makePuzzle([xor()], 'x', 'Odd one', '')
|
|
expect(spec.inputs).toEqual(['A', 'B'])
|
|
expect(spec.steps.map(s => s.expect.Q)).toEqual([0, 1, 1, 0])
|
|
expect(spec.author).toBe(16)
|
|
expect(JSON.stringify(spec)).not.toContain('nand')
|
|
expect(isTable({ ...spec, id: 'pz:x' })).toBe(true)
|
|
})
|
|
|
|
it('a different circuit with the same behaviour passes, and is scored by its own transistors', () => {
|
|
const spec = { ...makePuzzle([xor()], 'x', 'XOR', ''), id: 'pz:abc' }
|
|
const mine = xor('y')
|
|
// Add a useless gate: still correct, but costs 4 more.
|
|
mine.nodes.push({ id: 'extra', kind: 'nand', x: 20, y: 9 })
|
|
const score = scoreSolution([mine], 'y', spec)
|
|
expect(score.pass).toBe(true)
|
|
expect(score.transistors).toBe(20)
|
|
expect(score.delay).toBe(3)
|
|
})
|
|
|
|
it('a wrong circuit fails with the step that shows it', () => {
|
|
const spec = { ...makePuzzle([xor()], 'x', 'XOR', ''), id: 'pz:abc' }
|
|
const wrong = xor('w')
|
|
wrong.wires = wrong.wires.filter(w => w.id !== 'w8')
|
|
const score = scoreSolution([wrong], 'w', spec)
|
|
expect(score.pass).toBe(false)
|
|
expect(score.message).toMatch(/Q is \d, expected \d/)
|
|
const renamed = xor('r')
|
|
renamed.nodes.find(n => n.id === 'q')!.label = 'Out'
|
|
expect(scoreSolution([renamed], 'r', spec).message).toBe('Missing output pin "Q"')
|
|
})
|
|
|
|
it('a circuit with memory becomes a run of steps from power-on', () => {
|
|
// SR latch from two NANDs with active-low inputs.
|
|
const latch: ComponentDef = {
|
|
id: 'l',
|
|
name: 'Latch',
|
|
symbol: 'box',
|
|
nodes: [
|
|
{ id: 's', kind: 'input', x: 0, y: 0, label: 'S' },
|
|
{ id: 'r', kind: 'input', x: 0, y: 4, label: 'R' },
|
|
{ id: 'n1', kind: 'nand', x: 5, y: 0 },
|
|
{ id: 'n2', kind: 'nand', x: 5, y: 4 },
|
|
{ id: 'q', kind: 'output', x: 12, y: 0, label: 'Q' },
|
|
],
|
|
wires: [
|
|
{ id: 'w1', from: { node: 's', pin: 'q' }, to: { node: 'n1', pin: 'a' } },
|
|
{ id: 'w2', from: { node: 'n2', pin: 'q' }, to: { node: 'n1', pin: 'b' } },
|
|
{ id: 'w3', from: { node: 'r', pin: 'q' }, to: { node: 'n2', pin: 'b' } },
|
|
{ id: 'w4', from: { node: 'n1', pin: 'q' }, to: { node: 'n2', pin: 'a' } },
|
|
{ id: 'w5', from: { node: 'n1', pin: 'q' }, to: { node: 'q', pin: 'a' } },
|
|
],
|
|
}
|
|
const d = describeCircuit(id => (id === 'l' ? latch : undefined), 'l')
|
|
expect(d.sequential).toBe(true)
|
|
expect(d.steps.length).toBeGreaterThan(4)
|
|
const spec = { ...makePuzzle([latch], 'l', 'Latch', ''), id: 'pz:latch' }
|
|
expect(isTable(spec)).toBe(false)
|
|
expect(scoreSolution([{ ...latch, id: 'm' }], 'm', spec).pass).toBe(true)
|
|
})
|
|
|
|
it('refuses circuits that cannot be puzzles', () => {
|
|
const noOut = xor()
|
|
noOut.nodes = noOut.nodes.filter(n => n.kind !== 'output')
|
|
expect(() => makePuzzle([noOut], 'x', 'P', '')).toThrow('Add at least one output')
|
|
const twins = xor()
|
|
twins.nodes.find(n => n.id === 'b')!.label = 'a'
|
|
expect(() => makePuzzle([twins], 'x', 'P', '')).toThrow('its own name')
|
|
})
|
|
|
|
it('cleans puzzle data from outside', () => {
|
|
const spec = makePuzzle([xor()], 'x', 'XOR', 'Hello')
|
|
expect(cleanPuzzle(JSON.parse(JSON.stringify(spec)), 'pz:a').steps).toEqual(spec.steps)
|
|
expect(() => cleanPuzzle({ ...spec, steps: [{ set: { A: 2 }, expect: {} }] }, 'pz:a')).toThrow()
|
|
expect(() => cleanPuzzle({ ...spec, inputs: ['A', 'A'] }, 'pz:a')).toThrow()
|
|
expect(() => cleanPuzzle(null, 'pz:a')).toThrow()
|
|
})
|
|
|
|
it('a project keeps its puzzles and their components', () => {
|
|
const p = createProject({ examples: false })
|
|
const spec = { ...makePuzzle([xor()], 'x', 'XOR', ''), id: 'pz:abc12345' }
|
|
const def = startPuzzle(p, spec)
|
|
expect(def.challenge).toBe('pz:abc12345')
|
|
expect(startPuzzle(p, spec)).toBe(def)
|
|
const back = toProject(JSON.parse(JSON.stringify(p)))
|
|
expect(back.puzzles?.map(x => x.id)).toEqual(['pz:abc12345'])
|
|
expect(toProject({ ...JSON.parse(JSON.stringify(p)), puzzles: [{ ...spec, id: 'evil id' }] }).puzzles).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('puzzle of the week', () => {
|
|
it('is the same for everyone in a week and changes between weeks', () => {
|
|
expect(weeklyPuzzle('2026-43')).toEqual(weeklyPuzzle('2026-43'))
|
|
const names = new Set(['2026-40', '2026-41', '2026-42', '2026-43'].map(k => JSON.stringify(weeklyPuzzle(k).steps)))
|
|
expect(names.size).toBe(4)
|
|
})
|
|
|
|
it('has outputs that depend on every input', () => {
|
|
for (let w = 1; w <= 30; w++) {
|
|
const spec = weeklyPuzzle(`2027-${String(w).padStart(2, '0')}`)
|
|
expect(isTable(spec)).toBe(true)
|
|
const n = spec.inputs.length
|
|
for (const out of spec.outputs) {
|
|
const t = spec.steps.map(s => s.expect[out])
|
|
for (let k = 0; k < n; k++) expect(t.some((v, i) => v !== t[i ^ (1 << (n - 1 - k))]), `${spec.id} ${out} ${spec.inputs[k]}`).toBe(true)
|
|
}
|
|
}
|
|
})
|
|
|
|
it('counts ISO weeks', () => {
|
|
expect(weekKey(new Date('2026-10-20T12:00:00Z'))).toBe('2026-43')
|
|
expect(weekKey(new Date('2027-01-01T12:00:00Z'))).toBe('2026-53')
|
|
expect(weekKey(new Date('2027-01-04T12:00:00Z'))).toBe('2027-01')
|
|
})
|
|
})
|