Spaces:
Running
Running
File size: 9,359 Bytes
88d3d3b e646192 88d3d3b e646192 bbe4867 e646192 88d3d3b 1ca1018 88d3d3b bbe4867 d9cde51 88d3d3b e646192 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 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)
|