import asyncio import json import zipfile from pathlib import Path import pytest from bs4 import BeautifulSoup from ebooklib import epub from src.common.data_model import ManifestEntry, BookProfile, BookStructure from src.preprocessing.format_extractor import FormatExtractor from src.preprocessing.epub_cleaner import EpubCleaner from src.preprocessing.text_extractor import FineGrainedExtractor from src.assembly.format_restorer import FormatRestorer from src.assembly.restoration_engine import RestorationEngine from src.assembly.backfiller import BackfillEngine from src.assembly.builder import BilingualBuilder from src.translation.translator_engine import Translator from src.translation.glossary import BookGlossary, matches from src.translation.manifest_manager import ManifestManager from src.translation.llm_client import LLMClient from src.qa.epub_audit import audit_epub, compare_audits def entry(html='
Masayoshi Son built a company.
'): _, text, mapping, _, _ = FormatExtractor().extract(html) return ManifestEntry(entry_id='Text/ch.xhtml#p1', element_id='p1', file_path='Text/ch.xhtml', original_text=text, placeholders=mapping) @pytest.mark.parametrize('html', [ 'Hello world.
', 'The angle φ = 10, and φ1φ is literal mathematical text.
', 'A & B example.
', 'A[1] note.
', '日本語と中文 
text.
A formula.
', 'The first word.
', ]) def test_dom_roundtrip(html): e = entry(html) restored, ok = FormatRestorer().restore(e.original_text, e.placeholders, original_text=e.original_text) assert ok before, after = BeautifulSoup(html, 'html.parser').p, BeautifulSoup(''+restored+'
', 'html.parser').p assert before.get_text() == after.get_text() assert [(t.name, t.attrs) for t in before.find_all(True)] == [(t.name, t.attrs) for t in after.find_all(True)] @pytest.mark.parametrize('text', [ 'φ1φφ2φ中文φ/1φφ/2φ', # crossed 'φ1φφ2φ中文φ/2φφ/1φφ1φ', # duplicate 'φ1φ中文φ/1φ', # missing pair 'φ1φφ2φ中文φ/2φφ/1φφ99φ', # unknown 'φ1φφ2φ中文φ/2φφ/1φφbrokenφ', ]) def test_invalid_placeholders_never_restore(text): e = entry('Hello world
') assert FormatRestorer().restore(text, e.placeholders, original_text=e.original_text) == ('', False) def test_model_text_is_escaped(): result, ok = FormatRestorer().restore(' A & B', {}) assert ok and '