logigate/tests/golden-vectors.test.ts

100 lines
4.1 KiB
TypeScript

// Dumps a conformance corpus from the TypeScript implementation: for a set of
// circuits, the netlist shape, the transistor counts, the truth tables and the
// puzzle scores exactly as the TS computes them today.
//
// The Laravel port is validated against this file, so "the PHP agrees with the
// TS" becomes a test instead of a claim.
//
// pnpm vitest run tests/golden-vectors.test.ts (writes tests/fixtures/golden.json)
import { writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { it } from 'vitest'
import { exampleLibrary, starterCircuit } from '../app/lib/logic/examples'
import { compile } from '../app/lib/logic/compile'
import { nandCounts, stats, truthTable, TRANSISTORS_PER_NAND } from '../app/lib/logic/analyze'
import { describeCircuit, makePuzzle, scoreSolution, weeklyPuzzle, weekKey, rng } from '../app/lib/logic/puzzles'
import { toProject, LIMITS } from '../app/lib/logic/project'
import type { ComponentDef } from '../app/lib/logic/types'
const components: ComponentDef[] = [...exampleLibrary(), starterCircuit()]
const get = (id: string) => components.find(d => d.id === id)
const circuits = components.map(def => {
const nl = compile(get, def.id)
const nands = nandCounts(components).get(def.id) ?? 0
const table = truthTable(get, def.id)
return {
id: def.id,
name: def.name,
nands,
transistors: nands * TRANSISTORS_PER_NAND,
netlist: {
errors: nl.errors,
gates: nl.gates?.length ?? null,
sources: nl.sources ? Object.keys(nl.sources).sort() : null,
sinks: nl.sinks ? Object.keys(nl.sinks).sort() : null,
},
stats: nl.errors.length ? null : stats(nl),
describe: (() => { try { return describeCircuit(get, def.id) } catch (e) { return { error: (e as Error).message } } })(),
truth: 'error' in table ? table : { inputs: table.inputs, outputs: table.outputs, rows: table.rows },
}
})
// Every circuit scored against a puzzle made from itself: must pass, and the
// transistor count must match. This exercises compile + Simulator + scoring.
const selfScores = components.map(def => {
try {
const spec = { ...makePuzzle(components, def.id, def.name, ''), id: `self:${def.id}` }
return { id: def.id, spec, score: scoreSolution(components, def.id, spec) }
} catch (e) {
return { id: def.id, spec: null, score: { error: (e as Error).message } }
}
})
// Cross scoring: each circuit against every other circuit's puzzle. Most fail,
// and the failure messages are part of the contract the port has to reproduce.
const crossScores: unknown[] = []
for (const a of components) {
for (const b of components) {
if (a.id === b.id) continue
try {
const spec = { ...makePuzzle(components, b.id, b.name, ''), id: `x:${b.id}` }
crossScores.push({ solution: a.id, puzzle: b.id, score: scoreSolution(components, a.id, spec) })
} catch { /* puzzles can't be made from every circuit; skip those */ }
}
}
// The weekly puzzle is generated from the ISO week and must be identical in PHP.
const weeks = ['2026-01', '2026-07', '2026-26', '2026-43', '2026-45', '2026-52']
.map(key => ({ key, puzzle: weeklyPuzzle(key) }))
// The seeded RNG drives the weekly puzzle, so pin its sequence directly.
const rngRuns = [1, 42, 1337, 2026_43].map(seed => {
const r = rng(seed)
return { seed, values: Array.from({ length: 12 }, () => r()) }
})
const out = {
note: 'Generated from the TypeScript implementation. The PHP port must reproduce this exactly.',
generatedFrom: 'tests/golden-vectors.test.ts',
limits: { project: LIMITS, transistorsPerNand: TRANSISTORS_PER_NAND },
weekKeyOf: ['2026-01-01', '2026-06-15', '2026-11-11', '2026-12-31']
.map(d => ({ date: d, key: weekKey(new Date(d + 'T12:00:00Z')) })),
components: toProject({ name: 'golden', components }).components,
circuits,
selfScores,
crossScores,
weeks,
rngRuns,
}
it('writes the golden vectors', () => {
const path = resolve(import.meta.dirname, 'fixtures/golden.json')
writeFileSync(path, JSON.stringify(out, null, 1))
console.log(
`golden.json: ${circuits.length} circuits, ${selfScores.length} self scores, ` +
`${crossScores.length} cross scores, ${weeks.length} weeks`,
)
})