marki-docx/tests/test_importer.py

61 lines
2.5 KiB
Python
Raw Normal View History

"""Tests for DOCX→Markdown importer (FR-300, FR-400)."""
from __future__ import annotations
from pathlib import Path
from markidocx.builder import build_document
from markidocx.importer import import_document
from markidocx.manifest import load_manifest
class TestImportDocument:
def test_import_missing_docx_fails(self, tmp_project: Path) -> None:
manifest = load_manifest(tmp_project / "manifest.yaml")
result = import_document(manifest, tmp_project / "missing.docx")
assert not result.success
assert result.mapping_status == "failed"
def test_import_roundtrip_single_source(self, tmp_project: Path) -> None:
manifest = load_manifest(tmp_project / "manifest.yaml")
build_result = build_document(manifest)
assert build_result.success
result = import_document(manifest, build_result.output_path)
assert result.success
assert len(result.output_files) == 1
assert result.mapping_status == "redistributed"
assert result.output_files[0].exists()
def test_imported_markdown_has_headings(self, tmp_project: Path) -> None:
manifest = load_manifest(tmp_project / "manifest.yaml")
build_result = build_document(manifest)
import_result = import_document(manifest, build_result.output_path)
assert import_result.success
md = import_result.output_files[0].read_text(encoding="utf-8")
assert "# " in md # at least one heading
def test_import_multi_source_merged_fallback(self, tmp_path: Path) -> None:
import yaml
for name in ("ch1.md", "ch2.md", "ch3.md"):
(tmp_path / name).write_text(f"# {name}\n\nContent of {name}.", encoding="utf-8")
(tmp_path / "manifest.yaml").write_text(
yaml.dump(
{
"project": {"name": "MultiBook", "feature_level": "level1", "family": "book"},
"sources": [{"path": "ch1.md"}, {"path": "ch2.md"}, {"path": "ch3.md"}],
"output": {"dir": "./dist"},
}
)
)
(tmp_path / "dist").mkdir()
manifest = load_manifest(tmp_path / "manifest.yaml")
build_result = build_document(manifest)
assert build_result.success
import_result = import_document(manifest, build_result.output_path)
assert import_result.success
# Should redistribute or merge — either way produces output
assert len(import_result.output_files) >= 1