Files
whisper-remote/cli/tests/test_main.py
Luna 1c6415d306
Some checks failed
CI / Backend (pull_request) Failing after 15s
CI / CLI (pull_request) Successful in 28s
Handle CLI request errors and backend whisper timeout
2026-05-27 13:08:56 +00:00

64 lines
2.5 KiB
Python

import os
from argparse import Namespace
from pathlib import Path
import sys
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
import main
import httpx
def test_resolve_server_from_env(monkeypatch) -> None:
monkeypatch.setenv("WHISPER_REMOTE", "http://localhost:8000/")
assert main.resolve_server(Namespace(server=None)) == "http://localhost:8000"
def test_resolve_server_requires_value(monkeypatch) -> None:
monkeypatch.delenv("WHISPER_REMOTE", raising=False)
with pytest.raises(SystemExit):
main.resolve_server(Namespace(server=None))
def test_infer_output_path_for_directory(tmp_path: Path) -> None:
destination = main.infer_output_path(tmp_path, Path("clip.wav"), "srt")
assert destination == tmp_path / "clip.srt"
def test_infer_output_path_for_explicit_file(tmp_path: Path) -> None:
destination = main.infer_output_path(tmp_path / "custom-name.txt", Path("clip.wav"), "txt")
assert destination == tmp_path / "custom-name.txt"
def test_format_http_error_with_body() -> None:
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
response = httpx.Response(500, text="Internal Server Error", request=request)
message = main.format_http_error(response, "http://localhost:8000/transcriptions")
assert message == "HTTP 500 from http://localhost:8000/transcriptions: Internal Server Error"
def test_format_http_error_with_empty_body() -> None:
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
response = httpx.Response(500, text="", request=request)
message = main.format_http_error(response, "http://localhost:8000/transcriptions")
assert message == "HTTP 500 from http://localhost:8000/transcriptions: <empty response body>"
def test_format_request_error_timeout() -> None:
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
exc = httpx.ReadTimeout("read timed out", request=request)
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
assert message == "Request to http://localhost:8000/transcriptions timed out."
def test_format_request_error_network_failure() -> None:
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
exc = httpx.ConnectError("connection refused", request=request)
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
assert (
message
== "Request to http://localhost:8000/transcriptions failed: connection refused"
)