201 lines
7.1 KiB
Python
201 lines
7.1 KiB
Python
"""Regression tests for the OntoCast `convert_document` agent.
|
|
|
|
Covers the multi-file corpus extension described in
|
|
`docs/통합설계서.md` §5 Phase 0 and OntoCast 분석 §13.1 / §21.1.
|
|
|
|
Original behavior: the loop overwrote `state.input_text` on every iteration,
|
|
so multi-file input silently lost all but the last file. The fix accumulates
|
|
into one corpus with explicit file-boundary separators while keeping
|
|
single-file behavior byte-identical.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
VENDORED_ONTOCAST = REPO_ROOT / "vendored" / "ontocast"
|
|
if str(VENDORED_ONTOCAST) not in sys.path:
|
|
sys.path.insert(0, str(VENDORED_ONTOCAST))
|
|
|
|
from ontocast.agent import convert_document as convert_document_module # noqa: E402
|
|
from ontocast.onto.enum import Status # noqa: E402
|
|
|
|
|
|
class _StubConverter:
|
|
"""Minimal stand-in for `ConverterTool`.
|
|
|
|
Accepts a fake PDF/DOCX file (any bytes) and returns a dict shaped like
|
|
the real converter's output.
|
|
"""
|
|
|
|
supported_extensions = {".pdf", ".docx"}
|
|
|
|
def __init__(self, mapping: dict[bytes, str]) -> None:
|
|
self._mapping = mapping
|
|
|
|
def __call__(self, file_content: bytes) -> dict[str, str]:
|
|
return {"text": self._mapping[file_content]}
|
|
|
|
|
|
def _make_state(files: dict[str, bytes]) -> SimpleNamespace:
|
|
"""Build a lightweight stand-in for `AgentState`.
|
|
|
|
We intentionally avoid constructing the real Pydantic model here — its
|
|
initialization touches many unrelated fields and tools. We only mirror
|
|
the attributes that `convert_document` reads or writes.
|
|
"""
|
|
captured_text: list[str] = []
|
|
|
|
def set_text(text: str) -> None:
|
|
captured_text.append(text)
|
|
|
|
state = SimpleNamespace(
|
|
files=files,
|
|
status=None,
|
|
input_text="",
|
|
ontology_user_instruction="",
|
|
facts_user_instruction="",
|
|
source_url=None,
|
|
set_text=set_text,
|
|
_captured_text=captured_text,
|
|
)
|
|
return state
|
|
|
|
|
|
def _make_tools(converter_mapping: dict[bytes, str]) -> SimpleNamespace:
|
|
return SimpleNamespace(converter=_StubConverter(converter_mapping))
|
|
|
|
|
|
# ─── Case 1: single PDF — output must equal the file's text verbatim ─────
|
|
def test_single_pdf_passes_through_unchanged() -> None:
|
|
pdf_bytes = b"%PDF-1.4 fake"
|
|
state = _make_state({"sample.pdf": pdf_bytes})
|
|
tools = _make_tools({pdf_bytes: "Hello from PDF."})
|
|
|
|
result = convert_document_module.convert_document(state, tools)
|
|
|
|
assert result.status == Status.SUCCESS
|
|
assert state._captured_text == ["Hello from PDF."]
|
|
|
|
|
|
# ─── Case 2: single JSON — text + corpus metadata flows through ──────────
|
|
def test_single_json_extracts_metadata_and_text() -> None:
|
|
payload = {
|
|
"text": "Body of the article.",
|
|
"url": "https://example.com/a",
|
|
"ontology_user_instruction": "Focus on organizations.",
|
|
"facts_user_instruction": "Extract person-org links.",
|
|
}
|
|
state = _make_state({"a.json": json.dumps(payload).encode("utf-8")})
|
|
tools = _make_tools({})
|
|
|
|
result = convert_document_module.convert_document(state, tools)
|
|
|
|
assert result.status == Status.SUCCESS
|
|
assert state._captured_text == ["Body of the article."]
|
|
assert state.source_url == "https://example.com/a"
|
|
assert state.ontology_user_instruction == "Focus on organizations."
|
|
assert state.facts_user_instruction == "Extract person-org links."
|
|
|
|
|
|
# ─── Case 3: multiple PDFs — both bodies survive with boundary marker ────
|
|
def test_multiple_pdfs_are_concatenated_with_boundary() -> None:
|
|
"""The legacy bug: only the last file's text survived. After the fix,
|
|
both texts appear in the corpus separated by `=== File: <name> ===`."""
|
|
pdf_a = b"%PDF-1.4 A"
|
|
pdf_b = b"%PDF-1.4 B"
|
|
state = _make_state({"a.pdf": pdf_a, "b.pdf": pdf_b})
|
|
tools = _make_tools({pdf_a: "Text A.", pdf_b: "Text B."})
|
|
|
|
convert_document_module.convert_document(state, tools)
|
|
|
|
corpus = state._captured_text[-1]
|
|
assert "Text A." in corpus
|
|
assert "Text B." in corpus
|
|
assert "=== File: a.pdf ===" in corpus
|
|
assert "=== File: b.pdf ===" in corpus
|
|
# Ordering: a.pdf before b.pdf (insertion order preserved)
|
|
assert corpus.index("Text A.") < corpus.index("Text B.")
|
|
|
|
|
|
# ─── Case 4: multiple JSONs — first-wins for corpus metadata ─────────────
|
|
def test_multiple_jsons_keep_first_metadata() -> None:
|
|
"""`ontology_user_instruction`, `facts_user_instruction`, `source_url`
|
|
are corpus-level singletons. The first JSON file that provides each
|
|
wins; later JSONs do not overwrite."""
|
|
payload_a = {
|
|
"text": "Body A.",
|
|
"url": "https://first.example/a",
|
|
"ontology_user_instruction": "First instruction.",
|
|
"facts_user_instruction": "First facts.",
|
|
}
|
|
payload_b = {
|
|
"text": "Body B.",
|
|
"url": "https://second.example/b",
|
|
"ontology_user_instruction": "Second instruction (must be ignored).",
|
|
"facts_user_instruction": "Second facts (must be ignored).",
|
|
}
|
|
state = _make_state(
|
|
{
|
|
"a.json": json.dumps(payload_a).encode("utf-8"),
|
|
"b.json": json.dumps(payload_b).encode("utf-8"),
|
|
}
|
|
)
|
|
tools = _make_tools({})
|
|
|
|
convert_document_module.convert_document(state, tools)
|
|
|
|
assert state.source_url == "https://first.example/a"
|
|
assert state.ontology_user_instruction == "First instruction."
|
|
assert state.facts_user_instruction == "First facts."
|
|
corpus = state._captured_text[-1]
|
|
assert "Body A." in corpus and "Body B." in corpus
|
|
|
|
|
|
# ─── Case 5: unsupported extension fails fast ────────────────────────────
|
|
def test_unsupported_extension_returns_failed() -> None:
|
|
state = _make_state({"weird.xyz": b"???"})
|
|
tools = _make_tools({})
|
|
|
|
result = convert_document_module.convert_document(state, tools)
|
|
|
|
assert result.status == Status.FAILED
|
|
|
|
|
|
# ─── Case 6: empty files dict is a no-op success ─────────────────────────
|
|
def test_empty_files_is_noop_success() -> None:
|
|
state = _make_state({})
|
|
tools = _make_tools({})
|
|
|
|
result = convert_document_module.convert_document(state, tools)
|
|
|
|
assert result.status == Status.SUCCESS
|
|
assert state._captured_text == [] # set_text never called
|
|
|
|
|
|
# ─── Case 7: mixed PDF + JSON in one corpus ──────────────────────────────
|
|
def test_mixed_pdf_and_json_combine_with_boundaries() -> None:
|
|
pdf_bytes = b"%PDF-1.4 mix"
|
|
json_payload = {"text": "JSON body."}
|
|
state = _make_state(
|
|
{
|
|
"a.pdf": pdf_bytes,
|
|
"b.json": json.dumps(json_payload).encode("utf-8"),
|
|
}
|
|
)
|
|
tools = _make_tools({pdf_bytes: "PDF body."})
|
|
|
|
convert_document_module.convert_document(state, tools)
|
|
|
|
corpus = state._captured_text[-1]
|
|
assert "=== File: a.pdf ===" in corpus
|
|
assert "=== File: b.json ===" in corpus
|
|
assert "PDF body." in corpus
|
|
assert "JSON body." in corpus
|