"""Test smart-fit with mock translation (same length) to isolate the issue."""
import sys
sys.path.insert(0, '.')
import importlib.util
import fitz
from pathlib import Path
from unittest.mock import patch

spec = importlib.util.spec_from_file_location('pdf_mod', 'translators/pdf_translator.py')
pdf_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pdf_mod)

# Mock the translation to return the original text (no length change)
def mock_translate(self, text, target_lang, source_lang):
    return text  # No change

PDFTranslator = pdf_mod.PDFTranslator
with patch.object(PDFTranslator, '_translate_single', mock_translate):
    PDFTranslator().translate_file(
        Path('sample_files/test_corpus/test_pdf.pdf'),
        Path('sample_files/test_corpus/test_pdf_no_translation.pdf'),
        target_language='fr',
        source_language='en',
    )

# Check fonts
o = fitz.open('sample_files/test_corpus/test_pdf.pdf')
n = fitz.open('sample_files/test_corpus/test_pdf_no_translation.pdf')
print(f"=== Mock translation (no length change) ===")
print(f"Original:   {len(o)} pages, {o.page_count} blocks")
print(f"Translated: {len(n)} pages")
for i in [0]:
    o_sizes = set()
    n_sizes = set()
    for b in o[i].get_text("dict").get("blocks", []):
        for l in b.get("lines", []):
            for s in l.get("spans", []):
                o_sizes.add(round(s.get("size", 0), 1))
    for b in n[i].get_text("dict").get("blocks", []):
        for l in b.get("lines", []):
            for s in l.get("spans", []):
                n_sizes.add(round(s.get("size", 0), 1))
    print(f"  Page 1 Original:   {sorted(o_sizes)}")
    print(f"  Page 1 Translated: {sorted(n_sizes)}")
o.close()
n.close()
