# Compares the expected CTR written first (expected.py), the live Koldflux CTR calculator and ctr.py for every case.
import json, re, subprocess, sys
exp = json.load(open(sys.argv[1], encoding="utf-8"))
live = {r["id"]: r for r in json.load(open(sys.argv[2], encoding="utf-8"))}
ok_tool = ok_script = 0
def trim(s):
    return s.rstrip("0").rstrip(".") if "." in s else s
for c in exp:
    t = live[c["id"]]["answer"]
    m = re.match(r"^(-?[\d,]+(?:\.\d+)?) %$", t)
    tool = m.group(1).replace(",", "") if m else "no number"
    p = subprocess.run([sys.executable, "ctr.py", "ctr", c["clicks"] or "x", c["impressions"] or "x"], capture_output=True, text=True)
    mm = re.match(r"CTR = (-?[\d.]+)%", p.stdout)
    script = trim(mm.group(1)) if mm else "no number"
    want = c["expected"]
    tool_ok, script_ok = tool == want, script == want
    ok_tool += tool_ok; ok_script += script_ok
    print("%-30s expected=%-10s tool=%-10s script=%-10s %s %s | tool text: %s" % (c["id"], want, tool, script, "TOOL-OK" if tool_ok else "TOOL-DIFF", "SCRIPT-OK" if script_ok else "SCRIPT-DIFF", t))
print("tool agrees on %d of %d, script on %d of %d" % (ok_tool, len(exp), ok_script, len(exp)))
