absolution-omega / tests /test_api.py
2Btainment's picture
v1.1 Ω FUSION + display name Tune-Whisperer; keeps owner-only library/guest isolation (fusion scoped), Demucs 120s, /data
e880b2b verified
Raw History Blame Contribute Delete
8.36 kB
import io
import time
import zipfile
import numpy as np
import soundfile as sf
from fastapi.testclient import TestClient
from app.server import create_app
def wav_bytes(seconds=1, rate=8000):
t = np.arange(int(seconds * rate)) / rate
s = .2 * np.sin(2 * np.pi * 240 * t)
s[100:110] += .3
buffer = io.BytesIO()
sf.write(buffer, s, rate, format="WAV", subtype="PCM_16")
return buffer.getvalue()
def test_full_project_upload_job_preview_export(tmp_path):
app = create_app(tmp_path / "store")
with TestClient(app) as client:
assert client.get("/api/v1/health").json()["network_downloads"] is False
creation = client.post("/api/v1/projects", json={"name":"Demo"})
assert creation.status_code == 201
project_id = creation.json()["id"]
upload = client.post("/api/v1/assets", data={"project_id":project_id},
files={"file":("demo.wav",wav_bytes(),"audio/wav")})
assert upload.status_code == 201, upload.text
payload = upload.json()
assert payload["analysis"]["sample_rate"] == 8000
assert payload["asset"]["sha256"]
original = client.get(f"/api/v1/projects/{project_id}/audio/original?preview=true")
assert original.status_code == 200 and original.content[:4] == b"RIFF"
response = client.post("/api/v1/jobs/separate",
json={"project_id":project_id, "model":"hpss"})
assert response.status_code == 202, response.text
job_id = response.json()["id"]
for _ in range(120):
job = client.get(f"/api/v1/jobs/{job_id}").json()
if job["status"] in ("done", "failed"):
break
time.sleep(.1)
assert job["status"] == "done", job
results = client.get(f"/api/v1/projects/{project_id}/stems").json()
assert set(results["stems"]) == {"harmonic","percussive","residual"}
assert results["report"]["consistent"]
assert results["report"]["max_abs_reconstruction_error"] < 1e-8
audio_response = client.get(f"/api/v1/projects/{project_id}/audio/harmonic")
assert audio_response.status_code == 200
master, _ = sf.read(io.BytesIO(audio_response.content))
assert len(master) == 8000
# Stored FLOAT WAV stems still close the mixture despite Float32 rounding.
exports = []
for stem_name in ("harmonic", "percussive", "residual"):
content = client.get(f"/api/v1/projects/{project_id}/audio/{stem_name}").content
values, rate = sf.read(io.BytesIO(content), dtype="float64")
assert rate == 8000
exports.append(values)
original, _ = sf.read(io.BytesIO(wav_bytes()), dtype="float64")
assert np.max(np.abs(original - sum(exports))) < 1e-7
package = client.get(f"/api/v1/projects/{project_id}/export")
assert package.status_code == 200
with zipfile.ZipFile(io.BytesIO(package.content)) as bundle:
assert "manifest.json" in bundle.namelist()
assert "stems/harmonic.wav" in bundle.namelist()
assert "source.wav" in bundle.namelist()
assert client.get(f"/api/v1/projects/{project_id}").json()["asset"]["sha256"]
def test_invalid_upload_and_unconfigured_model(tmp_path, monkeypatch):
monkeypatch.delenv("ABSOLUTION_DEMUCS_REPO", raising=False)
app = create_app(tmp_path)
with TestClient(app) as client:
project_id = client.post("/api/v1/projects",json={"name":"Test"}).json()["id"]
bad = client.post("/api/v1/assets", data={"project_id":project_id},
files={"file":("bad.wav",b"not a WAV", "audio/wav")})
assert bad.status_code == 422
ext = client.post("/api/v1/assets", data={"project_id":project_id},
files={"file":("script.exe",b"???", "application/octet-stream")})
assert ext.status_code == 415
m = client.get("/api/v1/models").json()["models"]
assert m[0]["ready"] and not m[1]["ready"]
assert client.post("/api/v1/jobs/separate",
json={"project_id":project_id}).status_code == 409
invalid_id = client.get("/api/v1/projects/../../etc/passwd")
assert invalid_id.status_code in (404, 307)
def test_reject_claim_of_text_conditioning(tmp_path):
app = create_app(tmp_path)
with TestClient(app) as client:
project_id = client.post("/api/v1/projects",json={"name":"Test"}).json()["id"]
result = client.post("/api/v1/assets", data={"project_id":project_id},
files={"file":("valid.wav",wav_bytes(),"audio/wav")})
assert result.status_code == 201
job = client.post("/api/v1/jobs/separate",json={
"project_id": project_id, "model":"hpss",
"target_description":"isolate lead singer"
})
assert job.status_code == 422
def test_frontend_and_openapi_respond(tmp_path):
app = create_app(tmp_path)
with TestClient(app) as client:
html = client.get("/")
js = client.get("/static/studio.js")
css = client.get("/static/studio.css")
spec = client.get("/openapi.json")
assert html.status_code == 200 and "Tune-Whisperer" in html.text
assert js.status_code == 200 and "renderMix" in js.text
assert css.status_code == 200 and ".track-list" in css.text
assert spec.status_code == 200 and "/api/v1/jobs/separate" in spec.json()["paths"]
def test_site_demo_and_required_ui_routes(tmp_path):
app = create_app(tmp_path)
with TestClient(app) as client:
page = client.get("/")
assert page.status_code == 200
for label in ("Dein Klang.", "Alle Spuren", "NOISE-CORE", "BASS-CORE", "Demo laden"):
assert label in page.text
sample = client.get("/api/v1/demo")
assert sample.status_code == 200 and sample.content[:4] == b"RIFF"
probe = sf.info(io.BytesIO(sample.content))
assert probe.frames > 1000 and probe.samplerate >= 8000
assert client.get("/static/favicon.svg").status_code == 200
health = client.get("/api/v1/health").json()
assert health["auth"] == "hf_oauth_owner_only_library"
assert health["network_downloads"] is False
assert health["duration_limit_seconds"] is None
assert health["streaming_hpss"] is True
assert health["max_upload_bytes"] >= 1024 * 1024 * 1024
def test_api_accepts_and_separates_track_longer_than_180_seconds(tmp_path):
# A sparse WAV keeps test fixtures small to generate and ensures actual
# length validation, job processing, preview, and export paths work.
audio_path = tmp_path / "long.wav"
sample_rate = 8000
frames = sample_rate * 181
with sf.SoundFile(audio_path, "w", samplerate=sample_rate, channels=1) as target:
target.seek(frames - 1)
target.write(np.array([0.25], dtype=np.float32))
app = create_app(tmp_path / "db")
with TestClient(app) as client:
project_id = client.post("/api/v1/projects", json={"name": "Extended"}).json()["id"]
with audio_path.open("rb") as upload:
result = client.post("/api/v1/assets", data={"project_id": project_id},
files={"file": ("long.wav", upload, "audio/wav")})
assert result.status_code == 201, result.text
assert result.json()["analysis"]["duration_seconds"] == 181
job = client.post("/api/v1/jobs/separate",
json={"project_id": project_id, "model": "hpss"})
assert job.status_code == 202, job.text
job_id = job.json()["id"]
for _ in range(300):
status = client.get(f"/api/v1/jobs/{job_id}").json()
if status["status"] in ("done", "failed"):
break
time.sleep(.1)
assert status["status"] == "done", status
result = client.get(f"/api/v1/projects/{project_id}/stems").json()
assert result["report"]["consistent"]
for stem in result["stems"].values():
response = client.get(
f"/api/v1/projects/{project_id}/audio/{stem['name']}?preview=true"
)
assert response.status_code == 200
assert sf.info(io.BytesIO(response.content)).frames == frames