feat: replace per-author cap with interleaving to avoid dropping posts in feed and home

The old `diversify_by_author` capped each author to 2 posts per page, silently dropping excess rows. The new `interleave_by_author` reorders the full result set so no two consecutive posts share an author, preserving every post while breaking up same-author runs. `paginate_diverse` loses its `max_per_author` and `pool` parameters; the home route now fetches exactly 6 rows and interleaves them instead of over-fetching 50 and capping. Tests are rewritten to verify interleaving behavior and per-author chronological order preservation.
This commit is contained in:
2026-06-14 16:50:30 +00:00
parent 9ed611097d
commit aeea551e4b
9 changed files with 81 additions and 59 deletions
+26 -15
View File
@@ -185,29 +185,40 @@ def test_build_pagination_metadata():
assert clamped["total_pages"] == 1
def test_diversify_by_author_caps_per_author():
from devplacepy.database import diversify_by_author
def test_interleave_by_author_spreads_consecutive_runs():
from devplacepy.database import interleave_by_author
rows = [
{"uid": 1, "user_uid": "a"},
{"uid": 2, "user_uid": "a"},
{"uid": 3, "user_uid": "a"},
{"uid": 4, "user_uid": "b"},
{"uid": 5, "user_uid": "a"},
{"uid": 5, "user_uid": "b"},
{"uid": 6, "user_uid": "c"},
]
kept = diversify_by_author(rows, max_per_author=2, limit=6)
authors = [r["user_uid"] for r in kept]
assert authors.count("a") == 2
assert authors.count("b") == 1
assert authors.count("c") == 1
assert [r["uid"] for r in kept] == [1, 2, 4, 6]
spread = interleave_by_author(rows)
authors = [r["user_uid"] for r in spread]
assert len(spread) == len(rows)
assert all(authors[i] != authors[i + 1] for i in range(len(authors) - 1))
def test_diversify_by_author_respects_limit():
from devplacepy.database import diversify_by_author
def test_interleave_by_author_preserves_per_author_order():
from devplacepy.database import interleave_by_author
rows = [{"uid": i, "user_uid": str(i)} for i in range(10)]
kept = diversify_by_author(rows, max_per_author=2, limit=3)
assert len(kept) == 3
assert [r["uid"] for r in kept] == [0, 1, 2]
rows = [
{"uid": 1, "user_uid": "a"},
{"uid": 2, "user_uid": "a"},
{"uid": 3, "user_uid": "a"},
{"uid": 4, "user_uid": "b"},
]
spread = interleave_by_author(rows)
a_order = [r["uid"] for r in spread if r["user_uid"] == "a"]
assert a_order == [1, 2, 3]
def test_interleave_by_author_single_author_keeps_order():
from devplacepy.database import interleave_by_author
rows = [{"uid": i, "user_uid": "a"} for i in range(4)]
spread = interleave_by_author(rows)
assert [r["uid"] for r in spread] == [0, 1, 2, 3]