"""批量运行剩余 AI 测试文件，收集结果汇总。"""
import subprocess
import sys
import time

TEST_FILES = [
    "apps/backend/tests/test_ai_cache.py",
    "apps/backend/tests/test_ai_chat.py",
    "apps/backend/tests/test_ai_app2.py",
    "apps/backend/tests/test_ai_apps_prompt.py",
    "apps/backend/tests/test_ai_clue_writer.py",
    "apps/backend/tests/test_ai_dispatcher.py",
    "tests/test_p5_ai_integration_properties.py",
]

TIMEOUT = 300  # 5 分钟每个文件

results = []
for f in TEST_FILES:
    print(f"\n{'='*60}")
    print(f"Running: {f}")
    print(f"{'='*60}", flush=True)
    start = time.time()
    try:
        proc = subprocess.run(
            [sys.executable, "-m", "pytest", f, "-v", "--tb=short", "-x"],
            timeout=TIMEOUT,
        )
        elapsed = time.time() - start
        results.append((f, proc.returncode, f"{elapsed:.1f}s"))
    except subprocess.TimeoutExpired:
        elapsed = time.time() - start
        results.append((f, "TIMEOUT", f"{elapsed:.1f}s"))
        print(f"  >>> TIMEOUT after {elapsed:.1f}s")
    except Exception as e:
        elapsed = time.time() - start
        results.append((f, f"ERROR: {e}", f"{elapsed:.1f}s"))

print(f"\n\n{'='*60}")
print("SUMMARY")
print(f"{'='*60}")
print(f"{'File':<55} {'Result':<10} {'Time'}")
print("-" * 80)
for f, rc, t in results:
    short = f.split("/")[-1]
    status = "PASS" if rc == 0 else ("TIMEOUT" if rc == "TIMEOUT" else f"FAIL(rc={rc})")
    print(f"{short:<55} {status:<10} {t}")
