"""Execute the retained public notebook's code cells offline and fail on misses."""
import contextlib
import io
import json
import os
import pathlib

base = pathlib.Path(__file__).resolve().parent
os.environ["MODELCAP_EVIDENCE"] = str(base / "evidence.json")
notebook = json.loads((base / "modelcap-recompute.ipynb").read_text(encoding="utf-8"))
namespace = {}
output = io.StringIO()
with contextlib.redirect_stdout(output):
    for index, cell in enumerate(notebook["cells"]):
        if cell["cell_type"] == "code":
            exec(compile("".join(cell["source"]), f"notebook-cell-{index}", "exec"), namespace)
text = output.getvalue()
(base / "recompute-output.txt").write_text(text, encoding="utf-8")
print(text)
assert not namespace["misses"], namespace["misses"]
assert namespace["unbacked"] == 0
for scope in ("current", "allVersions"):
    assert all(row["rank"][scope] == position for position, row in enumerate(namespace["board"](scope), start=1))
