feat: transcribe Telegram voice messages via APInex
This commit is contained in:
1 parent
f6167bf2a4
commit
ea68cfba29
5 files changed
+191
-7
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import bot
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, status, payload):
|
||||
self.status = status
|
||||
self.payload = payload
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
async def json(self, content_type=None):
|
||||
return self.payload
|
||||
|
||||
|
||||
class FakeClientSession:
|
||||
def __init__(self, response, **kwargs):
|
||||
self.response = response
|
||||
self.kwargs = kwargs
|
||||
self.request: tuple[str, dict] = ("", {})
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
def post(self, url, **kwargs):
|
||||
self.request = (url, kwargs)
|
||||
return self.response
|
||||
|
||||
|
||||
class VoiceSttTests(unittest.IsolatedAsyncioTestCase):
|
||||
def test_extract_transcript(self):
|
||||
payload = {"results": {"channels": [{"alternatives": [{"transcript": " Привет, мир! "}]}]}}
|
||||
self.assertEqual(bot.extract_transcript(payload), "Привет, мир!")
|
||||
|
||||
def test_empty_response(self):
|
||||
self.assertEqual(bot.extract_transcript({"results": {"channels": []}}), "")
|
||||
self.assertEqual(bot.extract_transcript({}), "")
|
||||
|
||||
def test_split_long_transcript(self):
|
||||
chunks = bot.split_text("слово " * 2000, 4000)
|
||||
self.assertTrue(all(len(chunk) <= 4000 for chunk in chunks))
|
||||
self.assertEqual(" ".join(chunks).split(), ("слово " * 2000).split())
|
||||
|
||||
async def test_transcribe_sends_raw_ogg_and_parses_response(self):
|
||||
response = FakeResponse(200, {"results": {"channels": [{"alternatives": [{"transcript": "Распознано"}]}]}})
|
||||
fake_session = FakeClientSession(response)
|
||||
with patch.object(bot.aiohttp, "ClientSession", return_value=fake_session):
|
||||
result = await bot.transcribe_audio(b"ogg-data", "audio/ogg", "not-logged")
|
||||
self.assertEqual(result, "Распознано")
|
||||
url, kwargs = fake_session.request
|
||||
self.assertEqual(url, bot.APINEX_STT_URL)
|
||||
self.assertEqual(kwargs["data"], b"ogg-data")
|
||||
self.assertEqual(kwargs["headers"]["Content-Type"], "audio/ogg")
|
||||
self.assertEqual(kwargs["headers"]["Authorization"], "Bearer not-logged")
|
||||
|
||||
async def test_transcribe_raises_on_api_error(self):
|
||||
fake_session = FakeClientSession(FakeResponse(401, {}))
|
||||
with patch.object(bot.aiohttp, "ClientSession", return_value=fake_session):
|
||||
with self.assertRaises(RuntimeError):
|
||||
await bot.transcribe_audio(b"ogg-data", "audio/ogg", "not-logged")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user