from pathlib import Path
from zipfile import BadZipFile, ZipFile
import pytest
from defusedxml.common import EntitiesForbidden
from unstructured.chunking.title import chunk_by_title
from libs.epub import partition_epub
def test_partition_epub_preserves_spine_order_and_title_chunking(tmp_path: Path) -> None:
file_path = tmp_path / "book.epub"
with ZipFile(file_path, "w") as archive:
archive.writestr(
"META-INF/container.xml",
'
Navigation only
") archive.writestr( "OPS/Text/second.xhtml", "Second body.
" ) archive.writestr( "OPS/Text/first chapter.xhtml", "First body.
" ) elements = partition_epub(str(file_path)) assert [str(element) for element in elements] == [ "First chapter", "First body.", "Second chapter", "Second body.", ] chunks = chunk_by_title(elements, max_characters=100, combine_text_under_n_chars=0) assert [chunk.text for chunk in chunks] == ["First chapter\n\nFirst body.", "Second chapter\n\nSecond body."] def test_partition_epub_rejects_missing_package_path(tmp_path: Path) -> None: file_path = tmp_path / "missing-package.epub" with ZipFile(file_path, "w") as archive: archive.writestr("META-INF/container.xml", "