feat(screener): ai_news Claude Haiku analyzer (-10~+10 + clamp + JSON-fail soft)
This commit is contained in:
55
stock-lab/tests/test_ai_news_analyzer.py
Normal file
55
stock-lab/tests/test_ai_news_analyzer.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import json
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from app.screener.ai_news import analyzer
|
||||
|
||||
|
||||
def _mk_llm(content_text: str, in_tokens: int = 100, out_tokens: int = 20):
|
||||
llm = AsyncMock()
|
||||
resp = MagicMock()
|
||||
block = MagicMock()
|
||||
block.text = content_text
|
||||
resp.content = [block]
|
||||
resp.usage = MagicMock(input_tokens=in_tokens, output_tokens=out_tokens)
|
||||
llm.messages = MagicMock()
|
||||
llm.messages.create = AsyncMock(return_value=resp)
|
||||
return llm
|
||||
|
||||
|
||||
NEWS = [{"title": "삼성전자, HBM 양산"}, {"title": "메모리 가격 반등"}]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_score_sentiment_success_parses_json():
|
||||
llm = _mk_llm(json.dumps({"score": 7.5, "reason": "HBM 호재"}))
|
||||
out = await analyzer.score_sentiment(llm, "005930", NEWS, name="삼성전자")
|
||||
assert out["ticker"] == "005930"
|
||||
assert out["score_raw"] == 7.5
|
||||
assert out["reason"] == "HBM 호재"
|
||||
assert out["news_count"] == 2
|
||||
assert out["tokens_input"] == 100
|
||||
assert out["tokens_output"] == 20
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_score_sentiment_json_parse_fail_returns_zero():
|
||||
llm = _mk_llm("not valid json")
|
||||
out = await analyzer.score_sentiment(llm, "005930", NEWS)
|
||||
assert out["score_raw"] == 0.0
|
||||
assert "parse fail" in out["reason"]
|
||||
assert out["tokens_input"] == 100 # 호출은 발생했음
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_score_sentiment_clamps_out_of_range():
|
||||
llm = _mk_llm(json.dumps({"score": 15.0, "reason": "초강세"}))
|
||||
out = await analyzer.score_sentiment(llm, "005930", NEWS)
|
||||
assert out["score_raw"] == 10.0 # +10 클램프
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_score_sentiment_clamps_negative_out_of_range():
|
||||
llm = _mk_llm(json.dumps({"score": -42.0, "reason": "초악재"}))
|
||||
out = await analyzer.score_sentiment(llm, "005930", NEWS)
|
||||
assert out["score_raw"] == -10.0
|
||||
Reference in New Issue
Block a user