|
| 1 | +"""Tests for SpeechData.confidence threading from Sarvam's language_probability. |
| 2 | +
|
| 3 | +Verifies that both the REST and WS paths thread ``language_probability`` from |
| 4 | +Sarvam's response into ``SpeechData.confidence`` (instead of the previous |
| 5 | +hardcoded ``1.0``), with a defensive fallback when the field is absent or has |
| 6 | +an unexpected type. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from typing import Any |
| 12 | +from unittest.mock import MagicMock |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from livekit.agents import stt |
| 17 | +from livekit.plugins.sarvam.stt import SpeechStream |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Helpers — build a minimal STT instance + fake the channel/logger/state that |
| 21 | +# `_handle_transcript_data` touches. We bypass __init__ so the test doesn't |
| 22 | +# need an API key, an HTTP session, or a real WebSocket. |
| 23 | +# --------------------------------------------------------------------------- |
| 24 | + |
| 25 | + |
| 26 | +def _make_stream_under_test() -> tuple[SpeechStream, list[Any]]: |
| 27 | + """Construct a minimal SpeechStream and collect its emitted events. |
| 28 | +
|
| 29 | + Returns ``(stream_instance, captured_events)`` where each event sent via |
| 30 | + ``send_nowait`` is appended to ``captured_events``. We bypass ``__init__`` |
| 31 | + so the test doesn't need an API key, an HTTP session, or a real WebSocket. |
| 32 | + """ |
| 33 | + instance = SpeechStream.__new__(SpeechStream) |
| 34 | + captured: list[Any] = [] |
| 35 | + event_ch = MagicMock() |
| 36 | + event_ch.send_nowait = captured.append |
| 37 | + instance._event_ch = event_ch # type: ignore[attr-defined] |
| 38 | + instance._logger = MagicMock() # type: ignore[attr-defined] |
| 39 | + instance._build_log_context = lambda: {} # type: ignore[attr-defined] |
| 40 | + instance._server_request_id = None # type: ignore[attr-defined] |
| 41 | + instance._opts = MagicMock(language="en-IN") # type: ignore[attr-defined] |
| 42 | + return instance, captured |
| 43 | + |
| 44 | + |
| 45 | +def _ws_message(**transcript_overrides: Any) -> dict: |
| 46 | + """Build the outer WS message dict expected by ``_handle_transcript_data``. |
| 47 | +
|
| 48 | + Default shape mirrors a Saaras v3 streaming final-transcript chunk. |
| 49 | + """ |
| 50 | + transcript_data: dict[str, Any] = { |
| 51 | + "transcript": "नमस्ते", |
| 52 | + "language_code": "hi-IN", |
| 53 | + "speech_start": 0.0, |
| 54 | + "speech_end": 1.2, |
| 55 | + "metrics": {"audio_duration": 1.2}, |
| 56 | + "request_id": "req-test", |
| 57 | + } |
| 58 | + transcript_data.update(transcript_overrides) |
| 59 | + return {"type": "data", "data": transcript_data} |
| 60 | + |
| 61 | + |
| 62 | +def _final_event(captured: list[Any]) -> stt.SpeechEvent: |
| 63 | + finals = [ev for ev in captured if ev.type == stt.SpeechEventType.FINAL_TRANSCRIPT] |
| 64 | + assert finals, "no FINAL_TRANSCRIPT event emitted" |
| 65 | + return finals[0] |
| 66 | + |
| 67 | + |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | +# WS path — happy cases |
| 70 | +# --------------------------------------------------------------------------- |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "language_probability, expected_confidence", |
| 75 | + [ |
| 76 | + (0.87, 0.87), |
| 77 | + (1.0, 1.0), |
| 78 | + (0.0, 0.0), |
| 79 | + (0.5, 0.5), |
| 80 | + (0.123, 0.123), |
| 81 | + ], |
| 82 | +) |
| 83 | +async def test_ws_threads_language_probability_into_confidence( |
| 84 | + language_probability: float, expected_confidence: float |
| 85 | +) -> None: |
| 86 | + """WS path must thread Sarvam's language_probability into SpeechData.confidence.""" |
| 87 | + instance, captured = _make_stream_under_test() |
| 88 | + await instance._handle_transcript_data(_ws_message(language_probability=language_probability)) |
| 89 | + final = _final_event(captured) |
| 90 | + assert final.alternatives[0].confidence == pytest.approx(expected_confidence) |
| 91 | + |
| 92 | + |
| 93 | +# --------------------------------------------------------------------------- |
| 94 | +# WS path — defensive fallback cases (absent / null / wrong type) |
| 95 | +# --------------------------------------------------------------------------- |
| 96 | + |
| 97 | + |
| 98 | +async def test_ws_missing_language_probability_falls_back_to_1_0() -> None: |
| 99 | + """When the field is absent, confidence falls back to 1.0 (no crash).""" |
| 100 | + instance, captured = _make_stream_under_test() |
| 101 | + # _ws_message() helper omits language_probability by default |
| 102 | + await instance._handle_transcript_data(_ws_message()) |
| 103 | + final = _final_event(captured) |
| 104 | + assert final.alternatives[0].confidence == 1.0 |
| 105 | + |
| 106 | + |
| 107 | +async def test_ws_null_language_probability_falls_back_to_1_0() -> None: |
| 108 | + """Explicit null also falls back to 1.0.""" |
| 109 | + instance, captured = _make_stream_under_test() |
| 110 | + await instance._handle_transcript_data(_ws_message(language_probability=None)) |
| 111 | + final = _final_event(captured) |
| 112 | + assert final.alternatives[0].confidence == 1.0 |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize("bad_value", ["0.95", [], {}, object(), True, False]) |
| 116 | +async def test_ws_unexpected_type_falls_back_to_1_0(bad_value: Any) -> None: |
| 117 | + """String / list / dict / object / bool → confidence falls back to 1.0 with a debug log. |
| 118 | +
|
| 119 | + bool is included because Python's ``bool`` is a subclass of ``int``; |
| 120 | + without an explicit guard a JSON ``false`` from Sarvam would silently |
| 121 | + become ``confidence=0.0`` and wrongly flag a valid transcript as low |
| 122 | + confidence. Same defensive pattern as ``livekit-plugins-slng``. |
| 123 | + """ |
| 124 | + instance, captured = _make_stream_under_test() |
| 125 | + await instance._handle_transcript_data(_ws_message(language_probability=bad_value)) |
| 126 | + final = _final_event(captured) |
| 127 | + assert final.alternatives[0].confidence == 1.0 |
| 128 | + # The defensive branch logs a debug warning so contract drift is visible. |
| 129 | + assert instance._logger.debug.called # type: ignore[attr-defined] |
| 130 | + |
| 131 | + |
| 132 | +# --------------------------------------------------------------------------- |
| 133 | +# WS path — out-of-range values pass through verbatim (clamping is not this |
| 134 | +# layer's job; downstream consumers can clamp if they need to). |
| 135 | +# --------------------------------------------------------------------------- |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize("value", [-0.5, 1.5, 2.0]) |
| 139 | +async def test_ws_out_of_range_values_passed_through(value: float) -> None: |
| 140 | + """Out-of-[0,1] values are passed through verbatim (no clamping).""" |
| 141 | + instance, captured = _make_stream_under_test() |
| 142 | + await instance._handle_transcript_data(_ws_message(language_probability=value)) |
| 143 | + final = _final_event(captured) |
| 144 | + assert final.alternatives[0].confidence == pytest.approx(value) |
0 commit comments