541f59ed6e
Frontend (the premium baseline): - The hero is now the ONLY image slot. Soft feed images get an atmospheric gradient overlay; no over-reliance on inconsistent RSS image quality. - Every secondary/lane card is a uniform typographic editorial tile: no thumbnails, equal visual weight, a faint topic wordmark watermark, a slim sage top accent, consistent source, reason text as the trust signal, visible Replace with quiet tuning actions. Fixes the jarring mixed-media row rhythm and removes muddy thumbnails entirely. Backend (composition): - _select_diverse now balances topics: no more than 2 of one topic while other topics have candidates (relaxing source then topic caps only to fill), so the daily five stop clustering medical/science items. Candidates now carry s.topic. Tests updated for the topic-balance contract (79 total). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
from goodnews.briefs import _select_diverse
|
|
|
|
|
|
def row(id, source, topic):
|
|
# _select_diverse reads id, source_name, topic; plain dicts support [] access.
|
|
return {"id": id, "source_name": source, "topic": topic}
|
|
|
|
|
|
def test_prefers_distinct_sources_best_first():
|
|
rows = [
|
|
row(1, "A", "science"),
|
|
row(2, "A", "science"), # same source as #1 — skipped while others remain
|
|
row(3, "B", "health"),
|
|
row(4, "C", "environment"),
|
|
]
|
|
assert [r["id"] for r in _select_diverse(rows, limit=3)] == [1, 3, 4]
|
|
|
|
|
|
def test_caps_a_topic_when_alternatives_exist():
|
|
rows = [
|
|
row(1, "A", "science"), row(2, "B", "science"),
|
|
row(3, "C", "science"), row(4, "D", "science"),
|
|
row(5, "E", "community"), row(6, "F", "animals"), row(7, "G", "culture"),
|
|
]
|
|
selected = _select_diverse(rows, limit=5, max_per_topic=2)
|
|
topics = [r["topic"] for r in selected]
|
|
assert len(selected) == 5
|
|
assert topics.count("science") == 2 # capped, even though 4 were available
|
|
assert {"community", "animals", "culture"} <= set(topics)
|
|
|
|
|
|
def test_relaxes_cap_when_only_one_topic_available():
|
|
rows = [row(i, f"S{i}", "science") for i in range(1, 6)]
|
|
selected = _select_diverse(rows, limit=5)
|
|
assert len(selected) == 5 # all science: cap relaxed because nothing else exists
|
|
|
|
|
|
def test_backfills_repeating_source_when_needed():
|
|
rows = [row(1, "A", "science"), row(2, "A", "science"), row(3, "A", "science")]
|
|
assert len(_select_diverse(rows, limit=2)) == 2
|
|
|
|
|
|
def test_never_exceeds_limit():
|
|
rows = [row(i, f"S{i}", "science") for i in range(20)]
|
|
assert len(_select_diverse(rows, limit=5)) == 5
|