64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""Unit tests for the daily / weekly digest prompt builders."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from app.services.openrouter import (
|
|
build_daily_digest_prompt,
|
|
build_weekly_digest_prompt,
|
|
)
|
|
|
|
|
|
def _ctx():
|
|
return dict(
|
|
today=datetime(2026, 5, 25, 6, 30, tzinfo=timezone.utc),
|
|
quotes_by_group={"equities": [{"symbol": "SPX", "price": 7500.0,
|
|
"label": "S&P 500", "currency": "USD",
|
|
"source": "test", "note": "",
|
|
"as_of": None, "changes": {}}]},
|
|
headlines_by_bucket={"general": [{"when": "2026-05-25T05:00:00+00:00",
|
|
"source": "FT", "title": "Brent slides"}]},
|
|
reference_line="S&P 7,501 (ATH) · VIX 18.0 · US 10y 4.45%",
|
|
)
|
|
|
|
|
|
def test_daily_prompt_tone_intermediate():
|
|
sys_, usr = build_daily_digest_prompt(tone="INTERMEDIATE", **_ctx())
|
|
assert "INTERMEDIATE" in sys_.upper() or "intermediate" in sys_.lower()
|
|
assert "Brent slides" in usr
|
|
assert "daily" in sys_.lower()
|
|
|
|
|
|
def test_daily_prompt_tone_novice_differs():
|
|
sys_int, _ = build_daily_digest_prompt(tone="INTERMEDIATE", **_ctx())
|
|
sys_nov, _ = build_daily_digest_prompt(tone="NOVICE", **_ctx())
|
|
assert sys_int != sys_nov
|
|
|
|
|
|
def test_weekly_prompt_mentions_week():
|
|
sys_, usr = build_weekly_digest_prompt(tone="INTERMEDIATE", **_ctx())
|
|
assert "week" in sys_.lower() or "weekly" in sys_.lower()
|
|
assert "Brent slides" in usr
|
|
|
|
|
|
def test_prompts_return_strings():
|
|
for fn in (build_daily_digest_prompt, build_weekly_digest_prompt):
|
|
sys_, usr = fn(tone="INTERMEDIATE", **_ctx())
|
|
assert isinstance(sys_, str) and isinstance(usr, str)
|
|
assert len(sys_) > 50 and len(usr) > 50
|
|
|
|
|
|
def test_prompts_tolerate_empty_data():
|
|
"""No quotes, no headlines — builders must still produce non-empty
|
|
prompts without raising. Guards the `if headlines_by_bucket` and
|
|
`if quotes_by_group` branches in _digest_user_prompt."""
|
|
empty_ctx = dict(
|
|
today=datetime(2026, 5, 25, 6, 30, tzinfo=timezone.utc),
|
|
quotes_by_group={},
|
|
headlines_by_bucket={},
|
|
reference_line="S&P 7,501 (ATH)",
|
|
)
|
|
for fn in (build_daily_digest_prompt, build_weekly_digest_prompt):
|
|
sys_, usr = fn(tone="INTERMEDIATE", **empty_ctx)
|
|
assert "S&P 7,501" in usr
|
|
assert len(sys_) > 50
|