|
| 1 | +from unittest.mock import AsyncMock, MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from slack_sdk.web.async_client import AsyncWebClient |
| 5 | + |
| 6 | +from slack_bolt.context.say_stream.async_say_stream import AsyncSayStream |
| 7 | +from slack_bolt.warning import ExperimentalWarning |
| 8 | + |
| 9 | + |
| 10 | +class TestAsyncSayStream: |
| 11 | + def setup_method(self): |
| 12 | + self.mock_client = MagicMock(spec=AsyncWebClient) |
| 13 | + self.mock_client.chat_stream = AsyncMock() |
| 14 | + |
| 15 | + @pytest.mark.asyncio |
| 16 | + async def test_missing_channel_raises(self): |
| 17 | + say_stream = AsyncSayStream(client=self.mock_client, channel_id=None, thread_ts="111.222") |
| 18 | + with pytest.warns(ExperimentalWarning): |
| 19 | + with pytest.raises(ValueError, match="channel"): |
| 20 | + await say_stream() |
| 21 | + |
| 22 | + @pytest.mark.asyncio |
| 23 | + async def test_missing_thread_ts_raises(self): |
| 24 | + say_stream = AsyncSayStream(client=self.mock_client, channel_id="C111", thread_ts=None) |
| 25 | + with pytest.warns(ExperimentalWarning): |
| 26 | + with pytest.raises(ValueError, match="thread_ts"): |
| 27 | + await say_stream() |
| 28 | + |
| 29 | + @pytest.mark.asyncio |
| 30 | + async def test_default_params(self): |
| 31 | + say_stream = AsyncSayStream( |
| 32 | + client=self.mock_client, |
| 33 | + channel_id="C111", |
| 34 | + thread_ts="111.222", |
| 35 | + team_id="T111", |
| 36 | + user_id="U111", |
| 37 | + ) |
| 38 | + await say_stream() |
| 39 | + |
| 40 | + self.mock_client.chat_stream.assert_called_once_with( |
| 41 | + channel="C111", |
| 42 | + thread_ts="111.222", |
| 43 | + recipient_team_id="T111", |
| 44 | + recipient_user_id="U111", |
| 45 | + ) |
| 46 | + |
| 47 | + @pytest.mark.asyncio |
| 48 | + async def test_parameter_overrides(self): |
| 49 | + say_stream = AsyncSayStream( |
| 50 | + client=self.mock_client, |
| 51 | + channel_id="C111", |
| 52 | + thread_ts="111.222", |
| 53 | + team_id="T111", |
| 54 | + user_id="U111", |
| 55 | + ) |
| 56 | + await say_stream(channel="C222", thread_ts="333.444", recipient_team_id="T222", recipient_user_id="U222") |
| 57 | + |
| 58 | + self.mock_client.chat_stream.assert_called_once_with( |
| 59 | + channel="C222", |
| 60 | + thread_ts="333.444", |
| 61 | + recipient_team_id="T222", |
| 62 | + recipient_user_id="U222", |
| 63 | + ) |
| 64 | + |
| 65 | + @pytest.mark.asyncio |
| 66 | + async def test_buffer_size_passthrough(self): |
| 67 | + say_stream = AsyncSayStream( |
| 68 | + client=self.mock_client, |
| 69 | + channel_id="C111", |
| 70 | + thread_ts="111.222", |
| 71 | + ) |
| 72 | + await say_stream(buffer_size=100) |
| 73 | + |
| 74 | + self.mock_client.chat_stream.assert_called_once_with( |
| 75 | + buffer_size=100, |
| 76 | + channel="C111", |
| 77 | + thread_ts="111.222", |
| 78 | + recipient_team_id=None, |
| 79 | + recipient_user_id=None, |
| 80 | + ) |
| 81 | + |
| 82 | + @pytest.mark.asyncio |
| 83 | + async def test_experimental_warning(self): |
| 84 | + say_stream = AsyncSayStream( |
| 85 | + client=self.mock_client, |
| 86 | + channel_id="C111", |
| 87 | + thread_ts="111.222", |
| 88 | + ) |
| 89 | + with pytest.warns(ExperimentalWarning, match="say_stream is experimental"): |
| 90 | + await say_stream() |
0 commit comments