#!/usr/bin/env python3 """Dbg Mode 功能测试""" import sys import os import tempfile import json from pathlib import Path sys.path.insert(0, str(Path(__file__).parent / "lib")) # 实际的调试步骤 DBG_STEPS = ["confirm_symptoms", "load_context", "reproduce", "locate_root_cause", "fix", "verify_again", "document", "close"] def test_dbg_evidence_first_gate(): """测试证据先于修复门控""" from air_runtime.modes.dbg_mode import EvidenceFirstGate, WorkflowViolation with tempfile.TemporaryDirectory() as tmpdir: session_id = "dbg-001" gate = EvidenceFirstGate(session_id) # 初始状态:无证据,不能修改代码 assert gate.can_modify_code() == False # 尝试修改代码应该抛出异常 try: gate.gate_check() assert False, "应该抛出 WorkflowViolation" except WorkflowViolation as e: assert "取证" in str(e) # 收集证据后可以修改代码 gate.record_evidence("screenshot", "login.png") gate.record_evidence("log_analysis", "app.log") assert gate.can_modify_code() == True gate.gate_check() # 不应抛异常 print("✓ 证据先于修复门控测试通过") return True def test_dbg_start_session(): """测试启动调试会话""" from air_runtime.modes.dbg_mode import start_session, get_step with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 启动调试会话 result = start_session(project_root, "T-001") # 检查返回值 assert "sessionId" in result assert "sessionPath" in result assert "currentStep" in result # 检查会话文件存在 session_path = Path(result["sessionPath"]) assert session_path.exists() # 检查步骤是 confirm_symptoms step = get_step(session_path) assert step == "confirm_symptoms", f"初始步骤应为 confirm_symptoms,实际: {step}" print(f"✓ 启动调试会话测试通过,会话: {result['sessionId']}, 步骤: {step}") return True def test_dbg_advance_step(): """测试步骤推进(提供正确证据)""" from air_runtime.modes.dbg_mode import start_session, advance_step, get_step with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 启动会话 result = start_session(project_root, "T-002") session_path = Path(result["sessionPath"]) # confirm_symptoms 步骤需要 symptom, expected, actual evidence = { "symptom": "程序崩溃", "expected": "正常输出 hello world", "actual": "段错误 (segmentation fault)" } next_step = advance_step(session_path, evidence) assert next_step == "load_context", f"下一步应为 load_context,实际: {next_step}" print(f"✓ 步骤推进测试通过,下一步: {next_step}") return True def test_dbg_snapshot(): """测试快照功能""" from air_runtime.modes.dbg_mode import pre_fix_snapshot with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 初始化 git 仓库 import subprocess subprocess.run(["git", "init"], cwd=project_root, capture_output=True) subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=project_root, capture_output=True) subprocess.run(["git", "config", "user.name", "Test"], cwd=project_root, capture_output=True) # 创建源文件 src_dir = project_root / "src" src_dir.mkdir(parents=True, exist_ok=True) (src_dir / "main.cpp").write_text("int main() { return 0; }") # 初始提交 subprocess.run(["git", "add", "."], cwd=project_root, capture_output=True) subprocess.run(["git", "commit", "-m", "initial"], cwd=project_root, capture_output=True) # 创建快照 snapshot_id = pre_fix_snapshot(project_root, "T-003") # 快照 ID 可能是空的(因为没有 filesChanged),但函数应该正常执行 print(f"✓ 快照功能测试通过") return True def main(): print("=" * 50) print("Dbg Mode 功能测试") print("=" * 50) tests = [ ("证据先于修复门控", test_dbg_evidence_first_gate), ("启动调试会话", test_dbg_start_session), ("步骤推进", test_dbg_advance_step), ("快照功能", test_dbg_snapshot), ] passed = 0 failed = 0 for name, test_fn in tests: try: test_fn() passed += 1 except Exception as e: print(f"✗ {name} 失败: {e}") import traceback traceback.print_exc() failed += 1 print("=" * 50) print(f"测试结果: {passed} 通过, {failed} 失败") print("=" * 50) return failed == 0 if __name__ == "__main__": success = main() sys.exit(0 if success else 1)