This commit is contained in:
2026-09-14 12:01:04 +03:00
parent 0dad80bb17
commit 64578acf30
5 changed files with 167 additions and 25 deletions

View File

@@ -215,6 +215,48 @@ def test_docx_external_r_link_image(tmp_path: Path):
assert body.images, "r:link external image must be loaded"
assert "[IMG:" in (body.text or "")
assert "[IMG:" in (body.html_text or "")
# Успешен прочит → опаковане до <stem>_media/
packed = tmp_path / "ext_img_media" / "ext.png"
assert packed.is_file()
assert packed.stat().st_size == len(png)
def test_docx_external_r_link_sidecar_when_unc_missing(tmp_path: Path):
"""Ако r:link сочи към липсващ UNC/път, четем <stem>_media/<name> до .docx."""
png = _mini_png()
try:
from io import BytesIO
from PIL import Image
assert Image.open(BytesIO(png)).size[0] >= 50
except Exception:
pytest.skip("PIL unavailable for size check")
missing = tmp_path / "nowhere" / "missing_shot.png"
# Не създаваме missing — само sidecar
media = tmp_path / "linked_media"
media.mkdir()
(media / "missing_shot.png").write_bytes(png)
doc = Document()
doc.add_heading("1. Sidecar", level=1)
try:
_add_external_blip_paragraph(doc, missing)
except Exception as exc:
pytest.skip(f"cannot build r:link drawing: {exc}")
# Поправи target към несъществуващ път (get_or_add може да е създал file URI)
out = tmp_path / "linked.docx"
doc.save(out)
# Гарантираме sidecar до записания docx
side = tmp_path / "linked_media"
side.mkdir(exist_ok=True)
(side / "missing_shot.png").write_bytes(png)
sections = merge_preamble_sections(merge_short_sections(parse_docx(out)))
body = sections[0]
assert body.images, "sidecar next to docx must satisfy r:link"
assert "[IMG:" in (body.html_text or "")
@pytest.mark.skipif(not ATRA_MANUAL.is_file(), reason="atra-manual.docx not mounted")
@@ -233,13 +275,25 @@ def test_atra_manual_toc_bold_and_image_in_html():
# Style-bold подзаглавия + изричен bold по-нататък
assert any("<b>" in (s.html_text or "") for s in sections)
# Фигура 1 е външен UNC линк — ако share-ът е достъпен, трябва [IMG:]
overview = next((s for s in sections if "Преглед" in s.title), None)
assert overview is not None
if overview.images or "[IMG:" in (overview.text or ""):
assert "Фигура 1" in (overview.text or "")
# Drawing е ПРЕДИ caption; трябва [IMG:] ако UNC или atra-manual_media/123.png
unc = Path(r"\\192.168.88.15\tmp\___Proekti\2025 ATRA96\otchitane\123.png")
sidecar = ATRA_MANUAL.parent / "atra-manual_media" / "123.png"
can_load = False
try:
can_load = unc.is_file() or sidecar.is_file()
except OSError:
can_load = sidecar.is_file()
if can_load:
assert overview.images, "Фигура 1 image must be extracted"
assert "[IMG:" in (overview.text or "")
assert "[IMG:" in (overview.html_text or "")
# Ред: картинка, после caption (както в Word)
t = overview.text or ""
assert t.find("[IMG:") < t.find("Фигура 1")
else:
# Документът сочи към външен файл; ако UNC липсва, plain caption остава
assert "Фигура 1" in (overview.text or "")