|
| 1 | +# |
| 2 | +# Copyright (c) 2024-2026, Daily |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD 2-Clause License |
| 5 | +# |
| 6 | + |
| 7 | +import io |
| 8 | + |
| 9 | +import pytest |
| 10 | +from loguru import logger |
| 11 | + |
| 12 | +from pipecat.services.deepgram.stt import _derive_deepgram_urls |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "base_url, expected_ws, expected_http", |
| 17 | + [ |
| 18 | + # Secure schemes |
| 19 | + ("wss://mydeepgram.com", "wss://mydeepgram.com", "https://mydeepgram.com"), |
| 20 | + ("https://mydeepgram.com", "wss://mydeepgram.com", "https://mydeepgram.com"), |
| 21 | + # Insecure schemes (air-gapped deployments) |
| 22 | + ("ws://mydeepgram.com", "ws://mydeepgram.com", "http://mydeepgram.com"), |
| 23 | + ("http://mydeepgram.com", "ws://mydeepgram.com", "http://mydeepgram.com"), |
| 24 | + # Bare hostname defaults to secure |
| 25 | + ("mydeepgram.com", "wss://mydeepgram.com", "https://mydeepgram.com"), |
| 26 | + # With port |
| 27 | + ("ws://localhost:8080", "ws://localhost:8080", "http://localhost:8080"), |
| 28 | + ("wss://localhost:443", "wss://localhost:443", "https://localhost:443"), |
| 29 | + ("localhost:8080", "wss://localhost:8080", "https://localhost:8080"), |
| 30 | + # With path |
| 31 | + ("wss://host/v1/listen", "wss://host/v1/listen", "https://host/v1/listen"), |
| 32 | + ("http://host/v1/listen", "ws://host/v1/listen", "http://host/v1/listen"), |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_derive_deepgram_urls(base_url, expected_ws, expected_http): |
| 36 | + ws_url, http_url = _derive_deepgram_urls(base_url) |
| 37 | + assert ws_url == expected_ws |
| 38 | + assert http_url == expected_http |
| 39 | + |
| 40 | + |
| 41 | +def test_derive_deepgram_urls_unknown_scheme_warns(): |
| 42 | + sink = io.StringIO() |
| 43 | + handler_id = logger.add(sink, format="{message}") |
| 44 | + try: |
| 45 | + ws_url, http_url = _derive_deepgram_urls("ftp://mydeepgram.com") |
| 46 | + # Falls back to secure |
| 47 | + assert ws_url == "wss://mydeepgram.com" |
| 48 | + assert http_url == "https://mydeepgram.com" |
| 49 | + assert "Unrecognized scheme" in sink.getvalue() |
| 50 | + finally: |
| 51 | + logger.remove(handler_id) |
0 commit comments