Spaces:
Sleeping
Sleeping
refactor: improve test function descriptions and structure in test_analyze.py
Browse files- tests/test_analyze.py +161 -19
tests/test_analyze.py
CHANGED
|
@@ -3,18 +3,24 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import io
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from fastapi.testclient import TestClient
|
| 8 |
|
| 9 |
|
| 10 |
-
def test_analyze_missing_source_type(
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
response = client.post("/api/analyze")
|
| 13 |
assert response.status_code == 422
|
| 14 |
|
| 15 |
|
| 16 |
-
def test_analyze_invalid_source_type(
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
response = client.post(
|
| 19 |
"/api/analyze",
|
| 20 |
data={"sourceType": "unknown"},
|
|
@@ -25,8 +31,10 @@ def test_analyze_invalid_source_type(client: TestClient) -> None:
|
|
| 25 |
assert data["result"] is None
|
| 26 |
|
| 27 |
|
| 28 |
-
def test_analyze_unsupported_source(
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
for source in ("spotify", "apple"):
|
| 31 |
response = client.post(
|
| 32 |
"/api/analyze",
|
|
@@ -37,8 +45,10 @@ def test_analyze_unsupported_source(client: TestClient) -> None:
|
|
| 37 |
assert "unsupported_source" in data["errors"]
|
| 38 |
|
| 39 |
|
| 40 |
-
def test_analyze_youtube_missing_url(
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
response = client.post(
|
| 43 |
"/api/analyze",
|
| 44 |
data={"sourceType": "youtube"},
|
|
@@ -48,8 +58,10 @@ def test_analyze_youtube_missing_url(client: TestClient) -> None:
|
|
| 48 |
assert "missing_url" in data["errors"]
|
| 49 |
|
| 50 |
|
| 51 |
-
def test_analyze_file_missing_file(
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
response = client.post(
|
| 54 |
"/api/analyze",
|
| 55 |
data={"sourceType": "file"},
|
|
@@ -59,40 +71,52 @@ def test_analyze_file_missing_file(client: TestClient) -> None:
|
|
| 59 |
assert "missing_file" in data["errors"]
|
| 60 |
|
| 61 |
|
| 62 |
-
def test_analyze_file_invalid_type(
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
fake_file = io.BytesIO(b"not audio content " * 100)
|
| 65 |
response = client.post(
|
| 66 |
"/api/analyze",
|
| 67 |
data={"sourceType": "file"},
|
| 68 |
-
files={
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
assert response.status_code == 200
|
| 71 |
data = response.json()
|
| 72 |
assert "invalid_file_type" in data["errors"]
|
| 73 |
|
| 74 |
|
| 75 |
-
def test_analyze_file_too_small(
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
tiny_file = io.BytesIO(b"\x00" * 100)
|
| 78 |
response = client.post(
|
| 79 |
"/api/analyze",
|
| 80 |
data={"sourceType": "file"},
|
| 81 |
-
files={
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
assert response.status_code == 200
|
| 84 |
data = response.json()
|
| 85 |
assert "file_too_small" in data["errors"]
|
| 86 |
|
| 87 |
|
| 88 |
-
def
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
audio_content = b"\x00" * 2048
|
| 91 |
fake_audio = io.BytesIO(audio_content)
|
| 92 |
response = client.post(
|
| 93 |
"/api/analyze",
|
| 94 |
data={"sourceType": "file"},
|
| 95 |
-
files={
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
assert response.status_code == 200
|
| 98 |
data = response.json()
|
|
@@ -100,5 +124,123 @@ def test_analyze_file_happy_path(client: TestClient) -> None:
|
|
| 100 |
result = data["result"]
|
| 101 |
assert "isAIGenerated" in result
|
| 102 |
assert "confidence" in result
|
|
|
|
| 103 |
assert result["analysisMode"] == "preview"
|
| 104 |
assert result["decisionSource"] == "preview"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import io
|
| 6 |
+
import struct
|
| 7 |
+
import math
|
| 8 |
|
| 9 |
from fastapi.testclient import TestClient
|
| 10 |
|
| 11 |
|
| 12 |
+
def test_analyze_missing_source_type(
|
| 13 |
+
client: TestClient,
|
| 14 |
+
) -> None:
|
| 15 |
+
"""Reject request without sourceType."""
|
| 16 |
response = client.post("/api/analyze")
|
| 17 |
assert response.status_code == 422
|
| 18 |
|
| 19 |
|
| 20 |
+
def test_analyze_invalid_source_type(
|
| 21 |
+
client: TestClient,
|
| 22 |
+
) -> None:
|
| 23 |
+
"""Return error for unknown sourceType."""
|
| 24 |
response = client.post(
|
| 25 |
"/api/analyze",
|
| 26 |
data={"sourceType": "unknown"},
|
|
|
|
| 31 |
assert data["result"] is None
|
| 32 |
|
| 33 |
|
| 34 |
+
def test_analyze_unsupported_source(
|
| 35 |
+
client: TestClient,
|
| 36 |
+
) -> None:
|
| 37 |
+
"""Return unsupported_source for spotify/apple."""
|
| 38 |
for source in ("spotify", "apple"):
|
| 39 |
response = client.post(
|
| 40 |
"/api/analyze",
|
|
|
|
| 45 |
assert "unsupported_source" in data["errors"]
|
| 46 |
|
| 47 |
|
| 48 |
+
def test_analyze_youtube_missing_url(
|
| 49 |
+
client: TestClient,
|
| 50 |
+
) -> None:
|
| 51 |
+
"""YouTube without url returns missing_url."""
|
| 52 |
response = client.post(
|
| 53 |
"/api/analyze",
|
| 54 |
data={"sourceType": "youtube"},
|
|
|
|
| 58 |
assert "missing_url" in data["errors"]
|
| 59 |
|
| 60 |
|
| 61 |
+
def test_analyze_file_missing_file(
|
| 62 |
+
client: TestClient,
|
| 63 |
+
) -> None:
|
| 64 |
+
"""File without upload returns missing_file."""
|
| 65 |
response = client.post(
|
| 66 |
"/api/analyze",
|
| 67 |
data={"sourceType": "file"},
|
|
|
|
| 71 |
assert "missing_file" in data["errors"]
|
| 72 |
|
| 73 |
|
| 74 |
+
def test_analyze_file_invalid_type(
|
| 75 |
+
client: TestClient,
|
| 76 |
+
) -> None:
|
| 77 |
+
"""Reject non-audio file."""
|
| 78 |
fake_file = io.BytesIO(b"not audio content " * 100)
|
| 79 |
response = client.post(
|
| 80 |
"/api/analyze",
|
| 81 |
data={"sourceType": "file"},
|
| 82 |
+
files={
|
| 83 |
+
"file": ("test.txt", fake_file, "text/plain"),
|
| 84 |
+
},
|
| 85 |
)
|
| 86 |
assert response.status_code == 200
|
| 87 |
data = response.json()
|
| 88 |
assert "invalid_file_type" in data["errors"]
|
| 89 |
|
| 90 |
|
| 91 |
+
def test_analyze_file_too_small(
|
| 92 |
+
client: TestClient,
|
| 93 |
+
) -> None:
|
| 94 |
+
"""Reject files smaller than 1KB."""
|
| 95 |
tiny_file = io.BytesIO(b"\x00" * 100)
|
| 96 |
response = client.post(
|
| 97 |
"/api/analyze",
|
| 98 |
data={"sourceType": "file"},
|
| 99 |
+
files={
|
| 100 |
+
"file": ("tiny.wav", tiny_file, "audio/wav"),
|
| 101 |
+
},
|
| 102 |
)
|
| 103 |
assert response.status_code == 200
|
| 104 |
data = response.json()
|
| 105 |
assert "file_too_small" in data["errors"]
|
| 106 |
|
| 107 |
|
| 108 |
+
def test_analyze_file_fallback_on_bad_audio(
|
| 109 |
+
client: TestClient,
|
| 110 |
+
) -> None:
|
| 111 |
+
"""Non-parseable audio falls back to preview model."""
|
| 112 |
audio_content = b"\x00" * 2048
|
| 113 |
fake_audio = io.BytesIO(audio_content)
|
| 114 |
response = client.post(
|
| 115 |
"/api/analyze",
|
| 116 |
data={"sourceType": "file"},
|
| 117 |
+
files={
|
| 118 |
+
"file": ("test.wav", fake_audio, "audio/wav"),
|
| 119 |
+
},
|
| 120 |
)
|
| 121 |
assert response.status_code == 200
|
| 122 |
data = response.json()
|
|
|
|
| 124 |
result = data["result"]
|
| 125 |
assert "isAIGenerated" in result
|
| 126 |
assert "confidence" in result
|
| 127 |
+
# Bad audio triggers fallback to preview
|
| 128 |
assert result["analysisMode"] == "preview"
|
| 129 |
assert result["decisionSource"] == "preview"
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def _make_wav(
|
| 133 |
+
duration_sec: float = 3.0,
|
| 134 |
+
freq_hz: float = 440.0,
|
| 135 |
+
sample_rate: int = 22050,
|
| 136 |
+
) -> bytes:
|
| 137 |
+
"""
|
| 138 |
+
Generate a minimal valid WAV file with a sine tone.
|
| 139 |
+
|
| 140 |
+
Returns raw bytes of a valid WAV.
|
| 141 |
+
"""
|
| 142 |
+
n_samples = int(sample_rate * duration_sec)
|
| 143 |
+
samples = []
|
| 144 |
+
for i in range(n_samples):
|
| 145 |
+
t = i / sample_rate
|
| 146 |
+
val = math.sin(2 * math.pi * freq_hz * t)
|
| 147 |
+
samples.append(int(val * 32767))
|
| 148 |
+
|
| 149 |
+
# WAV header (16-bit mono PCM)
|
| 150 |
+
data_size = n_samples * 2
|
| 151 |
+
buf = io.BytesIO()
|
| 152 |
+
# RIFF header
|
| 153 |
+
buf.write(b"RIFF")
|
| 154 |
+
buf.write(struct.pack("<I", 36 + data_size))
|
| 155 |
+
buf.write(b"WAVE")
|
| 156 |
+
# fmt chunk
|
| 157 |
+
buf.write(b"fmt ")
|
| 158 |
+
buf.write(struct.pack("<I", 16)) # chunk size
|
| 159 |
+
buf.write(struct.pack("<H", 1)) # PCM
|
| 160 |
+
buf.write(struct.pack("<H", 1)) # mono
|
| 161 |
+
buf.write(struct.pack("<I", sample_rate))
|
| 162 |
+
buf.write(struct.pack("<I", sample_rate * 2)) # byte rate
|
| 163 |
+
buf.write(struct.pack("<H", 2)) # block align
|
| 164 |
+
buf.write(struct.pack("<H", 16)) # bits per sample
|
| 165 |
+
# data chunk
|
| 166 |
+
buf.write(b"data")
|
| 167 |
+
buf.write(struct.pack("<I", data_size))
|
| 168 |
+
for s in samples:
|
| 169 |
+
buf.write(struct.pack("<h", s))
|
| 170 |
+
|
| 171 |
+
return buf.getvalue()
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def test_analyze_file_real_audio(
|
| 175 |
+
client: TestClient,
|
| 176 |
+
) -> None:
|
| 177 |
+
"""
|
| 178 |
+
Real WAV audio triggers the full analysis pipeline.
|
| 179 |
+
|
| 180 |
+
Expects production mode with feature extraction
|
| 181 |
+
and score fusion (not preview fallback).
|
| 182 |
+
"""
|
| 183 |
+
wav_bytes = _make_wav(duration_sec=3.0, freq_hz=440.0)
|
| 184 |
+
audio_file = io.BytesIO(wav_bytes)
|
| 185 |
+
|
| 186 |
+
response = client.post(
|
| 187 |
+
"/api/analyze",
|
| 188 |
+
data={"sourceType": "file"},
|
| 189 |
+
files={
|
| 190 |
+
"file": (
|
| 191 |
+
"tone.wav", audio_file, "audio/wav",
|
| 192 |
+
),
|
| 193 |
+
},
|
| 194 |
+
)
|
| 195 |
+
assert response.status_code == 200
|
| 196 |
+
data = response.json()
|
| 197 |
+
assert data["result"] is not None
|
| 198 |
+
|
| 199 |
+
result = data["result"]
|
| 200 |
+
|
| 201 |
+
# Should use real analysis, not preview fallback
|
| 202 |
+
assert result["analysisMode"] == "production"
|
| 203 |
+
assert result["decisionSource"] in (
|
| 204 |
+
"auris_local", "auris_fusion",
|
| 205 |
+
)
|
| 206 |
+
assert result["modelVersion"] == "auris-v1-fusion"
|
| 207 |
+
|
| 208 |
+
# Confidence in valid range
|
| 209 |
+
assert 0.51 <= result["confidence"] <= 0.97
|
| 210 |
+
|
| 211 |
+
# Features should have real values
|
| 212 |
+
feat = result["features"]
|
| 213 |
+
assert 0.0 <= feat["spectralRegularity"] <= 0.99
|
| 214 |
+
assert 0.0 <= feat["temporalPatterns"] <= 0.99
|
| 215 |
+
assert 0.0 <= feat["harmonicStructure"] <= 0.99
|
| 216 |
+
|
| 217 |
+
# Audio info should reflect our input
|
| 218 |
+
info = result["audioInfo"]
|
| 219 |
+
assert info["duration"] > 2.0
|
| 220 |
+
assert info["sampleRate"] == 22050
|
| 221 |
+
|
| 222 |
+
# Indicators should be populated
|
| 223 |
+
assert len(feat["artificialIndicators"]) > 0
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def test_analyze_file_real_audio_has_processing_time(
|
| 227 |
+
client: TestClient,
|
| 228 |
+
) -> None:
|
| 229 |
+
"""Processing time should be realistic."""
|
| 230 |
+
wav_bytes = _make_wav(duration_sec=2.0, freq_hz=220.0)
|
| 231 |
+
audio_file = io.BytesIO(wav_bytes)
|
| 232 |
+
|
| 233 |
+
response = client.post(
|
| 234 |
+
"/api/analyze",
|
| 235 |
+
data={"sourceType": "file"},
|
| 236 |
+
files={
|
| 237 |
+
"file": (
|
| 238 |
+
"test2.wav", audio_file, "audio/wav",
|
| 239 |
+
),
|
| 240 |
+
},
|
| 241 |
+
)
|
| 242 |
+
data = response.json()
|
| 243 |
+
assert data["result"] is not None
|
| 244 |
+
# Should complete in reasonable time
|
| 245 |
+
assert data["result"]["processingTime"] < 30.0
|
| 246 |
+
assert data["result"]["processingTime"] > 0.0
|