test_explore_sort_cursor.py
python
sha256:533a428669ed74e9f74471413a844d4c2f95bb336de16b058c62d21ca1b0fc65
Merge branch 'task/wave-hub-ui-bugs' into dev
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """Explorer sort labels and keyset pagination (musehub#334). |
| 2 | |
| 3 | EXP_SORT_01 sort=forks orders by fork count, not commit count; sort=commits orders by commits |
| 4 | EXP_SORT_02 the Explorer offers no two labels for one ordering ("Recently updated" was "activity") |
| 5 | EXP_CUR_01 paging sort=activity returns every repo exactly once, in the unpaged order |
| 6 | EXP_CUR_02 the same for commits, trending, forks and created |
| 7 | EXP_CUR_03 a malformed, legacy (bare ISO timestamp) or other-sort cursor is the shared HTTP 400 |
| 8 | """ |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import base64 |
| 12 | import json |
| 13 | import secrets |
| 14 | from datetime import datetime, timedelta, timezone |
| 15 | |
| 16 | import pytest |
| 17 | from httpx import AsyncClient |
| 18 | from sqlalchemy import text |
| 19 | from sqlalchemy.ext.asyncio import AsyncSession |
| 20 | |
| 21 | from musehub.db.musehub_social_models import MusehubFork |
| 22 | from tests.factories import create_commit, create_repo |
| 23 | |
| 24 | |
| 25 | def _ago(days: float) -> datetime: |
| 26 | return datetime.now(tz=timezone.utc) - timedelta(days=days) |
| 27 | |
| 28 | |
| 29 | async def _repo(db: AsyncSession, *, created_days_ago: float, visibility: str = "public") -> str: |
| 30 | repo = await create_repo(db, visibility=visibility) |
| 31 | tag = secrets.token_hex(4) |
| 32 | await db.execute( |
| 33 | text("UPDATE musehub_repos SET owner = :o, slug = :s, name = :s, created_at = :c WHERE repo_id = :r"), |
| 34 | {"o": "o" + tag, "s": "s" + tag, "c": _ago(created_days_ago), "r": repo.repo_id}, |
| 35 | ) |
| 36 | await db.commit() |
| 37 | return str(repo.repo_id) |
| 38 | |
| 39 | |
| 40 | async def _commits(db: AsyncSession, repo_id: str, *days_ago: float) -> None: |
| 41 | for d in days_ago: |
| 42 | await create_commit(db, repo_id, timestamp=_ago(d)) |
| 43 | |
| 44 | |
| 45 | async def _forks(db: AsyncSession, source_id: str, n: int) -> None: |
| 46 | for _ in range(n): |
| 47 | fork_id = await _repo(db, created_days_ago=0.5, visibility="private") |
| 48 | db.add(MusehubFork(fork_id=secrets.token_hex(16), source_repo_id=source_id, |
| 49 | fork_repo_id=fork_id, forked_by="f" + secrets.token_hex(4))) |
| 50 | await db.commit() |
| 51 | |
| 52 | |
| 53 | async def _ids(client: AsyncClient, **params: str | int) -> tuple[list[str], str | None]: |
| 54 | resp = await client.get("/api/discover/repos", params=params) |
| 55 | assert resp.status_code == 200, resp.text |
| 56 | body = resp.json() |
| 57 | return [r["repoId"] for r in body["repos"]], body["nextCursor"] |
| 58 | |
| 59 | |
| 60 | async def _seed(db: AsyncSession) -> list[str]: |
| 61 | """Seven public repos whose created_at, newest-commit, commit-count and fork-count orders all differ, with ties.""" |
| 62 | ids = [await _repo(db, created_days_ago=d) for d in (70, 60, 50, 40, 30, 20, 10)] |
| 63 | await _commits(db, ids[0], 1, 2, 3) # oldest repo, most commits, newest commit |
| 64 | await _commits(db, ids[1], 5, 6) |
| 65 | await _commits(db, ids[2], 4, 9) # ties ids[1] on commit count |
| 66 | await _commits(db, ids[3], 15) |
| 67 | await _commits(db, ids[4], 12) # ties ids[3] on commit count |
| 68 | # ids[5] and ids[6]: no commits |
| 69 | await _forks(db, ids[6], 2) |
| 70 | await _forks(db, ids[3], 1) |
| 71 | await _forks(db, ids[5], 1) # ties ids[3] on fork count |
| 72 | return ids |
| 73 | |
| 74 | |
| 75 | async def test_exp_sort_01_forks_orders_by_fork_count(client: AsyncClient, db_session: AsyncSession) -> None: |
| 76 | a = await _repo(db_session, created_days_ago=30) |
| 77 | b = await _repo(db_session, created_days_ago=1) |
| 78 | await _commits(db_session, a, 1, 2, 3) |
| 79 | await _commits(db_session, b, 1) |
| 80 | await _forks(db_session, b, 2) |
| 81 | forks, _ = await _ids(client, sort="forks", limit=50) |
| 82 | commits, _ = await _ids(client, sort="commits", limit=50) |
| 83 | assert forks.index(b) < forks.index(a) |
| 84 | assert commits.index(a) < commits.index(b) |
| 85 | |
| 86 | |
| 87 | async def test_exp_sort_02_explorer_labels_are_distinct_orderings(client: AsyncClient) -> None: |
| 88 | html = (await client.get("/explore")).text |
| 89 | assert "Recently updated" not in html |
| 90 | assert "Most forked" in html and "Most commits" in html |
| 91 | |
| 92 | |
| 93 | @pytest.mark.parametrize("sort", ["activity", "commits", "trending", "forks", "created"]) |
| 94 | async def test_exp_cur_01_02_paging_returns_every_repo_once( |
| 95 | client: AsyncClient, db_session: AsyncSession, sort: str |
| 96 | ) -> None: |
| 97 | ids = await _seed(db_session) |
| 98 | unpaged, cursor = await _ids(client, sort=sort, limit=100) |
| 99 | assert cursor is None and set(unpaged) == set(ids) |
| 100 | paged: list[str] = [] |
| 101 | cursor = None |
| 102 | for _ in range(10): |
| 103 | params: dict[str, str | int] = {"sort": sort, "limit": 2} |
| 104 | if cursor: |
| 105 | params["cursor"] = cursor |
| 106 | page, cursor = await _ids(client, **params) |
| 107 | paged += page |
| 108 | if cursor is None: |
| 109 | break |
| 110 | assert paged == unpaged |
| 111 | |
| 112 | |
| 113 | async def test_exp_cur_01_activity_puts_repos_without_commits_last( |
| 114 | client: AsyncClient, db_session: AsyncSession |
| 115 | ) -> None: |
| 116 | ids = await _seed(db_session) |
| 117 | order, _ = await _ids(client, sort="activity", limit=100) |
| 118 | assert order[0] == ids[0] |
| 119 | assert set(order[-2:]) == {ids[5], ids[6]} |
| 120 | |
| 121 | |
| 122 | def _b64(raw: object) -> str: |
| 123 | return base64.urlsafe_b64encode(json.dumps(raw).encode()).decode().rstrip("=") |
| 124 | |
| 125 | |
| 126 | async def test_exp_cur_03_bad_cursor_is_the_shared_400(client: AsyncClient, db_session: AsyncSession) -> None: |
| 127 | await _seed(db_session) |
| 128 | _, commits_cursor = await _ids(client, sort="commits", limit=2) |
| 129 | assert commits_cursor is not None |
| 130 | bad = [ |
| 131 | ("activity", "not-a-cursor!!"), |
| 132 | ("created", _ago(1).isoformat()), # the old created_at cursor |
| 133 | ("forks", commits_cursor), # issued for another sort |
| 134 | ("activity", _b64(["activity", "2026-01-01T00:00:00", "2026-01-01T00:00:00+00:00", "x"])), # naive |
| 135 | ("forks", _b64(["forks", -1, "2026-01-01T00:00:00+00:00", "x"])), |
| 136 | ("forks", _b64(["forks", True, "2026-01-01T00:00:00+00:00", "x"])), |
| 137 | ("forks", _b64(["forks", 1, "2026-01-01T00:00:00+00:00"])), # no repo_id |
| 138 | ("forks", _b64({"sort": "forks"})), |
| 139 | ] |
| 140 | for sort, cursor in bad: |
| 141 | resp = await client.get("/api/discover/repos", params={"sort": sort, "cursor": cursor}) |
| 142 | assert resp.status_code == 400, (cursor, resp.status_code, resp.text) |
| 143 | assert resp.json() == {"detail": "invalid cursor"} |
| 144 | resp = await client.get("/explore", params={"sort": "forks", "cursor": "not-a-cursor!!"}) |
| 145 | assert resp.status_code == 400 |
File History
2 commits
sha256:533a428669ed74e9f74471413a844d4c2f95bb336de16b058c62d21ca1b0fc65
Merge branch 'task/wave-hub-ui-bugs' into dev
Opus 5.5
minor
⚠
1 day ago
sha256:838c391c589c00bdcc9ecfcb8970366e9334a8fbed079db384f9bf8c3c09c71c
fix(explore): 'Most forked' sorts by fork count and every s…
Opus 5.5
minor
⚠
1 day ago