File size: 11,002 Bytes
ae3f25f
 
834f8c4
ae3f25f
 
 
 
 
 
 
 
 
 
 
834f8c4
ae3f25f
 
834f8c4
 
c6c68c5
 
834f8c4
c6c68c5
834f8c4
ae3f25f
 
834f8c4
ae3f25f
 
 
 
 
 
 
 
 
 
 
834f8c4
 
ae3f25f
 
 
 
 
 
834f8c4
c6c68c5
 
 
ae3f25f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44b9746
ae3f25f
 
a523e89
c6c68c5
 
 
ae3f25f
 
 
 
 
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a523e89
c6c68c5
 
 
ae3f25f
 
 
 
 
 
 
 
 
 
 
 
 
 
a523e89
c6c68c5
 
 
ae3f25f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
44b9746
ae3f25f
44b9746
 
 
ae3f25f
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
a523e89
ae3f25f
 
c6c68c5
 
 
 
ae3f25f
 
 
 
 
 
 
 
 
a523e89
ae3f25f
c6c68c5
ae3f25f
 
 
 
 
 
 
 
 
 
44b9746
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
Integration tests using real YAML workflow examples.
Tests actual OrKa configurations with minimal mocking for external dependencies only.
"""

from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from orka.orchestrator import Orchestrator


class TestExampleWorkflows:
    """Test real workflow examples with minimal mocking."""

    @pytest.fixture(autouse=True)
    def setup_minimal_mocks(self):
        """Set up minimal mocks only for external dependencies that can't be tested in CI."""
        with (
            patch("redis.from_url") as mock_redis,
            patch("orka.agents.llm_agents.client") as mock_openai,
        ):
            # Mock Redis (external dependency)
            mock_redis.return_value = MagicMock()

            # Mock OpenAI responses (external API with costs)
            def create_openai_response(content="Test response"):
                response = MagicMock()
                response.choices = [MagicMock()]
                response.choices[0].message.content = content
                response.usage.total_tokens = 100
                response.usage.prompt_tokens = 80
                response.usage.completion_tokens = 20
                return response

            mock_openai.chat.completions.create.return_value = create_openai_response()

            # Note: Search tools are NOT mocked - they will use real implementations
            # This allows testing of actual search functionality and fallbacks

            yield {
                "redis": mock_redis,
                "openai": mock_openai,
            }

    def test_basic_example_workflow(self, setup_minimal_mocks):
        """Test the person_routing_with_search.yml workflow - fork/join with classification."""
        example_path = Path("examples/person_routing_with_search.yml")
        assert example_path.exists(), "person_routing_with_search.yml not found"

        # Create orchestrator with real config
        orchestrator = Orchestrator(str(example_path))

        # Verify orchestrator structure
        assert orchestrator.orchestrator_cfg["id"] == "orka-ui"
        assert orchestrator.orchestrator_cfg["strategy"] == "parallel"

        # Verify agents were created
        expected_agents = [
            "fork_2",
            "duckduckgo_4",
            "openai-classification_5",
            "join_1",
            "openai-binary_6",
            "router_7",
        ]
        for agent_id in expected_agents:
            assert agent_id in orchestrator.agents, f"Agent {agent_id} not found"

        # Test agent types
        assert orchestrator.agents["fork_2"].__class__.__name__ == "ForkNode"
        assert orchestrator.agents["duckduckgo_4"].__class__.__name__ == "DuckDuckGoTool"
        assert (
            orchestrator.agents["openai-classification_5"].__class__.__name__
            == "LocalLLMAgent"  # Updated: converted from OpenAI to Local LLM
        )

    def test_complex_flow_workflow_structure(self):
        """Test the temporal_change_search_synthesis.yml workflow - temporal analysis with search."""
        complex_path = Path("examples/temporal_change_search_synthesis.yml")
        assert complex_path.exists(), "temporal_change_search_synthesis.yml not found"

        # Create orchestrator
        orchestrator = Orchestrator(str(complex_path))

        # Verify complex workflow structure
        assert orchestrator.orchestrator_cfg["strategy"] == "sequential"

        # Verify all agents exist
        expected_agents = [
            "detect_change",
            "fork_temporal",
            "generate_before_query",
            "generate_after_query",
            "search_before",
            "search_after",
            "join_paths",
            "synthesize_timeline_answer",
        ]
        for agent_id in expected_agents:
            assert agent_id in orchestrator.agents, f"Agent {agent_id} not found"

        # Verify fork node has correct targets
        fork_agent = orchestrator.agents["fork_temporal"]
        assert hasattr(fork_agent, "targets")

    def test_basic_memory_workflow_structure(self):
        """Test the memory_read_fork_join_router.yml workflow - memory operations."""
        memory_path = Path("examples/memory_read_fork_join_router.yml")
        assert memory_path.exists(), "memory_read_fork_join_router.yml not found"

        # Create orchestrator
        orchestrator = Orchestrator(str(memory_path))

        # Verify memory agents exist
        memory_agents = ["memory-read_0", "memory-write_final"]
        for agent_id in memory_agents:
            assert agent_id in orchestrator.agents, f"Memory agent {agent_id} not found"

        # Verify other key agents
        key_agents = ["openai-answer_2", "fork_3", "join_9", "openai-binary_10", "router_11"]
        for agent_id in key_agents:
            assert agent_id in orchestrator.agents, f"Key agent {agent_id} not found"

    def test_failover_workflow_structure(self):
        """Test the failover_search_and_validate.yml workflow - error handling."""
        failover_path = Path("examples/failover_search_and_validate.yml")
        assert failover_path.exists(), "failover_search_and_validate.yml not found"

        # Create orchestrator
        orchestrator = Orchestrator(str(failover_path))

        # Verify decision-tree strategy
        assert orchestrator.orchestrator_cfg["strategy"] == "decision-tree"

        # Verify failover agents exist
        failover_agents = ["test_failover", "test_failover2"]
        for agent_id in failover_agents:
            assert agent_id in orchestrator.agents, f"Failover agent {agent_id} not found"

        # Verify other agents
        other_agents = ["answer_9", "need_answer", "router_answer"]
        for agent_id in other_agents:
            assert agent_id in orchestrator.agents, f"Agent {agent_id} not found"

    def test_agent_dependencies_basic_example(self):
        """Test that agent dependencies are properly configured in basic example."""
        example_path = Path("examples/person_routing_with_search.yml")
        orchestrator = Orchestrator(str(example_path))

        # Test specific dependency relationships
        join_agent = orchestrator.agents["join_1"]
        router_agent = orchestrator.agents["router_7"]

        # Verify join agent has the right group_id
        assert hasattr(join_agent, "group_id")

        # Verify router has decision key
        assert hasattr(router_agent, "decision_key") or hasattr(router_agent, "params")

    def test_memory_namespace_configuration(self):
        """Test memory namespace configuration in memory workflow."""
        memory_path = Path("examples/memory_read_fork_join_router.yml")
        orchestrator = Orchestrator(str(memory_path))

        # Check that memory agents have namespace
        memory_read = orchestrator.agents["memory-read_0"]
        memory_write = orchestrator.agents["memory-write_final"]

        # Both should be memory-related nodes
        assert hasattr(memory_read, "namespace") or hasattr(memory_read, "node_id")
        assert hasattr(memory_write, "namespace") or hasattr(memory_write, "node_id")

    def test_search_integration_configuration(self):
        """Test DuckDuckGo search integration in complex flow."""
        complex_path = Path("examples/temporal_change_search_synthesis.yml")
        orchestrator = Orchestrator(str(complex_path))

        # Verify search agents exist and are configured
        search_agents = ["search_before", "search_after"]
        for agent_id in search_agents:
            agent = orchestrator.agents[agent_id]
            assert agent.__class__.__name__ == "DuckDuckGoTool"
            assert hasattr(agent, "prompt") or hasattr(agent, "tool_id")

    def test_classification_options_configuration(self):
        """Test classification agent options are properly configured."""
        example_path = Path("examples/person_routing_with_search.yml")
        orchestrator = Orchestrator(str(example_path))

        # Check classification agent (updated for Local LLM)
        classifier = orchestrator.agents["openai-classification_5"]
        assert (
            classifier.__class__.__name__ == "LocalLLMAgent"
        )  # Updated: converted from OpenAI to Local LLM

        # Verify it has params configured (where options are stored)
        assert hasattr(classifier, "params")

    def test_router_mapping_configuration(self):
        """Test router mapping configuration in basic example."""
        example_path = Path("examples/person_routing_with_search.yml")
        orchestrator = Orchestrator(str(example_path))

        # Check router configuration
        router = orchestrator.agents["router_7"]
        assert router.__class__.__name__ == "RouterNode"

        # Router should have routing configuration
        assert hasattr(router, "routing_map") or hasattr(router, "params")

    def test_fork_targets_configuration(self):
        """Test fork node targets are properly configured."""
        example_path = Path("examples/person_routing_with_search.yml")
        orchestrator = Orchestrator(str(example_path))

        # Check fork configuration
        fork = orchestrator.agents["fork_2"]
        assert fork.__class__.__name__ == "ForkNode"

        # Fork should have targets configured
        assert hasattr(fork, "targets")

    def test_workflow_config_validation(self):
        """Test that all example workflows pass validation."""
        example_files = [
            "examples/person_routing_with_search.yml",
            "examples/temporal_change_search_synthesis.yml",
            "examples/memory_read_fork_join_router.yml",
            "examples/failover_search_and_validate.yml",
        ]

        for example_file in example_files:
            if Path(example_file).exists():
                # Should not raise an exception
                orchestrator = Orchestrator(example_file)
                assert orchestrator is not None
                assert len(orchestrator.agents) > 0

    def test_mixed_agent_types_integration(self):
        """Test that mixed agent types work together in complex workflows."""
        complex_path = Path("examples/temporal_change_search_synthesis.yml")
        orchestrator = Orchestrator(str(complex_path))

        # Verify we have different types of agents working together
        agent_types = set()
        for agent in orchestrator.agents.values():
            agent_types.add(agent.__class__.__name__)

        # Should have multiple different agent types
        assert len(agent_types) >= 3, f"Expected multiple agent types, got: {agent_types}"

        # Should include both LLM and search agents (updated for Local LLM)
        assert any(
            "LocalLLMAgent" in agent_type for agent_type in agent_types
        ), f"No LocalLLMAgent found in: {agent_types}"
        assert any(
            "DuckDuckGo" in agent_type for agent_type in agent_types
        ), f"No DuckDuckGo found in: {agent_types}"