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_stt.py · 5.9 KB · 194 lines
| 1 | """Tests for speech-to-text: the object_stt module and the /api/stt surface.""" |
| 2 | |
| 3 | import io |
| 4 | import json |
| 5 | import urllib.error |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | import object_server |
| 10 | import object_stt |
| 11 | |
| 12 | from test_object_server import create_identity_session, enable_admin_token, raw_request, request |
| 13 | |
| 14 | |
| 15 | def stt_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.STT_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 | class _FakeHttpResponse: |
| 29 | def __init__(self, status, body): |
| 30 | self.status = status |
| 31 | self._body = body |
| 32 | |
| 33 | def read(self): |
| 34 | return self._body |
| 35 | |
| 36 | def __enter__(self): |
| 37 | return self |
| 38 | |
| 39 | def __exit__(self, *args): |
| 40 | return False |
| 41 | |
| 42 | |
| 43 | def test_stt_endpoint_requires_flag_and_session(tmp_path, monkeypatch): |
| 44 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 45 | enable_admin_token(monkeypatch) |
| 46 | |
| 47 | status, _, disabled = request( |
| 48 | "/api/stt", method="POST", body=b"fake-audio-bytes", |
| 49 | headers=[("content-type", "audio/webm")], |
| 50 | ) |
| 51 | assert status == 403 and "disabled" in disabled["error"] |
| 52 | |
| 53 | monkeypatch.setenv(object_server.STT_ENABLED_ENV, "true") |
| 54 | status, _, anonymous = request( |
| 55 | "/api/stt", method="POST", body=b"fake-audio-bytes", |
| 56 | headers=[("content-type", "audio/webm")], |
| 57 | ) |
| 58 | assert status == 401 |
| 59 | |
| 60 | |
| 61 | def test_stt_endpoint_rejects_empty_body(tmp_path, monkeypatch): |
| 62 | stt_env(tmp_path, monkeypatch) |
| 63 | bearer = signed_in_bearer() |
| 64 | |
| 65 | status, _, body = request( |
| 66 | "/api/stt", method="POST", body=b"", |
| 67 | headers=bearer + [("content-type", "audio/webm")], |
| 68 | ) |
| 69 | assert status == 400 |
| 70 | assert "audio" in body["error"] |
| 71 | |
| 72 | |
| 73 | def test_stt_endpoint_requires_stored_key(tmp_path, monkeypatch): |
| 74 | stt_env(tmp_path, monkeypatch) |
| 75 | bearer = signed_in_bearer() |
| 76 | |
| 77 | status, _, body = request( |
| 78 | "/api/stt", method="POST", body=b"fake-audio-bytes", |
| 79 | headers=bearer + [("content-type", "audio/webm")], |
| 80 | ) |
| 81 | assert status == 400 |
| 82 | assert "service-keys" in body["error"] |
| 83 | |
| 84 | |
| 85 | def test_stt_endpoint_transcribes_via_stored_key(tmp_path, monkeypatch): |
| 86 | stt_env(tmp_path, monkeypatch) |
| 87 | bearer = signed_in_bearer() |
| 88 | |
| 89 | request( |
| 90 | "/identity/users/dan/service-keys", |
| 91 | method="PUT", |
| 92 | body=json.dumps({"service": "openai", "key": "sk-test-openai"}).encode(), |
| 93 | headers=bearer + [("content-type", "application/json")], |
| 94 | ) |
| 95 | |
| 96 | calls = [] |
| 97 | |
| 98 | def fake_transport(request_obj, timeout=None): |
| 99 | calls.append(request_obj) |
| 100 | response_body = json.dumps({"text": "show me my notes"}).encode() |
| 101 | return _FakeHttpResponse(200, response_body) |
| 102 | |
| 103 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 104 | |
| 105 | status, _, body = request( |
| 106 | "/api/stt", |
| 107 | method="POST", |
| 108 | body=b"\x1aE\xdf\xa3fake-webm-bytes", |
| 109 | headers=bearer + [("content-type", "audio/webm")], |
| 110 | ) |
| 111 | assert status == 200 |
| 112 | assert body["text"] == "show me my notes" |
| 113 | assert len(calls) == 1 |
| 114 | assert calls[0].full_url == object_stt.OPENAI_TRANSCRIBE_URL |
| 115 | assert b"fake-webm-bytes" in calls[0].data |
| 116 | assert calls[0].get_header("Content-type", "").startswith("multipart/form-data") |
| 117 | |
| 118 | |
| 119 | def test_stt_endpoint_provider_error_maps_to_502(tmp_path, monkeypatch): |
| 120 | stt_env(tmp_path, monkeypatch) |
| 121 | bearer = signed_in_bearer() |
| 122 | |
| 123 | request( |
| 124 | "/identity/users/dan/service-keys", |
| 125 | method="PUT", |
| 126 | body=json.dumps({"service": "openai", "key": "sk-test-openai"}).encode(), |
| 127 | headers=bearer + [("content-type", "application/json")], |
| 128 | ) |
| 129 | |
| 130 | def fake_transport(request_obj, timeout=None): |
| 131 | error_body = json.dumps({"error": {"message": "Invalid API key"}}).encode() |
| 132 | raise urllib.error.HTTPError( |
| 133 | request_obj.full_url, 401, "unauthorized", {}, io.BytesIO(error_body) |
| 134 | ) |
| 135 | |
| 136 | monkeypatch.setattr(object_server.urllib.request, "urlopen", fake_transport) |
| 137 | |
| 138 | status, _, body = request( |
| 139 | "/api/stt", |
| 140 | method="POST", |
| 141 | body=b"\x1aE\xdf\xa3fake-webm-bytes", |
| 142 | headers=bearer + [("content-type", "audio/webm")], |
| 143 | ) |
| 144 | assert status == 502 |
| 145 | assert "Invalid API key" in body["error"] |
| 146 | |
| 147 | |
| 148 | def test_stt_endpoint_rejects_oversized_audio(tmp_path, monkeypatch): |
| 149 | stt_env(tmp_path, monkeypatch) |
| 150 | bearer = signed_in_bearer() |
| 151 | |
| 152 | request( |
| 153 | "/identity/users/dan/service-keys", |
| 154 | method="PUT", |
| 155 | body=json.dumps({"service": "openai", "key": "sk-test-openai"}).encode(), |
| 156 | headers=bearer + [("content-type", "application/json")], |
| 157 | ) |
| 158 | |
| 159 | status, _, body = request( |
| 160 | "/api/stt", |
| 161 | method="POST", |
| 162 | body=b"x" * (object_server.STT_MAX_BYTES + 1), |
| 163 | headers=bearer + [("content-type", "audio/webm")], |
| 164 | ) |
| 165 | assert status == 413 |
| 166 | |
| 167 | |
| 168 | def test_transcribe_cloud_multipart_body_shape(): |
| 169 | captured = {} |
| 170 | |
| 171 | def fake_send_http(url, headers, body): |
| 172 | captured["url"] = url |
| 173 | captured["headers"] = headers |
| 174 | captured["body"] = body |
| 175 | return 200, json.dumps({"text": "hello world"}).encode() |
| 176 | |
| 177 | text = object_stt.transcribe_cloud( |
| 178 | b"raw-audio-bytes", "audio/webm", "sk-test", send_http=fake_send_http |
| 179 | ) |
| 180 | assert text == "hello world" |
| 181 | assert captured["url"] == object_stt.OPENAI_TRANSCRIBE_URL |
| 182 | assert captured["headers"]["authorization"] == "Bearer sk-test" |
| 183 | assert b"raw-audio-bytes" in captured["body"] |
| 184 | assert b'filename="audio.webm"' in captured["body"] |
| 185 | |
| 186 | |
| 187 | def test_transcribe_cloud_raises_on_provider_error(): |
| 188 | def fake_send_http(url, headers, body): |
| 189 | return 400, json.dumps({"error": {"message": "bad audio"}}).encode() |
| 190 | |
| 191 | with pytest.raises(object_stt.STTProviderError, match="bad audio"): |
| 192 | object_stt.transcribe_cloud( |
| 193 | b"raw-audio-bytes", "audio/webm", "sk-test", send_http=fake_send_http |
| 194 | ) |