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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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""
Tests for multi-agent planning and execution system.
"""

import pytest
import os
import json
from app.env import CRMQueryEnv
from app.tasks import get_task_by_id, get_all_task_ids
from app.multi_agent import (
    PlannerAgent, ExecutorAgent, Coordinator,
    Plan, PlanStep
)


class TestPlannerAgentInitialization:
    """Test planner agent initialization."""

    def test_planner_initialization(self):
        """Test planner agent can be initialized."""
        planner = PlannerAgent()
        assert planner.model == "gpt-3.5-turbo"
        assert planner.temperature == 0.0

    def test_planner_with_custom_api_key(self):
        """Test planner with custom API key."""
        api_key = "test-key"
        planner = PlannerAgent(api_key=api_key)
        assert planner.api_key == api_key

    def test_planner_uses_deterministic_temperature(self):
        """Test planner uses temperature=0 for determinism."""
        planner = PlannerAgent()
        assert planner.temperature == 0.0


class TestPlannerFallback:
    """Test planner fallback behavior."""

    def test_fallback_plan_generation(self):
        """Test that fallback plan is generated correctly."""
        planner = PlannerAgent()
        
        plan = planner._generate_fallback_plan(
            task_id="test_task",
            task_description="Test task description"
        )
        
        assert plan.task_id == "test_task"
        assert plan.description == "Test task description"
        assert len(plan.steps) > 0
        assert isinstance(plan.steps[0], PlanStep)

    def test_fallback_plan_structure(self):
        """Test fallback plan has correct structure."""
        planner = PlannerAgent()
        
        plan = planner._generate_fallback_plan("task_1", "Description")
        
        assert plan.total_steps > 0
        assert all(step.tool in ["search_customers", "search_orders", "search_tickets", "submit_answer"] 
                   for step in plan.steps)
        assert all(hasattr(step, 'rationale') for step in plan.steps)
        assert all(hasattr(step, 'expected_output') for step in plan.steps)


class TestExecutorAgentInitialization:
    """Test executor agent initialization."""

    def test_executor_initialization(self):
        """Test executor agent can be initialized."""
        executor = ExecutorAgent()
        assert executor.memory_hits == 0
        assert executor.memory_misses == 0
        assert isinstance(executor.execution_history, list)
        assert len(executor.execution_history) == 0

    def test_executor_has_memory_tracking(self):
        """Test executor has memory tracking fields."""
        executor = ExecutorAgent()
        
        assert hasattr(executor, 'retrieved_entities')
        assert hasattr(executor, 'memory_hits')
        assert hasattr(executor, 'memory_misses')

    def test_executor_reset(self):
        """Test executor reset method."""
        executor = ExecutorAgent()
        executor.memory_hits = 5
        executor.memory_misses = 3
        
        executor.reset()
        
        assert executor.memory_hits == 0
        assert executor.memory_misses == 0
        assert len(executor.execution_history) == 0


class TestPlanExecution:
    """Test plan execution against environment."""

    def test_execute_simple_plan(self):
        """Test executing a simple plan."""
        env = CRMQueryEnv()
        env.reset()
        
        executor = ExecutorAgent()
        
        plan = Plan(
            task_id=env.current_task_id,
            description="Test plan",
            total_steps=2,
            reasoning="Test reasoning",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="search_customers",
                    arguments={"tier": "Gold"},
                    rationale="Find gold tier customers",
                    expected_output="List of customers"
                ),
                PlanStep(
                    step_number=2,
                    tool="submit_answer",
                    arguments={"customer_ids": []},
                    rationale="Submit answer",
                    expected_output="Task completed"
                )
            ]
        )
        
        result = executor.execute_plan(plan, env)
        
        assert result["plan_id"] == env.current_task_id
        assert result["steps_executed"] > 0
        assert env.done

    def test_execution_tracks_memory(self):
        """Test that execution tracks retrieved entities."""
        env = CRMQueryEnv()
        env.reset()
        
        executor = ExecutorAgent()
        
        plan = Plan(
            task_id=env.current_task_id,
            description="Memory tracking test",
            total_steps=1,
            reasoning="Test",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="search_customers",
                    arguments={"tier": "Gold"},
                    rationale="Search",
                    expected_output="Results"
                )
            ]
        )
        
        result = executor.execute_plan(plan, env)
        
        assert len(executor.retrieved_entities['customers']) > 0
        assert result["memory_efficiency"] > 0

    def test_execution_stops_on_done(self):
        """Test execution stops when environment marks done."""
        env = CRMQueryEnv()
        env.reset()
        
        executor = ExecutorAgent()
        
        plan = Plan(
            task_id=env.current_task_id,
            description="Stop early test",
            total_steps=3,
            reasoning="Test",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="submit_answer",
                    arguments={"customer_ids": ["C001"]},
                    rationale="Early submit",
                    expected_output="Done"
                ),
                PlanStep(
                    step_number=2,
                    tool="search_customers",
                    arguments={"tier": "Gold"},
                    rationale="Should not execute",
                    expected_output="Should not reach"
                ),
                PlanStep(
                    step_number=3,
                    tool="submit_answer",
                    arguments={"customer_ids": ["C002"]},
                    rationale="Also should not execute",
                    expected_output="Should not reach"
                )
            ]
        )
        
        result = executor.execute_plan(plan, env)
        
        # Should stop after first submit_answer
        assert result["steps_executed"] == 1
        assert env.done


class TestCoordinatorInitialization:
    """Test coordinator initialization."""

    def test_coordinator_initialization(self):
        """Test coordinator can be initialized."""
        coordinator = Coordinator()
        assert hasattr(coordinator, 'planner')
        assert hasattr(coordinator, 'executor')
        assert isinstance(coordinator.planner, PlannerAgent)
        assert isinstance(coordinator.executor, ExecutorAgent)

    def test_coordinator_with_api_key(self):
        """Test coordinator with custom API key."""
        api_key = "test-key"
        coordinator = Coordinator(api_key=api_key)
        assert coordinator.planner.api_key == api_key


class TestCoordinatorPipeline:
    """Test full pipeline execution."""

    def test_pipeline_with_fallback(self):
        """Test pipeline with fallback plan."""
        env = CRMQueryEnv()
        env.reset()
        
        coordinator = Coordinator(api_key="invalid-key-for-fallback")
        
        # Pipeline should use fallback
        result = coordinator.run_pipeline(env, max_iterations=1)
        
        assert result["task_id"] == env.current_task_id
        assert result["iterations"] == 1
        assert len(result["plans"]) > 0 or "plan_error" in result

    def test_pipeline_executes_plans(self):
        """Test that pipeline executes generated plans."""
        env = CRMQueryEnv()
        env.reset()
        
        coordinator = Coordinator(api_key="invalid-key")
        
        result = coordinator.run_pipeline(env, max_iterations=1)
        
        # Even with errors, should attempt execution
        assert "task_id" in result
        assert "steps_taken" in result

    def test_pipeline_requires_reset(self):
        """Test that pipeline requires environment reset."""
        env = CRMQueryEnv()
        # Don't reset
        
        coordinator = Coordinator()
        
        with pytest.raises(ValueError):
            coordinator.run_pipeline(env)

    def test_pipeline_result_structure(self):
        """Test pipeline result has expected structure."""
        env = CRMQueryEnv()
        env.reset()
        
        coordinator = Coordinator(api_key="invalid-key")
        
        result = coordinator.run_pipeline(env, max_iterations=1)
        
        assert "task_id" in result
        assert "iterations" in result
        assert "execution_results" in result
        assert "episode_reward" in result
        assert "steps_taken" in result


class TestPlanStructure:
    """Test plan data structure."""

    def test_plan_creation(self):
        """Test creating a plan object."""
        plan = Plan(
            task_id="test_task",
            description="Test description",
            total_steps=2,
            reasoning="Test reasoning",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="search_customers",
                    arguments={"tier": "Gold"},
                    rationale="Find gold customers",
                    expected_output="List of customers"
                )
            ]
        )
        
        assert plan.task_id == "test_task"
        assert plan.total_steps == 2
        assert len(plan.steps) == 1

    def test_plan_model_dump(self):
        """Test plan can be serialized."""
        plan = Plan(
            task_id="test",
            description="Test",
            total_steps=1,
            reasoning="Test",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="search_customers",
                    arguments={},
                    rationale="Test",
                    expected_output="Test"
                )
            ]
        )
        
        dumped = plan.model_dump()
        assert isinstance(dumped, dict)
        assert "task_id" in dumped
        assert "steps" in dumped
        assert len(dumped["steps"]) == 1

    def test_plan_step_validation(self):
        """Test plan step has required fields."""
        step = PlanStep(
            step_number=1,
            tool="search_customers",
            arguments={"tier": "Gold"},
            rationale="Test",
            expected_output="Test output"
        )
        
        assert step.step_number == 1
        assert step.tool == "search_customers"
        assert isinstance(step.arguments, dict)


class TestMultiAgentMemory:
    """Test multi-agent memory tracking."""

    def test_executor_tracks_retrieved_entities(self):
        """Test executor tracks retrieved entities."""
        env = CRMQueryEnv()
        env.reset()
        
        executor = ExecutorAgent()
        
        plan = Plan(
            task_id=env.current_task_id,
            description="Memory test",
            total_steps=1,
            reasoning="Test",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="search_customers",
                    arguments={"tier": "Gold"},
                    rationale="Get customers",
                    expected_output="Customers"
                )
            ]
        )
        
        executor.execute_plan(plan, env)
        
        assert len(executor.retrieved_entities['customers']) > 0

    def test_executor_memory_efficiency_calculation(self):
        """Test executor calculates memory efficiency."""
        env = CRMQueryEnv()
        env.reset()
        
        executor = ExecutorAgent()
        
        # Plan with multiple same queries
        plan = Plan(
            task_id=env.current_task_id,
            description="Efficiency test",
            total_steps=2,
            reasoning="Test",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="search_customers",
                    arguments={"tier": "Gold"},
                    rationale="Query 1",
                    expected_output="Result 1"
                ),
                PlanStep(
                    step_number=2,
                    tool="search_orders",
                    arguments={"status": "Completed"},
                    rationale="Query 2",
                    expected_output="Result 2"
                )
            ]
        )
        
        result = executor.execute_plan(plan, env)
        
        assert "memory_efficiency" in result
        assert result["memory_efficiency"] >= 0.0


class TestMultiAgentErrorHandling:
    """Test error handling in multi-agent system."""

    def test_executor_handles_invalid_action(self):
        """Test executor handles invalid actions gracefully."""
        env = CRMQueryEnv()
        env.reset()
        
        executor = ExecutorAgent()
        
        plan = Plan(
            task_id=env.current_task_id,
            description="Error test",
            total_steps=1,
            reasoning="Test",
            steps=[
                PlanStep(
                    step_number=1,
                    tool="invalid_tool",
                    arguments={},
                    rationale="Invalid",
                    expected_output="Error"
                )
            ]
        )
        
        result = executor.execute_plan(plan, env)
        
        # Should handle gracefully
        assert isinstance(result, dict)

    def test_plan_missing_api_key(self):
        """Test plan generation handles missing API key gracefully."""
        planner = PlannerAgent(api_key=None)
        
        # Should use fallback when API key missing
        plan = planner._generate_fallback_plan("task", "description")
        
        assert isinstance(plan, Plan)
        assert len(plan.steps) > 0