2026-11-13 21:56:46 -05:00
|
|
|
"""Summarizer window, recap flag, no futures injection, and grading env knobs."""
|
2026-08-28 22:53:31 -04:00
|
|
|
|
2026-11-13 21:56:46 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
import summarizer
|
2026-08-28 22:53:31 -04:00
|
|
|
from summarizer import (
|
|
|
|
|
build_map_prompt,
|
|
|
|
|
build_master_prompt,
|
|
|
|
|
effective_window_minutes,
|
2026-11-13 21:56:46 -05:00
|
|
|
get_recent_news,
|
2026-08-28 22:53:31 -04:00
|
|
|
is_recap_run,
|
2026-11-13 21:56:46 -05:00
|
|
|
selection_config,
|
2026-08-28 22:53:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_interval_window_defaults_to_15_minutes(monkeypatch):
|
|
|
|
|
monkeypatch.delenv("NEWS_RECAP", raising=False)
|
|
|
|
|
monkeypatch.delenv("SUMMARY_WINDOW_HOURS", raising=False)
|
|
|
|
|
monkeypatch.setenv("SUMMARY_WINDOW_MINUTES", "15")
|
|
|
|
|
assert is_recap_run() is False
|
|
|
|
|
assert effective_window_minutes() == 15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recap_window_is_24_hours(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("NEWS_RECAP", "1")
|
|
|
|
|
monkeypatch.setenv("SUMMARY_WINDOW_MINUTES", "15")
|
|
|
|
|
assert is_recap_run() is True
|
|
|
|
|
assert effective_window_minutes() == 24 * 60
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_map_prompt_does_not_inject_futures():
|
|
|
|
|
prompt = build_map_prompt(
|
|
|
|
|
[{"title": "Blast", "domain": "ex.com", "url": "https://ex.com/1", "content": "x" * 120}]
|
|
|
|
|
)
|
|
|
|
|
assert "FUTURES PRICES" not in prompt
|
|
|
|
|
assert "ES=F" not in prompt
|
|
|
|
|
assert "Blast" in prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_master_prompt_interval_uses_summary_not_recap():
|
|
|
|
|
prompt = build_master_prompt("partial facts", recap=False)
|
|
|
|
|
assert "partial facts" in prompt
|
|
|
|
|
assert "daily recap" not in prompt.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_master_prompt_recap_uses_daily_template():
|
|
|
|
|
prompt = build_master_prompt("partial facts", recap=True)
|
|
|
|
|
assert "partial facts" in prompt
|
|
|
|
|
assert "daily recap" in prompt.lower()
|
|
|
|
|
assert "24" in prompt
|
2026-11-13 21:56:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── get_recent_news window: (now - window, now] ───────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _WindowCursor:
|
|
|
|
|
"""Cursor double that applies the (now-window, now] window to a fixed table.
|
|
|
|
|
|
|
|
|
|
It emulates PostgreSQL's ``NOW()`` semantics for the window bounds the query
|
|
|
|
|
declares, so a query missing the upper bound returns future-dated rows
|
|
|
|
|
exactly as the live table did. Rows with a NULL timestamp never satisfy a
|
|
|
|
|
range comparison, matching the pre-existing live behaviour.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, rows):
|
|
|
|
|
self.rows = rows
|
|
|
|
|
self.sql = ""
|
|
|
|
|
self.params = None
|
|
|
|
|
|
|
|
|
|
def execute(self, sql, params=None):
|
|
|
|
|
self.sql = " ".join(sql.split())
|
|
|
|
|
self.params = params
|
|
|
|
|
|
|
|
|
|
def fetchall(self):
|
|
|
|
|
mins = self.params[0]
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
start = now - timedelta(minutes=mins)
|
|
|
|
|
has_upper = "timestamp <= NOW()" in self.sql
|
|
|
|
|
out = []
|
|
|
|
|
for row in self.rows:
|
|
|
|
|
ts = row[5]
|
|
|
|
|
if ts is None or ts <= start:
|
|
|
|
|
continue
|
|
|
|
|
if has_upper and ts > now:
|
|
|
|
|
continue
|
|
|
|
|
out.append(row)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _WindowConn:
|
|
|
|
|
def __init__(self, rows):
|
|
|
|
|
self.rows = rows
|
|
|
|
|
|
|
|
|
|
def cursor(self):
|
|
|
|
|
return _WindowCursor(self.rows)
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _row(row_id: int, ts, domain: str = "ex.com") -> tuple:
|
|
|
|
|
return (row_id, "title", "x" * 120, f"https://ex.com/{row_id}", domain, ts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _patch_articles(monkeypatch, rows):
|
|
|
|
|
monkeypatch.setattr(summarizer.psycopg2, "connect", lambda **kwargs: _WindowConn(rows))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recent_news_excludes_future_dated_rows(monkeypatch):
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
rows = [
|
|
|
|
|
_row(1, now - timedelta(minutes=2), "fresh.example"),
|
|
|
|
|
_row(2, now + timedelta(days=3), "wiseye.org"),
|
|
|
|
|
_row(3, now + timedelta(days=6), "wiseye.org"),
|
|
|
|
|
]
|
|
|
|
|
_patch_articles(monkeypatch, rows)
|
|
|
|
|
|
|
|
|
|
news = get_recent_news(15)
|
|
|
|
|
|
|
|
|
|
assert [n["id"] for n in news] == [1]
|
|
|
|
|
assert {n["domain"] for n in news} == {"fresh.example"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recent_news_window_is_closed_at_now_and_open_at_the_start(monkeypatch):
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
rows = [
|
|
|
|
|
_row(1, now), # exactly now → inside (now - window, now]
|
|
|
|
|
_row(2, now - timedelta(minutes=20)), # older than the 15 min window
|
|
|
|
|
]
|
|
|
|
|
_patch_articles(monkeypatch, rows)
|
|
|
|
|
|
|
|
|
|
assert [n["id"] for n in get_recent_news(15)] == [1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recent_news_still_excludes_null_timestamps(monkeypatch):
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
_patch_articles(monkeypatch, [_row(1, None), _row(2, now - timedelta(minutes=1))])
|
|
|
|
|
|
|
|
|
|
assert [n["id"] for n in get_recent_news(15)] == [2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recent_news_query_keeps_content_filter_and_ordering(monkeypatch):
|
|
|
|
|
captured = {}
|
|
|
|
|
|
|
|
|
|
class _CaptureCursor(_WindowCursor):
|
|
|
|
|
def execute(self, sql, params=None):
|
|
|
|
|
super().execute(sql, params)
|
|
|
|
|
captured["sql"] = self.sql
|
|
|
|
|
|
|
|
|
|
class _CaptureConn(_WindowConn):
|
|
|
|
|
def cursor(self):
|
|
|
|
|
return _CaptureCursor([])
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(summarizer.psycopg2, "connect", lambda **kwargs: _CaptureConn([]))
|
|
|
|
|
get_recent_news(15)
|
|
|
|
|
|
|
|
|
|
sql = captured["sql"]
|
|
|
|
|
assert "timestamp > NOW() - make_interval(mins => %s)" in sql
|
|
|
|
|
assert "timestamp <= NOW()" in sql
|
|
|
|
|
assert "content IS NOT NULL AND length(content) > 100" in sql
|
|
|
|
|
assert "ORDER BY timestamp DESC" in sql
|
|
|
|
|
assert "IS NULL OR" not in sql
|
|
|
|
|
assert "COALESCE(timestamp" not in sql
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── selection_config: SUMMARY_FALLBACK_MIN_SCORE ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _clear_selection_env(monkeypatch):
|
|
|
|
|
for name in (
|
|
|
|
|
"SUMMARY_MIN_SCORE",
|
|
|
|
|
"SUMMARY_TOP_N",
|
|
|
|
|
"SUMMARY_FALLBACK_TOP",
|
|
|
|
|
"SUMMARY_FALLBACK_MIN_SCORE",
|
|
|
|
|
):
|
|
|
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_selection_config_defaults(monkeypatch):
|
|
|
|
|
_clear_selection_env(monkeypatch)
|
|
|
|
|
assert selection_config() == (60, 12, 3, 40)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_selection_config_reads_the_floor(monkeypatch):
|
|
|
|
|
_clear_selection_env(monkeypatch)
|
|
|
|
|
monkeypatch.setenv("SUMMARY_FALLBACK_MIN_SCORE", "55")
|
|
|
|
|
assert selection_config()[3] == 55
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_selection_config_floor_zero_is_honoured(monkeypatch):
|
|
|
|
|
_clear_selection_env(monkeypatch)
|
|
|
|
|
monkeypatch.setenv("SUMMARY_FALLBACK_MIN_SCORE", "0")
|
|
|
|
|
assert selection_config()[3] == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_selection_config_garbage_floor_warns_and_defaults(monkeypatch, caplog):
|
|
|
|
|
_clear_selection_env(monkeypatch)
|
|
|
|
|
monkeypatch.setenv("SUMMARY_FALLBACK_MIN_SCORE", "not-a-number")
|
|
|
|
|
with caplog.at_level("WARNING"):
|
|
|
|
|
assert selection_config()[3] == 40
|
|
|
|
|
assert any("SUMMARY_FALLBACK_MIN_SCORE" in rec.message for rec in caplog.records)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("raw,expected", [("100", 100), ("101", 100), ("-5", 0)])
|
|
|
|
|
def test_selection_config_floor_is_clamped(monkeypatch, raw, expected):
|
|
|
|
|
_clear_selection_env(monkeypatch)
|
|
|
|
|
monkeypatch.setenv("SUMMARY_FALLBACK_MIN_SCORE", raw)
|
|
|
|
|
assert selection_config()[3] == expected
|