Coverage for src/local_deep_research/web/utils/pagination.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-22 21:20 +0000

1"""Shared offset validation for history and library listings.""" 

2 

3MAX_OFFSET = 10_000 

4OFFSET_LIMIT_ERROR = f"Offset must not exceed {MAX_OFFSET}" 

5 

6 

7def parse_bounded_offset(value: str | None) -> int: 

8 """Return an offset within the listing limit, or raise ``ValueError``. 

9 

10 Missing, malformed and negative values retain the legacy zero fallback. 

11 Values above the ceiling are rejected rather than clamped to another page. 

12 Callers use ``OFFSET_LIMIT_ERROR`` for the HTTP 400 response rather than 

13 exposing exception text. 

14 """ 

15 try: 

16 offset = max(0, int(value)) if value is not None else 0 

17 except (TypeError, ValueError): 

18 return 0 

19 

20 if offset > MAX_OFFSET: 

21 raise ValueError(OFFSET_LIMIT_ERROR) 

22 return offset