#!/usr/bin/env python3
"""Test batch 2: re-test syntax-error ones + new ones."""
import subprocess, sys, os, json

BATCH = [
    "crawl_dxbei.py",      # Was SYNTAX_ERR - retest
    "crawl_dx.py",         # Was SYNTAX_ERR
    "crawl_susong_towns.py", # Was SYNTAX_ERR
    "crawl_zichuan.py",    # Was TIMEOUT
    "crawl_luan.py",       # ran OK
    "crawl_zhijin.py",     # new
    "crawl_zge.py",        # new
    "crawl_zyzf.py",       # new
    "crawl_jsshan.py",     # new
    "crawl_snjkq.py",      # new
]

DB = "/mnt/data/search.db"

results = []
total_before = int(subprocess.run(["sqlite3", DB, "SELECT COUNT(*) FROM gov_raw"], capture_output=True, text=True, timeout=10).stdout.strip())

for fname in BATCH:
    if not os.path.exists(fname):
        results.append({"file": fname, "status": "NOT_FOUND"})
        continue

    r_syntax = subprocess.run([sys.executable, "-m", "py_compile", fname], capture_output=True, text=True, timeout=10)
    if r_syntax.returncode != 0:
        results.append({"file": fname, "status": "SYNTAX_ERR", "detail": r_syntax.stderr.strip()[-80:]})
        continue

    try:
        r = subprocess.run([sys.executable, fname, "--pages", "1"], capture_output=True, text=True, timeout=60)
        if r.returncode != 0 and ("invalid" in r.stderr.lower() or "unknown" in r.stderr.lower() or "usage:" in r.stderr.lower()):
            r = subprocess.run([sys.executable, fname, "1"], capture_output=True, text=True, timeout=60)
        out = (r.stdout or "")[-150:].strip()
        err = (r.stderr or "")[-100:].strip()
        last_ok = ""
        for line in out.split("\n"):
            if any(x in line for x in ["新增", "New:", "完成", "Done", "OK", "✅", "📊", "共", "Total"]):
                last_ok = line
        detail = last_ok or out.split("\n")[-1] if out else (err or "no output")
        status = "OK" if r.returncode == 0 else "ERR({})".format(r.returncode)
    except subprocess.TimeoutExpired:
        status = "TIMEOUT"
        detail = ">60s"

    results.append({"file": fname, "status": status, "detail": detail[:80]})

total_after = int(subprocess.run(["sqlite3", DB, "SELECT COUNT(*) FROM gov_raw"], capture_output=True, text=True, timeout=10).stdout.strip())
net_new = total_after - total_before
results.append({"total_before": total_before, "total_after": total_after, "net_new": net_new})

print(json.dumps(results, ensure_ascii=False, indent=2))
