import pytest
from slack_mcp.client import slack
@pytest.mark.asyncio
class TestPaginateCursor:
async def test_single_page(self, mock_slack_clients):
bot = mock_slack_clients["bot"]
bot.mock_method.return_value = {
"items": [{"id": 1}, {"id": 2}],
"response_metadata": {"next_cursor": ""},
}
result = await slack.paginate_cursor("mock_method", "items", limit=10)
assert len(result) == 2
bot.mock_method.assert_called_once()
async def test_multiple_pages(self, mock_slack_clients):
bot = mock_slack_clients["bot"]
call_count = 0
async def mock_method(**kwargs):
nonlocal call_count
call_count += 1
if call_count == 1:
return {
"items": [{"id": 1}, {"id": 2}],
"response_metadata": {"next_cursor": "page2"},
}
elif call_count == 2:
return {
"items": [{"id": 3}],
"response_metadata": {"next_cursor": "page3"},
}
return {
"items": [{"id": 4}],
"response_metadata": {"next_cursor": ""},
}
bot.mock_method = mock_method
result = await slack.paginate_cursor("mock_method", "items", limit=2)
assert len(result) == 4
assert call_count == 3
async def test_respects_max_pages(self, mock_slack_clients):
bot = mock_slack_clients["bot"]
call_count = 0
async def mock_method(**kwargs):
nonlocal call_count
call_count += 1
return {
"items": [{"id": call_count}],
"response_metadata": {"next_cursor": f"page{call_count + 1}"},
}
bot.mock_method = mock_method
result = await slack.paginate_cursor("mock_method", "items", limit=1, max_pages=3)
assert len(result) == 3
assert call_count == 3
async def test_passes_kwargs(self, mock_slack_clients):
bot = mock_slack_clients["bot"]
received_kwargs = {}
async def mock_method(**kwargs):
received_kwargs.update(kwargs)
return {
"items": [],
"response_metadata": {"next_cursor": ""},
}
bot.mock_method = mock_method
await slack.paginate_cursor(
"mock_method", "items", limit=50, channel="C123", types="public_channel"
)
assert received_kwargs["channel"] == "C123"
assert received_kwargs["types"] == "public_channel"
assert received_kwargs["limit"] == 50
async def test_cursor_passed_on_subsequent_calls(self, mock_slack_clients):
bot = mock_slack_clients["bot"]
calls = []
async def mock_method(**kwargs):
calls.append(kwargs)
if len(calls) == 1:
return {
"items": [{"id": 1}],
"response_metadata": {"next_cursor": "abc123"},
}
return {
"items": [{"id": 2}],
"response_metadata": {"next_cursor": ""},
}
bot.mock_method = mock_method
await slack.paginate_cursor("mock_method", "items", limit=10)
assert "cursor" not in calls[0]
assert calls[1]["cursor"] == "abc123"
async def test_empty_response(self, mock_slack_clients):
bot = mock_slack_clients["bot"]
bot.mock_method.return_value = {
"items": [],
"response_metadata": {"next_cursor": ""},
}
result = await slack.paginate_cursor("mock_method", "items")
assert result == []