|
| 1 | +from typing import Any, List, Type, Union, Optional |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +from kernel.pagination import SyncOffsetPagination, AsyncOffsetPagination |
| 7 | + |
| 8 | +PageClass = Union[Type[SyncOffsetPagination[Any]], Type[AsyncOffsetPagination[Any]]] |
| 9 | + |
| 10 | +# build() and next_page_info() are plain synchronous methods on both classes, |
| 11 | +# so both generated variants are pinned against drifting apart on regeneration. |
| 12 | +both_classes = pytest.mark.parametrize("cls", [SyncOffsetPagination, AsyncOffsetPagination]) |
| 13 | + |
| 14 | + |
| 15 | +def _page(cls: PageClass, *, items: List[Any], next_offset: Optional[int], has_more: Optional[bool]) -> Any: |
| 16 | + headers: dict[str, str] = {} |
| 17 | + if next_offset is not None: |
| 18 | + headers["X-Next-Offset"] = str(next_offset) |
| 19 | + if has_more is not None: |
| 20 | + headers["X-Has-More"] = "true" if has_more else "false" |
| 21 | + response = httpx.Response(200, headers=headers) |
| 22 | + return cls.build(response=response, data=items) |
| 23 | + |
| 24 | + |
| 25 | +@both_classes |
| 26 | +def test_next_page_starts_at_exactly_x_next_offset(cls: PageClass) -> None: |
| 27 | + # X-Next-Offset already holds the next page's start. Adding the current |
| 28 | + # page length on top (the old behavior) skipped a full page per iteration. |
| 29 | + page = _page(cls, items=[{}] * 100, next_offset=100, has_more=True) |
| 30 | + info = page.next_page_info() |
| 31 | + assert info is not None |
| 32 | + assert info.params == {"offset": 100} |
| 33 | + |
| 34 | + |
| 35 | +@both_classes |
| 36 | +def test_stops_cleanly_when_last_page_omits_x_next_offset(cls: PageClass) -> None: |
| 37 | + page = _page(cls, items=[{}] * 50, next_offset=None, has_more=False) |
| 38 | + assert page.next_page_info() is None |
| 39 | + assert page.has_next_page() is False |
| 40 | + |
| 41 | + |
| 42 | +@both_classes |
| 43 | +def test_stops_when_x_has_more_false(cls: PageClass) -> None: |
| 44 | + page = _page(cls, items=[{}] * 50, next_offset=200, has_more=False) |
| 45 | + assert page.has_next_page() is False |
| 46 | + |
| 47 | + |
| 48 | +@both_classes |
| 49 | +def test_stops_on_next_offset_zero_sentinel(cls: PageClass) -> None: |
| 50 | + # 0 is the API's last-page sentinel. Assert via next_page_info directly, |
| 51 | + # with has_more=True, so the has_more gate cannot mask the 0 handling. |
| 52 | + page = _page(cls, items=[{}] * 50, next_offset=0, has_more=True) |
| 53 | + assert page.next_page_info() is None |
| 54 | + |
| 55 | + |
| 56 | +@both_classes |
| 57 | +def test_stops_on_empty_page(cls: PageClass) -> None: |
| 58 | + page = _page(cls, items=[], next_offset=300, has_more=True) |
| 59 | + assert page.has_next_page() is False |
| 60 | + |
| 61 | + |
| 62 | +@both_classes |
| 63 | +def test_refuses_to_silently_truncate_on_contradictory_headers(cls: PageClass) -> None: |
| 64 | + page = _page(cls, items=[{}] * 100, next_offset=None, has_more=True) |
| 65 | + with pytest.raises(RuntimeError, match="refusing to silently truncate"): |
| 66 | + page.has_next_page() |
0 commit comments