""" Unit tests for orchestrator Tests: - Orchestrator initialization - Task routing - Automata cascade - SLM fallback - Pipeline execution """ import pytest from unittest.mock import Mock, AsyncMock, patch from app.core.orchestrator import Orchestrator from app.models.schemas import TaskType, Language class TestOrchestratorInit: """Tests for orchestrator initialization""" @pytest.mark.asyncio async def test_initialization(self): """Should initialize successfully""" orchestrator = Orchestrator() await orchestrator.initialize() assert orchestrator.initialized assert orchestrator.router is not None assert orchestrator.pipeline is not None assert len(orchestrator.automata) > 0 @pytest.mark.asyncio async def test_automata_loaded(self): """Should load all automata""" orchestrator = Orchestrator() await orchestrator.initialize() expected_automata = [ "python_formatter", "python_linter", "ast_fixer", "runtime_fixer", "trace_parser", "test_template" ] for automaton_name in expected_automata: assert automaton_name in orchestrator.automata class TestOrchestratorRouting: """Tests for task routing""" @pytest.fixture async def orchestrator(self): orch = Orchestrator() await orch.initialize() return orch @pytest.mark.asyncio async def test_format_uses_automata(self, orchestrator): """Format task should use Black automaton""" code = "def hello( x,y ):\n z=x+y\n return z" result = await orchestrator.process( task=TaskType.FORMAT, code=code, language=Language.PYTHON ) assert result["success"] assert result["used_automata"] assert "def hello(x, y):" in result["result"] @pytest.mark.asyncio async def test_fix_tries_automata_first(self, orchestrator): """Fix task should try automata before SLM""" code = "def hello(name)\n print(name)" result = await orchestrator.process( task=TaskType.FIX, code=code, language=Language.PYTHON ) # Should use automata (AST fixer) assert result["success"] # Check pipeline assert len(result["pipeline"]) > 0 class TestOrchestratorPipeline: """Tests for pipeline execution""" @pytest.fixture async def orchestrator(self): orch = Orchestrator() await orch.initialize() return orch @pytest.mark.asyncio async def test_pipeline_records_steps(self, orchestrator): """Should record all pipeline steps""" code = "def hello( x ):\n return x" result = await orchestrator.process( task=TaskType.FORMAT, code=code, language=Language.PYTHON ) assert "pipeline" in result assert len(result["pipeline"]) > 0 # Check step structure step = result["pipeline"][0] assert "step_type" in step assert "component" in step assert "duration_ms" in step assert "success" in step @pytest.mark.asyncio async def test_duration_tracking(self, orchestrator): """Should track execution duration""" code = "def hello():\n pass" result = await orchestrator.process( task=TaskType.FORMAT, code=code, language=Language.PYTHON ) assert "total_duration_ms" in result assert result["total_duration_ms"] > 0 class TestOrchestratorStatus: """Tests for status reporting""" @pytest.mark.asyncio async def test_get_status(self): """Should return orchestrator status""" orchestrator = Orchestrator() await orchestrator.initialize() status = await orchestrator.get_status() assert "ready" in status assert status["ready"] is True assert "automata_available" in status assert len(status["automata_available"]) > 0 @pytest.mark.asyncio async def test_status_before_init(self): """Should report not ready before initialization""" orchestrator = Orchestrator() status = await orchestrator.get_status() assert status["ready"] is False class TestOrchestratorShutdown: """Tests for cleanup""" @pytest.mark.asyncio async def test_shutdown(self): """Should shutdown cleanly""" orchestrator = Orchestrator() await orchestrator.initialize() await orchestrator.shutdown() assert not orchestrator.initialized