askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_tts.py · 7.1 KB · 246 lines
| 1 | """Tests for text-to-speech: the object_tts module and the /api/tts surface.""" |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import object_server |
| 8 | import object_tts |
| 9 | |
| 10 | from test_object_server import create_identity_session, enable_admin_token, raw_request, request |
| 11 | |
| 12 | WAV_MAGIC = b"RIFF\x24\x00\x00\x00WAVEfmt \x00\x00\x00\x00" |
| 13 | |
| 14 | |
| 15 | def tts_env(tmp_path, monkeypatch): |
| 16 | data_dir = tmp_path / "data" |
| 17 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 18 | monkeypatch.setenv(object_server.TTS_ENABLED_ENV, "true") |
| 19 | enable_admin_token(monkeypatch) |
| 20 | return data_dir |
| 21 | |
| 22 | |
| 23 | def signed_in_bearer(): |
| 24 | token, _ = create_identity_session({"user_id": "dan"}) |
| 25 | return [("authorization", f"Bearer {token}")] |
| 26 | |
| 27 | |
| 28 | def test_tts_endpoint_requires_flag_and_session(tmp_path, monkeypatch): |
| 29 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 30 | enable_admin_token(monkeypatch) |
| 31 | |
| 32 | status, _, disabled = request( |
| 33 | "/api/tts", method="POST", body=json.dumps({"text": "hi"}).encode() |
| 34 | ) |
| 35 | assert status == 403 and "disabled" in disabled["error"] |
| 36 | |
| 37 | monkeypatch.setenv(object_server.TTS_ENABLED_ENV, "true") |
| 38 | status, _, anonymous = request( |
| 39 | "/api/tts", method="POST", body=json.dumps({"text": "hi"}).encode() |
| 40 | ) |
| 41 | assert status == 401 |
| 42 | |
| 43 | |
| 44 | def test_tts_engine_missing_returns_503(tmp_path, monkeypatch): |
| 45 | tts_env(tmp_path, monkeypatch) |
| 46 | bearer = signed_in_bearer() |
| 47 | monkeypatch.setattr(object_tts.shutil, "which", lambda name: None) |
| 48 | |
| 49 | status, _, body = request( |
| 50 | "/api/tts", |
| 51 | method="POST", |
| 52 | body=json.dumps({"text": "hello there"}).encode(), |
| 53 | headers=bearer, |
| 54 | ) |
| 55 | assert status == 503 |
| 56 | assert "engine" in body["error"].lower() |
| 57 | |
| 58 | |
| 59 | def test_tts_text_cap_413(tmp_path, monkeypatch): |
| 60 | tts_env(tmp_path, monkeypatch) |
| 61 | bearer = signed_in_bearer() |
| 62 | |
| 63 | status, _, body = request( |
| 64 | "/api/tts", |
| 65 | method="POST", |
| 66 | body=json.dumps({"text": "x" * (object_server.TTS_MAX_CHARS + 1)}).encode(), |
| 67 | headers=bearer, |
| 68 | ) |
| 69 | assert status == 413 |
| 70 | assert "800" in body["error"] |
| 71 | |
| 72 | |
| 73 | def test_tts_happy_path_caches_and_skips_subprocess_on_repeat(tmp_path, monkeypatch): |
| 74 | data_dir = tts_env(tmp_path, monkeypatch) |
| 75 | bearer = signed_in_bearer() |
| 76 | |
| 77 | monkeypatch.setattr( |
| 78 | object_tts.shutil, "which", lambda name: f"/usr/bin/{name}" if name == "espeak-ng" else None |
| 79 | ) |
| 80 | calls = [] |
| 81 | |
| 82 | def fake_run(args, capture_output=None, timeout=None, check=None): |
| 83 | calls.append(args) |
| 84 | |
| 85 | class FakeResult: |
| 86 | returncode = 0 |
| 87 | stdout = WAV_MAGIC |
| 88 | stderr = b"" |
| 89 | |
| 90 | return FakeResult() |
| 91 | |
| 92 | monkeypatch.setattr(object_tts.subprocess, "run", fake_run) |
| 93 | |
| 94 | status, headers, payload = raw_request( |
| 95 | "/api/tts", |
| 96 | method="POST", |
| 97 | body=json.dumps({"text": "one note matches"}).encode(), |
| 98 | headers=bearer, |
| 99 | ) |
| 100 | assert status == 200 |
| 101 | assert headers[b"content-type"] == b"audio/wav" |
| 102 | assert payload == WAV_MAGIC |
| 103 | assert len(calls) == 1 |
| 104 | |
| 105 | cache_file = object_tts.cache_path( |
| 106 | "espeak-ng", None, "one note matches", base_dir=data_dir |
| 107 | ) |
| 108 | assert cache_file.is_file() |
| 109 | assert cache_file.read_bytes() == WAV_MAGIC |
| 110 | |
| 111 | # Second identical request is served from cache: no second subprocess call. |
| 112 | status, headers, payload = raw_request( |
| 113 | "/api/tts", |
| 114 | method="POST", |
| 115 | body=json.dumps({"text": "one note matches"}).encode(), |
| 116 | headers=bearer, |
| 117 | ) |
| 118 | assert status == 200 |
| 119 | assert headers[b"content-type"] == b"audio/wav" |
| 120 | assert payload == WAV_MAGIC |
| 121 | assert len(calls) == 1 |
| 122 | |
| 123 | |
| 124 | def test_tts_engine_openai_requires_stored_key(tmp_path, monkeypatch): |
| 125 | tts_env(tmp_path, monkeypatch) |
| 126 | bearer = signed_in_bearer() |
| 127 | |
| 128 | status, _, body = request( |
| 129 | "/api/tts", |
| 130 | method="POST", |
| 131 | body=json.dumps({"text": "hello", "engine": "openai"}).encode(), |
| 132 | headers=bearer, |
| 133 | ) |
| 134 | assert status == 400 |
| 135 | assert "service-keys" in body["error"] |
| 136 | |
| 137 | |
| 138 | def test_tts_engine_invalid_value_400(tmp_path, monkeypatch): |
| 139 | tts_env(tmp_path, monkeypatch) |
| 140 | bearer = signed_in_bearer() |
| 141 | |
| 142 | status, _, body = request( |
| 143 | "/api/tts", |
| 144 | method="POST", |
| 145 | body=json.dumps({"text": "hello", "engine": "elevenlabs"}).encode(), |
| 146 | headers=bearer, |
| 147 | ) |
| 148 | assert status == 400 |
| 149 | assert "engine" in body["error"] |
| 150 | |
| 151 | |
| 152 | class _FakeHttpResponse: |
| 153 | def __init__(self, status, body): |
| 154 | self.status = status |
| 155 | self._body = body |
| 156 | |
| 157 | def read(self): |
| 158 | return self._body |
| 159 | |
| 160 | def __enter__(self): |
| 161 | return self |
| 162 | |
| 163 | def __exit__(self, *args): |
| 164 | return False |
| 165 | |
| 166 | |
| 167 | def test_tts_engine_openai_calls_cloud_and_caches(tmp_path, monkeypatch): |
| 168 | data_dir = tts_env(tmp_path, monkeypatch) |
| 169 | bearer = signed_in_bearer() |
| 170 | |
| 171 | request( |
| 172 | "/identity/users/dan/service-keys", |
| 173 | method="PUT", |
| 174 | body=json.dumps({"service": "openai", "key": "sk-test-openai"}).encode(), |
| 175 | headers=bearer + [("content-type", "application/json")], |
| 176 | ) |
| 177 | |
| 178 | mp3_bytes = b"\xff\xfb\x90\x00fake-mp3-bytes" |
| 179 | calls = [] |
| 180 | |
| 181 | def fake_transport(request_obj, timeout=None): |
| 182 | calls.append(request_obj) |
| 183 | return _FakeHttpResponse(200, mp3_bytes) |
| 184 | |
| 185 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 186 | |
| 187 | status, headers, payload = raw_request( |
| 188 | "/api/tts", |
| 189 | method="POST", |
| 190 | body=json.dumps({"text": "cloud voice test", "engine": "openai"}).encode(), |
| 191 | headers=bearer, |
| 192 | ) |
| 193 | assert status == 200 |
| 194 | assert headers[b"content-type"] == b"audio/mpeg" |
| 195 | assert payload == mp3_bytes |
| 196 | assert len(calls) == 1 |
| 197 | assert calls[0].full_url == object_tts.OPENAI_TTS_URL |
| 198 | |
| 199 | cache_file = object_tts.cache_path( |
| 200 | "openai", None, "cloud voice test", base_dir=data_dir, model=object_tts.DEFAULT_CLOUD_MODEL |
| 201 | ) |
| 202 | assert cache_file.is_file() |
| 203 | assert cache_file.read_bytes() == mp3_bytes |
| 204 | |
| 205 | # Second identical request is served from cache: no second provider call. |
| 206 | status, headers, payload = raw_request( |
| 207 | "/api/tts", |
| 208 | method="POST", |
| 209 | body=json.dumps({"text": "cloud voice test", "engine": "openai"}).encode(), |
| 210 | headers=bearer, |
| 211 | ) |
| 212 | assert status == 200 |
| 213 | assert payload == mp3_bytes |
| 214 | assert len(calls) == 1 |
| 215 | |
| 216 | |
| 217 | def test_tts_engine_openai_provider_error_maps_to_502(tmp_path, monkeypatch): |
| 218 | tts_env(tmp_path, monkeypatch) |
| 219 | bearer = signed_in_bearer() |
| 220 | |
| 221 | request( |
| 222 | "/identity/users/dan/service-keys", |
| 223 | method="PUT", |
| 224 | body=json.dumps({"service": "openai", "key": "sk-test-openai"}).encode(), |
| 225 | headers=bearer + [("content-type", "application/json")], |
| 226 | ) |
| 227 | |
| 228 | import io |
| 229 | import urllib.error |
| 230 | |
| 231 | def fake_transport(request_obj, timeout=None): |
| 232 | body = json.dumps({"error": {"message": "Invalid API key"}}).encode() |
| 233 | raise urllib.error.HTTPError( |
| 234 | request_obj.full_url, 401, "unauthorized", {}, io.BytesIO(body) |
| 235 | ) |
| 236 | |
| 237 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 238 | |
| 239 | status, _, body = request( |
| 240 | "/api/tts", |
| 241 | method="POST", |
| 242 | body=json.dumps({"text": "hello", "engine": "openai"}).encode(), |
| 243 | headers=bearer, |
| 244 | ) |
| 245 | assert status == 502 |
| 246 | assert "Invalid API key" in body["error"] |