83 lines
3.3 KiB
Python
83 lines
3.3 KiB
Python
import pytest
|
|
import respx
|
|
from httpx import Response
|
|
from app.pipeline import metadata
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_metadata_calls_claude_and_parses_json(monkeypatch):
|
|
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key")
|
|
payload = {
|
|
"content": [{"type": "text", "text": '{"title":"[Lo-fi] Drive | 85BPM",'
|
|
'"description":"chill","tags":["lofi","85bpm"],'
|
|
'"category_id":10}'}]
|
|
}
|
|
respx.post("https://api.anthropic.com/v1/messages").mock(
|
|
return_value=Response(200, json=payload)
|
|
)
|
|
result = await metadata.generate(
|
|
track={"title": "Drive", "genre": "lo-fi", "bpm": 85, "key": "C", "scale": "minor",
|
|
"moods": ["chill"], "instruments": ["piano"]},
|
|
template={"title": "[{genre}] {title} | {bpm}BPM",
|
|
"description": "{title}\n", "tags": [], "category_id": 10},
|
|
trend_keywords=["lofi", "study"],
|
|
feedback="",
|
|
)
|
|
assert result["title"].startswith("[Lo-fi]")
|
|
assert "lofi" in result["tags"]
|
|
assert result["used_fallback"] is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_metadata_fallback_when_no_api_key(monkeypatch):
|
|
monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False)
|
|
result = await metadata.generate(
|
|
track={"title": "Drive", "genre": "lo-fi", "bpm": 85, "key": "C", "scale": "minor",
|
|
"moods": [], "instruments": []},
|
|
template={"title": "[{genre}] {title} | {bpm}BPM",
|
|
"description": "{title}", "tags": ["lofi"], "category_id": 10},
|
|
trend_keywords=[],
|
|
)
|
|
# 템플릿 변수 그대로 치환된 폴백
|
|
assert result["title"] == "[lo-fi] Drive | 85BPM"
|
|
assert result["used_fallback"] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_metadata_includes_feedback_in_prompt(monkeypatch):
|
|
import json
|
|
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key")
|
|
captured = {}
|
|
def hook(req):
|
|
captured["body"] = json.loads(req.content)
|
|
return Response(200, json={"content": [{"type": "text",
|
|
"text": '{"title":"x","description":"y","tags":[],"category_id":10}'}]})
|
|
respx.post("https://api.anthropic.com/v1/messages").mock(side_effect=hook)
|
|
await metadata.generate(
|
|
track={"title": "X", "genre": "lo-fi", "bpm": 85, "key": "C", "scale": "minor",
|
|
"moods": [], "instruments": []},
|
|
template={"title": "{title}", "description": "{title}", "tags": [], "category_id": 10},
|
|
trend_keywords=[],
|
|
feedback="제목을 짧게",
|
|
)
|
|
assert "제목을 짧게" in str(captured["body"])
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_metadata_falls_back_on_api_error(monkeypatch):
|
|
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key")
|
|
respx.post("https://api.anthropic.com/v1/messages").mock(
|
|
return_value=Response(500)
|
|
)
|
|
result = await metadata.generate(
|
|
track={"title": "Drive", "genre": "lo-fi", "bpm": 85, "key": "C", "scale": "minor",
|
|
"moods": [], "instruments": []},
|
|
template={"title": "[{genre}] {title}", "description": "x", "tags": ["lofi"], "category_id": 10},
|
|
trend_keywords=[],
|
|
)
|
|
assert result["used_fallback"] is True
|
|
assert "Drive" in result["title"]
|