feat: v2.21.0 paging for homepage, likes and admin dashboard

The homepage showed only the 20 newest notes and /likes the 50 newest,
with no way back. The admin dashboard paged list_notes()'s default 50
newest, so it always said 'Page 1 of 2'. All three now count with
count_notes() and fetch one page with limit/offset. Public pages get
'<- Newer / Page N of M / Older ->' with rel=prev/next (h-feed paging),
?page=1 redirects to the bare URL, bad pages 404; the dashboard falls
back to the nearest valid page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-27 20:51:30 -07:00
1 parent 9074c9a08c
commit 615354d17e
11 files changed
+373 -60

No files matched your search

+184
View File
@@ -0,0 +1,184 @@
"""
Paging of the homepage, likes page and admin dashboard (v2.21.0)
Before v2.21.0 the homepage showed only the 20 newest notes, the likes page
the 50 newest likes, and the admin dashboard paged list_notes()'s default
50 newest notes, so older notes were unreachable from all three.
"""
import re
from datetime import datetime, timedelta, timezone
import pytest
from starpunk import create_app
from starpunk.auth import create_session
from starpunk.notes import count_notes, create_note
START = datetime(2026, 1, 1, tzinfo=timezone.utc)
@pytest.fixture
def app(tmp_path):
data = tmp_path / "data"
data.mkdir(parents=True, exist_ok=True)
return create_app(config={
"TESTING": True,
"DATABASE_PATH": data / "starpunk.db",
"DATA_PATH": data,
"NOTES_PATH": data / "notes",
"SESSION_SECRET": "test-secret-key",
"ADMIN_ME": "https://test.example.com",
"SITE_URL": "http://localhost:5000",
"SITE_NAME": "Test Site",
"DEV_MODE": False,
})
@pytest.fixture
def client(app):
return app.test_client()
def make_notes(app, published=0, drafts=0, likes=0):
"""Notes one minute apart, oldest first; content says which is which"""
n = 0
with app.app_context():
for i in range(published):
create_note(content=f"Published note {i:03d}", published=True,
created_at=START + timedelta(minutes=n))
n += 1
for i in range(drafts):
create_note(content=f"Draft note {i:03d}", published=False,
created_at=START + timedelta(minutes=n))
n += 1
for i in range(likes):
create_note(content=f"Liked thing {i:03d}", published=True,
like_of=f"https://example.com/liked/{i}",
created_at=START + timedelta(minutes=n))
n += 1
def shown(html, prefix):
return sorted(set(re.findall(rf"{prefix} (\d{{3}})", html)))
def rel_link(html, rel):
m = re.search(rf'<a rel="{rel}" href="([^"]+)"', html)
return m.group(1) if m else None
class TestCountNotes:
def test_count_matches_filters(self, app):
make_notes(app, published=7, drafts=3, likes=4)
with app.app_context():
assert count_notes() == 14
assert count_notes(published_only=True) == 11
assert count_notes(published_only=True, likes=False) == 7
assert count_notes(published_only=True, likes=True) == 4
class TestHomepagePaging:
@pytest.fixture(autouse=True)
def notes(self, app):
# 45 published notes = 3 pages of 20; drafts and likes stay off
make_notes(app, published=45, drafts=2, likes=3)
def test_first_page_is_newest_20(self, client):
html = client.get("/").get_data(as_text=True)
assert shown(html, "Published note") == [f"{i:03d}" for i in range(25, 45)]
assert "Page 1 of 3" in html
assert rel_link(html, "prev") is None
assert rel_link(html, "next") == "/?page=2"
def test_middle_page_links_both_ways(self, client):
html = client.get("/?page=2").get_data(as_text=True)
assert shown(html, "Published note") == [f"{i:03d}" for i in range(5, 25)]
# page 1 is the bare URL
assert rel_link(html, "prev") == "/"
assert rel_link(html, "next") == "/?page=3"
assert "<title>Page 2 · Test Site</title>" in html
def test_last_page_has_the_oldest(self, client):
html = client.get("/?page=3").get_data(as_text=True)
assert shown(html, "Published note") == [f"{i:03d}" for i in range(0, 5)]
assert rel_link(html, "next") is None
assert rel_link(html, "prev") == "/?page=2"
def test_every_published_note_reachable_once(self, client):
seen = []
for p in ("/", "/?page=2", "/?page=3"):
seen += re.findall(r"Published note (\d{3})", client.get(p).get_data(as_text=True))
assert sorted(set(seen)) == [f"{i:03d}" for i in range(45)]
def test_drafts_and_likes_never_listed(self, client):
for p in ("/", "/?page=2", "/?page=3"):
html = client.get(p).get_data(as_text=True)
assert "Draft note" not in html
assert "Liked thing" not in html
def test_page_one_redirects_to_bare_url(self, client):
r = client.get("/?page=1")
assert r.status_code == 301
assert r.headers["Location"].endswith("/")
assert "page=" not in r.headers["Location"]
@pytest.mark.parametrize("page", ["4", "0", "-1", "abc", "2.5", ""])
def test_bad_or_out_of_range_page_is_404(self, client, page):
assert client.get(f"/?page={page}").status_code == 404
class TestHomepageSmall:
def test_no_pager_when_one_page(self, app, client):
make_notes(app, published=5)
html = client.get("/").get_data(as_text=True)
assert 'class="pager"' not in html
assert "<title>Test Site</title>" in html
def test_empty_site_still_renders(self, client):
r = client.get("/")
assert r.status_code == 200
assert "No notes published yet" in r.get_data(as_text=True)
class TestLikesPaging:
def test_likes_page_two(self, app, client):
make_notes(app, published=2, likes=55) # 50 per page
first = client.get("/likes").get_data(as_text=True)
assert len(shown(first, "Liked thing")) == 50
assert rel_link(first, "next") == "/likes?page=2"
second = client.get("/likes?page=2").get_data(as_text=True)
assert shown(second, "Liked thing") == [f"{i:03d}" for i in range(0, 5)]
assert rel_link(second, "prev") == "/likes"
assert "Published note" not in first + second
assert client.get("/likes?page=3").status_code == 404
class TestAdminDashboardPaging:
@pytest.fixture
def admin(self, app, client):
with app.test_request_context():
token = create_session("https://test.example.com")
client.set_cookie("starpunk_session", token)
return client
def test_all_notes_reachable_past_fifty(self, app, admin):
# 60 published + 5 drafts = 65 notes = 3 pages of 25
make_notes(app, published=60, drafts=5)
html = admin.get("/admin/").get_data(as_text=True)
assert "Page 1 of 3" in html
last = admin.get("/admin/?page=3").get_data(as_text=True)
# the 15 oldest notes, which the old 50-note cap hid
assert shown(last, "Published note") == [f"{i:03d}" for i in range(0, 15)]
def test_drafts_listed(self, app, admin):
make_notes(app, published=3, drafts=2)
assert "Draft note 000" in admin.get("/admin/").get_data(as_text=True)
@pytest.mark.parametrize("page,expected", [("99", "Page 3 of 3"), ("abc", "Page 1 of 3"),
("0", "Page 1 of 3")])
def test_bad_page_falls_back(self, app, admin, page, expected):
make_notes(app, published=60)
r = admin.get(f"/admin/?page={page}")
assert r.status_code == 200
assert expected in r.get_data(as_text=True)