"""Caption bursts cannot starve PCM; every frame keeps its authority fence.""" import asyncio from types import SimpleNamespace import pytest from tests.unit.test_realtime_phase1 import runner, persisted, make, input_turn, response, delta, until, done def test_long_input_transcript_burst_preserves_text_and_control_order(runner, persisted): async def scenario(): runtime, _, _ = await make(SimpleNamespace(engine=None), runner, persisted) entered, release = asyncio.Event(), asyncio.Event() output = [] async def sink(event): if isinstance(event, dict) and event.get('type') == 'transcript' and not entered.is_set(): entered.set() await release.wait() output.append(event) async def asr(text, completed=False): event = dict(type='conversation.item.input_audio_transcription.' + ('completed' if completed else 'delta'), item_id='u1', content_index=0) event['transcript' if completed else 'delta'] = text await runtime.handle(event) try: turn = await input_turn(runtime) await until(lambda: not runtime.controls and runtime.active_control is None) runtime.emit = sink await asr('اول ') await entered.wait() parts = [f'بخش {i}، ' for i in range(200)] for part in parts[:100]: await asr(part) runtime.control(dict(type='error', turn_ref=turn.ref, error=dict( code='LIVE_TRANSCRIPTION_FAILED', message='test', retryable=False))) for part in parts[100:]: await asr(part) text = 'اول ' + ''.join(parts) await asr(text, completed=True) assert runtime.usable assert runtime.control_count <= 5 release.set() await until(lambda: any(isinstance(e, dict) and e.get('status') == 'completed' for e in output)) transcripts = [e for e in output if isinstance(e, dict) and e.get('type') == 'transcript'] assert ''.join(e['text'] for e in transcripts if e['status'] == 'partial') == text assert transcripts[-1]['text'] == text relevant = [e for e in output if isinstance(e, dict) and e.get('type') in ('transcript', 'error')] assert [e['type'] for e in relevant] == ['transcript', 'transcript', 'error', 'transcript', 'transcript'] assert all(e['turn_ref'] == turn.ref for e in relevant) await until(lambda: runtime.control_count == 0) assert runtime.control_bytes == 0 finally: release.set() await runtime.close() asyncio.run(scenario()) @pytest.mark.parametrize('revoked', [False, True]) def test_caption_burst_interleaves_audio_and_preserves_revocation(runner, persisted, revoked): async def scenario(): runtime, transport, _ = await make(SimpleNamespace(engine=None), runner, persisted) output = [] entered, release = asyncio.Event(), asyncio.Event() async def sink(event): if isinstance(event, dict) and event.get('type') == 'transcript' and not entered.is_set(): entered.set() await release.wait() output.append(event) try: await input_turn(runtime) reply = await response(runtime, transport) await until(lambda: not runtime.controls and runtime.active_control is None) runtime.emit = sink def caption(text, completed=False): runtime.assistant_transcript(reply, dict(type='transcript', turn_ref=reply.turn.ref, role='assistant', text=text), completed=completed) caption('first ') await entered.wait() parts = [f'{i} ' for i in range(40)] async with runtime.lock: for part in parts: caption(part) assert len(runtime.controls) == 1 assert runtime.controls[0].event['text'] == ''.join(parts) await runtime.handle(delta(19200)) if revoked: persisted['enabled'] = False release.set() if revoked: await until(lambda: not runtime.usable) assert runtime.failure == 'CONTEXT_REVOKED' assert not any(isinstance(e, bytes) for e in output) else: await until(lambda: sum(len(e) for e in output if isinstance(e, bytes)) == 19200) first_audio = next(i for i,e in enumerate(output) if isinstance(e, bytes)) queued_caption = next(i for i,e in enumerate(output) if isinstance(e, dict) and e.get('type') == 'transcript' and e['text'] == ''.join(parts)) started = next(i for i,e in enumerate(output) if isinstance(e, dict) and e['type'] == 'audio_started') assert started < first_audio < queued_caption text = 'first ' + ''.join(parts) caption(text, completed=True) await done(runtime) await until(lambda: any(isinstance(e,dict) and e['type']=='transcript' and e['status']=='completed' for e in output)) transcripts = [e for e in output if isinstance(e,dict) and e['type']=='transcript'] assert ''.join(e['text'] for e in transcripts if e['status']=='partial') == text assert transcripts[-1]['text'] == text assert runtime.usable finally: release.set() await runtime.close() asyncio.run(scenario())