melbinjp/DocQA
0
1"""Pages survive extraction, so a citation can name one.2 3The API returned `text`, `score`, `doc_id` and `source` and no page, because4`load_source` handed back one flat string: PyMuPDF's own per-page output was5joined with newlines before anything downstream could see it. Page identity was6destroyed at the first step, so no amount of work later could recover it.7 8The second half is the extractor order. MarkItDown ran first for PDFs and9PyMuPDF only when MarkItDown returned *nothing*, so a PDF read badly rather than10not at all never reached the fallback. The deployed Space returned arXiv112005.11401 with the spaces removed; the same PDF and the same pinned12`markitdown==0.1.6` extract cleanly locally, because `markitdown[pdf]` pulls an13unpinned `pdfminer.six` that each image resolves for itself. Text quality14therefore varied by deployment. Silent mangling is worse than failure: a mangled15chunk still embeds, still retrieves, and still gets quoted back to a user.16"""17 18import fitz19import pytest20 21from utils.loaders import load_source_pages22from utils.splitter import split_pages23 24 25def _pdf(pages: list[str]) -> bytes:26 """A real PDF, built with the same library the loader reads it back with."""27 doc = fitz.open()28 for body in pages:29 page = doc.new_page()30 page.insert_text((72, 720), body, fontsize=11)31 raw = doc.tobytes()32 doc.close()33 return raw34 35 36def test_each_page_comes_back_separately_and_numbered_from_one():37 raw = _pdf(["Rent is payable fortnightly in advance.",38 "The bond is four weeks rent.",39 "Pets require written consent."])40 pages = load_source_pages(raw, "pdf")41 42 assert [n for n, _ in pages] == [1, 2, 3]43 assert "fortnightly" in pages[0][1]44 assert "bond" in pages[1][1]45 assert "consent" in pages[2][1]46 47 48def test_the_words_keep_their_spaces():49 """The regression that made citations unreadable rather than merely absent."""50 raw = _pdf(["The landlord must give sixty days written notice."])51 _, text = load_source_pages(raw, "pdf")[0]52 53 assert "written notice" in text54 assert not [w for w in text.split() if len(w) > 25], text55 56 57def test_every_chunk_knows_which_page_it_came_from():58 raw = _pdf(["Clause one about rent.", "Clause two about the bond."])59 chunks = split_pages(load_source_pages(raw, "pdf"))60 61 assert chunks, "the fixture produced no chunks"62 assert {c["page"] for c in chunks} == {1, 2}63 for c in chunks:64 assert c["page"] is not None65 assert c["text"].strip()66 67 68def test_a_chunk_never_straddles_a_page_boundary():69 """Splitting per page is the point. Over one joined string a chunk could carry70 text from two pages and be attributed to whichever came first, which is a71 citation that is confidently wrong."""72 raw = _pdf(["AAA " * 60, "BBB " * 60])73 chunks = split_pages(load_source_pages(raw, "pdf"))74 75 for c in chunks:76 assert not ("AAA" in c["text"] and "BBB" in c["text"]), c77 78 79def test_a_format_without_pages_says_so_rather_than_guessing():80 pages = load_source_pages(b"Plain text has no pages.", "txt")81 assert [n for n, _ in pages] == [None]82 assert split_pages(pages)[0]["page"] is None83 