File size: 32,769 Bytes
cc036ff | 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 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 | """
Coverage expansion tests for proposal service.
Tests cover critical code paths in:
- proposal_service.py: Proposal generation, validation, approval
- Governance integration: Permission checks for INTERN agents
Target: Cover critical paths (happy path + error paths) to increase coverage.
"""
import pytest
from unittest.mock import Mock, patch, MagicMock, AsyncMock
from datetime import datetime, timedelta
from sqlalchemy.orm import Session
from core.proposal_service import ProposalService, PROPOSAL_EXECUTION_ENABLED
from core.models import (
AgentProposal,
AgentRegistry,
AgentStatus,
ProposalStatus,
ProposalType,
UserRole
)
class TestProposalServiceCoverage:
"""Coverage expansion for ProposalService."""
@pytest.fixture
def db_session(self):
"""Get test database session."""
from core.database import SessionLocal
session = SessionLocal()
yield session
session.rollback()
session.close()
@pytest.fixture
def proposal_service(self, db_session):
"""Get proposal service instance."""
return ProposalService(db_session)
@pytest.fixture
def intern_agent(self, db_session):
"""Create test INTERN agent."""
agent = AgentRegistry(
id="intern-agent-test",
name="Test Intern Agent",
status=AgentStatus.INTERN.value,
category="test",
module_path="test.module",
class_name="TestIntern",
confidence_score=0.6,
tenant_id="default",
user_id="test-user"
)
db_session.add(agent)
db_session.commit()
db_session.refresh(agent)
return agent
# Test: proposal creation
@pytest.mark.asyncio
async def test_create_action_proposal_success(self, proposal_service, intern_agent):
"""Create proposal for INTERN agent action."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={"test": "context"},
proposed_action={
"action_type": "canvas_present",
"canvas_type": "chart",
"data": {"test": "data"}
},
reasoning="This is a test proposal for coverage"
)
assert proposal.id is not None
assert proposal.agent_id == intern_agent.id
assert proposal.status == ProposalStatus.PROPOSED.value
assert proposal.proposal_type == ProposalType.ACTION.value
assert "Test Intern Agent" in proposal.description
@pytest.mark.asyncio
async def test_create_action_proposal_with_canvas(self, proposal_service, intern_agent):
"""Create proposal with canvas association."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "canvas_present"},
reasoning="Test",
canvas_id="canvas-123",
session_id="session-456",
title="Custom Proposal Title"
)
assert proposal.canvas_id == "canvas-123"
assert proposal.session_id == "session-456"
assert proposal.title == "Custom Proposal Title"
@pytest.mark.asyncio
async def test_create_action_proposal_agent_not_found(self, proposal_service):
"""Handle proposal creation for non-existent agent."""
with pytest.raises(ValueError, match="Agent .* not found"):
await proposal_service.create_action_proposal(
intern_agent_id="nonexistent-agent",
trigger_context={},
proposed_action={},
reasoning="Test"
)
@pytest.mark.asyncio
async def test_create_action_proposal_non_intern_status(self, proposal_service, db_session):
"""Create proposal for agent with non-INTERN status (should log warning)."""
agent = AgentRegistry(
id="autonomous-agent",
name="Autonomous Agent",
status=AgentStatus.AUTONOMOUS.value,
category="test",
module_path="test",
class_name="TestAuto",
tenant_id="default",
user_id="test-user"
)
db_session.add(agent)
db_session.commit()
# Should still create proposal, just log warning
proposal = await proposal_service.create_action_proposal(
intern_agent_id=agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test"
)
assert proposal is not None
# Test: proposal submission
@pytest.mark.asyncio
async def test_submit_for_approval_success(self, proposal_service, intern_agent):
"""Submit proposal for approval."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test"
)
# Should not raise
await proposal_service.submit_for_approval(proposal)
@pytest.mark.asyncio
async def test_submit_for_approval_invalid_status(self, proposal_service, intern_agent, db_session):
"""Reject submission of proposal with invalid status."""
proposal = AgentProposal(
id="test-proposal",
agent_id=intern_agent.id,
agent_name=intern_agent.name,
tenant_id="default",
user_id="test-user",
status=ProposalStatus.APPROVED.value, # Already approved
proposal_type=ProposalType.ACTION.value,
title="Test",
description="Test"
)
db_session.add(proposal)
db_session.commit()
with pytest.raises(ValueError, match="PROPOSED status"):
await proposal_service.submit_for_approval(proposal)
# Test: proposal approval
@pytest.mark.asyncio
async def test_approve_proposal_success(self, proposal_service, intern_agent):
"""Approve pending proposal."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "canvas_present"},
reasoning="Test approval"
)
with patch.object(proposal_service, '_execute_proposed_action', new_callable=AsyncMock) as mock_execute:
mock_execute.return_value = {"success": True, "executed_at": datetime.now().isoformat()}
result = await proposal_service.approve_proposal(
proposal.id,
user_id="admin-user"
)
assert result["success"] == True
# Verify proposal status updated
proposal_service.db.refresh(proposal)
assert proposal.status == ProposalStatus.EXECUTED.value
assert proposal.approved_by == "admin-user"
assert proposal.approved_at is not None
@pytest.mark.asyncio
async def test_approve_proposal_with_modifications(self, proposal_service, intern_agent):
"""Approve proposal with modifications."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "canvas_present", "param1": "value1"},
reasoning="Test"
)
modifications = {"param1": "modified_value", "param2": "new_value"}
with patch.object(proposal_service, '_execute_proposed_action', new_callable=AsyncMock) as mock_execute:
mock_execute.return_value = {"success": True}
await proposal_service.approve_proposal(
proposal.id,
user_id="admin-user",
modifications=modifications
)
proposal_service.db.refresh(proposal)
assert proposal.modifications == modifications
# Verify proposed_action was updated
assert proposal.proposed_action["param1"] == "modified_value"
assert proposal.proposed_action["param2"] == "new_value"
@pytest.mark.asyncio
async def test_approve_proposal_not_found(self, proposal_service):
"""Handle approval of non-existent proposal."""
with pytest.raises(ValueError, match="not found"):
await proposal_service.approve_proposal("nonexistent-id", "admin-user")
@pytest.mark.asyncio
async def test_approve_proposal_invalid_status(self, proposal_service, intern_agent, db_session):
"""Reject approval of already processed proposal."""
proposal = AgentProposal(
id="test-proposal",
agent_id=intern_agent.id,
agent_name=intern_agent.name,
tenant_id="default",
user_id="test-user",
status=ProposalStatus.EXECUTED.value, # Already executed
proposal_type=ProposalType.ACTION.value,
title="Test",
description="Test",
proposed_action={}
)
db_session.add(proposal)
db_session.commit()
with pytest.raises(ValueError, match="PROPOSED status"):
await proposal_service.approve_proposal("test-proposal", "admin-user")
# Test: proposal rejection
@pytest.mark.asyncio
async def test_reject_proposal_success(self, proposal_service, intern_agent):
"""Reject pending proposal."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test rejection"
)
await proposal_service.reject_proposal(
proposal.id,
user_id="admin-user",
reason="Unsafe action detected"
)
proposal_service.db.refresh(proposal)
assert proposal.status == ProposalStatus.REJECTED.value
assert proposal.approved_by == "admin-user"
assert proposal.approved_at is not None
assert proposal.execution_result["rejected"] == True
assert proposal.execution_result["reason"] == "Unsafe action detected"
@pytest.mark.asyncio
async def test_reject_proposal_not_found(self, proposal_service):
"""Handle rejection of non-existent proposal."""
with pytest.raises(ValueError, match="not found"):
await proposal_service.reject_proposal("nonexistent-id", "admin-user", "reason")
# Test: pending proposals retrieval
@pytest.mark.asyncio
async def test_get_pending_proposals(self, proposal_service, intern_agent):
"""Retrieve all pending proposals."""
# Create multiple proposals
for i in range(3):
await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={"index": i},
proposed_action={"action_type": "test"},
reasoning=f"Proposal {i}"
)
pending = await proposal_service.get_pending_proposals()
assert len(pending) >= 3
assert all(p.status == ProposalStatus.PROPOSED.value for p in pending)
@pytest.mark.asyncio
async def test_get_pending_proposals_by_agent(self, proposal_service, intern_agent, db_session):
"""Retrieve pending proposals for specific agent."""
# Create another agent
agent2 = AgentRegistry(
id="intern-agent-2",
name="Second Intern",
status=AgentStatus.INTERN.value,
category="test",
module_path="test",
class_name="Test",
tenant_id="default",
user_id="test-user"
)
db_session.add(agent2)
db_session.commit()
# Create proposals for both agents
await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Agent 1 proposal"
)
await proposal_service.create_action_proposal(
intern_agent_id=agent2.id,
trigger_context={},
proposed_action={},
reasoning="Agent 2 proposal"
)
# Get proposals for first agent only
agent1_proposals = await proposal_service.get_pending_proposals(agent_id=intern_agent.id)
assert all(p.agent_id == intern_agent.id for p in agent1_proposals)
@pytest.mark.asyncio
async def test_get_pending_proposals_by_canvas(self, proposal_service, intern_agent):
"""Retrieve pending proposals for specific canvas."""
await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Test",
canvas_id="canvas-123"
)
await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Test",
canvas_id="canvas-456"
)
canvas_proposals = await proposal_service.get_pending_proposals(canvas_id="canvas-123")
assert all(p.canvas_id == "canvas-123" for p in canvas_proposals)
@pytest.mark.asyncio
async def test_get_pending_proposals_limit(self, proposal_service, intern_agent):
"""Test limit parameter for pending proposals."""
# Create 10 proposals
for i in range(10):
await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={"index": i},
proposed_action={},
reasoning=f"Proposal {i}"
)
# Request only 5
limited = await proposal_service.get_pending_proposals(limit=5)
assert len(limited) == 5
# Test: proposal history
@pytest.mark.asyncio
async def test_get_proposal_history(self, proposal_service, intern_agent):
"""Retrieve agent's proposal history."""
# Create proposals with different statuses
proposal1 = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Proposal 1"
)
proposal1.status = ProposalStatus.APPROVED.value
proposal_service.db.commit()
proposal2 = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Proposal 2"
)
proposal2.status = ProposalStatus.REJECTED.value
proposal2.execution_result = {"rejected": True, "reason": "test"}
proposal_service.db.commit()
history = await proposal_service.get_proposal_history(intern_agent.id)
assert len(history) >= 2
assert all("proposal_id" in h for h in history)
assert all("status" in h for h in history)
assert all("created_at" in h for h in history)
# Test: execute proposed action (private method)
@pytest.mark.asyncio
async def test_execute_proposed_action_disabled(self, proposal_service, intern_agent):
"""Test execution when feature flag is disabled."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test"
)
with patch.object(proposal_service, 'PROPOSAL_EXECUTION_ENABLED', False):
result = await proposal_service._execute_proposed_action(proposal)
assert result["success"] == False
assert result["skipped"] == True
assert "disabled" in result["message"]
@pytest.mark.asyncio
async def test_execute_proposed_action_unknown_type(self, proposal_service, intern_agent):
"""Test execution with unknown action type."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "unknown_action"},
reasoning="Test"
)
result = await proposal_service._execute_proposed_action(proposal)
assert result["success"] == False
assert "Unknown action type" in result["error"]
@pytest.mark.asyncio
async def test_execute_proposed_action_exception_handling(self, proposal_service, intern_agent):
"""Test exception handling in action execution."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "browser_automate"},
reasoning="Test"
)
# Mock browser action to raise exception
with patch.object(proposal_service, '_execute_browser_action', new_callable=AsyncMock) as mock_browser:
mock_browser.side_effect = Exception("Browser error")
result = await proposal_service._execute_proposed_action(proposal)
assert result["success"] == False
assert "Browser error" in result["error"]
# Test: browser action execution
@pytest.mark.asyncio
async def test_execute_browser_action(self, proposal_service, intern_agent):
"""Test browser action execution."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={
"action_type": "browser_automate",
"url": "https://example.com",
"action": "navigate"
},
reasoning="Test browser action"
)
# Mock browser tool
with patch('core.proposal_service.browser_tool') as mock_browser:
mock_browser.execute.return_value = {"success": True, "url": "https://example.com"}
result = await proposal_service._execute_browser_action(proposal, proposal.proposed_action)
assert result["success"] == True
mock_browser.execute.assert_called_once()
# Test: canvas action execution
@pytest.mark.asyncio
async def test_execute_canvas_action(self, proposal_service, intern_agent):
"""Test canvas action execution."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={
"action_type": "canvas_present",
"canvas_type": "chart",
"data": {"test": "data"}
},
reasoning="Test canvas action"
)
# Mock canvas tool
with patch('core.proposal_service.canvas_tool') as mock_canvas:
mock_canvas.create_canvas.return_value = "canvas-123"
result = await proposal_service._execute_canvas_action(proposal, proposal.proposed_action)
assert result["success"] == True
mock_canvas.create_canvas.assert_called_once()
# Test: integration action execution
@pytest.mark.asyncio
async def test_execute_integration_action(self, proposal_service, intern_agent):
"""Test integration action execution."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={
"action_type": "integration_connect",
"integration_id": "asana-123",
"action": "create_task"
},
reasoning="Test integration action"
)
# Mock integration service
with patch('core.proposal_service.ServiceFactory') as mock_factory:
mock_service = AsyncMock()
mock_service.execute_action.return_value = {"success": True, "task_id": "task-123"}
mock_factory.get_service.return_value = mock_service
result = await proposal_service._execute_integration_action(proposal, proposal.proposed_action)
assert result["success"] == True
# Test: workflow action execution
@pytest.mark.asyncio
async def test_execute_workflow_action(self, proposal_service, intern_agent):
"""Test workflow action execution."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={
"action_type": "workflow_trigger",
"workflow_id": "workflow-123"
},
reasoning="Test workflow action"
)
# Mock workflow engine
with patch('core.proposal_service.WorkflowEngine') as mock_engine:
mock_engine_instance = AsyncMock()
mock_engine_instance.start_workflow.return_value = "execution-123"
mock_engine.return_value = mock_engine_instance
result = await proposal_service._execute_workflow_action(proposal, proposal.proposed_action)
assert result["success"] == True
# Test: device action execution
@pytest.mark.asyncio
async def test_execute_device_action(self, proposal_service, intern_agent):
"""Test device action execution."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={
"action_type": "device_command",
"command": "screenshot"
},
reasoning="Test device action"
)
# Mock device tool
with patch('core.proposal_service.device_tool') as mock_device:
mock_device.execute_command.return_value = {"success": True, "screenshot_path": "/tmp/screenshot.png"}
result = await proposal_service._execute_device_action(proposal, proposal.proposed_action)
assert result["success"] == True
# Test: agent execution action
@pytest.mark.asyncio
async def test_execute_agent_action(self, proposal_service, intern_agent):
"""Test agent execution action."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={
"action_type": "agent_execute",
"target_agent_id": "target-agent-123",
"prompt": "Execute this task"
},
reasoning="Test agent execution"
)
# Mock atom meta agent
with patch('core.proposal_service.atom_meta_agent') as mock_meta:
mock_meta.execute_agent.return_value = {"success": True, "response": "Task completed"}
result = await proposal_service._execute_agent_action(proposal, proposal.proposed_action)
assert result["success"] == True
# Test: proposal episode creation
@pytest.mark.asyncio
async def test_create_proposal_episode_approved(self, proposal_service, intern_agent):
"""Test episode creation for approved proposal."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test episode creation"
)
# Should not raise
await proposal_service._create_proposal_episode(
proposal=proposal,
outcome="approved",
execution_result={"success": True}
)
@pytest.mark.asyncio
async def test_create_proposal_episode_rejected(self, proposal_service, intern_agent):
"""Test episode creation for rejected proposal."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test"
)
# Should not raise
await proposal_service._create_proposal_episode(
proposal=proposal,
outcome="rejected",
rejection_reason="Unsafe action"
)
# Test: agent name denormalization
@pytest.mark.asyncio
async def test_agent_name_denormalized(self, proposal_service, intern_agent):
"""Verify agent name is denormalized into proposal."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test agent name"
)
assert proposal.agent_name == "Test Intern Agent"
assert proposal.agent_name in proposal.description
# Test: proposal with empty proposed_action
@pytest.mark.asyncio
async def test_create_proposal_empty_action(self, proposal_service, intern_agent):
"""Create proposal with empty proposed_action."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={}, # Empty action
reasoning="Test empty action"
)
assert proposal.proposed_action == {}
assert proposal.proposal_type == ProposalType.ACTION.value
# Test: confidence score in description
@pytest.mark.asyncio
async def test_confidence_in_description(self, proposal_service, db_session):
"""Verify agent confidence appears in proposal description."""
agent = AgentRegistry(
id="confident-agent",
name="Confident Agent",
status=AgentStatus.INTERN.value,
category="test",
module_path="test",
class_name="Test",
confidence_score=0.85,
tenant_id="default",
user_id="test-user"
)
db_session.add(agent)
db_session.commit()
proposal = await proposal_service.create_action_proposal(
intern_agent_id=agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test confidence"
)
assert "0.85" in proposal.description
# Test: proposal timestamps
@pytest.mark.asyncio
async def test_proposal_timestamps(self, proposal_service, intern_agent):
"""Verify proposal timestamps are set correctly."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Test timestamps"
)
assert proposal.created_at is not None
assert isinstance(proposal.created_at, datetime)
# Approve and check approved_at
with patch.object(proposal_service, '_execute_proposed_action', new_callable=AsyncMock) as mock_execute:
mock_execute.return_value = {"success": True}
await proposal_service.approve_proposal(proposal.id, "admin-user")
proposal_service.db.refresh(proposal)
assert proposal.approved_at is not None
assert proposal.completed_at is not None
# Test: multi-tenant isolation
@pytest.mark.asyncio
async def test_multi_tenant_proposals(self, proposal_service, intern_agent, db_session):
"""Test proposals are isolated by tenant."""
# Create agent in different tenant
agent2 = AgentRegistry(
id="tenant2-agent",
name="Tenant 2 Agent",
status=AgentStatus.INTERN.value,
category="test",
module_path="test",
class_name="Test",
tenant_id="tenant-2",
user_id="user-2"
)
db_session.add(agent2)
db_session.commit()
# Create proposals for both tenants
await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Tenant 1"
)
await proposal_service.create_action_proposal(
intern_agent_id=agent2.id,
trigger_context={},
proposed_action={},
reasoning="Tenant 2"
)
# Get proposals for tenant 1 only
tenant1_proposals = await proposal_service.get_pending_proposals(tenant_id="default")
assert all(p.tenant_id == "default" for p in tenant1_proposals)
# Get proposals for tenant 2 only
tenant2_proposals = await proposal_service.get_pending_proposals(tenant_id="tenant-2")
assert all(p.tenant_id == "tenant-2" for p in tenant2_proposals)
# Test: proposal data integrity
@pytest.mark.asyncio
async def test_proposal_data_integrity(self, proposal_service, intern_agent):
"""Verify proposal data is stored correctly."""
proposed_action = {
"action_type": "canvas_present",
"canvas_type": "form",
"data": {
"fields": [
{"name": "email", "type": "email"},
{"name": "message", "type": "text"}
]
}
}
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={"test": "context"},
proposed_action=proposed_action,
reasoning="Test data integrity"
)
assert proposal.proposed_action == proposed_action
assert proposal.proposed_action["data"]["fields"][0]["name"] == "email"
# Test: proposal status transitions
@pytest.mark.asyncio
async def test_proposal_status_transitions(self, proposal_service, intern_agent):
"""Verify proposal status transitions are correct."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Test status transitions"
)
# Initial status
assert proposal.status == ProposalStatus.PROPOSED.value
# After approval
with patch.object(proposal_service, '_execute_proposed_action', new_callable=AsyncMock) as mock_execute:
mock_execute.return_value = {"success": True}
await proposal_service.approve_proposal(proposal.id, "admin-user")
proposal_service.db.refresh(proposal)
# Status should be EXECUTED (not just APPROVED)
assert proposal.status == ProposalStatus.EXECUTED.value
# Test: proposal with session_id
@pytest.mark.asyncio
async def test_proposal_with_session(self, proposal_service, intern_agent):
"""Test proposal associated with chat session."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={},
reasoning="Test session association",
session_id="chat-session-123"
)
assert proposal.session_id == "chat-session-123"
# Retrieve by session
session_proposals = await proposal_service.get_pending_proposals(
agent_id=intern_agent.id
)
assert any(p.session_id == "chat-session-123" for p in session_proposals)
# Test: proposal execution result storage
@pytest.mark.asyncio
async def test_execution_result_storage(self, proposal_service, intern_agent):
"""Verify execution result is stored in proposal."""
proposal = await proposal_service.create_action_proposal(
intern_agent_id=intern_agent.id,
trigger_context={},
proposed_action={"action_type": "test"},
reasoning="Test execution result"
)
execution_result = {
"success": True,
"output": {"result": "completed"},
"executed_at": datetime.now().isoformat()
}
with patch.object(proposal_service, '_execute_proposed_action', new_callable=AsyncMock) as mock_execute:
mock_execute.return_value = execution_result
await proposal_service.approve_proposal(proposal.id, "admin-user")
proposal_service.db.refresh(proposal)
assert proposal.execution_result == execution_result
assert proposal.execution_result["success"] == True
|