|
| 1 | +from unittest.mock import AsyncMock, MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp.server.fastmcp import Context |
| 6 | +from mcp.shared.context import RequestContext |
| 7 | + |
| 8 | +pytestmark = pytest.mark.anyio |
| 9 | + |
| 10 | + |
| 11 | +async def test_progress_token_zero_first_call(): |
| 12 | + """Test that progress notifications work when progress_token is 0 on first call.""" |
| 13 | + |
| 14 | + # Create mock session with progress notification tracking |
| 15 | + mock_session = AsyncMock() |
| 16 | + mock_session.send_progress_notification = AsyncMock() |
| 17 | + |
| 18 | + # Create request context with progress token 0 |
| 19 | + mock_meta = MagicMock() |
| 20 | + mock_meta.progressToken = 0 # This is the key test case - token is 0 |
| 21 | + |
| 22 | + request_context = RequestContext( |
| 23 | + request_id="test-request", session=mock_session, meta=mock_meta |
| 24 | + ) |
| 25 | + |
| 26 | + # Create context with our mocks |
| 27 | + ctx = Context(request_context=request_context, fastmcp=MagicMock()) |
| 28 | + |
| 29 | + # Test progress reporting |
| 30 | + await ctx.report_progress(0, 10) # First call with 0 |
| 31 | + await ctx.report_progress(5, 10) # Middle progress |
| 32 | + await ctx.report_progress(10, 10) # Complete |
| 33 | + |
| 34 | + # Verify progress notifications |
| 35 | + assert ( |
| 36 | + mock_session.send_progress_notification.call_count == 3 |
| 37 | + ), "All progress notifications should be sent" |
| 38 | + mock_session.send_progress_notification.assert_any_call( |
| 39 | + progress_token=0, progress=0.0, total=10.0 |
| 40 | + ) |
| 41 | + mock_session.send_progress_notification.assert_any_call( |
| 42 | + progress_token=0, progress=5.0, total=10.0 |
| 43 | + ) |
| 44 | + mock_session.send_progress_notification.assert_any_call( |
| 45 | + progress_token=0, progress=10.0, total=10.0 |
| 46 | + ) |
0 commit comments