-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_graph.py
More file actions
471 lines (383 loc) · 17.5 KB
/
Copy pathtest_graph.py
File metadata and controls
471 lines (383 loc) · 17.5 KB
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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
"""
tests/test_graph.py — Unit tests for MultiAgentGraph orchestrator.
ResearchAgent and AnalystAgent are fully mocked so no LLM calls are made.
Tests verify the orchestrator's routing, state propagation, and error handling.
"""
from __future__ import annotations
import asyncio
from unittest.mock import MagicMock, patch
import pytest
from agents.analyst import AnalysisReport
from agents.base_agent import AgentExecutionError, AgentValidationError
from agents.researcher import ResearchResult
from core.graph import MultiAgentGraph
from domain_packs.research.research_analysis.pack import ResearchAnalysisPack
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
def mock_research_result() -> ResearchResult:
return ResearchResult(
query="What is AI?",
findings=["Finding 1", "Finding 2"],
summary="AI is a field of computer science.",
sources=["https://example.com"],
confidence=0.85,
metadata={"agent": "ResearchAgent"},
)
@pytest.fixture
def mock_analysis_report() -> AnalysisReport:
return AnalysisReport(
query="What is AI?",
executive_summary="AI transforms industries by automating complex tasks.",
key_insights=["Insight 1", "Insight 2"],
patterns=["Pattern 1"],
implications=["Implication 1"],
confidence=0.82,
research_summary="AI is a field of computer science.",
metadata={"agent": "AnalystAgent"},
)
# ---------------------------------------------------------------------------
# Pipeline success path
# ---------------------------------------------------------------------------
class TestMultiAgentGraphRun:
def test_run_returns_analysis_report(
self,
mock_research_result: ResearchResult,
mock_analysis_report: AnalysisReport,
) -> None:
"""Full pipeline should return an AnalysisReport on success."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.return_value = mock_research_result
mock_analyst_agent = MagicMock()
mock_analyst_agent.run_structured.return_value = mock_analysis_report
with (
patch("core.graph.ResearchAgent", return_value=mock_research_agent),
patch("core.graph.AnalystAgent", return_value=mock_analyst_agent),
):
graph = MultiAgentGraph(run_id="test-run-001")
report = graph.run("What is AI?")
assert isinstance(report, AnalysisReport)
assert report.query == "What is AI?"
assert report.confidence == 0.82
def test_run_passes_research_result_to_analyst(
self,
mock_research_result: ResearchResult,
mock_analysis_report: AnalysisReport,
) -> None:
"""AnalystAgent.run_structured should receive the ResearchResult."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.return_value = mock_research_result
mock_analyst_agent = MagicMock()
mock_analyst_agent.run_structured.return_value = mock_analysis_report
with (
patch("core.graph.ResearchAgent", return_value=mock_research_agent),
patch("core.graph.AnalystAgent", return_value=mock_analyst_agent),
):
graph = MultiAgentGraph()
graph.run("What is AI?")
# Analyst must have been called with the research result
call_args = mock_analyst_agent.run_structured.call_args
assert call_args is not None
passed_result = call_args[0][0]
assert isinstance(passed_result, ResearchResult)
assert passed_result.query == "What is AI?"
def test_run_raises_on_empty_query(self) -> None:
"""Empty query should raise AgentValidationError immediately."""
graph = MultiAgentGraph()
with pytest.raises(AgentValidationError):
graph.run(" ")
def test_run_raises_on_empty_string(self) -> None:
"""Empty string query should raise AgentValidationError."""
graph = MultiAgentGraph()
with pytest.raises(AgentValidationError):
graph.run("")
def test_run_id_defaults_to_uuid(self) -> None:
"""When no run_id is supplied, a UUID string is generated."""
graph = MultiAgentGraph()
assert graph.run_id
assert len(graph.run_id) == 36 # UUID4 format: 8-4-4-4-12
def test_run_id_is_preserved(self) -> None:
"""A supplied run_id should be stored on the instance."""
graph = MultiAgentGraph(run_id="my-custom-run-id")
assert graph.run_id == "my-custom-run-id"
# ---------------------------------------------------------------------------
# Error handling
# ---------------------------------------------------------------------------
class TestMultiAgentGraphErrors:
def test_research_failure_raises_execution_error(self) -> None:
"""When ResearchAgent fails the pipeline should raise AgentExecutionError."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.side_effect = AgentExecutionError(
"Research failed"
)
with patch("core.graph.ResearchAgent", return_value=mock_research_agent):
graph = MultiAgentGraph()
with pytest.raises(AgentExecutionError):
graph.run("What is AI?")
def test_analysis_not_called_when_research_fails(self) -> None:
"""When research fails, AnalystAgent should never be invoked."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.side_effect = AgentExecutionError("fail")
mock_analyst_agent = MagicMock()
with (
patch("core.graph.ResearchAgent", return_value=mock_research_agent),
patch("core.graph.AnalystAgent", return_value=mock_analyst_agent),
):
graph = MultiAgentGraph()
with pytest.raises(AgentExecutionError):
graph.run("What is AI?")
mock_analyst_agent.run_structured.assert_not_called()
def test_analysis_failure_raises_execution_error(
self, mock_research_result: ResearchResult
) -> None:
"""When AnalystAgent fails the pipeline should raise AgentExecutionError."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.return_value = mock_research_result
mock_analyst_agent = MagicMock()
mock_analyst_agent.run_structured.side_effect = AgentExecutionError(
"Analysis failed"
)
with (
patch("core.graph.ResearchAgent", return_value=mock_research_agent),
patch("core.graph.AnalystAgent", return_value=mock_analyst_agent),
):
graph = MultiAgentGraph()
with pytest.raises(AgentExecutionError):
graph.run("What is AI?")
# ---------------------------------------------------------------------------
# get_research_result
# ---------------------------------------------------------------------------
class TestMultiAgentGraphResearchOnly:
def test_get_research_result_returns_research_result(
self, mock_research_result: ResearchResult
) -> None:
"""get_research_result() should return a ResearchResult without analysis."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.return_value = mock_research_result
with patch("core.graph.ResearchAgent", return_value=mock_research_agent):
graph = MultiAgentGraph()
result = graph.get_research_result("What is AI?")
assert isinstance(result, ResearchResult)
assert result.query == "What is AI?"
def test_get_research_result_raises_on_empty_query(self) -> None:
"""Empty query should raise AgentValidationError."""
graph = MultiAgentGraph()
with pytest.raises(AgentValidationError):
graph.get_research_result("")
# ---------------------------------------------------------------------------
# Async pipeline
# ---------------------------------------------------------------------------
class TestMultiAgentGraphAsync:
def test_arun_returns_analysis_report(
self,
mock_research_result: ResearchResult,
mock_analysis_report: AnalysisReport,
) -> None:
"""arun() should return an AnalysisReport via async execution."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.return_value = mock_research_result
mock_analyst_agent = MagicMock()
mock_analyst_agent.run_structured.return_value = mock_analysis_report
with (
patch("core.graph.ResearchAgent", return_value=mock_research_agent),
patch("core.graph.AnalystAgent", return_value=mock_analyst_agent),
):
graph = MultiAgentGraph(run_id="async-test")
report = asyncio.run(graph.arun("What is AI?"))
assert isinstance(report, AnalysisReport)
assert report.query == "What is AI?"
# ---------------------------------------------------------------------------
# stream_events
# ---------------------------------------------------------------------------
async def _mock_events(events):
"""Helper: turn a list of dicts into an async generator."""
for event in events:
yield event
def _analysis_report_dict(**overrides):
"""Build a valid analysis_report dict with sensible defaults."""
base = {
"query": "test query",
"executive_summary": "summary",
"key_insights": ["insight"],
"patterns": ["pattern"],
"implications": ["impl"],
"confidence": 0.85,
"research_summary": "research",
}
base.update(overrides)
return base
class TestMultiAgentGraphStreamEvents:
"""Tests for ``MultiAgentGraph.stream_events()``."""
@pytest.fixture()
def graph(self) -> MultiAgentGraph:
"""Return a ``MultiAgentGraph`` with a real compiled graph."""
return MultiAgentGraph(run_id="test-stream")
@pytest.mark.asyncio
async def test_stream_events_raises_on_empty_query(self, graph):
"""Empty / whitespace query must raise AgentValidationError."""
with pytest.raises(AgentValidationError):
async for _ in graph.stream_events(""):
pass
with pytest.raises(AgentValidationError):
async for _ in graph.stream_events(" "):
pass
@pytest.mark.asyncio
async def test_stream_events_yields_phase_events(self, graph):
"""Phase start/end events for both nodes must be emitted."""
report_dict = _analysis_report_dict()
events = [
{"event": "on_chain_start", "name": "research_node", "data": {}},
{
"event": "on_chain_end",
"name": "research_node",
"data": {"output": {"research_result": {}}},
},
{"event": "on_chain_start", "name": "analysis_node", "data": {}},
{
"event": "on_chain_end",
"name": "analysis_node",
"data": {"output": {"analysis_report": report_dict}},
},
]
graph._graph.astream_events = lambda *a, **kw: _mock_events(events)
collected = []
async for evt in graph.stream_events("test query"):
collected.append(evt)
event_types = [e["type"] for e in collected]
assert event_types.count("phase_started") == 2
assert event_types.count("phase_completed") == 2
phases = [e["phase"] for e in collected if e["type"] == "phase_started"]
assert phases == ["research", "analysis"]
@pytest.mark.asyncio
async def test_stream_events_yields_token_events(self, graph):
"""on_chat_model_stream events must surface as token events."""
mock_chunk = MagicMock()
mock_chunk.content = "hello"
report_dict = _analysis_report_dict()
events = [
{"event": "on_chain_start", "name": "research_node", "data": {}},
{
"event": "on_chat_model_stream",
"name": "llm",
"data": {"chunk": mock_chunk},
"metadata": {"langgraph_node": "research_node"},
},
{
"event": "on_chain_end",
"name": "research_node",
"data": {"output": {}},
},
{"event": "on_chain_start", "name": "analysis_node", "data": {}},
{
"event": "on_chain_end",
"name": "analysis_node",
"data": {"output": {"analysis_report": report_dict}},
},
]
graph._graph.astream_events = lambda *a, **kw: _mock_events(events)
collected = []
async for evt in graph.stream_events("test query"):
collected.append(evt)
token_events = [e for e in collected if e["type"] == "token"]
assert len(token_events) == 1
assert token_events[0]["content"] == "hello"
assert token_events[0]["node"] == "research_node"
@pytest.mark.asyncio
async def test_stream_events_yields_pipeline_completed(self, graph):
"""Final event must be pipeline_completed with the AnalysisReport."""
report_dict = _analysis_report_dict(confidence=0.92)
events = [
{"event": "on_chain_start", "name": "research_node", "data": {}},
{
"event": "on_chain_end",
"name": "research_node",
"data": {"output": {}},
},
{"event": "on_chain_start", "name": "analysis_node", "data": {}},
{
"event": "on_chain_end",
"name": "analysis_node",
"data": {"output": {"analysis_report": report_dict}},
},
]
graph._graph.astream_events = lambda *a, **kw: _mock_events(events)
collected = []
async for evt in graph.stream_events("test query"):
collected.append(evt)
last = collected[-1]
assert last["type"] == "pipeline_completed"
assert isinstance(last["report"], dict)
assert last["report"]["confidence"] == 0.92
@pytest.mark.asyncio
async def test_stream_events_raises_when_no_report(self, graph):
"""Missing analysis_report in output must raise AgentExecutionError."""
events = [
{"event": "on_chain_start", "name": "research_node", "data": {}},
{
"event": "on_chain_end",
"name": "research_node",
"data": {"output": {}},
},
{"event": "on_chain_start", "name": "analysis_node", "data": {}},
{
"event": "on_chain_end",
"name": "analysis_node",
"data": {"output": {}},
},
]
graph._graph.astream_events = lambda *a, **kw: _mock_events(events)
with pytest.raises(AgentExecutionError, match="without an AnalysisReport"):
async for _ in graph.stream_events("test query"):
pass
# ---------------------------------------------------------------------------
# ResearchAnalysisPack — budget propagation
# ---------------------------------------------------------------------------
class TestResearchAnalysisPackBudget:
def test_pack_propagates_budget_to_agents(self) -> None:
"""budget_usd passed to ResearchAnalysisPack must be stored on the instance."""
pack = ResearchAnalysisPack(budget_usd=1.0)
assert pack._budget_usd == 1.0
pack.close()
def test_pack_no_budget_by_default(self) -> None:
"""When budget_usd is omitted the pack must store None."""
pack = ResearchAnalysisPack()
assert pack._budget_usd is None
pack.close()
def test_pack_cost_usd_property_reads_shared_tracker(self) -> None:
"""cost_usd must reflect the cumulative spend on the run's shared tracker."""
pack = ResearchAnalysisPack(budget_usd=5.0)
assert pack._cost_tracker.budget_usd == pytest.approx(5.0)
pack._cost_tracker.total_cost_usd = 0.45
assert pack.cost_usd == pytest.approx(0.45)
pack.close()
def test_pack_shares_one_tracker_across_agents(
self,
mock_research_result: ResearchResult,
mock_analysis_report: AnalysisReport,
) -> None:
"""Both pipeline agents must receive the pack's shared CostTracker."""
mock_research_agent = MagicMock()
mock_research_agent.run_structured.return_value = mock_research_result
mock_analyst_agent = MagicMock()
mock_analyst_agent.run_structured.return_value = mock_analysis_report
with (
patch(
"core.graph.ResearchAgent", return_value=mock_research_agent
) as ra_cls,
patch("core.graph.AnalystAgent", return_value=mock_analyst_agent) as aa_cls,
):
pack = ResearchAnalysisPack(budget_usd=2.0)
pack.run("shared budget query")
assert ra_cls.call_args.kwargs["cost_tracker"] is pack._cost_tracker
assert aa_cls.call_args.kwargs["cost_tracker"] is pack._cost_tracker
pack.close()
def test_pack_cost_usd_zero_when_no_agents_run(self) -> None:
"""cost_usd must be 0.0 before any agents are instantiated."""
pack = ResearchAnalysisPack()
assert pack.cost_usd == 0.0
pack.close()
def test_negative_budget_raises(self) -> None:
"""A negative budget_usd must raise ValueError at construction time."""
with pytest.raises(ValueError, match="non-negative"):
ResearchAnalysisPack(budget_usd=-1.0)