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

@ -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