|
| 1 | +""" |
| 2 | +Regression tests for GET /audio/{generation_id} on failed generations. |
| 3 | +
|
| 4 | +A failed generation stores an empty ``audio_path``. Previously, |
| 5 | +``config.resolve_storage_path("")`` resolved to the data directory itself, |
| 6 | +which exists, so the route's 404 guard passed and ``FileResponse`` raised |
| 7 | +``RuntimeError: File at path .../data is not a file`` — a 500 instead of |
| 8 | +a clean 404. |
| 9 | +
|
| 10 | +Usage: |
| 11 | + python -m pytest backend/tests/test_audio_failed_generation.py -v |
| 12 | +""" |
| 13 | + |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | +from fastapi import FastAPI |
| 19 | +from sqlalchemy import create_engine |
| 20 | +from sqlalchemy.orm import sessionmaker |
| 21 | +from starlette.testclient import TestClient |
| 22 | + |
| 23 | +# Repo root on sys.path so ``backend`` imports as a package (the audio |
| 24 | +# routes use package-relative imports). |
| 25 | +sys.path.insert(0, str(Path(__file__).parent.parent.parent)) |
| 26 | + |
| 27 | +from backend import config |
| 28 | +from backend.database import Base, Generation, VoiceProfile, get_db |
| 29 | +from backend.routes.audio import router as audio_router |
| 30 | + |
| 31 | + |
| 32 | +def test_resolve_storage_path_empty_returns_none(): |
| 33 | + """An empty stored path must not resolve to the data dir itself.""" |
| 34 | + assert config.resolve_storage_path("") is None |
| 35 | + assert config.resolve_storage_path(None) is None |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture() |
| 39 | +def client(tmp_path, monkeypatch): |
| 40 | + """Minimal app with only the audio routes and a temp sqlite DB.""" |
| 41 | + monkeypatch.setattr(config, "_data_dir", tmp_path) |
| 42 | + |
| 43 | + engine = create_engine( |
| 44 | + f"sqlite:///{tmp_path / 'test.db'}", |
| 45 | + connect_args={"check_same_thread": False}, |
| 46 | + ) |
| 47 | + Base.metadata.create_all(bind=engine) |
| 48 | + TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
| 49 | + |
| 50 | + session = TestingSessionLocal() |
| 51 | + profile = VoiceProfile(id="profile-1", name="Test Profile") |
| 52 | + session.add(profile) |
| 53 | + |
| 54 | + session.add_all( |
| 55 | + [ |
| 56 | + Generation( |
| 57 | + id="gen-failed-empty", |
| 58 | + profile_id="profile-1", |
| 59 | + text="failed generation", |
| 60 | + audio_path="", |
| 61 | + status="failed", |
| 62 | + error="engine exploded", |
| 63 | + ), |
| 64 | + Generation( |
| 65 | + id="gen-failed-null", |
| 66 | + profile_id="profile-1", |
| 67 | + text="failed generation", |
| 68 | + audio_path=None, |
| 69 | + status="failed", |
| 70 | + ), |
| 71 | + Generation( |
| 72 | + id="gen-missing-file", |
| 73 | + profile_id="profile-1", |
| 74 | + text="completed but file deleted", |
| 75 | + audio_path="generations/does-not-exist.wav", |
| 76 | + status="completed", |
| 77 | + ), |
| 78 | + ] |
| 79 | + ) |
| 80 | + session.commit() |
| 81 | + session.close() |
| 82 | + |
| 83 | + app = FastAPI() |
| 84 | + app.include_router(audio_router) |
| 85 | + |
| 86 | + def override_get_db(): |
| 87 | + db = TestingSessionLocal() |
| 88 | + try: |
| 89 | + yield db |
| 90 | + finally: |
| 91 | + db.close() |
| 92 | + |
| 93 | + app.dependency_overrides[get_db] = override_get_db |
| 94 | + return TestClient(app) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize("generation_id", ["gen-failed-empty", "gen-failed-null"]) |
| 98 | +def test_failed_generation_returns_404(client, generation_id): |
| 99 | + """Failed generations (empty/null audio_path) get a clean 404, not a 500.""" |
| 100 | + response = client.get(f"/audio/{generation_id}") |
| 101 | + assert response.status_code == 404 |
| 102 | + assert response.json()["detail"] == "Generation failed; no audio available" |
| 103 | + |
| 104 | + |
| 105 | +def test_missing_audio_file_returns_404(client): |
| 106 | + """A completed generation whose file vanished still 404s.""" |
| 107 | + response = client.get("/audio/gen-missing-file") |
| 108 | + assert response.status_code == 404 |
| 109 | + assert response.json()["detail"] == "Audio file not found" |
| 110 | + |
| 111 | + |
| 112 | +def test_unknown_generation_returns_404(client): |
| 113 | + response = client.get("/audio/no-such-generation") |
| 114 | + assert response.status_code == 404 |
| 115 | + assert response.json()["detail"] == "Generation not found" |
0 commit comments