gabriel / musehub public
test_objects_list_cursor.py python
62 lines 2.8 KB
Raw
sha256:0577579d44915d89d00bc5f88853a3ad18bc76c7e222515294866e1eb30d8bec feat(api): cursor-paginate GET /repos/{id}/objects (default… Opus 5.5 minor ⚠ breaking 2 days ago
1 """GET /api/repos/{repo_id}/objects is cursor-paginated (keyset on (path, object_id)): the whole listing (13.5k objects, ~2 MB on the largest
2 repository) used to come back from every call."""
3 from __future__ import annotations
4
5 import pytest
6 from httpx import AsyncClient
7 from sqlalchemy.ext.asyncio import AsyncSession
8
9 from muse.core.types import blob_id
10 from musehub.db.musehub_repo_models import MusehubObject, MusehubObjectRef
11 from tests.factories import create_repo
12
13
14 async def _seed(db: AsyncSession, repo_id: str, n: int) -> list[tuple[str, str]]:
15 keys = []
16 for i in range(n):
17 path = f"dir/f{i % 7:02d}.txt" # repeated paths: several objects per path across history
18 oid = blob_id(f"cursor-{repo_id}-{i}".encode())
19 db.add(MusehubObject(object_id=oid, path=path, size_bytes=i, storage_uri=f"s3://b/{oid}"))
20 db.add(MusehubObjectRef(repo_id=repo_id, object_id=oid))
21 keys.append((path, oid))
22 await db.commit()
23 return sorted(keys)
24
25
26 @pytest.mark.asyncio
27 async def test_pages_cover_every_object_exactly_once_in_order(client: AsyncClient, db_session: AsyncSession) -> None:
28 repo = await create_repo(db_session, visibility="public")
29 expected = await _seed(db_session, repo.repo_id, 23)
30 got, cursor, pages = [], None, 0
31 while True:
32 r = await client.get(f"/api/repos/{repo.repo_id}/objects", params={"limit": 5, **({"cursor": cursor} if cursor else {})})
33 assert r.status_code == 200, r.text
34 body = r.json()
35 got += [(o["path"], o["objectId"]) for o in body["objects"]]
36 pages += 1
37 cursor = body["nextCursor"]
38 if cursor is None:
39 break
40 assert got == expected and pages == 5 # 23 objects / 5 per page; no duplicates, no gaps, (path, id) order
41 assert len(body["objects"]) == 3
42
43
44 @pytest.mark.asyncio
45 async def test_default_page_and_last_page_has_no_cursor(client: AsyncClient, db_session: AsyncSession) -> None:
46 repo = await create_repo(db_session, visibility="public")
47 await _seed(db_session, repo.repo_id, 4)
48 body = (await client.get(f"/api/repos/{repo.repo_id}/objects")).json()
49 assert len(body["objects"]) == 4 and body["nextCursor"] is None
50
51
52 @pytest.mark.asyncio
53 @pytest.mark.parametrize("params", [{"cursor": "not-a-cursor"}, {"limit": 0}, {"limit": 1001}])
54 async def test_bad_cursor_or_limit_is_422(client: AsyncClient, db_session: AsyncSession, params: dict) -> None:
55 repo = await create_repo(db_session, visibility="public")
56 assert (await client.get(f"/api/repos/{repo.repo_id}/objects", params=params)).status_code == 422
57
58
59 @pytest.mark.asyncio
60 async def test_private_repo_still_requires_auth(client: AsyncClient, db_session: AsyncSession) -> None:
61 repo = await create_repo(db_session, visibility="private")
62 assert (await client.get(f"/api/repos/{repo.repo_id}/objects")).status_code == 401
File History 1 commit
sha256:0577579d44915d89d00bc5f88853a3ad18bc76c7e222515294866e1eb30d8bec feat(api): cursor-paginate GET /repos/{id}/objects (default… Opus 5.5 minor ⚠ 2 days ago