Files
DeepTutor/tests/textbook_struct/test_chapter_rebuild.py
Rudy Celekli cbed908f6a fix(textbook_struct): keep the higher-page chapter hit when deduping
_dedupe_keep_last documents 'keep the body hit (higher page)', but both
branches assigned by_title[title] = chapter unconditionally, so it actually
kept the last occurrence in list order. rebuild() iterates layout['pdf_info']
without sorting (unlike the sibling rebuild_from_headers_level, which sorts by
page_idx), so when pdf_info is not ascending the TOC hit (lower page) could be
listed last and win — giving the chapter the TOC start page and a wrong page
range. Compare page_idx and keep the higher one.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-26 10:52:35 -04:00

32 lines
1.1 KiB
Python

"""Chapter de-duplication must keep the body hit, not the last in list order."""
from __future__ import annotations
from deeptutor.textbook_struct.chapter_rebuild import Chapter, _dedupe_keep_last
def test_dedupe_keeps_higher_page_regardless_of_list_order() -> None:
# A chapter title appears on a TOC page (page 1) and in the body (page 10).
# _dedupe_keep_last promises to keep the body hit (the higher page). When
# pdf_info is not ascending by page_idx — the body entry listed first — the
# unconditional overwrite kept the TOC hit instead.
chapters = [
Chapter(title="Vectors", page_idx=10, bbox=[]), # body
Chapter(title="Vectors", page_idx=1, bbox=[]), # TOC, later in list
]
kept = _dedupe_keep_last(chapters)
assert [c.page_idx for c in kept] == [10]
def test_dedupe_keeps_higher_page_when_already_sorted() -> None:
chapters = [
Chapter(title="Vectors", page_idx=1, bbox=[]), # TOC
Chapter(title="Vectors", page_idx=10, bbox=[]), # body
]
kept = _dedupe_keep_last(chapters)
assert [c.page_idx for c in kept] == [10]