Spaces:
Running
Running
File size: 8,668 Bytes
8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab c046b21 8bcdcab 06b738c c046b21 06b738c 8bcdcab c046b21 8bcdcab 06b738c c046b21 08942e6 c046b21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | """Tests for /api/analyze endpoint."""
from __future__ import annotations
import io
import struct
import math
from fastapi.testclient import TestClient
def test_analyze_missing_source_type(
client: TestClient,
) -> None:
"""Reject request without sourceType."""
response = client.post("/api/analyze")
assert response.status_code == 422
def test_analyze_invalid_source_type(
client: TestClient,
) -> None:
"""Return error for unknown sourceType."""
response = client.post(
"/api/analyze",
data={"sourceType": "unknown"},
)
assert response.status_code == 200
data = response.json()
assert "invalid_source_type" in data["errors"]
assert data["result"] is None
def test_analyze_unsupported_source(
client: TestClient,
) -> None:
"""Return unsupported_source for spotify/apple."""
for source in ("spotify", "apple"):
response = client.post(
"/api/analyze",
data={"sourceType": source},
)
assert response.status_code == 200
data = response.json()
assert "unsupported_source" in data["errors"]
def test_analyze_youtube_missing_url(
client: TestClient,
) -> None:
"""YouTube without url returns missing_url."""
response = client.post(
"/api/analyze",
data={"sourceType": "youtube"},
)
assert response.status_code == 200
data = response.json()
assert "missing_url" in data["errors"]
def test_analyze_file_missing_file(
client: TestClient,
) -> None:
"""File without upload returns missing_file."""
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
)
assert response.status_code == 200
data = response.json()
assert "missing_file" in data["errors"]
def test_analyze_file_invalid_type(
client: TestClient,
) -> None:
"""Reject non-audio file."""
fake_file = io.BytesIO(b"not audio content " * 100)
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
files={
"file": ("test.txt", fake_file, "text/plain"),
},
)
assert response.status_code == 200
data = response.json()
assert "invalid_file_type" in data["errors"]
def test_analyze_file_too_small(
client: TestClient,
) -> None:
"""Reject files smaller than 1KB."""
tiny_file = io.BytesIO(b"\x00" * 100)
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
files={
"file": ("tiny.wav", tiny_file, "audio/wav"),
},
)
assert response.status_code == 200
data = response.json()
assert "file_too_small" in data["errors"]
def test_analyze_file_bad_audio_is_an_error(
client: TestClient,
) -> None:
"""Undecodable audio returns an error, never an invented result."""
audio_content = b"\x00" * 2048
fake_audio = io.BytesIO(audio_content)
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
files={
"file": ("test.wav", fake_audio, "audio/wav"),
},
)
assert response.status_code == 200
data = response.json()
assert data["result"] is None
assert data["errors"][0] in {"audio_decode_failed", "audio_silent", "audio_too_short"}
def test_analyze_accepts_audio_extension_with_video_mime(
client: TestClient,
) -> None:
"""A .webm labelled video/webm by the browser is audio, not an invalid type."""
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
files={
"file": ("take.webm", io.BytesIO(b"\x1a" * 100), "video/webm"),
},
)
assert response.status_code == 200
data = response.json()
assert data["errors"] == ["file_too_small"]
def test_jobs_rejects_invalid_source_immediately(
client: TestClient,
) -> None:
"""Validation errors come back at once, without starting a job."""
response = client.post(
"/api/analyze/jobs",
data={"sourceType": "unknown"},
)
assert response.status_code == 200
data = response.json()
assert data["jobId"] is None
assert data["status"] == "error"
assert "invalid_source_type" in data["response"]["errors"]
def test_jobs_missing_url(
client: TestClient,
) -> None:
response = client.post(
"/api/analyze/jobs",
data={"sourceType": "youtube"},
)
assert response.status_code == 200
assert "missing_url" in response.json()["response"]["errors"]
def test_jobs_unknown_id_is_404(
client: TestClient,
) -> None:
response = client.get("/api/analyze/jobs/does-not-exist")
assert response.status_code == 404
assert response.json()["detail"]["code"] == "job_not_found"
def test_rate_limit_key_uses_forwarded_client() -> None:
"""Behind the Space proxy each visitor gets their own bucket."""
from starlette.requests import Request
from app.routes.analyze import _client_ip
def make(headers):
return Request({
"type": "http",
"headers": [(k.encode(), v.encode()) for k, v in headers.items()],
"client": ("10.0.0.1", 1234),
})
assert _client_ip(make({"x-forwarded-for": "203.0.113.7, 10.0.0.1"})) == "203.0.113.7"
assert _client_ip(make({})) == "10.0.0.1"
def _make_wav(
duration_sec: float = 3.0,
freq_hz: float = 440.0,
sample_rate: int = 22050,
) -> bytes:
"""
Generate a minimal valid WAV file with a sine tone.
Returns raw bytes of a valid WAV.
"""
n_samples = int(sample_rate * duration_sec)
samples = []
for i in range(n_samples):
t = i / sample_rate
val = math.sin(2 * math.pi * freq_hz * t)
samples.append(int(val * 32767))
# WAV header (16-bit mono PCM)
data_size = n_samples * 2
buf = io.BytesIO()
# RIFF header
buf.write(b"RIFF")
buf.write(struct.pack("<I", 36 + data_size))
buf.write(b"WAVE")
# fmt chunk
buf.write(b"fmt ")
buf.write(struct.pack("<I", 16)) # chunk size
buf.write(struct.pack("<H", 1)) # PCM
buf.write(struct.pack("<H", 1)) # mono
buf.write(struct.pack("<I", sample_rate))
buf.write(struct.pack("<I", sample_rate * 2)) # byte rate
buf.write(struct.pack("<H", 2)) # block align
buf.write(struct.pack("<H", 16)) # bits per sample
# data chunk
buf.write(b"data")
buf.write(struct.pack("<I", data_size))
for s in samples:
buf.write(struct.pack("<h", s))
return buf.getvalue()
def test_analyze_file_real_audio(
client: TestClient,
) -> None:
"""
Real WAV audio triggers the full analysis pipeline.
Expects production mode with feature extraction
and score fusion (not preview fallback).
"""
wav_bytes = _make_wav(duration_sec=3.0, freq_hz=440.0)
audio_file = io.BytesIO(wav_bytes)
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
files={
"file": (
"tone.wav", audio_file, "audio/wav",
),
},
)
assert response.status_code == 200
data = response.json()
assert data["result"] is not None
result = data["result"]
# Should use real analysis, not preview fallback
assert result["analysisMode"] == "production"
assert result["decisionSource"].startswith("auris_")
assert result["modelVersion"] == "auris-v1-fusion"
# Confidence in valid range
assert 0.51 <= result["confidence"] <= 0.97
# Features should have real values
feat = result["features"]
assert 0.0 <= feat["spectralRegularity"] <= 0.99
assert 0.0 <= feat["temporalPatterns"] <= 0.99
assert 0.0 <= feat["harmonicStructure"] <= 0.99
# Audio info should reflect our input
info = result["audioInfo"]
assert info["duration"] > 2.0
assert info["sampleRate"] == 22050
# Indicators should be populated
assert len(feat["artificialIndicators"]) > 0
def test_analyze_file_real_audio_has_processing_time(
client: TestClient,
) -> None:
"""Processing time should be realistic."""
wav_bytes = _make_wav(duration_sec=2.0, freq_hz=220.0)
audio_file = io.BytesIO(wav_bytes)
response = client.post(
"/api/analyze",
data={"sourceType": "file"},
files={
"file": (
"test2.wav", audio_file, "audio/wav",
),
},
)
data = response.json()
assert data["result"] is not None
# Should complete in reasonable time
assert data["result"]["processingTime"] < 30.0
assert data["result"]["processingTime"] > 0.0
|