Files
mitus/tests/test_tracker.py

75 lines
2.2 KiB
Python

"""Tests for cht.stream.tracker — RecordingTracker."""
import time
from pathlib import Path
from unittest.mock import patch, MagicMock
import pytest
from cht.stream.tracker import RecordingTracker
class TestRecordingTracker:
def test_initial_duration_is_zero(self):
tracker = RecordingTracker(get_segments=lambda: [])
assert tracker.duration == 0.0
def test_callback_called_on_update(self, tmp_path):
seg = tmp_path / "rec.mp4"
seg.write_bytes(b"\x00" * 100_000)
cb = MagicMock()
tracker = RecordingTracker(
get_segments=lambda: [seg],
on_duration_update=cb,
)
with patch.object(tracker, "_probe_duration", return_value=10.0):
tracker.start()
time.sleep(3)
tracker.stop()
cb.assert_called()
assert cb.call_args[0][0] > 0
def test_no_callback_if_no_segments(self):
cb = MagicMock()
tracker = RecordingTracker(get_segments=lambda: [], on_duration_update=cb)
tracker.start()
time.sleep(3)
tracker.stop()
cb.assert_not_called()
def test_no_callback_if_file_missing(self, tmp_path):
cb = MagicMock()
tracker = RecordingTracker(
get_segments=lambda: [tmp_path / "nonexistent.mp4"],
on_duration_update=cb,
)
tracker.start()
time.sleep(3)
tracker.stop()
cb.assert_not_called()
def test_duration_only_increases(self, tmp_path):
seg = tmp_path / "rec.mp4"
seg.write_bytes(b"\x00" * 100_000)
durations = []
def on_update(d):
durations.append(d)
probe_values = iter([5.0, 3.0, 7.0]) # 3.0 is a regression — should be ignored
tracker = RecordingTracker(
get_segments=lambda: [seg],
on_duration_update=on_update,
)
with patch.object(tracker, "_probe_duration", side_effect=probe_values):
tracker.start()
time.sleep(7)
tracker.stop()
# Duration should never go backwards
for i in range(1, len(durations)):
assert durations[i] >= durations[i - 1], "Duration regressed"