fix(news): unwrap CDATA titles at ingest and on /api/news

Excelsior rows store the feed's CDATA wrapper as the title text, including
the surrounding whitespace. Strip a whole-title wrapper before insert and
again on GET /api/news so existing rows read clean without an UPDATE.
Plain titles and real HTML are unchanged.
This commit is contained in:
Sirius DevOps 2026-11-14 00:47:04 -05:00
commit 13a4049137
6 changed files with 154 additions and 1 deletions

View file

@ -176,6 +176,50 @@ def test_api_news_excludes_future_dated_rows(clean_news):
assert [a["title"] for a in limited] == ["recent", "recent2"]
@requires_db
def test_api_news_unwraps_stored_cdata_title(clean_news):
"""Dirty rows are fixed on read. A plain title, and HTML that is not a
CDATA wrapper, are returned unchanged. No UPDATE of historical rows.
"""
stored = "\n <![CDATA[Hello]]> "
_seed_article(
stored,
"https://www.excelsior.com.mx/cdata",
"www.excelsior.com.mx",
"2026-08-24T18:00:00+00:00",
)
_seed_article(
"Hello",
"https://a.example/plain",
"a.example",
"2026-08-24T17:00:00+00:00",
)
_seed_article(
"<b>Keep</b>",
"https://a.example/html",
"a.example",
"2026-08-24T16:00:00+00:00",
)
body = _get("/api/news").json()
by_url = {row["url"]: row["title"] for row in body}
assert by_url["https://www.excelsior.com.mx/cdata"] == "Hello"
assert by_url["https://a.example/plain"] == "Hello"
assert by_url["https://a.example/html"] == "<b>Keep</b>"
# Read-path only: the stored row stays dirty.
async def _stored_title() -> str:
conn = await asyncpg.connect(**_conn_kwargs())
try:
return await conn.fetchval(
"SELECT title FROM articles WHERE url = $1",
"https://www.excelsior.com.mx/cdata",
)
finally:
await conn.close()
assert asyncio.run(_stored_title()) == stored
@requires_db
def test_api_news_summaries_contract(clean_news):

View file

@ -133,3 +133,51 @@ def test_pipeline_keeps_unparseable_timestamp():
assert is_future_dated("not a date") is False
assert is_future_dated(None) is False
# ── CDATA title unwrap (ingest) ───────────────────────────────────────────
# Excelsior stores the wrapper as text, including the leading newline the
# feed leaves around <![CDATA[...]]>. A plain title, and real HTML that is
# not that wrapper, must pass through unchanged.
@pytest.mark.parametrize(
"raw,expected",
[
("<![CDATA[Hello]]>", "Hello"),
("\n <![CDATA[Hello]]>", "Hello"),
(
"\n <![CDATA[Hello]]> ",
"Hello",
),
("Hello", "Hello"),
(" Hello ", " Hello "),
("<b>Hello</b>", "<b>Hello</b>"),
("See <![CDATA[Hello]]>", "See <![CDATA[Hello]]>"),
(None, None),
],
)
def test_unwrap_cdata_title(raw, expected):
from newsScraper.feed_util import unwrap_cdata_title
assert unwrap_cdata_title(raw) == expected
@pytest.mark.parametrize(
"raw,expected",
[
("<![CDATA[Hello]]>", "Hello"),
("\n <![CDATA[Hello]]>", "Hello"),
("Hello", "Hello"),
("<b>Hello</b>", "<b>Hello</b>"),
],
)
def test_pipeline_stores_cdata_title_unwrapped(raw, expected):
pipe = _pipeline()
spider = _FakeSpider()
recent = _dt.datetime.now(_dt.timezone.utc) - _dt.timedelta(hours=1)
item = _item(recent)
item["title"] = raw
returned = pipe.process_item(item, spider)
assert returned["title"] == expected
stored = pipe.cur.executed[0][1][0]
assert stored == expected