import shutil import pymupdf as fitz import pytest from docx import Document from pptx import Presentation from pptx.util import Inches from wisegrade.parsing import _ocr_engine, parse_document def test_pdf_pages_and_images(tmp_path): pdf = fitz.open() pdf.new_page().insert_text((72, 72), "A sample engineering report with sufficiently long searchable text.") path = tmp_path / "report.pdf" pdf.save(path) parsed = parse_document(path, "final_report", tmp_path / "assets", ocr=False) assert parsed["pages"][0]["page_number"] == 1 assert "engineering" in parsed["pages"][0]["text"] assert (tmp_path / "assets" / "page-1.png").exists() def test_scanned_pdf_requires_offline_ocr_package(tmp_path): with pytest.raises(RuntimeError, match="离线OCR模型目录"): _ocr_engine(str(tmp_path / "missing-ocr")) @pytest.mark.skipif(not shutil.which("libreoffice"), reason="LibreOffice is installed in the deployment image") def test_docx_and_pptx_render(tmp_path): word = Document() word.add_heading("Automation experiment", 1) word.add_paragraph("The system measured repeatable engineering results.") table = word.add_table(rows=1, cols=2) table.cell(0, 0).text, table.cell(0, 1).text = "metric", "accuracy" docx = tmp_path / "stage.docx" word.save(docx) parsed = parse_document(docx, "stage2", tmp_path / "docx-assets", ocr=False) assert parsed["headings"] and parsed["tables"] assert parsed["pages"][0]["rendered_image_path"] deck = Presentation() slide = deck.slides.add_slide(deck.slide_layouts[5]) slide.shapes.title.text = "Robot presentation" box = slide.shapes.add_textbox(Inches(1), Inches(2), Inches(4), Inches(1)) box.text = "Results and analysis" pptx = tmp_path / "slides.pptx" deck.save(pptx) slides = parse_document(pptx, "presentation", tmp_path / "pptx-assets", ocr=False)["pages"] assert len(slides) == 1 and "Robot presentation" in slides[0]["text"] assert slides[0]["rendered_image_path"]