File size: 22,454 Bytes
56fd459 |
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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# Integration tests for agent workflows
import pytest
import asyncio
import json
import tempfile
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
from typing import List, Dict, Any
from ankigen_core.agents.integration import AgentOrchestrator, integrate_with_existing_workflow
from ankigen_core.agents.feature_flags import AgentFeatureFlags, AgentMode
from ankigen_core.agents.config import AgentConfigManager
from ankigen_core.llm_interface import OpenAIClientManager
from ankigen_core.models import Card, CardFront, CardBack
# Test fixtures
@pytest.fixture
def temp_config_dir():
"""Create temporary config directory for testing"""
with tempfile.TemporaryDirectory() as tmp_dir:
yield tmp_dir
@pytest.fixture
def sample_cards():
"""Sample cards for testing workflows"""
return [
Card(
card_type="basic",
front=CardFront(question="What is a Python function?"),
back=CardBack(
answer="A reusable block of code",
explanation="Functions help organize code into reusable components",
example="def hello(): print('hello')"
),
metadata={
"difficulty": "beginner",
"subject": "programming",
"topic": "Python Functions",
"learning_outcomes": ["understanding functions"],
"quality_score": 8.5
}
),
Card(
card_type="basic",
front=CardFront(question="How do you call a function in Python?"),
back=CardBack(
answer="By using the function name followed by parentheses",
explanation="Function calls execute the code inside the function",
example="hello()"
),
metadata={
"difficulty": "beginner",
"subject": "programming",
"topic": "Python Functions",
"learning_outcomes": ["function execution"],
"quality_score": 7.8
}
)
]
@pytest.fixture
def mock_openai_responses():
"""Mock OpenAI API responses for different agents"""
return {
"generation": {
"cards": [
{
"card_type": "basic",
"front": {"question": "What is a Python function?"},
"back": {
"answer": "A reusable block of code",
"explanation": "Functions help organize code",
"example": "def hello(): print('hello')"
},
"metadata": {
"difficulty": "beginner",
"subject": "programming",
"topic": "Functions"
}
}
]
},
"judgment": {
"approved": True,
"quality_score": 8.5,
"feedback": "Good question with clear answer",
"suggestions": []
},
"enhancement": {
"enhanced_explanation": "Functions help organize code into reusable, testable components",
"enhanced_example": "def greet(name): return f'Hello, {name}!'",
"additional_metadata": {
"complexity": "low",
"estimated_study_time": "5 minutes"
}
}
}
# Test complete agent workflow
@patch('ankigen_core.agents.integration.get_feature_flags')
@patch('ankigen_core.agents.integration.record_agent_execution')
async def test_complete_agent_workflow_success(mock_record, mock_get_flags, sample_cards, mock_openai_responses):
"""Test complete agent workflow from generation to enhancement"""
# Setup feature flags for full agent mode
feature_flags = AgentFeatureFlags(
mode=AgentMode.AGENT_ONLY,
enable_generation_coordinator=True,
enable_judge_coordinator=True,
enable_revision_agent=True,
enable_enhancement_agent=True,
enable_parallel_judging=True,
min_judge_consensus=0.6
)
mock_get_flags.return_value = feature_flags
# Mock client manager
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock()
mock_openai_client = MagicMock()
mock_client_manager.get_client.return_value = mock_openai_client
# Create orchestrator
orchestrator = AgentOrchestrator(mock_client_manager)
# Mock all agent components
with patch('ankigen_core.agents.integration.GenerationCoordinator') as mock_gen_coord, \
patch('ankigen_core.agents.integration.JudgeCoordinator') as mock_judge_coord, \
patch('ankigen_core.agents.integration.RevisionAgent') as mock_revision, \
patch('ankigen_core.agents.integration.EnhancementAgent') as mock_enhancement:
# Setup generation coordinator
mock_gen_instance = MagicMock()
mock_gen_instance.coordinate_generation = AsyncMock(return_value=sample_cards)
mock_gen_coord.return_value = mock_gen_instance
# Setup judge coordinator (approve all cards)
mock_judge_instance = MagicMock()
judge_results = [(card, ["positive feedback"], True) for card in sample_cards]
mock_judge_instance.coordinate_judgment = AsyncMock(return_value=judge_results)
mock_judge_coord.return_value = mock_judge_instance
# Setup enhancement agent
enhanced_cards = sample_cards.copy()
for card in enhanced_cards:
card.metadata["enhanced"] = True
mock_enhancement_instance = MagicMock()
mock_enhancement_instance.enhance_card_batch = AsyncMock(return_value=enhanced_cards)
mock_enhancement.return_value = mock_enhancement_instance
# Initialize and run workflow
await orchestrator.initialize("test-api-key")
cards, metadata = await orchestrator.generate_cards_with_agents(
topic="Python Functions",
subject="programming",
num_cards=2,
difficulty="beginner",
enable_quality_pipeline=True
)
# Verify results
assert len(cards) == 2
assert all(isinstance(card, Card) for card in cards)
assert all(card.metadata.get("enhanced") for card in cards)
# Verify metadata
assert metadata["generation_method"] == "agent_system"
assert metadata["cards_generated"] == 2
assert metadata["topic"] == "Python Functions"
assert metadata["subject"] == "programming"
assert "quality_results" in metadata
# Verify all phases were executed
mock_gen_instance.coordinate_generation.assert_called_once()
mock_judge_instance.coordinate_judgment.assert_called_once()
mock_enhancement_instance.enhance_card_batch.assert_called_once()
# Verify execution was recorded
mock_record.assert_called()
@patch('ankigen_core.agents.integration.get_feature_flags')
async def test_agent_workflow_with_card_rejection_and_revision(mock_get_flags, sample_cards):
"""Test workflow when cards are rejected and need revision"""
feature_flags = AgentFeatureFlags(
mode=AgentMode.AGENT_ONLY,
enable_generation_coordinator=True,
enable_judge_coordinator=True,
enable_revision_agent=True,
max_revision_iterations=2
)
mock_get_flags.return_value = feature_flags
# Mock client manager
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock()
mock_openai_client = MagicMock()
mock_client_manager.get_client.return_value = mock_openai_client
orchestrator = AgentOrchestrator(mock_client_manager)
with patch('ankigen_core.agents.integration.GenerationCoordinator') as mock_gen_coord, \
patch('ankigen_core.agents.integration.JudgeCoordinator') as mock_judge_coord, \
patch('ankigen_core.agents.integration.RevisionAgent') as mock_revision:
# Setup generation coordinator
mock_gen_instance = MagicMock()
mock_gen_instance.coordinate_generation = AsyncMock(return_value=sample_cards)
mock_gen_coord.return_value = mock_gen_instance
# Setup judge coordinator (reject first card, approve second)
judge_results_initial = [
(sample_cards[0], ["unclear question"], False), # Rejected
(sample_cards[1], ["good question"], True) # Approved
]
# Create revised card
revised_card = Card(
card_type="basic",
front=CardFront(question="What is a Python function and how is it used?"),
back=CardBack(
answer="A reusable block of code that performs a specific task",
explanation="Functions are fundamental building blocks in programming",
example="def add(a, b): return a + b"
),
metadata={"difficulty": "beginner", "revised": True}
)
# Judge approves revised card
judge_results_revision = [(revised_card, ["much improved"], True)]
mock_judge_instance = MagicMock()
mock_judge_instance.coordinate_judgment = AsyncMock(
side_effect=[judge_results_initial, judge_results_revision]
)
mock_judge_coord.return_value = mock_judge_instance
# Setup revision agent
mock_revision_instance = MagicMock()
mock_revision_instance.revise_card = AsyncMock(return_value=revised_card)
mock_revision.return_value = mock_revision_instance
# Initialize and run workflow
await orchestrator.initialize("test-api-key")
cards, metadata = await orchestrator.generate_cards_with_agents(
topic="Python Functions",
subject="programming",
num_cards=2,
difficulty="beginner"
)
# Verify results
assert len(cards) == 2 # Original approved card + revised card
assert sample_cards[1] in cards # Originally approved card
assert revised_card in cards # Revised card
# Verify quality results
quality_results = metadata["quality_results"]
assert quality_results["initially_approved"] == 1
assert quality_results["initially_rejected"] == 1
assert quality_results["successfully_revised"] == 1
assert quality_results["final_approval_rate"] == 1.0
# Verify revision was called
mock_revision_instance.revise_card.assert_called_once()
@patch('ankigen_core.agents.integration.get_feature_flags')
async def test_agent_workflow_hybrid_mode(mock_get_flags, sample_cards):
"""Test workflow in hybrid mode with selective agent usage"""
feature_flags = AgentFeatureFlags(
mode=AgentMode.HYBRID,
enable_subject_expert_agent=True,
enable_content_accuracy_judge=True,
enable_generation_coordinator=False, # Not enabled
enable_enhancement_agent=False # Not enabled
)
mock_get_flags.return_value = feature_flags
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock()
mock_openai_client = MagicMock()
mock_client_manager.get_client.return_value = mock_openai_client
orchestrator = AgentOrchestrator(mock_client_manager)
with patch('ankigen_core.agents.integration.SubjectExpertAgent') as mock_subject_expert:
# Setup subject expert agent (fallback when coordinator is disabled)
mock_expert_instance = MagicMock()
mock_expert_instance.generate_cards = AsyncMock(return_value=sample_cards)
mock_subject_expert.return_value = mock_expert_instance
# Initialize orchestrator (should only create enabled agents)
await orchestrator.initialize("test-api-key")
# Verify only enabled agents were created
assert orchestrator.generation_coordinator is None # Disabled
assert orchestrator.judge_coordinator is None # Not enabled in flags
assert orchestrator.enhancement_agent is None # Disabled
# Run generation
cards, metadata = await orchestrator.generate_cards_with_agents(
topic="Python Functions",
subject="programming",
num_cards=2
)
# Verify results
assert len(cards) == 2
assert metadata["generation_method"] == "agent_system"
# Verify subject expert was used
mock_subject_expert.assert_called_once_with(mock_openai_client, "programming")
mock_expert_instance.generate_cards.assert_called_once()
@patch('ankigen_core.agents.integration.get_feature_flags')
async def test_integrate_with_existing_workflow_function(mock_get_flags, sample_cards):
"""Test the integrate_with_existing_workflow function"""
feature_flags = AgentFeatureFlags(mode=AgentMode.AGENT_ONLY, enable_subject_expert_agent=True)
mock_get_flags.return_value = feature_flags
mock_client_manager = MagicMock(spec=OpenAIClientManager)
with patch('ankigen_core.agents.integration.AgentOrchestrator') as mock_orchestrator_class:
# Mock orchestrator instance
mock_orchestrator = MagicMock()
mock_orchestrator.initialize = AsyncMock()
mock_orchestrator.generate_cards_with_agents = AsyncMock(
return_value=(sample_cards, {"method": "agent_system"})
)
mock_orchestrator_class.return_value = mock_orchestrator
# Call integration function
cards, metadata = await integrate_with_existing_workflow(
client_manager=mock_client_manager,
api_key="test-key",
topic="Python Basics",
subject="programming",
num_cards=2,
difficulty="beginner"
)
# Verify results
assert cards == sample_cards
assert metadata == {"method": "agent_system"}
# Verify orchestrator was used correctly
mock_orchestrator_class.assert_called_once_with(mock_client_manager)
mock_orchestrator.initialize.assert_called_once_with("test-key")
mock_orchestrator.generate_cards_with_agents.assert_called_once_with(
topic="Python Basics",
subject="programming",
num_cards=2,
difficulty="beginner"
)
@patch('ankigen_core.agents.integration.get_feature_flags')
async def test_integrate_with_existing_workflow_legacy_fallback(mock_get_flags):
"""Test integration function with legacy fallback"""
feature_flags = AgentFeatureFlags(mode=AgentMode.LEGACY)
mock_get_flags.return_value = feature_flags
mock_client_manager = MagicMock(spec=OpenAIClientManager)
# Should raise NotImplementedError for legacy fallback
with pytest.raises(NotImplementedError, match="Legacy fallback not implemented"):
await integrate_with_existing_workflow(
client_manager=mock_client_manager,
api_key="test-key",
topic="Test"
)
async def test_agent_workflow_error_handling():
"""Test agent workflow error handling and recovery"""
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock(side_effect=Exception("API key invalid"))
orchestrator = AgentOrchestrator(mock_client_manager)
# Should raise initialization error
with pytest.raises(Exception, match="API key invalid"):
await orchestrator.initialize("invalid-key")
async def test_agent_workflow_timeout_handling():
"""Test agent workflow timeout handling"""
feature_flags = AgentFeatureFlags(
mode=AgentMode.AGENT_ONLY,
enable_generation_coordinator=True,
agent_timeout=0.1 # Very short timeout
)
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock()
mock_client_manager.get_client.return_value = MagicMock()
orchestrator = AgentOrchestrator(mock_client_manager)
orchestrator.feature_flags = feature_flags
with patch('ankigen_core.agents.integration.GenerationCoordinator') as mock_gen_coord:
# Setup generation coordinator with slow response
mock_gen_instance = MagicMock()
mock_gen_instance.coordinate_generation = AsyncMock()
async def slow_generation(*args, **kwargs):
await asyncio.sleep(1) # Longer than timeout
return []
mock_gen_instance.coordinate_generation.side_effect = slow_generation
mock_gen_coord.return_value = mock_gen_instance
await orchestrator.initialize("test-key")
# Should handle timeout gracefully (depends on implementation)
# This tests the timeout mechanism in the base agent wrapper
with pytest.raises(Exception): # Could be TimeoutError or other exception
await orchestrator.generate_cards_with_agents(
topic="Test",
subject="test",
num_cards=1
)
def test_agent_config_integration_with_workflow(temp_config_dir):
"""Test agent configuration integration with workflow"""
# Create test configuration
config_manager = AgentConfigManager(config_dir=temp_config_dir)
test_config = {
"agents": {
"subject_expert": {
"instructions": "You are a subject matter expert",
"model": "gpt-4o",
"temperature": 0.8,
"timeout": 45.0,
"custom_prompts": {
"programming": "Focus on code examples and best practices"
}
}
}
}
config_manager.load_config_from_dict(test_config)
# Verify config was loaded
subject_config = config_manager.get_config("subject_expert")
assert subject_config is not None
assert subject_config.temperature == 0.8
assert subject_config.timeout == 45.0
assert "programming" in subject_config.custom_prompts
@patch('ankigen_core.agents.integration.get_feature_flags')
async def test_agent_performance_metrics_collection(mock_get_flags, sample_cards):
"""Test that performance metrics are collected during workflow"""
feature_flags = AgentFeatureFlags(
mode=AgentMode.AGENT_ONLY,
enable_generation_coordinator=True,
enable_agent_tracing=True
)
mock_get_flags.return_value = feature_flags
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock()
mock_client_manager.get_client.return_value = MagicMock()
orchestrator = AgentOrchestrator(mock_client_manager)
with patch('ankigen_core.agents.integration.GenerationCoordinator') as mock_gen_coord, \
patch('ankigen_core.agents.integration.get_metrics') as mock_get_metrics:
# Setup generation coordinator
mock_gen_instance = MagicMock()
mock_gen_instance.coordinate_generation = AsyncMock(return_value=sample_cards)
mock_gen_coord.return_value = mock_gen_instance
# Setup metrics
mock_metrics = MagicMock()
mock_metrics.get_performance_report.return_value = {"avg_response_time": 1.5}
mock_metrics.get_quality_metrics.return_value = {"avg_quality": 8.2}
mock_get_metrics.return_value = mock_metrics
await orchestrator.initialize("test-key")
# Generate cards
await orchestrator.generate_cards_with_agents(
topic="Test",
subject="test",
num_cards=1
)
# Get performance metrics
performance = orchestrator.get_performance_metrics()
# Verify metrics structure
assert "agent_performance" in performance
assert "quality_metrics" in performance
assert "feature_flags" in performance
assert "enabled_agents" in performance
# Verify metrics were retrieved
mock_metrics.get_performance_report.assert_called_once_with(hours=24)
mock_metrics.get_quality_metrics.assert_called_once()
# Stress test for concurrent agent operations
@patch('ankigen_core.agents.integration.get_feature_flags')
async def test_concurrent_agent_operations(mock_get_flags, sample_cards):
"""Test concurrent agent operations"""
feature_flags = AgentFeatureFlags(
mode=AgentMode.AGENT_ONLY,
enable_generation_coordinator=True,
enable_parallel_judging=True
)
mock_get_flags.return_value = feature_flags
mock_client_manager = MagicMock(spec=OpenAIClientManager)
mock_client_manager.initialize_client = AsyncMock()
mock_client_manager.get_client.return_value = MagicMock()
# Create multiple orchestrators for concurrent operations
orchestrators = [AgentOrchestrator(mock_client_manager) for _ in range(3)]
with patch('ankigen_core.agents.integration.GenerationCoordinator') as mock_gen_coord:
# Setup generation coordinator
mock_gen_instance = MagicMock()
mock_gen_instance.coordinate_generation = AsyncMock(return_value=sample_cards)
mock_gen_coord.return_value = mock_gen_instance
# Initialize all orchestrators
await asyncio.gather(*[orch.initialize("test-key") for orch in orchestrators])
# Run concurrent card generation
tasks = [
orch.generate_cards_with_agents(
topic=f"Topic {i}",
subject="test",
num_cards=1
)
for i, orch in enumerate(orchestrators)
]
results = await asyncio.gather(*tasks)
# Verify all operations completed successfully
assert len(results) == 3
for cards, metadata in results:
assert len(cards) == 2 # sample_cards has 2 cards
assert metadata["generation_method"] == "agent_system" |