/api/music/{lyrics, credits, timestamped-lyrics, style-boost}
모두 sync_forward 모듈로 위임 → Windows :18711/api/music-render/sync/*.
SUNO_API_KEY가 NAS에 없으므로 직접 호출 불가.
run_*, generate_*, get_* import 제거 (Windows로 이전됨).
SUNO_MODELS만 잔존 (정적 데이터).
추가 cleanup (T11 reviewer 지적):
- _push_render_job의 datetime import를 모듈 상위로
- 11 endpoint의 unused BackgroundTasks 매개변수 제거
generate_batch: SUNO_API_KEY 체크를 os.getenv()로 전환 + 테스트 monkeypatch 갱신.
Plan-B-Music Phase 3 (cutover 2/4).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
import pytest
|
|
from unittest.mock import AsyncMock, patch
|
|
from fastapi.testclient import TestClient
|
|
import app.main as main_module
|
|
from app import db
|
|
|
|
|
|
@pytest.fixture
|
|
def client(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(db, "DB_PATH", str(tmp_path / "music.db"))
|
|
db.init_db()
|
|
monkeypatch.setenv("SUNO_API_KEY", "test")
|
|
# main.py의 SUNO_API_KEY 모듈 변수도 갱신 필요할 수 있음
|
|
monkeypatch.setattr(main_module, "SUNO_API_KEY", "test", raising=False)
|
|
return TestClient(main_module.app)
|
|
|
|
|
|
def test_create_batch_201(client):
|
|
with patch.object(main_module, "_run_batch", new=AsyncMock()):
|
|
r = client.post("/api/music/generate-batch",
|
|
json={"genre": "lo-fi", "count": 3})
|
|
assert r.status_code == 201, r.text
|
|
body = r.json()
|
|
assert body["genre"] == "lo-fi"
|
|
assert body["count"] == 3
|
|
assert body["status"] == "queued"
|
|
|
|
|
|
def test_create_batch_rejects_count_too_high(client):
|
|
r = client.post("/api/music/generate-batch",
|
|
json={"genre": "lo-fi", "count": 11})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_create_batch_rejects_count_zero(client):
|
|
r = client.post("/api/music/generate-batch",
|
|
json={"genre": "lo-fi", "count": 0})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_create_batch_rejects_no_genre(client):
|
|
r = client.post("/api/music/generate-batch", json={"count": 3})
|
|
assert r.status_code in (400, 422)
|
|
|
|
|
|
def test_create_batch_rejects_invalid_duration(client):
|
|
r = client.post("/api/music/generate-batch",
|
|
json={"genre": "lo-fi", "count": 3, "target_duration_sec": 30})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_create_batch_rejects_no_suno_key(client, monkeypatch):
|
|
monkeypatch.delenv("SUNO_API_KEY", raising=False)
|
|
r = client.post("/api/music/generate-batch",
|
|
json={"genre": "lo-fi", "count": 3})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_get_batch_returns_tracks(client):
|
|
bid = db.create_batch_job(genre="lo-fi", count=2)
|
|
db.append_batch_track(bid, 999) # phantom track
|
|
r = client.get(f"/api/music/generate-batch/{bid}")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["track_ids"] == [999]
|
|
assert body["tracks"] == [] # 999 not in music_library
|
|
|
|
|
|
def test_get_batch_404(client):
|
|
r = client.get("/api/music/generate-batch/99999")
|
|
assert r.status_code == 404
|
|
|
|
|
|
def test_list_batches(client):
|
|
db.create_batch_job(genre="lo-fi", count=1)
|
|
db.create_batch_job(genre="phonk", count=2)
|
|
r = client.get("/api/music/generate-batch")
|
|
assert r.status_code == 200
|
|
assert len(r.json()["batches"]) == 2
|
|
|
|
|
|
def test_list_batches_active_filter(client):
|
|
b1 = db.create_batch_job(genre="lo-fi", count=1)
|
|
b2 = db.create_batch_job(genre="phonk", count=2)
|
|
db.update_batch_job(b1, status="failed")
|
|
r = client.get("/api/music/generate-batch?status=active")
|
|
ids = [j["id"] for j in r.json()["batches"]]
|
|
assert b2 in ids
|
|
assert b1 not in ids
|