From d8d672722742ca476d7b2a44ff61dd180743b5c5 Mon Sep 17 00:00:00 2001 From: Yiorgis Gozadinos Date: Thu, 28 May 2026 18:06:47 +0300 Subject: [PATCH] improve coverage --- tests/test_pdf_attachments.py | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/test_pdf_attachments.py b/tests/test_pdf_attachments.py index a838e473..1feb1b49 100644 --- a/tests/test_pdf_attachments.py +++ b/tests/test_pdf_attachments.py @@ -273,6 +273,62 @@ async def test_parent_without_uri_is_skipped(temp_db_path, monkeypatch): assert await client.list_documents() == [] +async def test_malformed_pdf_logs_warning_and_skips(temp_db_path, monkeypatch, caplog): + """A non-PDF body labelled as application/pdf must not crash the helper — + pypdfium2's open raises PdfiumError, which we log and return from.""" + import logging + + monkeypatch.setattr( + "haiku.rag.client.documents._ingest_fetch_result", + fake_ingest_fetch_result, + ) + async with HaikuRAG(temp_db_path, create=True) as client: + parent_uri = "file:///fixtures/junk.pdf" + garbage = b"this is not a pdf at all" + parent = await _make_parent(client, parent_uri, garbage) + with caplog.at_level(logging.WARNING, logger="haiku.rag.client.documents"): + await _reconcile_pdf_attachments(client, parent, garbage, depth=0) + assert await client.list_documents(filter=parent_uri_filter(parent_uri)) == [] + + +async def test_unsupported_attachment_continues_loop(temp_db_path, monkeypatch): + """One attachment whose ingest raises UnsupportedSourceError must not + prevent siblings from being ingested. The unsupported attachment is + skipped with a warning; the others land.""" + from haiku.rag.client.exceptions import UnsupportedSourceError + + async def picky_fake( + client, + result, + *, + title, + user_metadata, + stored_uri, + existing_doc, + depth=0, + ): + if stored_uri.endswith("unsupported.xyz"): + raise UnsupportedSourceError("nope") + return await fake_ingest_fetch_result( + client, + result, + title=title, + user_metadata=user_metadata, + stored_uri=stored_uri, + existing_doc=existing_doc, + depth=depth, + ) + + monkeypatch.setattr("haiku.rag.client.documents._ingest_fetch_result", picky_fake) + async with HaikuRAG(temp_db_path, create=True) as client: + parent_uri = "file:///fixtures/parent.pdf" + pdf_bytes = build_pdf([("ok.txt", b"keep me"), ("unsupported.xyz", b"data")]) + parent = await _make_parent(client, parent_uri, pdf_bytes) + await _reconcile_pdf_attachments(client, parent, pdf_bytes, depth=0) + children = await client.list_documents(filter=parent_uri_filter(parent_uri)) + assert {c.uri for c in children} == {f"{parent_uri}#attachment=ok.txt"} + + async def test_cascade_delete_removes_reconciled_children(temp_db_path, monkeypatch): monkeypatch.setattr( "haiku.rag.client.documents._ingest_fetch_result",