Test async context manager handles initialization failure.
(dummy_tool_manager, monkeypatch)
| 580 | |
| 581 | @pytest.mark.asyncio |
| 582 | async def test_context_manager_failure(dummy_tool_manager, monkeypatch): |
| 583 | """Test async context manager handles initialization failure.""" |
| 584 | monkeypatch.setattr( |
| 585 | "mcp_cli.chat.chat_context.generate_system_prompt", |
| 586 | lambda tools=None, **kw: "SYS_PROMPT", |
| 587 | ) |
| 588 | |
| 589 | from unittest.mock import Mock |
| 590 | from mcp_cli.model_management import ModelManager |
| 591 | |
| 592 | mock_manager = Mock(spec=ModelManager) |
| 593 | mock_manager.get_client.return_value = None |
| 594 | mock_manager.get_active_provider.return_value = "mock" |
| 595 | mock_manager.get_active_model.return_value = "mock-model" |
| 596 | |
| 597 | ctx = ChatContext.create( |
| 598 | tool_manager=dummy_tool_manager, model_manager=mock_manager |
| 599 | ) |
| 600 | |
| 601 | # Make initialize return False |
| 602 | async def fail_init(): |
| 603 | return False |
| 604 | |
| 605 | ctx.initialize = fail_init |
| 606 | |
| 607 | with pytest.raises(RuntimeError, match="Failed to initialize"): |
| 608 | async with ctx: |
| 609 | pass |
| 610 | |
| 611 | |
| 612 | @pytest.mark.asyncio |