PageIndex/tests/test_page_content.py
mountain 2a69c76b7f fix: exact md page selection + restore CHATGPT_API_KEY alias
Address PR #272 review:

- get_md_page_content / retrieve._get_md_page_content returned every node
  whose line_num fell in [min(pages), max(pages)], so a non-contiguous spec
  like "5,100" over-fetched everything in between. Match the exact requested
  line numbers instead, mirroring the PDF path. (Same bug as #280.)
- Restore the CHATGPT_API_KEY -> OPENAI_API_KEY backward-compat alias dropped
  when pageindex/utils.py became a re-export shim; users with only
  CHATGPT_API_KEY set would otherwise fail auth after upgrading. It now runs
  in __init__.py right after load_dotenv.

Adds regression tests for both.

Claude-Session: https://claude.ai/code/session_01Kx5DgKbhK1N8autqXH8SmS
2026-07-08 17:33:41 +08:00

36 lines
1.4 KiB
Python

"""Markdown page-content selection must return exactly the requested lines,
mirroring the PDF path — not the whole [min, max] range (PR #272 review / #280)."""
def _md_structure():
# line_num 40 sits *between* 5 and 100 but is NOT requested below.
return [
{"line_num": 5, "text": "line five", "nodes": [
{"line_num": 40, "text": "line forty (should be excluded)", "nodes": []},
]},
{"line_num": 100, "text": "line hundred", "nodes": []},
{"line_num": 101, "text": "line 101", "nodes": []},
]
def test_get_md_page_content_returns_only_requested_lines():
from pageindex.index.utils import get_md_page_content
out = get_md_page_content(_md_structure(), [5, 100])
# exactly the two requested lines — not 5, 40, 100 (the old range behavior)
assert [r["page"] for r in out] == [5, 100]
assert all("forty" not in r["content"] for r in out)
def test_get_md_page_content_empty_spec():
from pageindex.index.utils import get_md_page_content
assert get_md_page_content(_md_structure(), []) == []
def test_retrieve_md_page_content_returns_only_requested_lines():
# The legacy retrieve path has its own copy of the same logic.
from pageindex.retrieve import _get_md_page_content
out = _get_md_page_content({"structure": _md_structure()}, [5, 100])
assert [r["page"] for r in out] == [5, 100]