fix: fence voice turns and playback; add opt-in face context
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
"""No live models/sockets: actual Dialogue + HTTP doubles, including drain/finalization race."""
|
||||
import asyncio
|
||||
import io
|
||||
import json
|
||||
import time
|
||||
import unittest
|
||||
import wave
|
||||
import httpx
|
||||
from gateway import Dialogue
|
||||
from tests_fake import FakeModel
|
||||
from test_gateway import Socket
|
||||
from test_face_context import cue
|
||||
|
||||
|
||||
def wav(pcm):
|
||||
out = io.BytesIO()
|
||||
with wave.open(out, 'wb') as file:
|
||||
file.setnchannels(1); file.setsampwidth(2); file.setframerate(48000); file.writeframes(pcm)
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
class ReplyFaceTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def round(self, observation=False, empty_llm=False, empty_tts=False, empty_stt=False):
|
||||
messages = []
|
||||
def backend(request):
|
||||
if 'transcriptions' in request.url.path:
|
||||
return httpx.Response(200, json={'text': '' if empty_stt else '公开合成测试'})
|
||||
if 'chat' in request.url.path:
|
||||
messages.append(json.loads(request.content)['messages'])
|
||||
# The actual deployed Qwen template accepts only one initial system.
|
||||
self.assertEqual(sum(m['role']=='system' for m in messages[-1]), 1)
|
||||
body = '' if empty_llm else '测试回答'
|
||||
return httpx.Response(200, text='data: '+json.dumps({'choices':[{'delta':{'content':body}}]})+'\n\ndata: [DONE]\n\n')
|
||||
return httpx.Response(200, content=wav(b'' if empty_tts else b'\x10\x01'*960))
|
||||
socket = Socket()
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(backend)) as client:
|
||||
d = Dialogue(socket, FakeModel(), client)
|
||||
if observation:
|
||||
self.assertTrue(d.face_context.update(cue(turn_id=0), 0))
|
||||
await d.invalidate('new_turn')
|
||||
await d.run(d.turn, bytes(640))
|
||||
return d, socket, messages
|
||||
|
||||
async def test_optional_ephemeral_message(self):
|
||||
d, socket, messages = await self.round(observation=True)
|
||||
self.assertEqual([m['role'] for m in messages[0]], ['system','user'])
|
||||
self.assertIn('摄像头观察', messages[0][0]['content'])
|
||||
self.assertEqual(d.face_messages, {})
|
||||
self.assertIsNone(d.face_context.value)
|
||||
self.assertTrue(socket.frames)
|
||||
d, socket, messages = await self.round(observation=False)
|
||||
self.assertEqual([m['role'] for m in messages[0]], ['system','user'])
|
||||
self.assertNotIn('摄像头观察', messages[0][0]['content'])
|
||||
|
||||
async def test_expired_and_barge_clear(self):
|
||||
d = Dialogue(Socket(), FakeModel(), None)
|
||||
d.face_context.update(cue(turn_id=0),0,now=time.monotonic()-4)
|
||||
await d.invalidate('new_turn')
|
||||
self.assertIsNone(d.face_context.take(d.turn))
|
||||
for reason in ('listen_start','voice_barge_in','client_abort','history_new'):
|
||||
d.face_context.update(cue(turn_id=d.turn),d.turn)
|
||||
await d.invalidate(reason)
|
||||
self.assertIsNone(d.face_context.value)
|
||||
|
||||
async def test_empty_body_or_pcm_explicit_error(self):
|
||||
for options in ({'empty_llm':True}, {'empty_tts':True}):
|
||||
d, socket, _ = await self.round(**options)
|
||||
self.assertTrue(any(e['type']=='error' and e['code']=='backend_failed' for e in socket.events))
|
||||
self.assertFalse(socket.frames)
|
||||
|
||||
async def test_empty_stt_recovers_listening(self):
|
||||
d, socket, messages = await self.round(empty_stt=True)
|
||||
self.assertFalse(messages)
|
||||
self.assertTrue(any(e['type']=='listen' and e['state']=='resumed' for e in socket.events))
|
||||
|
||||
async def test_native_drain_during_history_finalize_not_lost(self):
|
||||
entered, finish = asyncio.Event(), asyncio.Event()
|
||||
class History:
|
||||
async def begin(self,*args): pass
|
||||
async def context(self,*args): return []
|
||||
async def rows(self,*args): return []
|
||||
async def finish(self,*args): entered.set(); await finish.wait()
|
||||
async def drained(self,*args): pass
|
||||
def backend(request):
|
||||
if 'transcriptions' in request.url.path: return httpx.Response(200,json={'text':'公开测试'})
|
||||
if 'chat' in request.url.path: return httpx.Response(200,text='data: {"choices":[{"delta":{"content":"回答"}}]}\n\ndata: [DONE]\n\n')
|
||||
return httpx.Response(200,content=wav(b'\x10\x01'*960))
|
||||
socket = Socket()
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(backend)) as client:
|
||||
d=Dialogue(socket,FakeModel(),client,History(),'synthetic-conversation')
|
||||
d.run_id='synthetic-run'
|
||||
d.task=asyncio.create_task(d.run(0,bytes(640)))
|
||||
await asyncio.wait_for(entered.wait(),2)
|
||||
self.assertTrue(any(e['type']=='tts' and e.get('state')=='stop' for e in socket.events))
|
||||
await d.request_drained(0)
|
||||
self.assertEqual(d.pending_drained,0)
|
||||
finish.set()
|
||||
await asyncio.wait_for(d.task,2)
|
||||
self.assertFalse(d.playback_waiting)
|
||||
self.assertEqual(sum(e['type']=='listen' and e['state']=='resumed' for e in socket.events),1)
|
||||
await d.request_drained(0) # duplicate must not send another ack
|
||||
self.assertEqual(sum(e['type']=='listen' and e['state']=='resumed' for e in socket.events),1)
|
||||
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
Reference in New Issue
Block a user