2026-03-18 19:25:23 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
2026-04-08 15:21:20 +08:00
|
|
|
import base64
|
2026-03-18 19:25:23 +08:00
|
|
|
from pathlib import Path
|
2026-04-08 15:21:20 +08:00
|
|
|
from types import SimpleNamespace
|
2026-03-18 19:25:23 +08:00
|
|
|
|
|
|
|
|
import fitz
|
|
|
|
|
import pytest
|
|
|
|
|
import yuxi.plugins.parser.unified as parser_unified
|
|
|
|
|
from docx import Document
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
from yuxi.plugins.parser import Parser
|
|
|
|
|
from yuxi.plugins.parser.factory import DocumentProcessorFactory
|
|
|
|
|
|
2026-03-30 15:24:47 +08:00
|
|
|
DATA_DIR = Path(__file__).resolve().parents[2] / "data"
|
2026-03-18 19:25:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_pdf(file_path: Path, text: str) -> None:
|
|
|
|
|
doc = fitz.open()
|
|
|
|
|
page = doc.new_page()
|
|
|
|
|
page.insert_text((72, 72), text)
|
|
|
|
|
doc.save(str(file_path))
|
|
|
|
|
doc.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_docx(file_path: Path, text: str) -> None:
|
|
|
|
|
document = Document()
|
|
|
|
|
document.add_paragraph(text)
|
|
|
|
|
document.save(str(file_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_png(file_path: Path) -> None:
|
|
|
|
|
image = Image.new("RGB", (120, 80), "white")
|
|
|
|
|
image.save(str(file_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_parser_parse_pdf_file_returns_markdown_text(tmp_path: Path):
|
|
|
|
|
file_path = tmp_path / "parser_test.pdf"
|
|
|
|
|
_build_pdf(file_path, "Parser PDF content")
|
|
|
|
|
|
|
|
|
|
markdown = Parser.parse(str(file_path))
|
|
|
|
|
|
|
|
|
|
assert isinstance(markdown, str)
|
|
|
|
|
assert "Parser" in markdown
|
|
|
|
|
assert "content" in markdown
|
|
|
|
|
assert len(markdown.strip()) > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_parser_parse_docx_file_returns_markdown_text(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
|
|
|
file_path = tmp_path / "parser_test.docx"
|
|
|
|
|
_build_docx(file_path, "Parser DOCX content")
|
|
|
|
|
|
|
|
|
|
# 避免测试依赖 docling 行为,直接验证统一 parser 可回退到 python-docx。
|
|
|
|
|
def _raise_docling_error(*args, **kwargs):
|
|
|
|
|
raise RuntimeError("force fallback to python-docx")
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(parser_unified, "_convert_with_docling", _raise_docling_error)
|
|
|
|
|
|
|
|
|
|
markdown = Parser.parse(str(file_path))
|
|
|
|
|
|
|
|
|
|
assert isinstance(markdown, str)
|
|
|
|
|
assert "Parser DOCX content" in markdown
|
|
|
|
|
assert len(markdown.strip()) > 0
|
|
|
|
|
|
|
|
|
|
|
2026-04-08 15:21:20 +08:00
|
|
|
def test_convert_with_docling_reinserts_image_links_in_document_order(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
):
|
|
|
|
|
file_path = tmp_path / "parser_test.docx"
|
|
|
|
|
file_path.write_bytes(b"fake docx")
|
|
|
|
|
first_image = base64.b64encode(b"first image").decode()
|
|
|
|
|
second_image = base64.b64encode(b"second image").decode()
|
|
|
|
|
fake_doc = SimpleNamespace(
|
|
|
|
|
pictures=[
|
|
|
|
|
SimpleNamespace(image=SimpleNamespace(uri=f"data:image/png;base64,{first_image}")),
|
2026-05-12 10:17:44 +08:00
|
|
|
SimpleNamespace(image=SimpleNamespace(uri="https://example.test/remote.png")),
|
2026-04-08 15:21:20 +08:00
|
|
|
SimpleNamespace(image=SimpleNamespace(uri=f"data:image/png;base64,{second_image}")),
|
|
|
|
|
],
|
2026-05-12 10:17:44 +08:00
|
|
|
export_to_markdown=lambda: "before\n<!-- image -->\nremote\n<!-- image -->\nbetween\n<!-- image -->\nafter",
|
2026-04-08 15:21:20 +08:00
|
|
|
)
|
|
|
|
|
fake_result = SimpleNamespace(status=SimpleNamespace(name="SUCCESS"), document=fake_doc)
|
|
|
|
|
uploaded_images: list[bytes] = []
|
|
|
|
|
|
|
|
|
|
class FakeConverter:
|
|
|
|
|
def convert(self, path: Path):
|
|
|
|
|
assert path == file_path
|
|
|
|
|
return fake_result
|
|
|
|
|
|
|
|
|
|
def _fake_upload_image_to_minio(image_data, filename, bucket_name, object_prefix):
|
|
|
|
|
uploaded_images.append(image_data)
|
|
|
|
|
return f"https://example.test/{len(uploaded_images)}.png"
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(parser_unified, "_get_docling_converter", lambda: FakeConverter())
|
|
|
|
|
monkeypatch.setattr(parser_unified, "_upload_image_to_minio", _fake_upload_image_to_minio)
|
|
|
|
|
image_timestamps = iter([1.0, 2.0])
|
|
|
|
|
monkeypatch.setattr(parser_unified.time, "time", lambda: next(image_timestamps))
|
|
|
|
|
|
|
|
|
|
markdown = parser_unified._convert_with_docling(file_path)
|
|
|
|
|
|
|
|
|
|
assert uploaded_images == [b"first image", b"second image"]
|
|
|
|
|
assert markdown == (
|
|
|
|
|
"before\n"
|
|
|
|
|
"\n"
|
2026-05-12 10:17:44 +08:00
|
|
|
"remote\n"
|
|
|
|
|
"\n"
|
2026-04-08 15:21:20 +08:00
|
|
|
"between\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"after"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-05-12 10:17:44 +08:00
|
|
|
def test_convert_with_docling_keeps_image_placeholder_when_upload_fails(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
):
|
|
|
|
|
file_path = tmp_path / "parser_test.docx"
|
|
|
|
|
file_path.write_bytes(b"fake docx")
|
|
|
|
|
image = base64.b64encode(b"image data").decode()
|
|
|
|
|
fake_doc = SimpleNamespace(
|
|
|
|
|
pictures=[SimpleNamespace(image=SimpleNamespace(uri=f"data:image/png;base64,{image}"))],
|
|
|
|
|
export_to_markdown=lambda: "before\n<!-- image -->\nafter",
|
|
|
|
|
)
|
|
|
|
|
fake_result = SimpleNamespace(status=SimpleNamespace(name="SUCCESS"), document=fake_doc)
|
|
|
|
|
|
|
|
|
|
class FakeConverter:
|
|
|
|
|
def convert(self, path: Path):
|
|
|
|
|
assert path == file_path
|
|
|
|
|
return fake_result
|
|
|
|
|
|
|
|
|
|
def _raise_upload_error(*args, **kwargs):
|
|
|
|
|
raise RuntimeError("upload failed")
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(parser_unified, "_get_docling_converter", lambda: FakeConverter())
|
|
|
|
|
monkeypatch.setattr(parser_unified, "_upload_image_to_minio", _raise_upload_error)
|
|
|
|
|
monkeypatch.setattr(parser_unified.time, "time", lambda: 1.0)
|
|
|
|
|
|
|
|
|
|
markdown = parser_unified._convert_with_docling(file_path)
|
|
|
|
|
|
|
|
|
|
assert markdown == "before\n[图片: image_1000000.png]\nafter"
|
|
|
|
|
|
|
|
|
|
|
2026-03-18 19:25:23 +08:00
|
|
|
def test_parser_parse_png_file_returns_markdown_text_with_mocked_ocr(
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
):
|
|
|
|
|
file_path = tmp_path / "parser_test.png"
|
|
|
|
|
_build_png(file_path)
|
|
|
|
|
|
|
|
|
|
async def _fake_parse_image_async(file, params=None):
|
|
|
|
|
return "Parser PNG content"
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(parser_unified, "parse_image_async", _fake_parse_image_async)
|
|
|
|
|
|
2026-05-16 17:21:27 +08:00
|
|
|
markdown = Parser.parse(str(file_path), params={"ocr_engine": "rapid_ocr"})
|
2026-03-18 19:25:23 +08:00
|
|
|
|
|
|
|
|
assert isinstance(markdown, str)
|
|
|
|
|
assert "Parser PNG content" in markdown
|
|
|
|
|
assert len(markdown.strip()) > 0
|
|
|
|
|
|
|
|
|
|
|
2026-05-16 17:21:27 +08:00
|
|
|
def test_parse_image_uses_ocr_engine_config(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
|
|
|
file_path = tmp_path / "parser_test.png"
|
|
|
|
|
_build_png(file_path)
|
|
|
|
|
captured = {}
|
|
|
|
|
|
|
|
|
|
def _fake_process_file(processor_type, file, params=None):
|
|
|
|
|
captured["processor_type"] = processor_type
|
|
|
|
|
captured["file"] = file
|
|
|
|
|
captured["params"] = params
|
|
|
|
|
return "OCR content"
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(DocumentProcessorFactory, "process_file", _fake_process_file)
|
|
|
|
|
|
|
|
|
|
result = parser_unified.parse_image(
|
|
|
|
|
str(file_path),
|
|
|
|
|
params={
|
|
|
|
|
"ocr_engine": "mineru_ocr",
|
|
|
|
|
"backend": "old-backend",
|
|
|
|
|
"ocr_engine_config": {"backend": "pipeline", "formula_enable": False},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result == "OCR content"
|
|
|
|
|
assert captured["processor_type"] == "mineru_ocr"
|
|
|
|
|
assert captured["file"] == str(file_path)
|
|
|
|
|
assert captured["params"]["backend"] == "pipeline"
|
|
|
|
|
assert captured["params"]["formula_enable"] is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_image_ignores_enable_ocr(tmp_path: Path) -> None:
|
|
|
|
|
file_path = tmp_path / "parser_test.png"
|
|
|
|
|
_build_png(file_path)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="必须启用OCR"):
|
|
|
|
|
parser_unified.parse_image(str(file_path), params={"enable_ocr": "rapid_ocr"})
|
|
|
|
|
|
|
|
|
|
|
2026-03-18 19:25:23 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_parser_aparse_pdf_file_returns_markdown_text(tmp_path: Path):
|
|
|
|
|
file_path = tmp_path / "parser_test_async.pdf"
|
|
|
|
|
_build_pdf(file_path, "Async Parser PDF content")
|
|
|
|
|
|
|
|
|
|
markdown = await Parser.aparse(str(file_path))
|
|
|
|
|
|
|
|
|
|
assert isinstance(markdown, str)
|
|
|
|
|
assert "Async" in markdown
|
|
|
|
|
assert "content" in markdown
|
|
|
|
|
assert len(markdown.strip()) > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_parser_aparse_image_file_with_mineru_when_available():
|
|
|
|
|
file_path = DATA_DIR / "测试图片.png"
|
|
|
|
|
assert file_path.exists(), f"测试文件不存在: {file_path}"
|
|
|
|
|
|
|
|
|
|
health = await asyncio.to_thread(DocumentProcessorFactory.check_health, "mineru_ocr")
|
|
|
|
|
if health.get("status") != "healthy":
|
|
|
|
|
pytest.skip(f"mineru_ocr 不可用: {health.get('message', 'unknown')}")
|
|
|
|
|
|
|
|
|
|
markdown = await Parser.aparse(
|
|
|
|
|
str(file_path),
|
2026-05-16 17:21:27 +08:00
|
|
|
params={"ocr_engine": "mineru_ocr", "backend": "pipeline"},
|
2026-03-18 19:25:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert isinstance(markdown, str)
|
|
|
|
|
assert len(markdown) > 100
|
|
|
|
|
assert len(markdown.strip()) > 0
|