"""Tests for the Custom Deck builder (backend/deck_builder.py). Runs off the real default template (database/ppt_template.pptx) plus a blank fallback path, so no extra binaries are needed. Conftest already puts backend/ on sys.path. """ import io import pytest import deck_builder @pytest.fixture(scope='module') def default_out(tmp_path_factory): d = tmp_path_factory.mktemp('decks') return d / 'out.pptx' def _all_text(slide): parts = [] for shp in slide.shapes: try: t = shp.text_frame.text.strip() except Exception: # noqa: BLE001 continue if t: parts.append(t) for ph in slide.placeholders: if ph.has_text_frame and ph.text_frame.text.strip(): parts.append(ph.text_frame.text.strip()) return '\n'.join(parts) def _pics(slide): return [s for s in slide.shapes if s.shape_type == 13] # PICTURE def test_cover_slide_first(png_bytes, default_out): slides = [{'title': 'Real content', 'body': ['Hello']}] out, warnings = deck_builder.generate_deck( slides, template_path=deck_builder.DEFAULT_TEMPLATE, out_path=str(default_out), log=lambda m: None, cover={'title': 'Deck Title Here', 'subtitle': 'NEET 2026', 'date': '18 Sep 2026'}) prs = __import__('pptx').Presentation(str(out)) assert len(prs.slides) == 2 text0 = _all_text(prs.slides[0]) assert 'PRESENTATION' in text0 and 'Deck Title Here' in text0 assert 'NEET 2026' in text0 and '18 Sep 2026' in text0 def test_title_and_bullets(png_bytes, default_out): slides = [{'title': 'Newton', 'body': ['First law', 'Second law'], 'notes': 'Say this aloud'}] out, _ = deck_builder.generate_deck( slides, template_path=deck_builder.DEFAULT_TEMPLATE, out_path=str(default_out), log=lambda m: None) prs = __import__('pptx').Presentation(str(out)) assert len(prs.slides) == 1 # seeded template slide dropped text = _all_text(prs.slides[0]) assert 'Newton' in text and 'First law' in text and 'Second law' in text assert 'Say this aloud' in prs.slides[0].notes_slide.notes_text_frame.text def test_multi_image_with_captions(png_bytes, default_out): slides = [{'title': 'Figures', 'images': [{'data': png_bytes, 'caption': 'Fig A'}, {'data': png_bytes, 'caption': 'Fig B'}]}] out, _ = deck_builder.generate_deck( slides, template_path=str(default_out.parent / 'blank.pptx'), # blank 16:9 out_path=str(default_out), log=lambda m: None) prs = __import__('pptx').Presentation(str(out)) slide = prs.slides[0] assert len(_pics(slide)) == 2 text = _all_text(slide) assert 'Fig A' in text and 'Fig B' in text def test_table_values(png_bytes, default_out): slides = [{'title': 'Rates', 'table': 'Formula\tValue\nR=V/I\t2 Ω\nP=VI\t48 W'}] out, _ = deck_builder.generate_deck( slides, template_path=str(default_out.parent / 'blank.pptx'), out_path=str(default_out), log=lambda m: None) prs = __import__('pptx').Presentation(str(out)) tbl = next(s for s in prs.slides[0].shapes if s.has_table).table assert tbl.cell(0, 0).text == 'Formula' assert tbl.cell(1, 1).text == '2 Ω' assert tbl.cell(2, 0).text == 'P=VI' def test_empty_slide_skipped_with_warning(default_out): slides = [{'title': 'Keep me'}, {}, # fully empty {'title': 'Also keep'}] out, warnings = deck_builder.generate_deck( slides, template_path=str(default_out.parent / 'blank.pptx'), out_path=str(default_out), log=lambda m: None) prs = __import__('pptx').Presentation(str(out)) assert prs.slides[0].shapes and any(w for w in warnings if 'empty' in w) def test_blank_fallback_and_dark_text(png_bytes, default_out): out, _ = deck_builder.generate_deck( [{'title': 'Blank deck', 'body': ['Works without a template']}], template_path=str(default_out.with_suffix('.missing.pptx')), # unused out_path=str(default_out), log=lambda m: None, text_style='light-bg') prs = __import__('pptx').Presentation(str(out)) slide = prs.slides[0] assert 'Blank deck' in _all_text(slide) # dark text requested -> ink colour must be dark, not white dark = any(r.font.color.rgb == deck_builder._INK for shp in slide.shapes if shp.has_text_frame for p in shp.text_frame.paragraphs for r in p.runs if r.font.color and r.font.color.type is not None) assert dark def test_extreme_aspect_inside_bounds(png_bytes, default_out): from PIL import Image wide = io.BytesIO() Image.new('RGB', (2000, 60), 'blue').save(wide, 'PNG') tall = io.BytesIO() Image.new('RGB', (40, 2000), 'red').save(tall, 'PNG') slides = [{'images': [{'data': wide.getvalue(), 'caption': ''}, {'data': tall.getvalue(), 'caption': ''}]}] out, _ = deck_builder.generate_deck( slides, template_path=None, out_path=str(default_out), log=lambda m: None) prs = __import__('pptx').Presentation(str(out)) W, H = prs.slide_width, prs.slide_height for pic in _pics(prs.slides[0]): assert 0 <= pic.left and pic.top >= 0 assert pic.left + pic.width <= W + 1000 and pic.top + pic.height <= H + 1000 def test_image_verify(png_bytes): assert deck_builder.image_verify(png_bytes) assert not deck_builder.image_verify(b'this is definitely not an image') def test_parse_pasted_table(): rows = deck_builder.parse_pasted_table('A\tB\n1\t2\n3') assert rows == [['A', 'B'], ['1', '2'], ['3', '']] assert deck_builder.parse_pasted_table('') == [] assert deck_builder.parse_pasted_table('\n\t\n') == []