Skip to content

Your lightweight open-source personal agent.

19.1 KB
Raw Blame
from pathlib import Path
from zipfile import ZipFile

import pytest

from nanobot.utils.document import (
    PdfSafetyError,
    _is_text_extension,
    extract_pdf_pages,
    extract_text,
    open_document_line_source,
)


class TestExtractText:

    def test_extract_text_unsupported_returns_none(self, tmp_path: Path):
        unsupported_file = tmp_path / "file.xyz"
        unsupported_file.write_text("content")

        result = extract_text(unsupported_file)
        assert result is None

    def test_extract_text_file_not_found(self, tmp_path: Path):
        missing_file = tmp_path / "nonexistent.txt"

        result = extract_text(missing_file)
        assert result is not None
        assert "[error: file not found:" in result

    def test_extract_text_txt_file(self, tmp_path: Path):
        txt_file = tmp_path / "test.txt"
        content = "Hello, world!\nThis is a test."
        txt_file.write_text(content, encoding="utf-8")

        result = extract_text(txt_file)
        assert result == content

    @pytest.mark.parametrize("encoding", ["utf-8-sig", "utf-16", "utf-32"])
    def test_extract_text_respects_unicode_bom(self, tmp_path: Path, encoding: str):
        txt_file = tmp_path / "unicode.txt"
        content = "Hello 世界\nSecond line"
        txt_file.write_bytes(content.encode(encoding))

        assert extract_text(txt_file) == content

    def test_extract_text_accepts_string_path(self, tmp_path: Path):
        txt_file = tmp_path / "string-path.txt"
        txt_file.write_text("string path", encoding="utf-8")

        assert extract_text(str(txt_file)) == "string path"

    def test_extract_text_txt_file_with_truncation(self, tmp_path: Path):
        txt_file = tmp_path / "large.txt"
        content = "x" * 300_000
        txt_file.write_text(content, encoding="utf-8")

        result = extract_text(txt_file)
        assert len(result) < 300_000
        assert "(truncated," in result
        assert "chars total)" in result

    def test_extract_text_md_file(self, tmp_path: Path):
        md_file = tmp_path / "test.md"
        content = "# Header\n\nSome markdown content."
        md_file.write_text(content, encoding="utf-8")

        result = extract_text(md_file)
        assert result == content

    def test_extract_text_csv_file(self, tmp_path: Path):
        csv_file = tmp_path / "test.csv"
        content = "name,age\nAlice,30\nBob,25"
        csv_file.write_text(content, encoding="utf-8")

        result = extract_text(csv_file)
        assert result == content

    def test_extract_text_json_file(self, tmp_path: Path):
        json_file = tmp_path / "test.json"
        content = '{"key": "value", "number": 42}'
        json_file.write_text(content, encoding="utf-8")

        result = extract_text(json_file)
        assert result == content

    def test_pdf_search_lines_expose_page_continuation(
        self,
        tmp_path: Path,
        monkeypatch: pytest.MonkeyPatch,
    ):
        pdf_file = tmp_path / "large.pdf"
        pdf_file.write_bytes(b"%PDF")

        class _Page:
            @staticmethod
            def get_contents():
                return None

            @staticmethod
            def extract_text():
                return "needle"

        class _Reader:
            def __init__(self, *_args, **_kwargs):
                self.pages = [_Page() for _ in range(250)]

        monkeypatch.setattr("pypdf.PdfReader", _Reader)

        source = open_document_line_source(pdf_file, pages="101-200")

        assert source is not None
        iterator = source.lines
        next(iterator)
        line = next(iterator)
        iterator.close()
        assert line.locator == "page=101,line=1"
        assert source.continuation == "pages='201-250'"

    def test_extract_text_xlsx(self, tmp_path: Path):
        from openpyxl import Workbook

        xlsx_file = tmp_path / "test.xlsx"
        wb = Workbook()
        ws = wb.active
        ws.title = "Sheet1"
        ws["A1"] = "Name"
        ws["B1"] = "Age"
        ws["A2"] = "Alice"
        ws["B2"] = 30
        ws["A3"] = "Bob"
        ws["B3"] = 25

        ws2 = wb.create_sheet("Sheet2")
        ws2["A1"] = "Product"
        ws2["B1"] = "Price"
        ws2["A2"] = "Widget"
        ws2["B2"] = 9.99

        wb.save(xlsx_file)
        wb.close()

        result = extract_text(xlsx_file)
        assert result is not None
        assert "--- Sheet: Sheet1 ---" in result
        assert "--- Sheet: Sheet2 ---" in result
        assert "Alice" in result
        assert "Bob" in result
        assert "Widget" in result
        assert "9.99" in result

    @pytest.mark.parametrize("chart_index", [0, 1, 2])
    def test_extract_text_xlsx_with_chartsheet(self, tmp_path: Path, chart_index: int):
        from openpyxl import Workbook
        from openpyxl.chart import BarChart, Reference

        xlsx_file = tmp_path / "report.xlsx"
        workbook = Workbook()
        first_sheet = workbook.active
        first_sheet.title = "Sales"
        first_sheet.append(["Revenue", 42])
        second_sheet = workbook.create_sheet("Notes")
        second_sheet.append(["Forecast", 50])
        chart = BarChart()
        chart.add_data(Reference(first_sheet, min_col=2, min_row=1))
        workbook.create_chartsheet("Dashboard", index=chart_index).add_chart(chart)
        workbook.save(xlsx_file)
        workbook.close()

        assert extract_text(xlsx_file) == (
            "--- Sheet: Sales ---\nRevenue\t42\n\n--- Sheet: Notes ---\nForecast\t50"
        )
        source = open_document_line_source(xlsx_file)
        assert source is not None
        assert [
            (line.text, line.extracted_line, line.locator)
            for line in source.lines
            if line.searchable
        ] == [
            ("Revenue\t42", 2, "sheet='Sales',row=1"),
            ("Forecast\t50", 5, "sheet='Notes',row=1"),
        ]

    def test_extract_text_xlsx_empty_sheet(self, tmp_path: Path):
        from openpyxl import Workbook

        xlsx_file = tmp_path / "empty.xlsx"
        wb = Workbook()
        wb.remove(wb.active)
        wb.create_sheet("EmptySheet")
        wb.save(xlsx_file)
        wb.close()

        result = extract_text(xlsx_file)
        assert result == "--- Sheet: EmptySheet ---" or result == ""

    def test_extract_text_docx(self, tmp_path: Path):
        from docx import Document

        docx_file = tmp_path / "test.docx"
        doc = Document()
        doc.add_heading("Test Document", 0)
        doc.add_paragraph("This is paragraph one.")
        doc.add_paragraph("This is paragraph two.")
        doc.save(docx_file)

        result = extract_text(docx_file)
        assert result is not None
        assert "Test Document" in result
        assert "This is paragraph one." in result
        assert "This is paragraph two." in result

    def test_extract_text_docx_preserves_paragraph_and_table_order(self, tmp_path: Path):
        from docx import Document

        docx_file = tmp_path / "form.docx"
        doc = Document()
        doc.add_paragraph("Applicant details")
        table = doc.add_table(rows=2, cols=2)
        table.cell(0, 0).text = "Name"
        table.cell(0, 1).text = "Ada Lovelace"
        table.cell(1, 0).text = "Project"
        table.cell(1, 1).text = "Analytical Engine"
        doc.add_paragraph("End of form")
        doc.save(docx_file)

        result = extract_text(docx_file)

        assert result is not None
        assert "Name\tAda Lovelace" in result
        assert "Project\tAnalytical Engine" in result
        assert result.index("Applicant details") < result.index("Name\tAda Lovelace")
        assert result.index("Analytical Engine") < result.index("End of form")

    def test_extract_text_docx_preserves_nested_table_text(self, tmp_path: Path):
        from docx import Document

        docx_file = tmp_path / "nested-form.docx"
        doc = Document()
        outer_cell = doc.add_table(rows=1, cols=1).cell(0, 0)
        outer_cell.add_paragraph("Contact")
        nested = outer_cell.add_table(rows=1, cols=2)
        nested.cell(0, 0).text = "Email"
        nested.cell(0, 1).text = "[email protected]"
        outer_cell.add_paragraph("Preferred contact")
        doc.save(docx_file)

        result = extract_text(docx_file)

        assert result is not None
        assert "Contact" in result
        assert "Email" in result
        assert "[email protected]" in result
        assert "Preferred contact" in result
        assert (
            result.index("Contact")
            < result.index("Email")
            < result.index("[email protected]")
            < result.index("Preferred contact")
        )

    def test_extract_text_docx_does_not_expand_grid_spans(
        self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
    ):
        from docx import Document
        from docx.table import _Row

        docx_file = tmp_path / "merged.docx"
        doc = Document()
        table = doc.add_table(rows=1, cols=2)
        table.cell(0, 0).merge(table.cell(0, 1)).text = "Only once"
        doc.save(docx_file)

        def fail_on_expansion(_row: _Row):
            pytest.fail("row.cells expands gridSpan before extraction can apply a bound")

        monkeypatch.setattr(_Row, "cells", property(fail_on_expansion))

        assert extract_text(docx_file) == "Only once"

    def test_extract_text_docx_keeps_vertical_merges_compact(self, tmp_path: Path):
        from docx import Document

        docx_file = tmp_path / "vertical-merge.docx"
        doc = Document()
        table = doc.add_table(rows=2, cols=2)
        table.cell(0, 0).merge(table.cell(1, 0)).text = "Group"
        table.cell(0, 1).text = "First"
        table.cell(1, 1).text = "Second"
        doc.save(docx_file)

        assert extract_text(docx_file) == "Group\tFirst\n\tSecond"

    def test_extract_text_docx_bounds_physical_table_cells(
        self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
    ):
        from docx import Document

        from nanobot.utils import document as document_utils

        docx_file = tmp_path / "too-many-cells.docx"
        doc = Document()
        doc.add_table(rows=1, cols=2)
        doc.save(docx_file)
        monkeypatch.setattr(document_utils, "_MAX_DOCX_TABLE_CELLS", 1)

        result = extract_text(docx_file)

        assert result is not None
        assert result.startswith("[error: unsafe DOCX:")

    def test_extract_text_docx_bounds_table_nesting(
        self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
    ):
        from docx import Document

        from nanobot.utils import document as document_utils

        docx_file = tmp_path / "nested-too-deep.docx"
        doc = Document()
        outer_cell = doc.add_table(rows=1, cols=1).cell(0, 0)
        outer_cell.add_table(rows=1, cols=1).cell(0, 0).text = "Nested"
        doc.save(docx_file)
        monkeypatch.setattr(document_utils, "_MAX_DOCX_TABLE_DEPTH", 1)

        result = extract_text(docx_file)

        assert result is not None
        assert result.startswith("[error: unsafe DOCX:")

    def test_extract_text_docx_empty(self, tmp_path: Path):
        from docx import Document

        docx_file = tmp_path / "empty.docx"
        doc = Document()
        doc.save(docx_file)

        result = extract_text(docx_file)
        assert result == ""

    def test_extract_text_pptx(self, tmp_path: Path):
        from pptx import Presentation

        pptx_file = tmp_path / "test.pptx"
        prs = Presentation()

        slide1 = prs.slides.add_slide(prs.slide_layouts[0])
        for shape in slide1.shapes:
            if hasattr(shape, "text"):
                shape.text = "First Slide Title"

        slide2 = prs.slides.add_slide(prs.slide_layouts[5])
        left = top = width = height = 1000000
        textbox = slide2.shapes.add_textbox(left, top, width, height)
        text_frame = textbox.text_frame
        text_frame.text = "Bullet point content"

        prs.save(pptx_file)

        result = extract_text(pptx_file)
        assert result is not None
        assert "--- Slide 1 ---" in result
        assert "--- Slide 2 ---" in result
        # Text content may vary depending on PowerPoint layout defaults
        assert len(result) > 0

    def test_extract_text_pptx_table(self, tmp_path: Path):
        from pptx import Presentation
        from pptx.util import Inches

        pptx_file = tmp_path / "table.pptx"
        prs = Presentation()
        slide = prs.slides.add_slide(prs.slide_layouts[5])
        table = slide.shapes.add_table(
            2, 2, Inches(1), Inches(1), Inches(4), Inches(1)
        ).table
        table.cell(0, 0).text = "Header A"
        table.cell(0, 1).text = "Header B"
        table.cell(1, 0).text = "Alice"
        table.cell(1, 1).text = "Bob"
        prs.save(pptx_file)

        result = extract_text(pptx_file)
        assert result is not None
        assert "Header A" in result
        assert "Header B" in result
        assert "Alice" in result
        assert "Bob" in result

    @pytest.mark.parametrize(
        ("cells", "expected"),
        [
            (("Alice", "", "100"), "Alice\t\t100"),
            (("", "10", "100"), "\t10\t100"),
            (("Alice", "10", ""), "Alice\t10\t"),
            (("", " ", ""), None),
        ],
    )
    def test_extract_text_pptx_table_preserves_empty_columns(
        self, tmp_path: Path, cells: tuple[str, ...], expected: str | None
    ):
        from pptx import Presentation
        from pptx.util import Inches

        pptx_file = tmp_path / "empty-columns.pptx"
        prs = Presentation()
        slide = prs.slides.add_slide(prs.slide_layouts[6])
        table = slide.shapes.add_table(
            2, 3, Inches(1), Inches(1), Inches(4), Inches(1)
        ).table
        for column, text in enumerate(("Name", "Q1", "Q2")):
            table.cell(0, column).text = text
        for column, text in enumerate(cells):
            table.cell(1, column).text = text
        prs.save(pptx_file)

        result = extract_text(pptx_file)
        expected_lines = ["--- Slide 1 ---", "Name\tQ1\tQ2"]
        if expected is not None:
            expected_lines.append(expected)
        assert result == "\n".join(expected_lines)

    def test_extract_text_pptx_grouped_shapes(self, tmp_path: Path):
        from pptx import Presentation
        from pptx.util import Inches

        pptx_file = tmp_path / "grouped.pptx"
        prs = Presentation()
        slide = prs.slides.add_slide(prs.slide_layouts[5])
        group = slide.shapes.add_group_shape()
        inner = group.shapes.add_textbox(
            Inches(1), Inches(1), Inches(3), Inches(1)
        )
        inner.text_frame.text = "Inside group"
        prs.save(pptx_file)

        result = extract_text(pptx_file)
        assert result is not None
        assert "Inside group" in result

    def test_extract_text_rejects_oversized_office_archive(self, tmp_path, monkeypatch):
        office_file = tmp_path / "oversized.docx"
        with ZipFile(office_file, "w") as archive:
            archive.writestr("word/document.xml", "x" * 32)

        monkeypatch.setattr("nanobot.utils.document._MAX_OFFICE_UNCOMPRESSED_SIZE", 16)

        assert "Office document expands beyond" in (extract_text(office_file) or "")

    def test_extract_text_stops_streaming_xlsx_at_text_limit(self, tmp_path, monkeypatch):
        from openpyxl import Workbook, load_workbook

        xlsx_file = tmp_path / "large.xlsx"
        wb = Workbook(write_only=True)
        ws = wb.create_sheet()
        for index in range(100):
            ws.append([f"row-{index}-" + "x" * 20])
        wb.save(xlsx_file)

        visited = 0
        real_load_workbook = load_workbook

        def tracked_load_workbook(*args, **kwargs):
            workbook = real_load_workbook(*args, **kwargs)
            worksheet = workbook[workbook.sheetnames[0]]
            original_iter_rows = worksheet.iter_rows

            def tracked_rows(*row_args, **row_kwargs):
                nonlocal visited
                for row in original_iter_rows(*row_args, **row_kwargs):
                    visited += 1
                    yield row

            worksheet.iter_rows = tracked_rows
            return workbook

        monkeypatch.setattr("openpyxl.load_workbook", tracked_load_workbook)
        monkeypatch.setattr("nanobot.utils.document._MAX_TEXT_LENGTH", 80)

        result = extract_text(xlsx_file)

        assert result is not None
        assert "truncated at 80 chars" in result
        assert visited < 100

    def test_extract_pdf_pages_rejects_large_content_stream(self, tmp_path, monkeypatch):
        class _Contents:
            @staticmethod
            def get_data():
                return b"x" * 17

        class _Page:
            @staticmethod
            def get_contents():
                return _Contents()

            @staticmethod
            def extract_text():
                return "should not be reached"

        class _Reader:
            def __init__(self, *_args, **_kwargs):
                self.pages = [_Page()]

        monkeypatch.setattr("pypdf.PdfReader", _Reader)
        monkeypatch.setattr("nanobot.utils.document._MAX_PDF_CONTENT_STREAM_SIZE", 16)

        with pytest.raises(PdfSafetyError, match="content stream exceeds"):
            extract_pdf_pages(tmp_path / "large.pdf")

    def test_extract_text_pdf_not_found(self, tmp_path: Path):
        missing_pdf = tmp_path / "nonexistent.pdf"

        result = extract_text(missing_pdf)
        assert result is not None
        assert "[error: file not found:" in result

    def test_extract_text_image_files(self, tmp_path: Path):
        png_file = tmp_path / "test.png"
        png_data = (
            b"\x89PNG\r\n\x1a\n"
            b"\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
            b"\x08\x02\x00\x00\x00\x90wS\xde"
            b"\x00\x00\x00\x0cIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01"
            b"\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82"
        )
        png_file.write_bytes(png_data)

        result = extract_text(png_file)
        assert result is not None
        assert "[image:" in result
        assert "test.png" in result


class TestIsTextExtension:

    def test_text_extensions_return_true(self):
        assert _is_text_extension(".txt") is True
        assert _is_text_extension(".md") is True
        assert _is_text_extension(".csv") is True
        assert _is_text_extension(".json") is True
        assert _is_text_extension(".yaml") is True
        assert _is_text_extension(".yml") is True
        assert _is_text_extension(".xml") is True
        assert _is_text_extension(".html") is True
        assert _is_text_extension(".htm") is True

    def test_non_text_extensions_return_false(self):
        assert _is_text_extension(".pdf") is False
        assert _is_text_extension(".docx") is False
        assert _is_text_extension(".xlsx") is False
        assert _is_text_extension(".pptx") is False
        assert _is_text_extension(".png") is False
        assert _is_text_extension(".xyz") is False

    def test_case_sensitivity(self):
        """Test that _is_text_extension requires lowercase extension.

        Note: The main extract_text function handles case-insensitivity by
        converting extensions to lowercase before calling _is_text_extension.
        """
        assert _is_text_extension(".txt") is True
        assert _is_text_extension(".TXT") is False
        assert _is_text_extension(".pdf") is False