"""Tests for harness/A/frames.py -- uniform/selective frame sampling.""" import numpy as np import pytest from harness.A import frames as frame_sampling def test_sample_frames_rejects_unknown_selection(tmp_path): video = tmp_path / "scene.mp4" video.write_bytes(b"not a real video") with pytest.raises(ValueError): frame_sampling.sample_frames(str(video), 8, "random") def test_sample_frames_rejects_nonpositive_frame_count(tmp_path): video = tmp_path / "scene.mp4" video.write_bytes(b"not a real video") with pytest.raises(ValueError): frame_sampling.sample_frames(str(video), 0, "uniform") def test_sample_frames_rejects_missing_video(tmp_path): with pytest.raises(FileNotFoundError): frame_sampling.sample_frames(str(tmp_path / "missing.mp4"), 8, "uniform") def test_sample_frames_returns_pil_images_in_order(tmp_path, monkeypatch): video = tmp_path / "scene.mp4" video.write_bytes(b"not a real video") fake_frames = np.stack( [np.full((4, 4, 3), value, dtype=np.uint8) for value in (10, 20, 30)] ) monkeypatch.setattr( frame_sampling, "_sample_video_frames", lambda path, count, selection: (fake_frames, np.array([0.0, 1.0, 2.0])), ) class _UnreadableCapture: def get(self, prop): return 0.0 def release(self): pass monkeypatch.setattr( frame_sampling.cv2, "VideoCapture", lambda path: _UnreadableCapture() ) result, timestamps, indices = frame_sampling.sample_frames(str(video), 3, "uniform") assert len(result) == 3 assert np.array(result[0])[0, 0, 0] == 10 assert np.array(result[2])[0, 0, 0] == 30 assert timestamps == [0.0, 1.0, 2.0] # fps falls back to 1.0 for the fake (unreadable) video, so index == round(t * 1.0) == t. assert indices == [0, 1, 2] def test_sample_frames_derives_indices_from_real_fps(tmp_path, monkeypatch): video = tmp_path / "scene.mp4" video.write_bytes(b"not a real video") fake_frames = np.stack([np.full((2, 2, 3), 1, dtype=np.uint8)] * 3) monkeypatch.setattr( frame_sampling, "_sample_video_frames", lambda path, count, selection: (fake_frames, np.array([0.0, 0.5, 1.0])), ) class _FakeCapture: def get(self, prop): return 30.0 def release(self): pass monkeypatch.setattr(frame_sampling.cv2, "VideoCapture", lambda path: _FakeCapture()) _, timestamps, indices = frame_sampling.sample_frames(str(video), 3, "uniform") assert timestamps == [0.0, 0.5, 1.0] assert indices == [0, 15, 30]