71 lines
2.7 KiB
Bash
Executable File
71 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# CHECK -- users-census extract (Extract lane, 2026-10-29).
|
|
# Re-derives the census from the live jsonplaceholder API and diffs it against
|
|
# the committed users-census.{csv,md,json}. Exit code IS the verdict: 0 = PASS.
|
|
set -u
|
|
cd /root/troupe-standup || exit 1
|
|
|
|
WORK="$(mktemp -d)" || exit 1
|
|
trap 'rm -rf "$WORK"' EXIT
|
|
|
|
curl -s -m 20 -o "$WORK/users.json" https://jsonplaceholder.typicode.com/users || exit 1
|
|
curl -s -m 20 -o "$WORK/todos.json" https://jsonplaceholder.typicode.com/todos || exit 1
|
|
|
|
python3 - "$WORK" <<'PY'
|
|
import json, subprocess, sys
|
|
|
|
work = sys.argv[1]
|
|
users = json.load(open(f"{work}/users.json"))
|
|
todos = json.load(open(f"{work}/todos.json"))
|
|
by = {}
|
|
for t in todos:
|
|
by.setdefault(t["userId"], []).append(t)
|
|
|
|
rows = []
|
|
for u in sorted(users, key=lambda x: x["id"]):
|
|
ts = by.get(u["id"], [])
|
|
rows.append([u["id"], u["name"], u["username"], u["email"], u["address"]["city"],
|
|
len(ts), sum(1 for t in ts if t["completed"])])
|
|
|
|
def sh(cmd):
|
|
return subprocess.run(["bash", "-c", cmd], capture_output=True, text=True).stdout
|
|
|
|
# 1) CSV replay.
|
|
got_csv = open("wizard-work/ledger/users-census.csv", encoding="utf-8").read().strip().replace("\r", "")
|
|
expected_csv = "id,name,username,email,city,todo_count,completed_count\n" + "\n".join(",".join(map(str, r)) for r in rows)
|
|
if got_csv != expected_csv:
|
|
print("FAIL users-census.csv is not a replay of the source\n")
|
|
print("expected:", expected_csv)
|
|
print("got :", got_csv)
|
|
sys.exit(1)
|
|
|
|
# 2) Markdown data rows replay.
|
|
md = open("wizard-work/ledger/users-census.md", encoding="utf-8").read().splitlines()
|
|
data_rows = []
|
|
for l in md:
|
|
segs = [p.strip() for p in l.split("|")]
|
|
if len(segs) >= 2 and segs[1].isdigit():
|
|
data_rows.append(l)
|
|
expected_md = [f"| {r[0]} | {r[1]} | {r[2]} | {r[3]} | {r[4]} | {r[5]} | {r[6]} |" for r in rows]
|
|
if data_rows != expected_md:
|
|
print("FAIL users-census.md rows do not replay\n")
|
|
print("got :", data_rows)
|
|
print("expected:", expected_md)
|
|
sys.exit(1)
|
|
|
|
# 3) JSON replay: structural fields + totals + user id set.
|
|
j = json.load(open("wizard-work/ledger/users-activity.json", encoding="utf-8"))
|
|
if j.get("n_users") != len(rows) or j.get("n_todos") != len(todos):
|
|
print("FAIL users-activity.json n_* mismatch")
|
|
sys.exit(1)
|
|
if j.get("totals", {}).get("completed") != sum(r[6] for r in rows):
|
|
print("FAIL users-activity.json totals.completed mismatch")
|
|
sys.exit(1)
|
|
if sorted(x["id"] for x in j.get("users", [])) != list(range(1, len(rows) + 1)):
|
|
print("FAIL users-activity.json user ids are not 1..N")
|
|
sys.exit(1)
|
|
|
|
print(f"PASS: users={len(rows)} todos={len(todos)} completed={sum(r[6] for r in rows)}")
|
|
PY
|
|
exit $?
|