#!/usr/bin/env python3
"""A fake betterbench for dev/test.sh: writes a plausible results.json.
FAKE_BB_SLEEP=S sleeps S seconds first; FAKE_BB_FAIL=N exits N."""
import json, os, sys, time
a = sys.argv[1:]
if a[:1] == ["--version"]:
    print("betterbench " + os.environ.get("FAKE_BB_VERSION", "0.6.1")); sys.exit()
print("[1/3] single stream"); sys.stdout.flush()
time.sleep(float(os.environ.get("FAKE_BB_SLEEP", "0")))
if os.environ.get("FAKE_BB_FAIL"):
    print("Traceback: boom"); sys.exit(int(os.environ["FAKE_BB_FAIL"]))
out = a[a.index("--out") + 1]
rec = [{"ok": True, "completion_tokens": 256, "decode_tps": 50.0 + i, "ttft_ms": 120 + i} for i in range(5)]
res = {"single_stream": {"code": rec},
       "concurrency": [{"level": 1, "aggregate_tps": 50, "wall_s": 10},
                       {"level": 4, "aggregate_tps": 160, "wall_s": 10}],
       "prefill": [{"target_depth": 4000, "pp_tps": [1500, 1600]}, {"target_depth": 64000, "skipped": True}],
       "env": {}, "argv": a}
json.dump(res, open(out, "w"))
# The header as BetterBench renders it, for report-header.py
model = a[a.index("--model") + 1]
sw = '<span class="swatch"></span>'
chips = [sw + model, f'<code>{a[a.index("--endpoint") + 1]}</code>', "corpus v1.0", "5 passes/cat", "temp 0.7",
         "cold prefix cache (nonce)", "8,192 tok context"]
chips += [sw + n.replace("=", ": ", 1) for i, n in enumerate(a) if i and a[i - 1] == "--note"]
bar = "".join(f'<span class="chip">{c}</span>' for c in chips)
open(out.replace(".json", ".html"), "w").write(
    f'<html><style>a{{}}</style><body><header><h1>{model}</h1>\n<div class="runbar">{bar}</div>\n</header>'
    '<main>the numbers</main></body></html>')
print("[3/3] done")
