Files
zyt/app/tests/test_live_video_repository.py
2026-10-30 13:41:35 +08:00

83 lines
3.1 KiB
Python

from __future__ import annotations
import pytest
from doctor_workstation.core.errors import ApiProtocolError
from doctor_workstation.services.repository import RemoteDoctorRepository
class Client:
def __init__(self, events=None):
self.calls = []
self.events = events if events is not None else [{"event": "done", "data": {"diagnosis_id": 9, "live_context": {"revision": "a" * 64}}}]
def post_event_stream(self, endpoint, body, **kwargs):
self.calls.append((endpoint, body, kwargs))
yield from self.events
def invoke(client, **changes):
args = {"call_record_id": 71, "transcription_session_id": "tr-local", "request_id": "a" * 36}
args.update(changes)
return list(RemoteDoctorRepository(client).stream_video_ai(9, **args))
def test_scope_is_server_bound_and_structured_done_is_preserved():
client = Client()
result = invoke(client)
assert result[0]["live_context"]["revision"] == "a" * 64
endpoint, body, options = client.calls[0]
assert endpoint == "tcm.diagnosis/aiAssistantStream"
assert body["call_record_id"] == 71 and body["transcription_session_id"] == "tr-local"
assert body["task"] == "live_consultation"
assert "audio" not in body and "transcript" not in body
assert options["timeout"] == 105
@pytest.mark.parametrize("changes", [{"call_record_id": True}, {"call_record_id": 0},
{"transcription_session_id": ""}, {"transcription_session_id": "../other"},
{"task": "prescription_generate"}])
def test_bad_identity_or_task_never_sends(changes):
client = Client()
with pytest.raises(ValueError):
invoke(client, **changes)
assert client.calls == []
def test_early_disconnect_does_not_fallback_to_unbound_general_assistant():
client = Client([])
with pytest.raises(ApiProtocolError):
invoke(client)
assert len(client.calls) == 1
def test_cancelled_stream_does_not_publish_late_done():
client = Client()
assert invoke(client, cancelled=lambda: True) == []
def test_validated_progress_is_forwarded_before_done_without_ending_stream():
client = Client([
{"event": "live_update", "data": {"diagnosis_id": 9, "live_assistance": {"questions": []}}},
{"event": "done", "data": {"diagnosis_id": 9, "live_context": {"revision": "a" * 64}}},
])
events = invoke(client)
assert [event["event"] for event in events] == ["live_update", "done"]
assert events[0]["live_assistance"] == {"questions": []}
def test_progress_does_not_substitute_for_final_done():
client = Client([{"event": "live_update", "data": {"diagnosis_id": 9}}])
with pytest.raises(ApiProtocolError):
invoke(client)
def test_server_analysis_scope_is_preserved_in_stream_metadata():
client = Client([
{"event": kind, "data": {"analysis_scope": "current_call_only"}}
for kind in ("start", "live_update", "done")
])
events = invoke(client)
assert [event["analysis_scope"] for event in events] == ["current_call_only"] * 3
assert "analysis_scope" not in client.calls[0][1]