#!/usr/bin/env python3
"""Run a scenario twice and verify deterministic artifacts are identical."""
from __future__ import annotations

import hashlib
import shutil
import subprocess
import sys
from pathlib import Path

FILES = [
    "summary.json",
    "timeline.csv",
    "rdma_packets.csv",
    "openzfs_txg.csv",
    "gpu_kernels.csv",
    "sst_events.csv",
    "sst_components.csv",
    "scaling_decisions.csv",
]


def digest(path: Path) -> str:
    h = hashlib.sha256()
    for name in FILES:
        data = (path / name).read_bytes()
        h.update(name.encode())
        h.update(b"\0")
        h.update(data)
        h.update(b"\0")
    return h.hexdigest()


def main() -> None:
    if len(sys.argv) != 6:
        raise SystemExit("usage: repeatability.py BIN SCENARIO SYSTEM OUT_A OUT_B")
    binary, scenario, system, out_a, out_b = sys.argv[1:]
    out_a = Path(out_a)
    out_b = Path(out_b)
    shutil.rmtree(out_a, ignore_errors=True)
    shutil.rmtree(out_b, ignore_errors=True)
    subprocess.run([binary, system, "--scenario", scenario, "--out", str(out_a)], check=True)
    subprocess.run([binary, system, "--scenario", scenario, "--out", str(out_b)], check=True)
    da = digest(out_a)
    db = digest(out_b)
    assert da == db, f"repeatability mismatch: {da} != {db}"
    print(f"repeatability {system}: {da}")


if __name__ == "__main__":
    main()
