from pathlib import Path
import json
import subprocess

ROOT = Path(__file__).resolve().parents[1]
ENGINE = ROOT / "web/static/struc_i_v1_0_4_engine.js"


def test_deterministic_engine_fixture():
    script = f"""
const engine=require({json.dumps(str(ENGINE))});
let s=123456789;
Math.random=()=>{{s=(1664525*s+1013904223)>>>0; return s/4294967296;}};
(async()=>{{
 const r=await engine.computeRegisteredProfile([0,0.35,0.9,1.4],{{kappa_min:0.01,kappa_max:1,kappa_steps:40,mc_runs:2000}});
 console.log(JSON.stringify({{p0:r.profile[0],p20:r.profile[20],p39:r.profile[39],classification:r.classification}}));
}})();
"""
    result = subprocess.run(["node", "-e", script], capture_output=True, text=True, check=True)
    data = json.loads(result.stdout)
    assert data["p0"] == {"kappa": 0.01, "epsilon": 0.004999999999999999, "nu": 0, "vulnCount": 0, "inv": 0, "rho": 0, "Ak": 1}
    assert data["p20"]["kappa"] == 0.10608183551394483
    assert data["p20"]["Ak"] == 1
    assert data["p39"] == {"kappa": 1, "epsilon": 0.4999999999999999, "nu": 2, "vulnCount": 3, "inv": 0.421, "rho": 0.2105, "Ak": 1}
    assert data["classification"]["regime"] == "Geometric Persistence"
    assert data["classification"]["state"] == "Stable Structure"
