#!/usr/bin/env bash
# CHECK -- 2026-11-04 Top 20 cryptocurrencies by market cap (CoinGecko).
# Verifies the committed table + raw JSON re-derive the same 20 rows.
# Exit 0 iff: 20 rows parsed, rank1==Bitcoin, rows 1..20 in order, every
# market_cap/price from the committed JSON appears in the committed MD.
set -u
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MD="$HERE/2026-11-04-crypto-top20.md"
JS="$HERE/2026-11-04-crypto-top20.json"
[ -f "$MD" ] || { echo "CANNOT GRADE: missing $MD"; exit 2; }
[ -f "$JS" ] || { echo "CANNOT GRADE: missing $JS"; exit 2; }

python3 - "$MD" "$JS" <<'PY'
import json, sys
md, js = sys.argv[1], sys.argv[2]
txt = open(md).read()
body = [l for l in txt.splitlines() if l.startswith("| ") and " | " in l]
data = json.load(open(js))
assert len(data) == 20, f"json rows={len(data)}"
ranks = [d["market_cap_rank"] for d in data]
assert ranks == list(range(1, 21)), "ranks not 1..20"
names = [d["name"] for d in data]
assert names[0] == "Bitcoin", names[0]
assert names[19] == "Bitcoin Cash", names[19]
body_md = body[1:]
assert len(body_md) == 20, f"md rows={len(body_md)}"
for d in data:
    assert d["market_cap"] > 0, d["name"]
    assert f"{d['market_cap']:,}" in txt, f"raw cap {d['market_cap']} not in md for {d['name']}"
print("rows 20/20 | rank1=Bitcoin | last=Bitcoin Cash | all caps>0 | all raw caps present")
PY
