* refactor: remove redundant comments and docstrings * fix(signal): annotate async context manager as a generator
257 lines
8.2 KiB
Python
257 lines
8.2 KiB
Python
# ruff: noqa: E402
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("lark_oapi")
|
|
|
|
from nanobot.bus.queue import MessageBus
|
|
from nanobot.channels.feishu.runtime import FeishuChannel, FeishuConfig
|
|
|
|
|
|
def _make_channel() -> FeishuChannel:
|
|
config = FeishuConfig(
|
|
enabled=True,
|
|
app_id="cli_test",
|
|
app_secret="secret",
|
|
allow_from=["*"],
|
|
)
|
|
ch = FeishuChannel(config, MessageBus())
|
|
ch._client = MagicMock()
|
|
ch._loop = None
|
|
return ch
|
|
|
|
|
|
def _mock_reaction_create_response(reaction_id: str = "reaction_001", success: bool = True):
|
|
resp = MagicMock()
|
|
resp.success.return_value = success
|
|
resp.code = 0 if success else 99999
|
|
resp.msg = "ok" if success else "error"
|
|
if success:
|
|
resp.data = SimpleNamespace(reaction_id=reaction_id)
|
|
else:
|
|
resp.data = None
|
|
return resp
|
|
|
|
|
|
class TestAddReactionSync:
|
|
def test_returns_reaction_id_on_success(self):
|
|
ch = _make_channel()
|
|
ch._client.im.v1.message_reaction.create.return_value = _mock_reaction_create_response("rx_42")
|
|
result = ch._add_reaction_sync("om_001", "THUMBSUP")
|
|
assert result == "rx_42"
|
|
|
|
def test_returns_none_when_response_fails(self):
|
|
ch = _make_channel()
|
|
ch._client.im.v1.message_reaction.create.return_value = _mock_reaction_create_response(success=False)
|
|
assert ch._add_reaction_sync("om_001", "THUMBSUP") is None
|
|
|
|
def test_returns_none_when_response_data_is_none(self):
|
|
ch = _make_channel()
|
|
resp = MagicMock()
|
|
resp.success.return_value = True
|
|
resp.data = None
|
|
ch._client.im.v1.message_reaction.create.return_value = resp
|
|
assert ch._add_reaction_sync("om_001", "THUMBSUP") is None
|
|
|
|
def test_returns_none_on_exception(self):
|
|
ch = _make_channel()
|
|
ch._client.im.v1.message_reaction.create.side_effect = RuntimeError("network error")
|
|
assert ch._add_reaction_sync("om_001", "THUMBSUP") is None
|
|
|
|
|
|
class TestAddReactionAsync:
|
|
@pytest.mark.asyncio
|
|
async def test_returns_reaction_id(self):
|
|
ch = _make_channel()
|
|
ch._add_reaction_sync = MagicMock(return_value="rx_99")
|
|
result = await ch._add_reaction("om_001", "EYES")
|
|
assert result == "rx_99"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_returns_none_when_no_client(self):
|
|
ch = _make_channel()
|
|
ch._client = None
|
|
result = await ch._add_reaction("om_001", "THUMBSUP")
|
|
assert result is None
|
|
|
|
|
|
class TestRemoveReactionSync:
|
|
def test_calls_delete_on_success(self):
|
|
ch = _make_channel()
|
|
resp = MagicMock()
|
|
resp.success.return_value = True
|
|
ch._client.im.v1.message_reaction.delete.return_value = resp
|
|
|
|
ch._remove_reaction_sync("om_001", "rx_42")
|
|
|
|
ch._client.im.v1.message_reaction.delete.assert_called_once()
|
|
|
|
def test_handles_failure_gracefully(self):
|
|
ch = _make_channel()
|
|
resp = MagicMock()
|
|
resp.success.return_value = False
|
|
resp.code = 99999
|
|
resp.msg = "not found"
|
|
ch._client.im.v1.message_reaction.delete.return_value = resp
|
|
|
|
ch._remove_reaction_sync("om_001", "rx_42")
|
|
ch._client.im.v1.message_reaction.delete.assert_called_once()
|
|
|
|
def test_handles_exception_gracefully(self):
|
|
ch = _make_channel()
|
|
ch._client.im.v1.message_reaction.delete.side_effect = RuntimeError("network error")
|
|
|
|
ch._remove_reaction_sync("om_001", "rx_42")
|
|
ch._client.im.v1.message_reaction.delete.assert_called_once()
|
|
|
|
|
|
class TestRemoveReactionAsync:
|
|
@pytest.mark.asyncio
|
|
async def test_calls_sync_helper(self):
|
|
ch = _make_channel()
|
|
ch._remove_reaction_sync = MagicMock()
|
|
|
|
await ch._remove_reaction("om_001", "rx_42")
|
|
|
|
ch._remove_reaction_sync.assert_called_once_with("om_001", "rx_42")
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_noop_when_no_client(self):
|
|
ch = _make_channel()
|
|
ch._client = None
|
|
ch._remove_reaction_sync = MagicMock()
|
|
|
|
await ch._remove_reaction("om_001", "rx_42")
|
|
|
|
ch._remove_reaction_sync.assert_not_called()
|
|
|
|
|
|
class TestStreamEndReactionCleanup:
|
|
@pytest.mark.asyncio
|
|
async def test_stream_buffers_are_scoped_by_message_id(self):
|
|
ch = _make_channel()
|
|
ch._create_streaming_card_sync = MagicMock(return_value=None)
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "first",
|
|
metadata={"message_id": "om_first"},
|
|
)
|
|
await ch.send_delta(
|
|
"oc_chat1", "second",
|
|
metadata={"message_id": "om_second"},
|
|
)
|
|
|
|
assert ch._stream_bufs["om_first"].text == "first"
|
|
assert ch._stream_bufs["om_second"].text == "second"
|
|
assert "oc_chat1" not in ch._stream_bufs
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_removes_reaction_on_stream_end(self):
|
|
ch = _make_channel()
|
|
ch._reaction_ids.update({"om_001": "rx_42", "om_other": "rx_other"})
|
|
ch._remove_reaction = AsyncMock()
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "",
|
|
metadata={"message_id": "om_001"},
|
|
stream_end=True,
|
|
)
|
|
|
|
ch._remove_reaction.assert_awaited_once_with("om_001", "rx_42")
|
|
assert ch._reaction_ids == {"om_other": "rx_other"}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_removal_when_message_id_missing(self):
|
|
ch = _make_channel()
|
|
ch._reaction_ids["om_001"] = "rx_42"
|
|
ch._remove_reaction = AsyncMock()
|
|
|
|
await ch.send_delta("oc_chat1", "", stream_end=True)
|
|
|
|
ch._remove_reaction.assert_not_awaited()
|
|
assert ch._reaction_ids == {"om_001": "rx_42"}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_removal_when_reaction_id_missing(self):
|
|
ch = _make_channel()
|
|
ch._remove_reaction = AsyncMock()
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "",
|
|
metadata={"message_id": "om_001"},
|
|
stream_end=True,
|
|
)
|
|
|
|
ch._remove_reaction.assert_not_called()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_removal_when_not_stream_end(self):
|
|
ch = _make_channel()
|
|
ch._reaction_ids["om_001"] = "rx_42"
|
|
ch._create_streaming_card_sync = MagicMock(return_value=None)
|
|
ch._remove_reaction = AsyncMock()
|
|
ch._add_reaction = AsyncMock()
|
|
ch.config.done_emoji = "DONE"
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "more text",
|
|
metadata={"message_id": "om_001"},
|
|
)
|
|
|
|
ch._remove_reaction.assert_not_awaited()
|
|
ch._add_reaction.assert_not_awaited()
|
|
assert ch._reaction_ids == {"om_001": "rx_42"}
|
|
assert ch._stream_bufs["om_001"].text == "more text"
|
|
assert "oc_chat1" not in ch._stream_bufs
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_removal_when_resuming(self):
|
|
ch = _make_channel()
|
|
ch.config.done_emoji = "DONE"
|
|
ch._reaction_ids["om_001"] = "rx_42"
|
|
ch._remove_reaction = AsyncMock()
|
|
ch._add_reaction = AsyncMock()
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "",
|
|
metadata={"message_id": "om_001"},
|
|
stream_end=True,
|
|
resuming=True,
|
|
)
|
|
|
|
ch._remove_reaction.assert_not_called()
|
|
ch._add_reaction.assert_not_called()
|
|
assert ch._reaction_ids.get("om_001") == "rx_42"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_done_emoji_only_on_final_stream_end(self):
|
|
ch = _make_channel()
|
|
ch.config.done_emoji = "DONE"
|
|
ch._reaction_ids.update({"om_001": "rx_42", "om_other": "rx_other"})
|
|
ch._remove_reaction = AsyncMock()
|
|
ch._add_reaction = AsyncMock()
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "",
|
|
metadata={"message_id": "om_001"},
|
|
stream_end=True,
|
|
resuming=True,
|
|
)
|
|
ch._remove_reaction.assert_not_called()
|
|
ch._add_reaction.assert_not_called()
|
|
|
|
assert ch._reaction_ids == {"om_001": "rx_42", "om_other": "rx_other"}
|
|
|
|
await ch.send_delta(
|
|
"oc_chat1", "",
|
|
metadata={"message_id": "om_001"},
|
|
stream_end=True,
|
|
resuming=False,
|
|
)
|
|
ch._remove_reaction.assert_awaited_once_with("om_001", "rx_42")
|
|
ch._add_reaction.assert_awaited_once_with("om_001", "DONE")
|
|
assert ch._reaction_ids == {"om_other": "rx_other"}
|