Files
upbeatBytes/tests/test_feed_sort.py
T
thejayman77 38889f76e5 Source feeds: click a source to see its publication feed
Click a source name on any card → a feed of just that source's articles,
newest-first, still accepted / non-duplicate / boundary-filtered (the calm
promise isn't bypassed). A natural way to follow a publication's feel.

* queries.feed + /api/feed: source_id filter; Article output gains source_id.
* Frontend: source label is a button → transient 'source:<id>' view (like
  'tag:<slug>'), rendered in the feed grid with Load more, header = source name.
* Ad-hoc, not a pinned lane. Foundation for a future source page (metadata) +
  Follow; shareable /source/<slug> route and source_view analytics come then.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 08:30:33 -04:00

41 lines
1.7 KiB
Python

from goodnews.db import connect, init_db
from goodnews import queries
def _article(c, aid, *, when):
c.execute(
"INSERT INTO articles (id, source_id, canonical_url, title, url_hash, published_at) "
"VALUES (?, 1, ?, ?, ?, ?)",
(aid, f"http://s/{aid}", f"T{aid}", f"h{aid}", when),
)
c.execute(
"INSERT INTO article_scores (article_id, accepted, constructive_score) VALUES (?, 1, 5)",
(aid,),
)
def test_latest_sorts_strictly_by_recency(tmp_path):
c = connect(str(tmp_path / "t.db")); init_db(c)
c.execute("INSERT INTO sources (id, name, feed_url) VALUES (1, 'S', 'http://s/f')")
# Insert out of order; the dates are what should drive 'latest'.
_article(c, 1, when="2026-03-01T00:00:00")
_article(c, 2, when="2026-06-01T00:00:00") # newest
_article(c, 3, when="2026-01-01T00:00:00") # oldest
c.commit()
latest = [a["id"] for a in queries.feed(c, sort="latest")]
assert latest == [2, 1, 3] # newest → oldest, regardless of insert order
def test_feed_source_id_filters_to_one_source(tmp_path):
c = connect(str(tmp_path / "t.db")); init_db(c)
c.execute("INSERT INTO sources (id,name,feed_url) VALUES (1,'A','http://a/f'),(2,'B','http://b/f')")
for aid, sid in [(1, 1), (2, 2), (3, 1)]:
c.execute("INSERT INTO articles (id,source_id,canonical_url,title,url_hash,published_at) "
"VALUES (?,?,?,?,?,'2026-01-01T00:00:00')", (aid, sid, f"u{aid}", f"T{aid}", f"h{aid}"))
c.execute("INSERT INTO article_scores (article_id,accepted) VALUES (?,1)", (aid,))
c.commit()
ids = {a["id"] for a in queries.feed(c, source_id=1)}
assert ids == {1, 3} # only source A
assert all(a["source_id"] == 1 for a in queries.feed(c, source_id=1))