Video2Guide / tests /test_image_input.py
Claude
Claude Opus 4.8
Add AI slide-to-transcript alignment for untimestamped images
e646192
Raw History Blame Contribute Delete
9.36 kB
from pathlib import Path
import pytest
from app.pipeline.image_input import (
ImageInputError,
all_have_timestamps,
build_frames_even_split,
build_frames_from_images,
build_frames_from_starts,
build_frames_ordered_by_name,
order_by_name,
parse_timestamp_from_name,
)
def test_parse_plain_hhmmss():
assert parse_timestamp_from_name("00-05-30.png") == 330.0
assert parse_timestamp_from_name("01-02-03.png") == 3723.0
assert parse_timestamp_from_name("00-00-00.png") == 0.0
def test_parse_accepts_various_delimiters():
# colon, dot, underscore, hyphen all read the same
assert parse_timestamp_from_name("00:05:30.png") == 330.0
assert parse_timestamp_from_name("00_05_30.png") == 330.0
assert parse_timestamp_from_name("slide.00.05.30.png") == 330.0
def test_parse_extracts_from_name_with_extra_metadata():
# A trailing time after prefix text is picked up.
assert parse_timestamp_from_name("Lecture3_slide_00-05-30.png") == 330.0
def test_parse_prefers_last_in_range_over_a_leading_date():
# "2024-05-30" is a date; the real timestamp is the trailing 00-05-30.
# With the transcript range as a guard, the date (which would be a
# huge second count) is rejected and the real time wins.
name = "2024-05-30_capture_00-05-30.png"
assert parse_timestamp_from_name(name, max_seconds=3600) == 330.0
def test_parse_date_prefix_no_separator_word():
# Security/bug audit regression: "YYYY-MM-DD_HH-MM-SS" with no word
# between date and time. The old regex fused the date's fields with the
# time's ('01-15_00' → 01:15:00) and lost the real time. Requiring a
# consistent delimiter (the mixed '-' then '_' can't match) fixes it.
assert parse_timestamp_from_name("2024-01-15_00-05-30.png", max_seconds=3600) == 330.0
# Even without the range guard, the trailing consistent-delimiter time wins.
assert parse_timestamp_from_name("2024-01-15_00-05-30.png") == 330.0
def test_parse_counter_prefix_picks_in_range_time():
# "frame-12-00-05-30.png" offers overlapping candidates 12:00:05 and
# 00:05:30; the transcript-range guard keeps the real one.
assert parse_timestamp_from_name("frame-12-00-05-30.png", max_seconds=600) == 330.0
def test_parse_rejects_mixed_delimiter_time():
# A "time" whose two separators differ isn't a real HH-MM-SS token and
# must not be accepted (it's how dates masquerade).
with pytest.raises(ImageInputError):
parse_timestamp_from_name("00-05_30.png")
def test_parse_rejects_unreadable_name():
with pytest.raises(ImageInputError):
parse_timestamp_from_name("slide3.png")
with pytest.raises(ImageInputError):
parse_timestamp_from_name("vlcsnap-image.png")
def test_parse_rejects_out_of_range_timestamp():
# 01-00-00 is 3600s; transcript only runs to 600s.
with pytest.raises(ImageInputError):
parse_timestamp_from_name("01-00-00.png", max_seconds=600)
def test_parse_rejects_impossible_clock_fields():
# 99 minutes / 88 seconds aren't valid clock fields; nothing else in
# the name is a time, so it's unreadable.
with pytest.raises(ImageInputError):
parse_timestamp_from_name("frame-00-99-88.png")
def _touch(tmp_path: Path, name: str) -> Path:
p = tmp_path / name
p.write_bytes(b"\x89PNG\r\n") # not a real image; parser only reads names
return p
def test_build_frames_orders_and_windows(tmp_path):
imgs = [
_touch(tmp_path, "00-05-30.png"),
_touch(tmp_path, "00-00-00.png"),
_touch(tmp_path, "00-02-00.png"),
]
frames = build_frames_from_images(imgs, transcript_end=400.0)
# Sorted by timestamp regardless of input order.
assert [f.start_seconds for f in frames] == [0.0, 120.0, 330.0]
# Each end = next start; last end = transcript end.
assert [f.end_seconds for f in frames] == [120.0, 330.0, 400.0]
# Indices are contiguous, image paths preserved.
assert [f.index for f in frames] == [0, 1, 2]
assert frames[0].image_path.name == "00-00-00.png"
def test_build_frames_rejects_duplicate_timestamps(tmp_path):
imgs = [
_touch(tmp_path, "a_00-01-00.png"),
_touch(tmp_path, "b_00-01-00.png"),
]
with pytest.raises(ImageInputError):
build_frames_from_images(imgs, transcript_end=300.0)
def test_build_frames_rejects_empty(tmp_path):
with pytest.raises(ImageInputError):
build_frames_from_images([], transcript_end=300.0)
def test_ordered_by_name_natural_sort_no_timestamps(tmp_path):
"""No-transcript mode: arbitrary filenames, ordered naturally (slide2
before slide10), synthetic sequential positions, no HH-MM-SS needed."""
imgs = [
_touch(tmp_path, "slide10.png"),
_touch(tmp_path, "slide2.png"),
_touch(tmp_path, "intro.png"),
]
frames = build_frames_ordered_by_name(imgs)
# 'intro' < 'slide2' < 'slide10' by natural key.
assert [f.image_path.name for f in frames] == ["intro.png", "slide2.png", "slide10.png"]
assert [f.index for f in frames] == [0, 1, 2]
# Positions are synthetic and strictly increasing (ordering only).
assert [f.start_seconds for f in frames] == [0.0, 1.0, 2.0]
def test_ordered_by_name_rejects_empty(tmp_path):
with pytest.raises(ImageInputError):
build_frames_ordered_by_name([])
def test_build_frames_without_transcript_end(tmp_path):
"""Transcript is optional: with no transcript_end, parsing runs without
the range guard and the last slide runs to its own timestamp."""
imgs = [
_touch(tmp_path, "00-00-00.png"),
_touch(tmp_path, "00-02-00.png"),
_touch(tmp_path, "00-05-30.png"),
]
frames = build_frames_from_images(imgs) # transcript_end omitted
assert [f.start_seconds for f in frames] == [0.0, 120.0, 330.0]
# Each end = next start; the LAST runs to its own start (no known end).
assert [f.end_seconds for f in frames] == [120.0, 330.0, 330.0]
def test_build_frames_last_slide_at_transcript_end(tmp_path):
# A slide right at the transcript end gets a non-degenerate (zero-width
# is clamped to itself) final window rather than crashing.
imgs = [_touch(tmp_path, "00-00-00.png"), _touch(tmp_path, "00-05-00.png")]
frames = build_frames_from_images(imgs, transcript_end=300.0)
assert frames[-1].start_seconds == 300.0
assert frames[-1].end_seconds == 300.0
def test_all_have_timestamps(tmp_path):
timed = [_touch(tmp_path, "00-00-00.png"), _touch(tmp_path, "00-05-30.png")]
assert all_have_timestamps(timed, 3600) is True
# One name without a readable time → False (route to align/even-split).
mixed = [_touch(tmp_path, "00-00-00.png"), _touch(tmp_path, "slide2.png")]
assert all_have_timestamps(mixed, 3600) is False
# An in-name time that's out of the transcript's range doesn't count.
outrange = [_touch(tmp_path, "01-00-00.png")]
assert all_have_timestamps(outrange, 600) is False
def test_order_by_name_natural(tmp_path):
imgs = [
_touch(tmp_path, "slide10.png"),
_touch(tmp_path, "slide2.png"),
_touch(tmp_path, "slide1.png"),
]
assert [p.name for p in order_by_name(imgs)] == [
"slide1.png",
"slide2.png",
"slide10.png",
]
def test_build_frames_from_starts_windows(tmp_path):
ordered = [
_touch(tmp_path, "a.png"),
_touch(tmp_path, "b.png"),
_touch(tmp_path, "c.png"),
]
frames = build_frames_from_starts(ordered, [0.0, 60.0, 200.0], transcript_end=300.0)
assert [f.start_seconds for f in frames] == [0.0, 60.0, 200.0]
assert [f.end_seconds for f in frames] == [60.0, 200.0, 300.0]
assert [f.image_path.name for f in frames] == ["a.png", "b.png", "c.png"]
def test_build_frames_from_starts_clamps_non_decreasing_and_in_range(tmp_path):
ordered = [_touch(tmp_path, "a.png"), _touch(tmp_path, "b.png"), _touch(tmp_path, "c.png")]
# A stray out-of-order / out-of-range start must not invert a window.
frames = build_frames_from_starts(ordered, [50.0, 20.0, 999.0], transcript_end=300.0)
starts = [f.start_seconds for f in frames]
assert starts == sorted(starts) # non-decreasing
assert all(0.0 <= f.start_seconds <= 300.0 for f in frames)
assert all(f.end_seconds >= f.start_seconds for f in frames)
def test_build_frames_from_starts_rejects_count_mismatch(tmp_path):
ordered = [_touch(tmp_path, "a.png"), _touch(tmp_path, "b.png")]
with pytest.raises(ImageInputError):
build_frames_from_starts(ordered, [0.0], transcript_end=100.0)
def test_build_frames_even_split_spreads_across_span(tmp_path):
imgs = [
_touch(tmp_path, "slide2.png"),
_touch(tmp_path, "slide1.png"),
_touch(tmp_path, "slide3.png"),
]
frames = build_frames_even_split(imgs, transcript_start=0.0, transcript_end=300.0)
# Ordered by natural filename order, evenly spaced across [0, 300).
assert [f.image_path.name for f in frames] == ["slide1.png", "slide2.png", "slide3.png"]
assert [f.start_seconds for f in frames] == [0.0, 100.0, 200.0]
assert frames[-1].end_seconds == 300.0
def test_build_frames_even_split_rejects_empty(tmp_path):
with pytest.raises(ImageInputError):
build_frames_even_split([], transcript_start=0.0, transcript_end=100.0)