test(spec): golden-fixture cross-runtime parity gate for the node picker

Replace the env-driven parity harness with a shared scenario module and a
committed golden the node picker asserts independently. The goja side asserts
the same golden, so neither runtime invokes the other at test time.
This commit is contained in:
pj committed 2026-06-01 17:27:14 +05:30
1 parent 0fa6968a5a
commit e3bb9aeeb2
3 files changed
+193 -27

No files matched your search

+42 -27
View File
@@ -1,37 +1,52 @@
// Cross-runtime parity harness. Run under node (tsx) by the Go test
// internal/verifier/parity_test.go, which feeds the SAME seed and candidate
// list into the goja verifier and asserts an identical action stream. Both
// engines run the SAME pick.ts over the SAME Pcg, so any divergence here is an
// engine-level (number/bigint) parity bug.
// Shared cross-runtime parity scenario: the FIXED seed, FIXED candidate list,
// and FIXED action root that both the node test (parity.test.ts) and the goja
// test (internal/verifier/parity_test.go) drive. Each side runs the SAME
// pick.ts over the SAME Pcg and asserts the SAME committed golden
// (fixtures/parity-golden.json); matching one golden on both sides proves the
// two engines agree without either invoking the other.
//
// Inputs come from env so the Go side controls them:
// SANDERLING_PARITY_SEED decimal uint64 seed
// SANDERLING_PARITY_STEPS action count to emit
// SANDERLING_PARITY_CANDIDATES JSON array of {x,y,selector,width,height}
//
// Output: one JSON array of serialized actions on stdout.
// The candidate ORDER and the per-tick PCG draw order are the parity contract.
// The weighted root mixes a tap branch (1 candidate draw) with a typing branch
// (1 candidate draw + 1 corpus draw), so a tick exercises weighted selection, a
// builtin, and the input corpus together; reordering candidates or adding or
// dropping a draw on either side shifts the stream and fails the golden.
import { installRuntime } from "../src/runtime-entry.ts";
import { Pcg } from "../src/pcg.ts";
import { nextAction } from "../src/pick.ts";
import { serializeAction, type SerializedAction } from "../src/runtime-entry.ts";
import { taps, typing, weighted } from "../src/actions.ts";
import type { BuiltinVerb, Candidate, GeneratorNode, Host } from "../src/action-tree.ts";
const seed = BigInt(process.env.SANDERLING_PARITY_SEED ?? "0");
const steps = Number(process.env.SANDERLING_PARITY_STEPS ?? "0");
const candidates = JSON.parse(process.env.SANDERLING_PARITY_CANDIDATES ?? "[]") as Candidate[];
export const PARITY_SEED_HI = 0x9e3779b97f4a7c15n;
export const PARITY_STEPS = 20;
const host: Host = {
export const PARITY_CANDIDATES: Candidate[] = [
{ x: 50, y: 60, selector: "id:alpha", width: 100, height: 40 },
{ x: 150, y: 160, selector: "id:beta", width: 120, height: 48 },
{ x: 250, y: 260, selector: "id:gamma", width: 80, height: 32 },
];
// A 3:1 weighted split over taps and typing. The stub host returns the same
// candidate list for every verb so the only variables are the draw order and
// the JS engine's number/bigint behavior.
export const PARITY_ROOT: GeneratorNode = weighted([3, taps], [1, typing]);
const HOST: Host = {
platform: () => "android",
queryCandidates: (_verb: BuiltinVerb) => candidates,
queryCandidates: (_verb: BuiltinVerb) => PARITY_CANDIDATES,
reportUnsupported: () => {},
seedHi: () => seed,
seedHi: () => PARITY_SEED_HI,
seedLo: () => 0n,
};
const root: GeneratorNode = { kind: "builtin", verb: "taps" };
installRuntime(host, root, () => ({}));
const nextAction = (globalThis as { __sanderlingNextAction__: () => unknown })
.__sanderlingNextAction__;
const stream = [];
for (let i = 0; i < steps; i++) stream.push(nextAction());
process.stdout.write(JSON.stringify(stream));
// runParity emits the parity scenario's action stream from a fresh Pcg. It
// drives pick.ts directly (not installRuntime, whose globals are locked once)
// so the caller can run it repeatedly to assert determinism.
export function runParity(): (SerializedAction | null)[] {
const rng = new Pcg(PARITY_SEED_HI, 0n);
const stream: (SerializedAction | null)[] = [];
for (let i = 0; i < PARITY_STEPS; i++) {
stream.push(serializeAction(nextAction(PARITY_ROOT, rng, HOST)));
}
return stream;
}