import io import zipfile from pathlib import Path from PIL import Image import pytest from app.pipeline.html_gen import ( Segment, _draft_alt_from_ocr, _format_timestamp, bundle_zip, build_segments, render_guide, render_review, ) from app.pipeline.types import SceneFrame def _make_image(path: Path, color=(200, 50, 50)) -> None: path.parent.mkdir(parents=True, exist_ok=True) Image.new("RGB", (32, 24), color).save(path, "JPEG") def test_format_timestamp(): assert _format_timestamp(0) == "0:00" assert _format_timestamp(65) == "1:05" assert _format_timestamp(3725) == "1:02:05" def test_draft_alt_text_does_not_leak_author_instructions(): # The draft state belongs in data-alt-draft="true", NOT in the alt text # itself — screen readers would otherwise read "Review and rewrite" out # loud to the end user. with_text = _draft_alt_from_ocr("Hello world") empty = _draft_alt_from_ocr("") assert "Hello world" in with_text for msg in (with_text, empty): assert "Review and rewrite" not in msg assert "Replace with" not in msg def test_draft_alt_text_truncates_long_ocr(): long = "word " * 200 out = _draft_alt_from_ocr(long) assert len(out) <= 160 def test_render_guide_includes_wcag_landmarks(tmp_path): img = tmp_path / "scene_0000.jpg" _make_image(img) frame = SceneFrame(0, 0.0, 5.0, img) segments = build_segments([frame], ["Some narration."], ["OCR text"], inline_images=False) html = render_guide("My Guide", segments, lang="en", inline_images=False) assert '' in html assert 'role="banner"' in html assert 'role="contentinfo"' in html assert "" in html and "My Guide" in html assert "Some narration." in html assert 'class="doc-header"' in html assert 'class="figure"' in html assert "Auto-Generated Study Guide" in html def test_render_guide_inline_images_embeds_data_uri(tmp_path): img = tmp_path / "scene_0000.jpg" _make_image(img) frame = SceneFrame(0, 0.0, 5.0, img) segments = build_segments([frame], [""], [""], inline_images=True) html = render_guide("Inline", segments, inline_images=True) assert "data:image/jpeg;base64," in html def test_render_guide_zip_mode_uses_static_folder_path(tmp_path): # When not inlining, the template must emit static/ for both # frames and audio so the bundle has a single asset folder. img = tmp_path / "scene_0000.jpg" _make_image(img) frame = SceneFrame(0, 0.0, 5.0, img) segments = build_segments([frame], [["hello"]], [""], inline_images=False) segments[0].audio_filename = "segment_0000.mp3" html = render_guide("Zip", segments, inline_images=False) assert "static/segment_0000.mp3" in html assert "static/scene_0000.jpg" in html assert "audio/segment_0000.mp3" not in html assert "data:audio/mpeg;base64," not in html def test_render_guide_single_mode_inlines_audio_data_uri(tmp_path): img = tmp_path / "scene_0000.jpg" _make_image(img) frame = SceneFrame(0, 0.0, 5.0, img) segments = build_segments([frame], [["hello"]], [""], inline_images=True) segments[0].audio_data_uri = "data:audio/mpeg;base64,AAAA" html = render_guide("Single", segments, inline_images=True) # Inline data URI present, no relative-folder reference leaking through. assert "data:audio/mpeg;base64,AAAA" in html assert "static/segment_0000.mp3" not in html def test_bundle_zip_puts_all_assets_under_static(tmp_path): from app.pipeline.html_gen import bundle_zip import zipfile, io img = tmp_path / "scene_0000.jpg" _make_image(img) audio = tmp_path / "segment_0000.mp3" audio.write_bytes(b"\xff\xfb fake mp3") data = bundle_zip("", [img], audio_paths=[audio]) with zipfile.ZipFile(io.BytesIO(data)) as zf: names = set(zf.namelist()) assert "static/scene_0000.jpg" in names assert "static/segment_0000.mp3" in names # No legacy audio/ folder. assert not any(n.startswith("audio/") for n in names) # HTML at the root, single asset folder. top_level = {n.split("/", 1)[0] for n in names} assert top_level == {"study-guide.html", "static"} def test_metadata_round_trip_preserves_audio_filename(tmp_path): from app.pipeline.metadata import ( SegmentMetadata, load_metadata, page_metadata_from_segments, dump_metadata, ) img = tmp_path / "scene_0000.jpg" _make_image(img) frame = SceneFrame(0, 0.0, 5.0, img) segments = build_segments([frame], [["hello"]], [""], inline_images=False) segments[0].audio_filename = "segment_0000.mp3" page = page_metadata_from_segments(title="T", segments=segments) out = tmp_path / "meta.json" dump_metadata(out, page) loaded = load_metadata(out) assert isinstance(loaded.segments[0], SegmentMetadata) assert loaded.segments[0].audio_filename == "segment_0000.mp3" def test_metadata_to_segment_inlines_audio_from_static_folder(tmp_path): from app.pipeline.html_gen import metadata_to_segment from app.pipeline.metadata import SegmentMetadata frames_dir = tmp_path / "static" frames_dir.mkdir() img = frames_dir / "scene_0000.jpg" _make_image(img) # New layout: audio sits alongside frames in the static dir. (frames_dir / "segment_0000.mp3").write_bytes(b"\xff\xfb fake mp3") sm = SegmentMetadata( index=0, start_seconds=0.0, end_seconds=5.0, image_filename="scene_0000.jpg", section_title="t", alt_text="a", audio_filename="segment_0000.mp3", ) seg = metadata_to_segment(sm, frames_dir, inline_images=True) assert seg.audio_data_uri is not None assert seg.audio_data_uri.startswith("data:audio/mpeg;base64,") assert seg.audio_filename == "segment_0000.mp3" def test_metadata_to_segment_falls_back_to_legacy_audio_folder(tmp_path): # Pre-existing on-disk bundles had a sibling `audio/` directory; the # loader should still find clips there for backward compatibility. from app.pipeline.html_gen import metadata_to_segment from app.pipeline.metadata import SegmentMetadata frames_dir = tmp_path / "static" frames_dir.mkdir() img = frames_dir / "scene_0000.jpg" _make_image(img) legacy = tmp_path / "audio" legacy.mkdir() (legacy / "segment_0000.mp3").write_bytes(b"\xff\xfb legacy mp3") sm = SegmentMetadata( index=0, start_seconds=0.0, end_seconds=5.0, image_filename="scene_0000.jpg", section_title="t", alt_text="a", audio_filename="segment_0000.mp3", ) seg = metadata_to_segment(sm, frames_dir, inline_images=True) assert seg.audio_data_uri is not None assert seg.audio_data_uri.startswith("data:audio/mpeg;base64,") def test_metadata_to_segment_no_audio_when_inline_false(tmp_path): from app.pipeline.html_gen import metadata_to_segment from app.pipeline.metadata import SegmentMetadata frames_dir = tmp_path / "static" frames_dir.mkdir() (frames_dir / "segment_0000.mp3").write_bytes(b"x") img = frames_dir / "scene_0000.jpg" _make_image(img) sm = SegmentMetadata( index=0, start_seconds=0.0, end_seconds=5.0, image_filename="scene_0000.jpg", section_title="t", alt_text="a", audio_filename="segment_0000.mp3", ) seg = metadata_to_segment(sm, frames_dir, inline_images=False) # Filename preserved so the template can emit static/; no inlining. assert seg.audio_filename == "segment_0000.mp3" assert seg.audio_data_uri is None def test_build_segments_assigns_default_section_titles(tmp_path): img1 = tmp_path / "scene_0000.jpg" img2 = tmp_path / "scene_0001.jpg" _make_image(img1) _make_image(img2) frames = [SceneFrame(0, 0.0, 5.0, img1), SceneFrame(1, 5.0, 65.0, img2)] segments = build_segments(frames, ["", ""], ["", ""], inline_images=False) assert segments[0].section_title == "Segment 1 — 0:00" assert segments[1].section_title == "Segment 2 — 0:05" def test_render_review_emits_editor_chrome_and_bindings(tmp_path): img = tmp_path / "scene_0000.jpg" _make_image(img) frame = SceneFrame(0, 0.0, 5.0, img) segments = build_segments(frame and [frame], ["The narration."], ["OCR content"], inline_images=True) html = render_review("Review Me", segments, lang="en") assert 'data-vgm-review="1"' in html assert 'class="vgm-toolbar"' in html assert 'data-action="save-json"' in html assert 'data-action="load-json"' in html assert 'data-action="download-html"' in html assert 'data-bind="title"' in html assert 'data-bind="section_title"' in html assert 'data-bind="alt_text"' in html assert 'data-bind="include"' in html assert 'data-bind="narration"' in html assert 'data-bind="ocr_text"' in html assert 'data-bind-target="section_title"' in html assert 'data-bind-target="alt_text"' in html assert "The narration." in html assert "OCR content" in html assert 'data-included="true"' in html assert "data:image/jpeg;base64," in html def test_render_review_alt_text_is_textarea_for_multiline_descriptions(tmp_path): """Per accessibility-team feedback: the alt-text field needs room for multi-paragraph descriptions, not a single-line input.""" img = tmp_path / "scene_0000.jpg" _make_image(img) segments = build_segments([SceneFrame(0, 0.0, 5.0, img)], [""], [""], inline_images=True) html = render_review("Review Me", segments, lang="en") # Old contract was an ; the # field is now a