#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""冒烟：抽跑刚修的 5 个脚本，确认能正常跑完且不再出现 IntegrityError/回滚。"""
import sqlite3
import subprocess
import time

D = "/root/gov_crawler"
CASES = ["crawl_wuwei.py", "crawl_xiyang.py", "crawl_yanchang.py",
         "crawl_xianning.py", "crawl_yichang_hbj.py"]

c = sqlite3.connect("/root/search.db", timeout=180)
c.execute("PRAGMA busy_timeout=180000")

for fn in CASES:
    b = c.execute("SELECT COUNT(*) FROM gov_raw WHERE script_name=?", (fn,)).fetchone()[0]
    t0 = time.time()
    err = ""
    try:
        p = subprocess.run(["python3", fn], cwd=D, capture_output=True, text=True, timeout=150)
        out = (p.stdout or "") + (p.stderr or "")
        rc = p.returncode
        low = out.lower()
        for kw in ("integrityerror", "traceback", "constraint failed"):
            if kw in low:
                err = "⚠️ 输出含 " + kw
    except subprocess.TimeoutExpired:
        rc, err = 124, "⏱ 超时 150s"
    a = c.execute("SELECT COUNT(*) FROM gov_raw WHERE script_name=?", (fn,)).fetchone()[0]
    m = c.execute("SELECT MAX(publish_date) FROM gov_raw WHERE script_name=?", (fn,)).fetchone()[0]
    print("  %-26s rc=%-4s %5.1fs  库 %d -> %d (%+d)  最新 %s  %s" %
          (fn, rc, time.time() - t0, b, a, a - b, m or "-", err))
c.close()
