The reduce model returns url="" and save_batch was persisting that blank. Match each headline to a selected article title and store that article's url. A null article url or an ambiguous match stores "" — never a guessed or model-supplied URL.
214 lines
5.9 KiB
Python
214 lines
5.9 KiB
Python
"""Ticker and map rows persist the selected article URL, never the model's."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
import summarizer
|
|
from intel import article_url_for_row
|
|
|
|
|
|
ARTICLES = [
|
|
{
|
|
"id": 1,
|
|
"title": "Strike in Kyiv",
|
|
"content": "x" * 120,
|
|
"url": "https://ex.com/1",
|
|
"domain": "ex.com",
|
|
},
|
|
{
|
|
"id": 2,
|
|
"title": "Floods in Paris",
|
|
"content": "y" * 120,
|
|
"url": "https://ex.com/2",
|
|
"domain": "ex.com",
|
|
},
|
|
{
|
|
"id": 3,
|
|
"title": "Quiet day in Oslo",
|
|
"content": "z" * 120,
|
|
"url": None,
|
|
"domain": "ex.com",
|
|
},
|
|
]
|
|
|
|
|
|
def _block(n: int, score: int) -> str:
|
|
return "\n".join(
|
|
[
|
|
f"Article {n}:",
|
|
f"- Score: {score}",
|
|
"- Importance: high",
|
|
"- Fact bullet",
|
|
"- Location: Kyiv",
|
|
"- Category: politics",
|
|
"- Lat: 50.45",
|
|
"- Lon: 30.52",
|
|
]
|
|
)
|
|
|
|
|
|
REDUCE = {
|
|
"summary_en": "Lead: strike in Kyiv. Also floods in Paris and a quiet day in Oslo.",
|
|
# Headlines are swapped vs article order so a positional stamp would cross-wire.
|
|
# Model url is blank or a decoy — neither may be stored.
|
|
"ticker": [
|
|
{"headline": "Floods in Paris", "importance": "high", "url": ""},
|
|
{
|
|
"headline": "Strike in Kyiv",
|
|
"importance": "critical",
|
|
"url": "https://wrong.example/model",
|
|
},
|
|
{
|
|
"headline": "Quiet day in Oslo",
|
|
"importance": "high",
|
|
"url": "https://invented.example",
|
|
},
|
|
],
|
|
"map_items": [
|
|
{
|
|
"headline": "Floods in Paris",
|
|
"importance": "high",
|
|
"location_name": "Paris",
|
|
"lat": 48.85,
|
|
"lon": 2.35,
|
|
"location_confidence": "city",
|
|
"category": "environment/disaster",
|
|
"url": "",
|
|
},
|
|
{
|
|
"headline": "Strike in Kyiv",
|
|
"importance": "critical",
|
|
"location_name": "Kyiv",
|
|
"lat": 50.45,
|
|
"lon": 30.52,
|
|
"location_confidence": "city",
|
|
"category": "military/conflict",
|
|
"url": "",
|
|
},
|
|
{
|
|
"headline": "Quiet day in Oslo",
|
|
"importance": "high",
|
|
"location_name": "Oslo",
|
|
"lat": 59.91,
|
|
"lon": 10.75,
|
|
"location_confidence": "city",
|
|
"category": "society",
|
|
"url": "https://invented.example",
|
|
},
|
|
],
|
|
}
|
|
|
|
|
|
class _FakeCursor:
|
|
def __init__(self, log: list):
|
|
self.log = log
|
|
self._sql = ""
|
|
|
|
def execute(self, sql, params=None):
|
|
self._sql = " ".join(sql.split())
|
|
self.log.append(("execute", self._sql, params))
|
|
|
|
def executemany(self, sql, rows):
|
|
self.log.append(("executemany", " ".join(sql.split()), list(rows)))
|
|
|
|
def fetchone(self):
|
|
if "article_summaries" in self._sql:
|
|
return (42,)
|
|
return None
|
|
|
|
def fetchall(self):
|
|
return []
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
class _FakeConn:
|
|
def __init__(self, log: list):
|
|
self.log = log
|
|
|
|
def cursor(self):
|
|
return _FakeCursor(self.log)
|
|
|
|
def commit(self):
|
|
self.log.append(("commit",))
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
def _news_item_urls(db_log: list, kind: str) -> list:
|
|
urls = []
|
|
for entry in db_log:
|
|
if entry[0] != "execute" or "INSERT INTO news_items" not in entry[1]:
|
|
continue
|
|
params = entry[2]
|
|
if not params or params[1] != kind:
|
|
continue
|
|
urls.append(params[-1])
|
|
return urls
|
|
|
|
|
|
def test_matcher_uses_article_url_not_model_url():
|
|
row = {"headline": "Strike in Kyiv", "importance": "critical", "url": ""}
|
|
assert article_url_for_row(row, ARTICLES) == "https://ex.com/1"
|
|
|
|
|
|
def test_matcher_does_not_cross_wire_and_null_url_stays_blank():
|
|
paris = {"headline": "Floods in Paris", "url": "https://ex.com/1"}
|
|
oslo = {"headline": "Quiet day in Oslo", "url": "https://invented.example"}
|
|
assert article_url_for_row(paris, ARTICLES) == "https://ex.com/2"
|
|
assert article_url_for_row(oslo, ARTICLES) == ""
|
|
|
|
|
|
def test_matcher_refuses_a_tie_instead_of_guessing():
|
|
twins = [
|
|
{"title": "Same story", "url": "https://ex.com/1"},
|
|
{"title": "Same story", "url": "https://ex.com/2"},
|
|
]
|
|
assert article_url_for_row({"headline": "Same story", "url": ""}, twins) == ""
|
|
|
|
|
|
def test_unmatched_headline_does_not_invent_a_url():
|
|
row = {"headline": "Unrelated market tape", "url": "https://model.example/kept"}
|
|
assert article_url_for_row(row, ARTICLES) == ""
|
|
|
|
|
|
@pytest.fixture
|
|
def harness(monkeypatch):
|
|
calls: list[tuple[str, str]] = []
|
|
db_log: list = []
|
|
map_reply = "\n\n".join([_block(1, 92), _block(2, 88), _block(3, 80)])
|
|
reduce_reply = json.dumps(REDUCE)
|
|
|
|
def fake_call_llm(prompt, *, api_key, model, base_url, json_mode=False):
|
|
calls.append(("json" if json_mode else "text", prompt))
|
|
return reduce_reply if json_mode else map_reply
|
|
|
|
monkeypatch.setenv("NOUS_API_KEY", "test-key")
|
|
monkeypatch.setenv("SUMMARY_MODEL", "test-model")
|
|
monkeypatch.setattr(summarizer, "call_llm", fake_call_llm)
|
|
monkeypatch.setattr(summarizer, "get_recent_news", lambda *a, **k: ARTICLES)
|
|
monkeypatch.setattr(summarizer, "_already_summarized_this_interval", lambda: False)
|
|
monkeypatch.setattr(summarizer, "_already_recapped_today", lambda: False)
|
|
monkeypatch.setattr(summarizer.psycopg2, "connect", lambda **kwargs: _FakeConn(db_log))
|
|
return calls, db_log
|
|
|
|
|
|
def test_persist_stamps_article_url_on_ticker_and_map(harness):
|
|
_, db_log = harness
|
|
summarizer.summarize_news()
|
|
|
|
assert _news_item_urls(db_log, "ticker") == [
|
|
"https://ex.com/2",
|
|
"https://ex.com/1",
|
|
"",
|
|
]
|
|
assert _news_item_urls(db_log, "map") == [
|
|
"https://ex.com/2",
|
|
"https://ex.com/1",
|
|
"",
|
|
]
|