test: replace false-green checks with behavioral coverage (#6282)

* test: replace false-green checks with behavioral coverage

* test: complete behavior-focused audit coverage
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-18 03:16:25 +08:00
1 parent c7ab7aa6da
commit 74942bd190
91 files changed
+1891 -1391

No files matched your search

@@ -1,5 +1,7 @@
"""Tests for Feishu/Lark domain configuration."""
from unittest.mock import MagicMock
from unittest.mock import ANY, AsyncMock, MagicMock
import pytest
from nanobot.bus.queue import MessageBus
from nanobot.channels.feishu.runtime import FeishuChannel, FeishuConfig
@@ -44,3 +46,46 @@ class TestFeishuConfigDomain:
def test_channel_persists_feishu_domain_from_config(self):
ch = _make_channel(domain="feishu")
assert ch.config.domain == "feishu"
@pytest.mark.asyncio
@pytest.mark.parametrize("domain, expected_domain", [
("feishu", "https://open.feishu.cn"),
("lark", "https://open.larksuite.com"),
])
async def test_start_selects_domain_for_http_and_websocket_sdk_clients(
domain, expected_domain, monkeypatch, tmp_path,
):
lark = pytest.importorskip("lark_oapi")
# Startup must never persist fixture credentials into the active host config.
monkeypatch.setattr("nanobot.config.loader.get_config_path", lambda: tmp_path / "config.json")
monkeypatch.setattr(
"nanobot.channels.feishu.runtime.sync_saved_feishu_identity_boundary",
lambda **_kwargs: False,
)
builder = MagicMock()
for method in ("app_id", "app_secret", "domain", "log_level"):
getattr(builder, method).return_value = builder
monkeypatch.setattr(lark.Client, "builder", lambda: builder)
ws_client = MagicMock()
monkeypatch.setattr(lark.ws, "Client", ws_client)
ch = _make_channel(domain)
async def started(*_args):
ch._running = False
# Keep production start/domain selection, but never open an SDK connection.
ch._ws_runner = MagicMock(start_client=AsyncMock(side_effect=started), stop_client=AsyncMock())
monkeypatch.setattr(ch, "_fetch_bot_open_id", lambda: None)
try:
await ch.start()
builder.domain.assert_called_once_with(expected_domain)
ws_client.assert_called_once_with(
"cli_test", "secret", domain=expected_domain,
event_handler=ANY,
log_level=lark.LogLevel.INFO,
)
assert ch._client is builder.build.return_value
ch._ws_runner.start_client.assert_awaited_once_with(ch.name, ws_client.return_value)
finally:
await ch.stop()
@@ -9,7 +9,7 @@ import pytest
pytest.importorskip("lark_oapi")
from nanobot.bus.queue import MessageBus
from nanobot.channels.feishu.runtime import FeishuChannel, FeishuConfig, _FeishuStreamBuf
from nanobot.channels.feishu.runtime import FeishuChannel, FeishuConfig
def _make_channel() -> FeishuChannel:
@@ -187,12 +187,7 @@ class TestStreamEndReactionCleanup:
@pytest.mark.asyncio
async def test_removes_reaction_on_stream_end(self):
ch = _make_channel()
ch._stream_bufs["oc_chat1"] = _FeishuStreamBuf(
text="Done", card_id="card_1", sequence=3, last_edit=0.0,
)
ch._reaction_ids["om_001"] = "rx_42"
ch._client.cardkit.v1.card_element.content.return_value = MagicMock(success=MagicMock(return_value=True))
ch._client.cardkit.v1.card.settings.return_value = MagicMock(success=MagicMock(return_value=True))
ch._reaction_ids.update({"om_001": "rx_42", "om_other": "rx_other"})
ch._remove_reaction = AsyncMock()
await ch.send_delta(
@@ -201,33 +196,23 @@ class TestStreamEndReactionCleanup:
stream_end=True,
)
ch._remove_reaction.assert_called_once_with("om_001", "rx_42")
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._stream_bufs["oc_chat1"] = _FeishuStreamBuf(
text="Done", card_id="card_1", sequence=3, last_edit=0.0,
)
ch._client.cardkit.v1.card_element.content.return_value = MagicMock(success=MagicMock(return_value=True))
ch._client.cardkit.v1.card.settings.return_value = MagicMock(success=MagicMock(return_value=True))
ch._reaction_ids["om_001"] = "rx_42"
ch._remove_reaction = AsyncMock()
await ch.send_delta(
"oc_chat1", "",
stream_end=True,
)
await ch.send_delta("oc_chat1", "", stream_end=True)
ch._remove_reaction.assert_not_called()
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._stream_bufs["oc_chat1"] = _FeishuStreamBuf(
text="Done", card_id="card_1", sequence=3, last_edit=0.0,
)
ch._client.cardkit.v1.card_element.content.return_value = MagicMock(success=MagicMock(return_value=True))
ch._client.cardkit.v1.card.settings.return_value = MagicMock(success=MagicMock(return_value=True))
ch._remove_reaction = AsyncMock()
await ch.send_delta(
@@ -241,26 +226,29 @@ class TestStreamEndReactionCleanup:
@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", "reaction_id": "rx_42"},
metadata={"message_id": "om_001"},
)
ch._remove_reaction.assert_not_called()
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):
"""resuming=True means more tool-call rounds follow; reaction must persist."""
ch = _make_channel()
ch.config.done_emoji = "DONE"
ch._stream_bufs["oc_chat1"] = _FeishuStreamBuf(
text="partial", card_id="card_1", sequence=3, last_edit=0.0,
)
ch._reaction_ids["om_001"] = "rx_42"
ch._client.cardkit.v1.card_element.content.return_value = MagicMock(success=MagicMock(return_value=True))
ch._client.cardkit.v1.card.settings.return_value = MagicMock(success=MagicMock(return_value=True))
ch._remove_reaction = AsyncMock()
ch._add_reaction = AsyncMock()
@@ -281,12 +269,7 @@ class TestStreamEndReactionCleanup:
"""Across resuming rounds, done_emoji is added only on the final round."""
ch = _make_channel()
ch.config.done_emoji = "DONE"
ch._stream_bufs["oc_chat1"] = _FeishuStreamBuf(
text="t", card_id="card_1", sequence=3, last_edit=0.0,
)
ch._reaction_ids["om_001"] = "rx_42"
ch._client.cardkit.v1.card_element.content.return_value = MagicMock(success=MagicMock(return_value=True))
ch._client.cardkit.v1.card.settings.return_value = MagicMock(success=MagicMock(return_value=True))
ch._reaction_ids.update({"om_001": "rx_42", "om_other": "rx_other"})
ch._remove_reaction = AsyncMock()
ch._add_reaction = AsyncMock()
@@ -300,10 +283,8 @@ class TestStreamEndReactionCleanup:
ch._remove_reaction.assert_not_called()
ch._add_reaction.assert_not_called()
# Re-prime the stream buffer for the final round (the previous stream end popped it).
ch._stream_bufs["oc_chat1"] = _FeishuStreamBuf(
text="t", card_id="card_1", sequence=5, last_edit=0.0,
)
assert ch._reaction_ids == {"om_001": "rx_42", "om_other": "rx_other"}
# Final stream end (resuming=False): OnIt removed, done_emoji added.
await ch.send_delta(
"oc_chat1", "",
@@ -311,5 +292,6 @@ class TestStreamEndReactionCleanup:
stream_end=True,
resuming=False,
)
ch._remove_reaction.assert_called_once_with("om_001", "rx_42")
ch._add_reaction.assert_called_once_with("om_001", "DONE")
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"}