|
7 | 7 | StreamingClient, |
8 | 8 | StreamingClientOptions, |
9 | 9 | StreamingParameters, |
| 10 | + TurnEvent, |
10 | 11 | ) |
11 | 12 |
|
12 | 13 |
|
@@ -262,3 +263,92 @@ def mocked_websocket_connect( |
262 | 263 | assert "AssemblyAI/1.0" in actual_additional_headers["User-Agent"] |
263 | 264 |
|
264 | 265 | assert actual_open_timeout == 15 |
| 266 | + |
| 267 | + |
| 268 | +def test_client_connect_with_speaker_labels(mocker: MockFixture): |
| 269 | + actual_url = None |
| 270 | + |
| 271 | + def mocked_websocket_connect( |
| 272 | + url: str, additional_headers: dict, open_timeout: float |
| 273 | + ): |
| 274 | + nonlocal actual_url |
| 275 | + actual_url = url |
| 276 | + |
| 277 | + mocker.patch( |
| 278 | + "assemblyai.streaming.v3.client.websocket_connect", |
| 279 | + new=mocked_websocket_connect, |
| 280 | + ) |
| 281 | + |
| 282 | + _disable_rw_threads(mocker) |
| 283 | + |
| 284 | + options = StreamingClientOptions(api_key="test", api_host="api.example.com") |
| 285 | + client = StreamingClient(options) |
| 286 | + |
| 287 | + params = StreamingParameters( |
| 288 | + sample_rate=16000, |
| 289 | + speaker_labels=True, |
| 290 | + max_speakers=3, |
| 291 | + ) |
| 292 | + |
| 293 | + client.connect(params) |
| 294 | + |
| 295 | + assert "speaker_labels=True" in actual_url |
| 296 | + assert "max_speakers=3" in actual_url |
| 297 | + |
| 298 | + |
| 299 | +def test_client_connect_with_whisper_rt(mocker: MockFixture): |
| 300 | + actual_url = None |
| 301 | + |
| 302 | + def mocked_websocket_connect( |
| 303 | + url: str, additional_headers: dict, open_timeout: float |
| 304 | + ): |
| 305 | + nonlocal actual_url |
| 306 | + actual_url = url |
| 307 | + |
| 308 | + mocker.patch( |
| 309 | + "assemblyai.streaming.v3.client.websocket_connect", |
| 310 | + new=mocked_websocket_connect, |
| 311 | + ) |
| 312 | + |
| 313 | + _disable_rw_threads(mocker) |
| 314 | + |
| 315 | + options = StreamingClientOptions(api_key="test", api_host="api.example.com") |
| 316 | + client = StreamingClient(options) |
| 317 | + |
| 318 | + params = StreamingParameters( |
| 319 | + sample_rate=16000, |
| 320 | + speech_model=SpeechModel.whisper_rt, |
| 321 | + ) |
| 322 | + |
| 323 | + client.connect(params) |
| 324 | + |
| 325 | + assert "speech_model=whisper-rt" in actual_url |
| 326 | + |
| 327 | + |
| 328 | +def test_turn_event_with_speaker_label(): |
| 329 | + data = { |
| 330 | + "type": "Turn", |
| 331 | + "turn_order": 1, |
| 332 | + "turn_is_formatted": True, |
| 333 | + "end_of_turn": False, |
| 334 | + "transcript": "Hello world", |
| 335 | + "end_of_turn_confidence": 0.85, |
| 336 | + "words": [], |
| 337 | + "speaker_label": "B", |
| 338 | + } |
| 339 | + event = TurnEvent.parse_obj(data) |
| 340 | + assert event.speaker_label == "B" |
| 341 | + |
| 342 | + |
| 343 | +def test_turn_event_without_speaker_label(): |
| 344 | + data = { |
| 345 | + "type": "Turn", |
| 346 | + "turn_order": 1, |
| 347 | + "turn_is_formatted": True, |
| 348 | + "end_of_turn": False, |
| 349 | + "transcript": "Hello world", |
| 350 | + "end_of_turn_confidence": 0.85, |
| 351 | + "words": [], |
| 352 | + } |
| 353 | + event = TurnEvent.parse_obj(data) |
| 354 | + assert event.speaker_label is None |
0 commit comments