60 lines
2.7 KiB
Python
60 lines
2.7 KiB
Python
import asyncio
|
|
import io
|
|
import json
|
|
import time
|
|
import unittest
|
|
import wave
|
|
|
|
import httpx
|
|
|
|
from gateway import Dialogue, DEFAULT_VOICE_ID
|
|
from tests_fake import FakeModel
|
|
from test_gateway import Socket
|
|
|
|
|
|
class ContinuityTests(unittest.IsolatedAsyncioTestCase):
|
|
async def test_multidelta_one_tts_and_realtime_text(self):
|
|
socket=Socket();tts=[]
|
|
wav=io.BytesIO()
|
|
with wave.open(wav,'wb') as w:
|
|
w.setnchannels(1);w.setsampwidth(2);w.setframerate(48000);w.writeframes(bytes(1920))
|
|
parts=['你好,今天一起','去散步。','记得喝水!']
|
|
def backend(request):
|
|
if 'transcriptions' in request.url.path:return httpx.Response(200,json={'text':'公开问题'})
|
|
if 'chat' in request.url.path:
|
|
stream=''.join('data: '+json.dumps({'choices':[{'delta':{'content':x}}]},ensure_ascii=False)+'\n\n' for x in parts)+'data: [DONE]\n\n'
|
|
return httpx.Response(200,text=stream)
|
|
tts.append(json.loads(request.content))
|
|
self.assertEqual(len([e for e in socket.events if e['type']=='llm']),3)
|
|
return httpx.Response(200,content=wav.getvalue())
|
|
async with httpx.AsyncClient(transport=httpx.MockTransport(backend)) as client:
|
|
d=Dialogue(socket,FakeModel(),client)
|
|
await d.run(0,bytes(640))
|
|
self.assertEqual(len(tts),1)
|
|
self.assertEqual(tts[0]['input'],''.join(parts))
|
|
self.assertEqual(tts[0]['voice'],DEFAULT_VOICE_ID)
|
|
metrics=next(e['values'] for e in socket.events if e['type']=='metrics')
|
|
self.assertEqual(metrics['tts_request_count'],1)
|
|
self.assertEqual(metrics['voice_mode'],'continuous_voice')
|
|
self.assertIn('llm_finished_ms',metrics)
|
|
|
|
async def test_cancel_before_finish_never_starts_tts(self):
|
|
started=asyncio.Event();tts=[]
|
|
class Slow(httpx.AsyncByteStream):
|
|
async def __aiter__(self):
|
|
yield b'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
|
|
started.set()
|
|
await asyncio.sleep(10)
|
|
yield b'data: [DONE]\n\n'
|
|
def backend(request):
|
|
if 'chat' in request.url.path:return httpx.Response(200,stream=Slow())
|
|
tts.append(request);return httpx.Response(200)
|
|
async with httpx.AsyncClient(transport=httpx.MockTransport(backend)) as client:
|
|
d=Dialogue(Socket(),FakeModel(),client);q=asyncio.Queue(2);d.answers[0]=''
|
|
task=asyncio.create_task(d.generate(0,'question',q,{},time.perf_counter()))
|
|
await started.wait();d.task=task
|
|
await d.invalidate('client_abort')
|
|
await d.close()
|
|
self.assertTrue(q.empty())
|
|
self.assertEqual(tts,[])
|