""" test_renderer.py — Tests for the LibreOffice slide renderer. Live-render tests are skipped when soffice is not on $PATH. When soffice IS available: - Both supplied decks render with the correct slide counts. - At least 95% of slides in each deck render successfully. - PNG dimensions are consistent within each deck. - Two concurrent render_deck() calls do not collide. When soffice is NOT available: - render_deck() returns skipped=True without raising. """ from __future__ import annotations import concurrent.futures import shutil import tempfile from pathlib import Path import pytest from services.lo_runner import soffice_available from services.renderer import RenderResult, render_deck, warmup # --------------------------------------------------------------------------- # Paths to supplied test decks # --------------------------------------------------------------------------- _REPO_ROOT = Path(__file__).parent.parent.parent.parent # …/Farient Case Study - (RobinBot) _DOC_REF = _REPO_ROOT / "Doc Reference" IDEXX_PPTX = _DOC_REF / "Annotated_20260511 IDEXX Peer Group Assessment - DRAFT 20260330 (3).pptx" JACOBS_PPTX = _DOC_REF / "20260429 Jacobs Assessment of Compensation Peers - DRAFT 20260327 (3).pptx" IDEXX_SLIDE_COUNT = 24 JACOBS_SLIDE_COUNT = 33 SOFFICE_AVAILABLE = soffice_available() requires_soffice = pytest.mark.skipif( not SOFFICE_AVAILABLE, reason="soffice not found on $PATH — live-render tests skipped", ) # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _render_into_tmpdir(pptx_path: Path) -> tuple[RenderResult, Path]: """Render *pptx_path* into a fresh temp directory; return (result, tmp_dir).""" tmp = Path(tempfile.mkdtemp(prefix="test_render_")) result = render_deck(pptx_path, tmp) return result, tmp # --------------------------------------------------------------------------- # soffice-absent tests (always run) # --------------------------------------------------------------------------- class TestNoSoffice: """Tests that must pass regardless of whether soffice is installed.""" def test_skipped_when_soffice_absent(self, monkeypatch): """render_deck returns skipped=True and does not raise when soffice is missing.""" # Force soffice_available() to return False inside renderer import services.lo_runner as lo_mod monkeypatch.setattr(lo_mod, "soffice_available", lambda: False) # Re-import renderer to pick up monkeypatched dependency import importlib import services.renderer as rend_mod monkeypatch.setattr(rend_mod, "soffice_available", lambda: False) with tempfile.TemporaryDirectory() as tmp: result = render_deck(JACOBS_PPTX, Path(tmp)) assert result.skipped is True assert result.reason is not None assert "unavailable" in result.reason.lower() assert result.render_failed is False assert result.slide_paths == [] def test_result_model_forbids_extra_fields(self): """RenderResult with extra='forbid' raises on unknown fields.""" import pydantic with pytest.raises((pydantic.ValidationError, TypeError)): RenderResult(unknown_field="oops") # type: ignore[call-arg] def test_warmup_does_not_raise_when_soffice_absent(self, monkeypatch): """warmup() must not raise even when soffice is missing.""" import services.renderer as rend_mod monkeypatch.setattr(rend_mod, "soffice_available", lambda: False) warmup() # must not raise # --------------------------------------------------------------------------- # Live-render tests (skipped when soffice absent) # --------------------------------------------------------------------------- @requires_soffice class TestLiveRender: """Tests that run only when soffice is available.""" def test_idexx_slide_count(self): """IDEXX deck renders to exactly 24 PNGs (or per-slide errors summing to 24).""" result, tmp = _render_into_tmpdir(IDEXX_PPTX) try: assert not result.skipped, "unexpected skip" assert not result.render_failed, f"render failed: {result.reason}" total = result.total assert total == IDEXX_SLIDE_COUNT, ( f"Expected {IDEXX_SLIDE_COUNT} slides total, got {total} " f"({result.success_count} ok, {len(result.slide_errors)} errors)" ) finally: shutil.rmtree(tmp, ignore_errors=True) def test_jacobs_slide_count(self): """Jacobs deck renders to exactly 33 PNGs (or per-slide errors summing to 33).""" result, tmp = _render_into_tmpdir(JACOBS_PPTX) try: assert not result.skipped, "unexpected skip" assert not result.render_failed, f"render failed: {result.reason}" total = result.total assert total == JACOBS_SLIDE_COUNT, ( f"Expected {JACOBS_SLIDE_COUNT} slides total, got {total} " f"({result.success_count} ok, {len(result.slide_errors)} errors)" ) finally: shutil.rmtree(tmp, ignore_errors=True) def test_idexx_success_rate(self): """At least 95% of IDEXX slides render successfully.""" result, tmp = _render_into_tmpdir(IDEXX_PPTX) try: assert not result.skipped and not result.render_failed rate = result.success_count / result.total assert rate >= 0.95, ( f"IDEXX success rate {rate:.1%} below 95% " f"({result.success_count}/{result.total})" ) finally: shutil.rmtree(tmp, ignore_errors=True) def test_jacobs_success_rate(self): """At least 95% of Jacobs slides render successfully.""" result, tmp = _render_into_tmpdir(JACOBS_PPTX) try: assert not result.skipped and not result.render_failed rate = result.success_count / result.total assert rate >= 0.95, ( f"Jacobs success rate {rate:.1%} below 95% " f"({result.success_count}/{result.total})" ) finally: shutil.rmtree(tmp, ignore_errors=True) def test_idexx_consistent_dimensions(self): """All successfully rendered IDEXX slides must have the same pixel dimensions.""" from PIL import Image result, tmp = _render_into_tmpdir(IDEXX_PPTX) try: assert not result.skipped and not result.render_failed assert len(result.slide_paths) > 0, "no slides rendered" sizes = set() for p in result.slide_paths: with Image.open(p) as img: sizes.add(img.size) assert len(sizes) == 1, f"Inconsistent PNG dimensions in IDEXX deck: {sizes}" finally: shutil.rmtree(tmp, ignore_errors=True) def test_jacobs_consistent_dimensions(self): """All successfully rendered Jacobs slides must have the same pixel dimensions.""" from PIL import Image result, tmp = _render_into_tmpdir(JACOBS_PPTX) try: assert not result.skipped and not result.render_failed assert len(result.slide_paths) > 0, "no slides rendered" sizes = set() for p in result.slide_paths: with Image.open(p) as img: sizes.add(img.size) assert len(sizes) == 1, f"Inconsistent PNG dimensions in Jacobs deck: {sizes}" finally: shutil.rmtree(tmp, ignore_errors=True) def test_concurrent_renders_do_not_collide(self): """Two concurrent render_deck() calls must both succeed without interfering.""" tmps: list[Path] = [] try: with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool: fut_idexx = pool.submit(_render_into_tmpdir, IDEXX_PPTX) fut_jacobs = pool.submit(_render_into_tmpdir, JACOBS_PPTX) result_idexx, tmp_idexx = fut_idexx.result(timeout=180) result_jacobs, tmp_jacobs = fut_jacobs.result(timeout=180) tmps.extend([tmp_idexx, tmp_jacobs]) # Both must succeed assert not result_idexx.skipped and not result_idexx.render_failed, ( f"IDEXX concurrent render failed: {result_idexx.reason}" ) assert not result_jacobs.skipped and not result_jacobs.render_failed, ( f"Jacobs concurrent render failed: {result_jacobs.reason}" ) # Both must produce the right number of slides assert result_idexx.total == IDEXX_SLIDE_COUNT, ( f"IDEXX: expected {IDEXX_SLIDE_COUNT}, got {result_idexx.total}" ) assert result_jacobs.total == JACOBS_SLIDE_COUNT, ( f"Jacobs: expected {JACOBS_SLIDE_COUNT}, got {result_jacobs.total}" ) finally: for t in tmps: shutil.rmtree(t, ignore_errors=True) def test_png_files_are_named_correctly(self): """PNG files must follow the slide_NNN.png zero-padded naming convention.""" result, tmp = _render_into_tmpdir(IDEXX_PPTX) try: assert not result.skipped and not result.render_failed for path in result.slide_paths: name = path.name assert name.startswith("slide_"), f"Unexpected filename: {name}" assert name.endswith(".png"), f"Unexpected extension: {name}" idx_str = name[len("slide_") : -len(".png")] assert len(idx_str) == 3 and idx_str.isdigit(), ( f"Expected 3-digit zero-padded index, got: {idx_str!r}" ) finally: shutil.rmtree(tmp, ignore_errors=True) def test_warmup_runs_without_error(self): """warmup() must complete without raising when soffice is available.""" warmup() # must not raise