"""The Python route model (core/atlas/solver.py, built from core/spec/game.yaml) must give the same policy as the JavaScript one the site and the bot still use (core/solver.js): same choices everywhere, same numbers. A quick version for CI: one colour, both samples' coding, 3 posterior draws. python tests/parity/compare_solver.py """ import json import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / "core")) sys.path.insert(0, str(ROOT / "planner")) sys.path.insert(0, str(Path(__file__).parent)) import build_policy # noqa: E402 from atlas import solver # noqa: E402 from compare_policy import compare # noqa: E402 COLOURS, DRAWS = ["Blue"], 3 runs = build_policy.coded_runs(ROOT / "data/public/form_responses.json") py = build_policy.clean(solver.build_policy(runs, draws=DRAWS, colours=COLOURS)) js = json.loads(subprocess.check_output(["node", "-e", f""" const S=require('./core/solver.js'),E=require('./core/engine.js'),fs=require('fs'); const raw=JSON.parse(fs.readFileSync('data/public/form_responses.json'));const {{runs}}=E.codeRows(raw.rows,raw.headers); console.log(JSON.stringify(S.buildPolicy(runs,{{draws:{DRAWS},colours:{json.dumps(COLOURS)}}})));"""], cwd=ROOT)) with tempfile.TemporaryDirectory() as d: a, b = Path(d, "js.json"), Path(d, "py.json") a.write_text(json.dumps(js)) b.write_text(json.dumps(py)) o = compare(a, b, 1e-9) bad = o["keys"] + o["other"] + o["numbers"] print(f"route model ({', '.join(COLOURS)}, {DRAWS} draws): " + ("ALL MATCH" if not bad else f"{len(bad)} DIFFER"), f"(largest relative difference {o['max']:.2g})") for x in bad[:10]: print(" ", x) sys.exit(1 if bad else 0)