#!/usr/bin/env python3 """Eng Mode 功能测试""" import sys import os import tempfile import json from pathlib import Path import time sys.path.insert(0, str(Path(__file__).parent / "lib")) def test_eng_init(): """测试调度器初始化""" from air_runtime.modes.eng_mode import enter_engine, _init_state with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) state_path, _ = enter_engine(project_root) # 检查状态文件存在 assert Path(state_path).exists() # 检查初始状态 from air_runtime.io import safe_json_load state = safe_json_load(Path(state_path)) assert state["enabled"] == True assert state["engineMode"] == "idle" assert state["nextAction"] == "plan" print("✓ 调度器初始化测试通过") return True def test_eng_build_plan(): """测试构建执行计划""" from air_runtime.modes.eng_mode import build_engine_plan with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 创建 AirPlan 目录 airplan_dir = project_root / "AirPlan" airplan_dir.mkdir(parents=True, exist_ok=True) # 创建 todo.md todo_path = airplan_dir / "todo.md" todo_path.write_text(""" | Task | Status | Files/Dirs | Done When | |------|--------|------------|-----------| | [T-001] 任务A | TODO | src/a.cpp | 编译通过 | | [T-002] 任务B | TODO | src/b.cpp | 编译通过 | """) result = build_engine_plan(project_root, todo_path) assert "planPath" in result assert Path(result["planPath"]).exists() print(f"✓ 构建执行计划测试通过,任务数: {len(result['selectedTasks'])}") return True def test_eng_dispatch(): """测试任务派发""" from air_runtime.modes.eng_mode import dispatch_worker_group, enter_engine with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 初始化 enter_engine(project_root) # 创建 todo.md todo_path = project_root / "AirPlan" / "todo.md" todo_path.write_text(""" | Task | Status | Files/Dirs | Done When | |------|--------|------------|-----------| | [T-001] 任务1 | TODO | src/main.cpp | 编译通过 | | [T-002] 任务2 | TODO | src/utils.cpp | 编译通过 | """) # 派发 result = dispatch_worker_group(project_root) # 检查返回值 assert "waveId" in result assert "taskIds" in result assert len(result["taskIds"]) > 0, "应有派发的任务" print(f"✓ 任务派发测试通过,波次: {result['waveId']}, 任务数: {len(result['taskIds'])}") return True def test_eng_monitor(): """测试监控引擎""" from air_runtime.modes.eng_mode import monitor_engine, enter_engine with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 初始化 enter_engine(project_root) # 监控 result = monitor_engine(project_root) # 检查返回值 assert "engineMode" in result assert "activeWorkerCount" in result assert "nextAction" in result print(f"✓ 监控引擎测试通过,worker数: {result['activeWorkerCount']}") return True def test_eng_block_release(): """测试 block-release 阻止派发""" from air_runtime.modes.eng_mode import dispatch_worker_group, enter_engine from air_runtime.io import atomic_json_write from air_runtime.paths import airplan_root with tempfile.TemporaryDirectory() as tmpdir: project_root = Path(tmpdir) # 初始化 enter_engine(project_root) # 创建 todo.md todo_path = project_root / "AirPlan" / "todo.md" todo_path.write_text(""" | Task | Status | Files/Dirs | Done When | |------|--------|------------|-----------| | [T-001] 任务1 | TODO | src/main.cpp | 编译通过 | """) # 模拟一个 block-release 的 review 报告 rvr_root = project_root / "AirPlan" / "state" / "airrvr" / "reports" rvr_root.mkdir(parents=True, exist_ok=True) report = { "taskId": "T-001", "verdict": "fail", "highRiskAudit": { "deliveryVerdict": "block-release", "overallRisk": "critical" } } atomic_json_write(rvr_root / "T-001.json", report) # 尝试派发,应该被阻止 result = dispatch_worker_group(project_root) assert result.get("blocked") == True, "block-release 应该阻止派发" assert "block-release" in result.get("reason", ""), "阻止原因应包含 block-release" print(f"✓ block-release 阻止派发测试通过") return True def main(): print("=" * 50) print("Eng Mode 功能测试") print("=" * 50) tests = [ ("调度器初始化", test_eng_init), ("构建执行计划", test_eng_build_plan), ("任务派发", test_eng_dispatch), ("监控引擎", test_eng_monitor), ("block-release阻止", test_eng_block_release), ] 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)