A golden vectors: a conformance corpus dumped from the TS implementation
The Laravel backend has to re-run puzzle checks and count transistors server-side, so its PHP port of compile, the simulator, analyze and the puzzle scoring must agree with the TypeScript bit for bit. tests/fixtures/golden.json holds what the TS computes today: 20 circuits with their netlist shape, nand and transistor counts and truth tables, 324 scored runs with their exact pass/fail and message, six generated weekly puzzles, and the seeded RNG sequences behind them. Four circuits score as failures because they have no input or output pins; that is the real behaviour and belongs in the corpus. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
3bb88a379f
commit
0e72c3dc38
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,99 @@
|
|||
// 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`,
|
||||
)
|
||||
})
|
||||
Loading…
Reference in New Issue