|
@@ -1,13 +1,16 @@
|
|
|
"""Tests hors Discord : python -m unittest test_voice_stt."""
|
|
"""Tests hors Discord : python -m unittest test_voice_stt."""
|
|
|
import asyncio
|
|
import asyncio
|
|
|
|
|
+import ast
|
|
|
import time
|
|
import time
|
|
|
import unittest
|
|
import unittest
|
|
|
import wave
|
|
import wave
|
|
|
from io import BytesIO
|
|
from io import BytesIO
|
|
|
|
|
+from pathlib import Path
|
|
|
from types import SimpleNamespace
|
|
from types import SimpleNamespace
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
|
|
|
|
|
|
import httpx
|
|
import httpx
|
|
|
|
|
+import discord
|
|
|
from voice_stt import STTSink
|
|
from voice_stt import STTSink
|
|
|
|
|
|
|
|
|
|
|
|
@@ -87,5 +90,48 @@ class VoiceTests(unittest.IsolatedAsyncioTestCase):
|
|
|
self.reply.assert_not_awaited()
|
|
self.reply.assert_not_awaited()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class CommandTests(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
+ async def test_join_replies_in_command_channel_and_quit_closes_sink(self):
|
|
|
|
|
+ # Charger uniquement la fonction : aucun token ni connexion Discord.
|
|
|
|
|
+ tree = ast.parse(Path(__file__).with_name('chatbot.py').read_text(encoding='utf-8'))
|
|
|
|
|
+ function = next(node for node in tree.body if isinstance(node, ast.AsyncFunctionDef)
|
|
|
|
|
+ and node.name == 'handle_voice_command')
|
|
|
|
|
+ http = AsyncMock()
|
|
|
|
|
+ http.__aenter__.return_value.get.return_value = Mock()
|
|
|
|
|
+ sink = SimpleNamespace(close=AsyncMock())
|
|
|
|
|
+ make_sink = Mock(return_value=sink)
|
|
|
|
|
+ voice = SimpleNamespace(sink=sink, listen=Mock(), disconnect=AsyncMock())
|
|
|
|
|
+ channel = SimpleNamespace(connect=AsyncMock(return_value=voice))
|
|
|
|
|
+ voice.channel = channel
|
|
|
|
|
+ message = SimpleNamespace(
|
|
|
|
|
+ content='!join', guild=SimpleNamespace(voice_client=None),
|
|
|
|
|
+ author=SimpleNamespace(voice=SimpleNamespace(channel=channel)),
|
|
|
|
|
+ channel=SimpleNamespace(send=AsyncMock(), typing=AsyncMock()),
|
|
|
|
|
+ )
|
|
|
|
|
+ message.channel.typing = Mock(return_value=AsyncMock())
|
|
|
|
|
+ call_api = AsyncMock(return_value=SimpleNamespace(
|
|
|
|
|
+ choices=[SimpleNamespace(message=SimpleNamespace(content='Réponse'))]))
|
|
|
|
|
+ namespace = dict(
|
|
|
|
|
+ VOICE_ENABLED=True, STT_URL='http://stt/transcribe',
|
|
|
|
|
+ voice_command_lock=asyncio.Lock(), httpx=SimpleNamespace(AsyncClient=Mock(return_value=http)),
|
|
|
|
|
+ voice_recv=SimpleNamespace(VoiceRecvClient=object), STTSink=make_sink,
|
|
|
|
|
+ call_openai_api=call_api, transform_emote=lambda text, output: text,
|
|
|
|
|
+ filter_message=lambda text: text, split_message=lambda text: [text],
|
|
|
|
|
+ discord=discord, logger=Mock(),
|
|
|
|
|
+ )
|
|
|
|
|
+ exec(compile(ast.Module(body=[function], type_ignores=[]), '<commands>', 'exec'), namespace)
|
|
|
|
|
+ await namespace['handle_voice_command'](message)
|
|
|
|
|
+ voice.listen.assert_called_once_with(sink)
|
|
|
|
|
+ callback = make_sink.call_args.args[0]
|
|
|
|
|
+ await callback(SimpleNamespace(display_name='Alice'), 'Bonjour')
|
|
|
|
|
+ call_api.assert_awaited_once_with('Bonjour', 'Alice')
|
|
|
|
|
+ self.assertEqual(message.channel.send.call_args.args[0], 'Réponse')
|
|
|
|
|
+ message.content = '!quit'
|
|
|
|
|
+ message.guild.voice_client = voice
|
|
|
|
|
+ await namespace['handle_voice_command'](message)
|
|
|
|
|
+ sink.close.assert_awaited_once()
|
|
|
|
|
+ voice.disconnect.assert_awaited_once_with(force=True)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
|
unittest.main()
|
|
unittest.main()
|