fix: delay transient typing indicators

This commit is contained in:
jxxghp
2026-05-23 00:41:12 +08:00
parent 9190699cd1
commit efdb4d1b28
4 changed files with 98 additions and 5 deletions

View File

@@ -1,10 +1,13 @@
import asyncio
import json
import unittest
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock, patch
from app.agent import _finish_processing_status
from app.modules.discord import DiscordModule
from app.modules.discord.discord import Discord
from app.modules.slack import SlackModule
from app.schemas.message import ChannelCapability, ChannelCapabilityManager
from app.schemas.types import MessageChannel
@@ -144,5 +147,35 @@ class TestMessageProcessingStatus(unittest.TestCase):
)
class TestDiscordTypingLifecycle(IsolatedAsyncioTestCase):
async def test_short_typing_task_can_stop_before_first_trigger(self):
"""
短响应在首次 Discord typing 触发前结束时,不应留下客户端自然保留的输入状态。
"""
discord_client = Discord.__new__(Discord)
discord_client._loop = asyncio.get_running_loop()
discord_client._typing_tasks = {}
discord_client._typing_stop_events = {}
discord_client._typing_interval_seconds = 0.01
discord_client._typing_max_duration_seconds = 1
channel = MagicMock()
channel.trigger_typing = AsyncMock()
with patch.object(discord_client, "_resolve_channel", return_value=channel):
started = await discord_client._start_typing_task(
typing_key="chat:30003",
chat_id="30003",
max_duration_seconds=1,
initial_delay_seconds=0.05,
)
stopped = await discord_client._stop_typing_task("chat:30003")
await asyncio.sleep(0.08)
self.assertTrue(started)
self.assertTrue(stopped)
channel.trigger_typing.assert_not_called()
self.assertNotIn("chat:30003", discord_client._typing_tasks)
if __name__ == "__main__":
unittest.main()

View File

@@ -46,7 +46,11 @@ class TestTelegramTypingLifecycle(unittest.TestCase):
def test_start_typing_can_stop_by_chat_id(self):
telegram = self._telegram_client()
telegram._start_typing_task("chat-1", max_duration_seconds=1)
telegram._start_typing_task(
"chat-1",
max_duration_seconds=1,
initial_delay_seconds=0,
)
time.sleep(0.03)
self.assertIn("chat-1", Telegram._typing_tasks)
@@ -58,7 +62,11 @@ class TestTelegramTypingLifecycle(unittest.TestCase):
telegram = self._telegram_client()
Telegram._user_chat_mapping["10001"] = "chat-2"
telegram._start_typing_task("chat-2", max_duration_seconds=1)
telegram._start_typing_task(
"chat-2",
max_duration_seconds=1,
initial_delay_seconds=0,
)
time.sleep(0.03)
self.assertTrue(telegram.stop_typing(userid="10001"))
@@ -67,11 +75,32 @@ class TestTelegramTypingLifecycle(unittest.TestCase):
def test_typing_task_has_max_duration_guard(self):
telegram = self._telegram_client()
telegram._start_typing_task("chat-3", max_duration_seconds=0.02)
telegram._start_typing_task(
"chat-3",
max_duration_seconds=0.02,
initial_delay_seconds=0,
)
time.sleep(0.08)
self.assertNotIn("chat-3", Telegram._typing_tasks)
def test_short_typing_task_can_stop_before_first_chat_action(self):
"""
短响应在首次 typing 发出前结束时,不应留下客户端自然过期的残留状态。
"""
telegram = self._telegram_client()
telegram._start_typing_task(
"chat-4",
max_duration_seconds=1,
initial_delay_seconds=0.05,
)
telegram.stop_typing(chat_id="chat-4")
time.sleep(0.08)
telegram._bot.send_chat_action.assert_not_called()
self.assertNotIn("chat-4", Telegram._typing_tasks)
def test_agent_managed_send_msg_keeps_typing_for_worker_cleanup(self):
telegram = self._telegram_client()
sent = SimpleNamespace(message_id=1, chat=SimpleNamespace(id="chat-1"))