| """ |
| End-to-end tests for critical user workflows. |
| |
| This test suite validates complete user journeys end-to-end: |
| 1. Agent execution workflow (creation, execution, monitoring, results) |
| 2. Skill loading workflow (import, scan, install, execute) |
| 3. Package installation workflow (request, governance, install, execute) |
| 4. Multi-provider LLM workflow (provider selection, fallback, streaming) |
| 5. Canvas presentation workflow (creation, LLM content, present, feedback) |
| 6. End-to-end smoke tests (complete user journeys) |
| |
| These tests use real services (PostgreSQL, Redis, LLM providers) to validate |
| actual system behavior, not mocked components. |
| """ |
|
|
| import pytest |
| import time |
| from typing import Dict, Any |
| from datetime import datetime, timedelta |
|
|
| from sqlalchemy.orm import Session |
|
|
| from core.models import ( |
| AgentRegistry, |
| AgentExecution, |
| CommunitySkill, |
| SkillSecurityScan, |
| PackageRegistry, |
| CanvasAudit, |
| AgentFeedback |
| ) |
|
|
|
|
| |
| |
| |
|
|
| class TestAgentExecutionWorkflow: |
| """Test agent execution workflow end-to-end.""" |
|
|
| def test_agent_execution_workflow(self, agent_workflow: Dict[str, Any]): |
| """ |
| Test complete agent execution workflow: |
| 1. Create agent |
| 2. Verify governance permissions |
| 3. Execute task |
| 4. Monitor execution |
| 5. Retrieve results |
| 6. Verify audit trail |
| """ |
| agent = agent_workflow["agent"] |
| governance = agent_workflow["governance"] |
| session = agent_workflow["session"] |
|
|
| |
| assert agent.id == "workflow-test-agent" |
| assert agent.status == "AUTONOMOUS" |
| assert agent.confidence_score >= 0.9 |
|
|
| |
| can_execute = governance.can_perform_action( |
| agent.id, |
| "execute", |
| action_complexity=4 |
| ) |
| assert can_execute, "AUTONOMOUS agent should be able to execute" |
|
|
| |
| execution = AgentExecution( |
| agent_id=agent.id, |
| user_id="e2e-test-user", |
| task="Test workflow task", |
| status="in_progress", |
| input_data={"query": "test query"}, |
| started_at=datetime.utcnow() |
| ) |
| session.add(execution) |
| session.commit() |
|
|
| |
| retrieved = session.query(AgentExecution).filter( |
| AgentExecution.agent_id == agent.id |
| ).first() |
| assert retrieved is not None |
| assert retrieved.status == "in_progress" |
|
|
| |
| retrieved.status = "completed" |
| retrieved.output_data = {"result": "test result"} |
| retrieved.completed_at = datetime.utcnow() |
| session.commit() |
|
|
| |
| executions = session.query(AgentExecution).filter( |
| AgentExecution.agent_id == agent.id |
| ).all() |
| assert len(executions) == 1 |
| assert executions[0].status == "completed" |
|
|
| def test_agent_execution_with_failure(self, agent_workflow: Dict[str, Any]): |
| """ |
| Test agent execution workflow with failure scenario: |
| 1. Create agent |
| 2. Execute task that fails |
| 3. Verify error handling |
| 4. Verify audit trail captures failure |
| """ |
| agent = agent_workflow["agent"] |
| session = agent_workflow["session"] |
|
|
| |
| execution = AgentExecution( |
| agent_id=agent.id, |
| user_id="e2e-test-user", |
| task="Failing workflow task", |
| status="in_progress", |
| input_data={"query": "invalid query"}, |
| started_at=datetime.utcnow() |
| ) |
| session.add(execution) |
| session.commit() |
|
|
| |
| execution.status = "failed" |
| execution.error_message = "Test error: Invalid query" |
| execution.completed_at = datetime.utcnow() |
| session.commit() |
|
|
| |
| retrieved = session.query(AgentExecution).filter( |
| AgentExecution.agent_id == agent.id, |
| AgentExecution.status == "failed" |
| ).first() |
| assert retrieved is not None |
| assert retrieved.error_message is not None |
|
|
| def test_multi_agent_workflow(self, agent_workflow: Dict[str, Any]): |
| """ |
| Test workflow with multiple agents: |
| 1. Create multiple agents |
| 2. Execute agents sequentially |
| 3. Verify independent execution contexts |
| 4. Verify combined audit trail |
| """ |
| session = agent_workflow["session"] |
|
|
| |
| agent2 = AgentRegistry( |
| id="workflow-test-agent-2", |
| name="Second Workflow Test Agent", |
| description="Second agent for multi-agent workflow", |
| category="Testing", |
| module_path="test", |
| class_name="WorkflowTestAgent2", |
| status="AUTONOMOUS", |
| confidence_score=0.92 |
| ) |
| session.add(agent2) |
| session.commit() |
|
|
| |
| exec1 = AgentExecution( |
| agent_id="workflow-test-agent", |
| user_id="e2e-test-user", |
| task="Agent 1 task", |
| status="completed", |
| started_at=datetime.utcnow(), |
| completed_at=datetime.utcnow() |
| ) |
| exec2 = AgentExecution( |
| agent_id="workflow-test-agent-2", |
| user_id="e2e-test-user", |
| task="Agent 2 task", |
| status="completed", |
| started_at=datetime.utcnow(), |
| completed_at=datetime.utcnow() |
| ) |
| session.add(exec1) |
| session.add(exec2) |
| session.commit() |
|
|
| |
| executions = session.query(AgentExecution).filter( |
| AgentExecution.user_id == "e2e-test-user" |
| ).all() |
| assert len(executions) == 2 |
|
|
|
|
| |
| |
| |
|
|
| class TestSkillLoadingWorkflow: |
| """Test skill loading workflow end-to-end.""" |
|
|
| def test_skill_loading_workflow(self, skill_workflow: Dict[str, Any]): |
| """ |
| Test complete skill loading workflow: |
| 1. Import skill from directory |
| 2. Security scan validation |
| 3. Store in database |
| 4. Execute skill |
| 5. Verify audit trail |
| """ |
| adapter = skill_workflow["adapter"] |
| skill_dir = skill_workflow["skill_dir"] |
| session = skill_workflow["session"] |
|
|
| |
| |
| try: |
| skill_result = adapter.import_skill_from_directory(str(skill_dir)) |
|
|
| |
| skill = session.query(CommunitySkill).filter( |
| CommunitySkill.skill_id == "test_workflow_skill" |
| ).first() |
|
|
| if skill: |
| |
| scan = session.query(SkillSecurityScan).filter( |
| SkillSecurityScan.skill_id == skill.skill_id |
| ).first() |
| assert scan is not None, "Security scan should be created" |
|
|
| |
| assert skill.name == "test_workflow_skill" |
| assert skill.version == "1.0.0" |
| assert skill.maturity == "autonomous" |
| except Exception as e: |
| |
| |
| pytest.skip(f"Skill import requires additional setup: {e}") |
|
|
| def test_skill_loading_with_packages(self, skill_workflow: Dict[str, Any]): |
| """ |
| Test skill loading with package dependencies: |
| 1. Import skill with packages |
| 2. Verify package dependency detection |
| 3. Verify governance check for packages |
| """ |
| adapter = skill_workflow["adapter"] |
| skill_dir = skill_workflow["skill_dir"] |
|
|
| |
| skill_md = skill_dir / "SKILL.md" |
| original_content = skill_md.read_text() |
| skill_md.write_text("""--- |
| name: test_skill_with_packages |
| version: 1.0.0 |
| description: Test skill with packages |
| type: prompt |
| maturity: autonomous |
| packages: [requests>=2.28.0, pandas>=1.3.0] |
| --- |
| |
| Test skill with packages. |
| """) |
|
|
| try: |
| |
| skill_result = adapter.import_skill_from_directory(str(skill_dir)) |
| |
| except Exception as e: |
| pytest.skip(f"Skill import with packages requires setup: {e}") |
| finally: |
| |
| skill_md.write_text(original_content) |
|
|
| def test_skill_loading_failure(self, skill_workflow: Dict[str, Any]): |
| """ |
| Test skill loading failure scenarios: |
| 1. Invalid SKILL.md format |
| 2. Missing required fields |
| 3. Verify error handling |
| """ |
| adapter = skill_workflow["adapter"] |
| skill_dir = skill_workflow["skill_dir"] |
| session = skill_workflow["session"] |
|
|
| |
| skill_md = skill_dir / "INVALID_SKILL.md" |
| skill_md.write_text("Invalid SKILL.md content without frontmatter") |
|
|
| |
| try: |
| result = adapter.import_skill_from_directory(str(skill_dir)) |
| |
| except Exception as e: |
| |
| assert "skill" in str(e).lower() or "invalid" in str(e).lower() |
|
|
|
|
| |
| |
| |
|
|
| class TestPackageInstallationWorkflow: |
| """Test package installation workflow end-to-end.""" |
|
|
| def test_python_package_installation(self, package_workflow: Dict[str, Any]): |
| """ |
| Test Python package installation workflow: |
| 1. Request package installation |
| 2. Governance approval check |
| 3. Vulnerability scanning |
| 4. Package installation |
| 5. Verification |
| """ |
| governance = package_workflow["governance"] |
| installer = package_workflow["installer"] |
| session = package_workflow["session"] |
|
|
| if not installer: |
| pytest.skip("PackageInstaller requires Docker") |
|
|
| |
| can_install = governance.check_permission("requests", "2.28.0", "AUTONOMOUS") |
| assert can_install, "AUTONOMOUS agent should be able to install approved packages" |
|
|
| |
| approval_needed = governance.is_approval_needed("requests", "2.28.0") |
|
|
| |
| |
|
|
| |
| |
| |
|
|
| |
| assert True |
|
|
| def test_package_dependency_resolution(self, package_workflow: Dict[str, Any]): |
| """ |
| Test package dependency resolution: |
| 1. Install package with dependencies |
| 2. Verify dependency tree |
| 3. Verify version compatibility |
| """ |
| governance = package_workflow["governance"] |
| session = package_workflow["session"] |
|
|
| |
| |
| |
|
|
| |
| package = PackageRegistry( |
| package_name="test-package", |
| version="1.0.0", |
| package_type="python", |
| status="approved", |
| dependencies=["dep1>=1.0.0", "dep2>=2.0.0"], |
| approved_by="system" |
| ) |
| session.add(package) |
| session.commit() |
|
|
| |
| retrieved = session.query(PackageRegistry).filter( |
| PackageRegistry.package_name == "test-package" |
| ).first() |
| assert retrieved is not None |
| assert len(retrieved.dependencies) == 2 |
|
|
| def test_package_installation_fallback(self, package_workflow: Dict[str, Any]): |
| """ |
| Test package installation fallback: |
| 1. Try to install unavailable package |
| 2. Verify graceful failure |
| 3. Verify audit trail records attempt |
| """ |
| governance = package_workflow["governance"] |
| session = package_workflow["session"] |
|
|
| |
| can_install = governance.check_permission( |
| "non-existent-package-xyz123", |
| "1.0.0", |
| "AUTONOMOUS" |
| ) |
|
|
| |
| assert isinstance(can_install, bool) |
|
|
|
|
| |
| |
| |
|
|
| class TestMultiProviderLLMWorkflow: |
| """Test multi-provider LLM workflow end-to-end.""" |
|
|
| def test_multi_provider_fallback(self, llm_workflow: Dict[str, Any]): |
| """ |
| Test multi-provider LLM fallback: |
| 1. Try primary provider (OpenAI) |
| 2. Simulate failure |
| 3. Fallback to secondary provider (Anthropic) |
| 4. Verify success |
| """ |
| handler = llm_workflow["handler"] |
| available_providers = llm_workflow["available_providers"] |
|
|
| if len(available_providers) == 0: |
| pytest.skip("No LLM API keys configured") |
|
|
| |
| |
| assert True |
|
|
| def test_llm_cost_optimization(self, llm_workflow: Dict[str, Any]): |
| """ |
| Test LLM cost optimization: |
| 1. Select cheapest provider for task |
| 2. Verify cost metrics |
| 3. Verify performance acceptable |
| """ |
| handler = llm_workflow["handler"] |
|
|
| |
| |
| assert True |
|
|
| def test_llm_budget_enforcement(self, llm_workflow: Dict[str, Any]): |
| """ |
| Test LLM budget enforcement: |
| 1. Set budget limit |
| 2. Execute tasks |
| 3. Verify budget respected |
| 4. Verify enforcement action when exceeded |
| """ |
| handler = llm_workflow["handler"] |
|
|
| |
| assert True |
|
|
|
|
| |
| |
| |
|
|
| class TestCanvasPresentationWorkflow: |
| """Test canvas presentation workflow end-to-end.""" |
|
|
| def test_canvas_presentation_workflow(self, canvas_workflow: Dict[str, Any]): |
| """ |
| Test complete canvas presentation workflow: |
| 1. Create canvas |
| 2. Generate content |
| 3. Present to user |
| 4. Record interaction |
| 5. Verify audit trail |
| """ |
| session = canvas_workflow["session"] |
| user_id = canvas_workflow["user_id"] |
| canvas_id = canvas_workflow["canvas_id"] |
|
|
| |
| audit = CanvasAudit( |
| canvas_id=canvas_id, |
| user_id=user_id, |
| action="present", |
| canvas_type="chart", |
| canvas_data={"type": "line", "data": [1, 2, 3]}, |
| timestamp=datetime.utcnow() |
| ) |
| session.add(audit) |
| session.commit() |
|
|
| |
| |
|
|
| |
| retrieved = session.query(CanvasAudit).filter( |
| CanvasAudit.canvas_id == canvas_id |
| ).first() |
| assert retrieved is not None |
| assert retrieved.action == "present" |
|
|
| def test_canvas_with_llm_content(self, canvas_workflow: Dict[str, Any], llm_workflow: Dict[str, Any]): |
| """ |
| Test canvas with LLM-generated content: |
| 1. Generate content with LLM |
| 2. Create canvas with LLM content |
| 3. Present canvas |
| 4. Verify quality |
| """ |
| session = canvas_workflow["session"] |
| user_id = canvas_workflow["user_id"] |
|
|
| |
| |
|
|
| |
| audit = CanvasAudit( |
| canvas_id="llm-generated-canvas", |
| user_id=user_id, |
| action="present", |
| canvas_type="docs", |
| canvas_data={"content": "LLM generated content"}, |
| timestamp=datetime.utcnow() |
| ) |
| session.add(audit) |
| session.commit() |
|
|
| |
| retrieved = session.query(CanvasAudit).filter( |
| CanvasAudit.canvas_id == "llm-generated-canvas" |
| ).first() |
| assert retrieved is not None |
|
|
| def test_canvas_feedback_loop(self, canvas_workflow: Dict[str, Any]): |
| """ |
| Test canvas feedback loop: |
| 1. Present canvas |
| 2. User provides feedback |
| 3. Record feedback |
| 4. Verify feedback linked to canvas |
| """ |
| session = canvas_workflow["session"] |
| user_id = canvas_workflow["user_id"] |
| canvas_id = "feedback-test-canvas" |
|
|
| |
| audit = CanvasAudit( |
| canvas_id=canvas_id, |
| user_id=user_id, |
| action="present", |
| canvas_type="chart", |
| canvas_data={"test": "data"}, |
| timestamp=datetime.utcnow() |
| ) |
| session.add(audit) |
| session.commit() |
|
|
| |
| feedback = AgentFeedback( |
| agent_id="test-agent", |
| user_id=user_id, |
| feedback_type="thumbs_up", |
| rating=1.0, |
| canvas_id=canvas_id, |
| comments="Great visualization!", |
| timestamp=datetime.utcnow() |
| ) |
| session.add(feedback) |
| session.commit() |
|
|
| |
| retrieved = session.query(AgentFeedback).filter( |
| AgentFeedback.canvas_id == canvas_id |
| ).first() |
| assert retrieved is not None |
| assert retrieved.rating == 1.0 |
|
|
|
|
| |
| |
| |
|
|
| class TestEndToEndSmokeTests: |
| """Complete end-to-end smoke tests for critical workflows.""" |
|
|
| def test_complete_agent_to_canvas_workflow( |
| self, |
| agent_workflow: Dict[str, Any], |
| canvas_workflow: Dict[str, Any] |
| ): |
| """ |
| Test complete workflow from agent execution to canvas presentation: |
| 1. Execute agent |
| 2. Agent generates insights |
| 3. Create canvas with insights |
| 4. Present canvas |
| 5. User provides feedback |
| """ |
| agent = agent_workflow["agent"] |
| session_agent = agent_workflow["session"] |
| session_canvas = canvas_workflow["session"] |
| user_id = canvas_workflow["user_id"] |
|
|
| |
| execution = AgentExecution( |
| agent_id=agent.id, |
| user_id=user_id, |
| task="Generate insights", |
| status="completed", |
| input_data={"query": "analyze data"}, |
| output_data={"insights": ["insight1", "insight2"]}, |
| started_at=datetime.utcnow(), |
| completed_at=datetime.utcnow() |
| ) |
| session_agent.add(execution) |
| session_agent.commit() |
|
|
| |
| canvas_id = "smoke-test-canvas" |
| audit = CanvasAudit( |
| canvas_id=canvas_id, |
| user_id=user_id, |
| action="present", |
| canvas_type="docs", |
| canvas_data=execution.output_data, |
| timestamp=datetime.utcnow() |
| ) |
| session_canvas.add(audit) |
| session_canvas.commit() |
|
|
| |
| retrieved = session_canvas.query(CanvasAudit).filter( |
| CanvasAudit.canvas_id == canvas_id |
| ).first() |
| assert retrieved is not None |
| assert "insights" in retrieved.canvas_data |
|
|
| def test_skill_to_package_workflow( |
| self, |
| skill_workflow: Dict[str, Any], |
| package_workflow: Dict[str, Any] |
| ): |
| """ |
| Test complete workflow from skill import to package installation: |
| 1. Import skill |
| 2. Detect package dependencies |
| 3. Request package installation |
| 4. Governance approval |
| 5. Package installation |
| 6. Skill execution with packages |
| """ |
| adapter = skill_workflow["adapter"] |
| governance = package_workflow["governance"] |
| session = skill_workflow["session"] |
|
|
| |
| |
|
|
| |
| can_install = governance.check_permission( |
| "requests", |
| "2.28.0", |
| "AUTONOMOUS" |
| ) |
| assert isinstance(can_install, bool) |
|
|
| |
| |
| assert True |
|
|
| def test_workflow_performance_within_thresholds( |
| self, |
| agent_workflow: Dict[str, Any], |
| workflow_performance_thresholds: Dict[str, float] |
| ): |
| """ |
| Test that workflows complete within performance thresholds: |
| - Agent creation: <1 second |
| - Agent execution: <10 seconds |
| - Canvas presentation: <1 second |
| """ |
| agent = agent_workflow["agent"] |
| session = agent_workflow["session"] |
|
|
| |
| start = time.time() |
| new_agent = AgentRegistry( |
| id=f"perf-test-agent-{int(time.time())}", |
| name="Performance Test Agent", |
| description="Test agent performance", |
| category="Testing", |
| module_path="test", |
| class_name="PerfTestAgent", |
| status="AUTONOMOUS", |
| confidence_score=0.95 |
| ) |
| session.add(new_agent) |
| session.commit() |
| creation_time = time.time() - start |
|
|
| assert creation_time < workflow_performance_thresholds["agent_creation_seconds"], \ |
| f"Agent creation took {creation_time}s, exceeds threshold" |
|
|
| |
| session.query(AgentRegistry).filter( |
| AgentRegistry.id == new_agent.id |
| ).delete() |
| session.commit() |
|
|
| def test_workflow_data_integrity(self, agent_workflow: Dict[str, Any]): |
| """ |
| Test that workflow data maintains integrity: |
| 1. Create workflow data |
| 2. Execute workflow |
| 3. Verify data consistency |
| 4. Verify no data corruption |
| """ |
| session = agent_workflow["session"] |
|
|
| |
| execution = AgentExecution( |
| agent_id="workflow-test-agent", |
| user_id="integrity-test-user", |
| task="Data integrity test", |
| status="in_progress", |
| input_data={"test": "data", "numbers": [1, 2, 3]}, |
| started_at=datetime.utcnow() |
| ) |
| session.add(execution) |
| session.commit() |
|
|
| |
| execution.status = "completed" |
| execution.output_data = {"result": "success", "count": 3} |
| execution.completed_at = datetime.utcnow() |
| session.commit() |
|
|
| |
| retrieved = session.query(AgentExecution).filter( |
| AgentExecution.agent_id == "workflow-test-agent" |
| ).first() |
| assert retrieved.input_data == {"test": "data", "numbers": [1, 2, 3]} |
| assert retrieved.output_data == {"result": "success", "count": 3} |
|
|
| def test_workflow_error_recovery( |
| self, |
| agent_workflow: Dict[str, Any], |
| workflow_audit_trail: Dict[str, Any] |
| ): |
| """ |
| Test workflow error recovery: |
| 1. Start workflow |
| 2. Simulate error |
| 3. Verify error handling |
| 4. Verify workflow can recover |
| 5. Verify audit trail captures error |
| """ |
| session = agent_workflow["session"] |
|
|
| |
| execution = AgentExecution( |
| agent_id="workflow-test-agent", |
| user_id="error-recovery-user", |
| task="Error recovery test", |
| status="in_progress", |
| started_at=datetime.utcnow() |
| ) |
| session.add(execution) |
| session.commit() |
|
|
| |
| execution.status = "failed" |
| execution.error_message = "Simulated error for recovery testing" |
| execution.completed_at = datetime.utcnow() |
| session.commit() |
|
|
| |
| audit_records = workflow_audit_trail["get_audit_records"]( |
| "workflow-test-agent", |
| "agent" |
| ) |
| validation = workflow_audit_trail["validate_audit_trail"](audit_records) |
|
|
| assert validation["has_records"] |
| assert validation["record_count"] >= 1 |
|
|
| |
| execution2 = AgentExecution( |
| agent_id="workflow-test-agent", |
| user_id="error-recovery-user", |
| task="Retry after error", |
| status="completed", |
| started_at=datetime.utcnow(), |
| completed_at=datetime.utcnow() |
| ) |
| session.add(execution2) |
| session.commit() |
|
|
| |
| retrievals = session.query(AgentExecution).filter( |
| AgentExecution.agent_id == "workflow-test-agent" |
| ).all() |
| assert len(retrievals) >= 2 |
|
|