"""Real PostgreSQL/API selection pins provider, replay and fresh-session switch.""" from uuid import uuid4 import pytest from sqlalchemy import text from tests.integration.test_phase2_postgres import api, seeded_env, login @pytest.mark.parametrize('api',['openai'],indirect=True) def test_provider_choice_persists_conflicts_and_switches_only_in_fresh_session(api,monkeypatch): client,wire,runner,e=api from app.main import app from app.api.v1.dependencies import get_voice_settings e.voice_config=e.voice_config.model_copy(update={'voice_selectable_providers':('gemini',)}) app.dependency_overrides[get_voice_settings]=lambda:e.voice_config headers=login(client,e) providers=client.get('/api/v1/voice/sessions/providers') assert providers.status_code==200 and providers.json()['connection_checked'] is False assert {r['id']:r['can_start'] for r in providers.json()['providers']}=={'openai':True,'local_s2s':False,'gemini':True} cid=client.post('/api/v1/conversations',headers=headers,json={'mode':'voice','client_request_id':str(uuid4())}).json()['id'] body=dict(conversation_id=cid,client_request_id=str(uuid4()),provider='gemini') first=client.post('/api/v1/voice/sessions',headers=headers,json=body) assert first.status_code==201 and first.json()['provider']=='gemini' sid=first.json()['session_id'] with e.engine.connect() as conn: saved=conn.execute(text('SELECT provider,execution_profile FROM live_sessions WHERE id=:id'),{'id':sid}).one() assert saved[0]==saved[1]['provider']=='gemini' assert client.post('/api/v1/voice/sessions',headers=headers,json=dict(body,provider='openai')).status_code==409 assert client.post('/api/v1/voice/sessions',headers=headers,json=dict(body,client_request_id=str(uuid4()),provider='openai')).status_code==409 # Replay remains pinned even when an operator removes the UI choice. e.voice_config=e.voice_config.model_copy(update={'voice_selectable_providers':()}) replay=client.post('/api/v1/voice/sessions',headers=headers,json=body) assert replay.status_code==202 and replay.json()['provider']=='gemini' e.voice_config=e.voice_config.model_copy(update={'voice_selectable_providers':('gemini',)}) async def connect_selected(*_): e.connections.append('gemini') return wire monkeypatch.setattr('app.infrastructure.voice_provider.connect_gemini',connect_selected) with client.websocket_connect('ws://localhost:8000'+first.json()['stream_path'],headers={'Origin':'http://localhost:8000'}) as ws: client.portal.call(wire.inbound.put,{'setupComplete':{}}) assert ws.receive_json()['type']=='session_ready' assert any('setup' in message for message in wire.sent) assert client.post('/api/v1/voice/sessions/'+sid+'/close',headers=headers,json={}).status_code==200 fresh=client.post('/api/v1/voice/sessions',headers=headers,json=dict(body,client_request_id=str(uuid4()),provider='openai')) assert fresh.status_code==201 and fresh.json()['provider']=='openai' assert fresh.json()['session_id']!=sid and e.connections==['gemini'] def test_provider_discovery_requires_authentication(api): client,*_=api assert client.get('/api/v1/voice/sessions/providers').status_code==401 @pytest.mark.parametrize('api',['openai'],indirect=True) def test_competing_explicit_providers_cannot_reinterpret_one_request(api): from concurrent.futures import ThreadPoolExecutor from app.main import app from app.api.v1.dependencies import get_voice_settings client,wire,runner,e=api e.voice_config=e.voice_config.model_copy(update={'voice_selectable_providers':('gemini',)}) app.dependency_overrides[get_voice_settings]=lambda:e.voice_config headers=login(client,e) cid=client.post('/api/v1/conversations',headers=headers,json={'mode':'voice','client_request_id':str(uuid4())}).json()['id'] request=str(uuid4()) def create(provider): return client.post('/api/v1/voice/sessions',headers=headers,json=dict(conversation_id=cid,client_request_id=request,provider=provider)) with ThreadPoolExecutor(max_workers=2) as pool: results=list(pool.map(create,['openai','gemini'])) assert sorted(r.status_code for r in results)==[201,409] winner=next(r.json() for r in results if r.status_code==201) with e.engine.connect() as conn: rows=conn.execute(text('SELECT provider FROM live_sessions WHERE conversation_id=:id'),{'id':cid}).scalars().all() assert rows==[winner['provider']] and not e.connections