|
| 1 | +"""Layout checks must not invent a frame rate when the video does not report one.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import cv2 |
| 8 | +import pytest |
| 9 | +import yaml |
| 10 | + |
| 11 | +from docgen.config import Config |
| 12 | +from docgen.manim_layout import LayoutValidator |
| 13 | + |
| 14 | + |
| 15 | +def _cfg(tmp_path: Path) -> Config: |
| 16 | + raw = {"segments": {"all": ["01"], "default": ["01"]}} |
| 17 | + path = tmp_path / "docgen.yaml" |
| 18 | + path.write_text(yaml.dump(raw), encoding="utf-8") |
| 19 | + return Config.from_yaml(path) |
| 20 | + |
| 21 | + |
| 22 | +class _Cap: |
| 23 | + def __init__(self, *_args: object, **_kwargs: object) -> None: |
| 24 | + self.fps = 0.0 |
| 25 | + self.frames = 0.0 |
| 26 | + self.sampled = False |
| 27 | + |
| 28 | + def isOpened(self) -> bool: |
| 29 | + return True |
| 30 | + |
| 31 | + def get(self, prop: int) -> float: |
| 32 | + if prop == cv2.CAP_PROP_FRAME_COUNT: |
| 33 | + return self.frames |
| 34 | + if prop in (cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FRAME_WIDTH): |
| 35 | + return 100.0 |
| 36 | + return self.fps |
| 37 | + |
| 38 | + def set(self, *_args: object) -> None: |
| 39 | + self.sampled = True |
| 40 | + |
| 41 | + def read(self) -> tuple[bool, None]: |
| 42 | + self.sampled = True |
| 43 | + return False, None |
| 44 | + |
| 45 | + def release(self) -> None: |
| 46 | + return None |
| 47 | + |
| 48 | + |
| 49 | +def test_zero_fps_is_refused(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 50 | + cap = _Cap() |
| 51 | + monkeypatch.setattr(cv2, "VideoCapture", lambda *_a, **_k: cap) |
| 52 | + with pytest.raises(ValueError, match="positive finite"): |
| 53 | + LayoutValidator(_cfg(tmp_path)).validate_video(tmp_path / "clip.mp4") |
| 54 | + assert cap.sampled is False |
| 55 | + |
| 56 | + |
| 57 | +def test_positive_fps_still_samples(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 58 | + cap = _Cap() |
| 59 | + cap.fps = 30.0 |
| 60 | + cap.frames = 90.0 |
| 61 | + monkeypatch.setattr(cv2, "VideoCapture", lambda *_a, **_k: cap) |
| 62 | + report = LayoutValidator(_cfg(tmp_path)).validate_video(tmp_path / "clip.mp4") |
| 63 | + assert cap.sampled is True |
| 64 | + assert report.passed is True |
0 commit comments