# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """Dropping metadata tracks exiftool cannot see, and proving they are gone.""" from __future__ import annotations from pathlib import Path import pytest from signal_bot import media_tracks from signal_bot.external import Result class FakeRun: """Stands in for signal_bot.external.run, one canned answer per binary.""" def __init__(self, ffmpeg: Result, ffprobe: Result | None = None) -> None: self.ffmpeg = ffmpeg self.ffprobe = ffprobe or Result(ok=True, stdout="video\naudio\n", stderr="") self.commands: list[list[str]] = [] def __call__(self, command: list[str], *, timeout: float = 0) -> Result: """Answer as the binary the command names, and write the output file.""" self.commands.append(list(command)) if "ffprobe" in command[0]: return self.ffprobe if self.ffmpeg.ok: # The real ffmpeg leaves a file behind, and the code checks for it. Path(command[-1]).write_bytes(b"remuxed") return self.ffmpeg @pytest.fixture def found(monkeypatch: pytest.MonkeyPatch) -> None: """Both binaries present, so tests exercise the logic and not locate().""" monkeypatch.setattr(media_tracks, "locate", lambda binary: f"/usr/bin/{binary}") def _use(monkeypatch: pytest.MonkeyPatch, runner: FakeRun) -> None: monkeypatch.setattr(media_tracks, "run", runner) def test_a_clean_remux_returns_the_new_bytes(monkeypatch: pytest.MonkeyPatch, found: None) -> None: _use(monkeypatch, FakeRun(Result(ok=True, stdout="", stderr=""))) assert media_tracks.flatten(b"video", ".mp4") == b"remuxed" def test_audio_is_mapped_optionally(monkeypatch: pytest.MonkeyPatch, found: None) -> None: # "0:a?" and not "0:a": a clip with no sound is still a clip. runner = FakeRun(Result(ok=True, stdout="", stderr="")) _use(monkeypatch, runner) media_tracks.flatten(b"video", ".mp4") assert "0:a?" in runner.commands[0] def test_a_missing_ffmpeg_refuses_rather_than_storing( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(media_tracks, "locate", lambda binary: None) assert media_tracks.flatten(b"video", ".mp4") is None def test_a_missing_ffprobe_refuses(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( media_tracks, "locate", lambda binary: None if binary == "ffprobe" else "/usr/bin/ffmpeg" ) assert media_tracks.flatten(b"video", ".mp4") is None def test_an_ffmpeg_failure_refuses(monkeypatch: pytest.MonkeyPatch, found: None) -> None: _use(monkeypatch, FakeRun(Result(ok=False, stdout="", stderr="boom"))) assert media_tracks.flatten(b"video", ".mp4") is None def test_a_surviving_data_stream_refuses(monkeypatch: pytest.MonkeyPatch, found: None) -> None: # The whole point: a metadata track that outlived the re-mux must not be # stored, because no tag allowlist downstream can see it. _use( monkeypatch, FakeRun( Result(ok=True, stdout="", stderr=""), Result(ok=True, stdout="video\naudio\ndata\n", stderr=""), ), ) assert media_tracks.flatten(b"video", ".mp4") is None def test_an_unreadable_result_refuses(monkeypatch: pytest.MonkeyPatch, found: None) -> None: _use( monkeypatch, FakeRun(Result(ok=True, stdout="", stderr=""), Result(ok=False, stdout="", stderr="x")), ) assert media_tracks.flatten(b"video", ".mp4") is None def test_a_file_ffprobe_describes_as_nothing_refuses( monkeypatch: pytest.MonkeyPatch, found: None ) -> None: # Empty is a failure, not "no bad streams": nobody can vouch for it. _use( monkeypatch, FakeRun(Result(ok=True, stdout="", stderr=""), Result(ok=True, stdout="\n", stderr="")), ) assert media_tracks.flatten(b"video", ".mp4") is None def test_streams_strips_the_trailing_comma_ffprobe_emits( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: _use(monkeypatch, FakeRun(Result(ok=True, stdout="", stderr=""))) assert media_tracks.streams(tmp_path / "x.mp4", "/usr/bin/ffprobe") == ("video", "audio") def test_streams_reports_failure_as_none(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: _use( monkeypatch, FakeRun(Result(ok=True, stdout="", stderr=""), Result(ok=False, stdout="", stderr="")), ) assert media_tracks.streams(tmp_path / "x.mp4", "/usr/bin/ffprobe") is None def test_locate_prefers_path(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(media_tracks.shutil, "which", lambda binary: f"/somewhere/{binary}") assert media_tracks.locate("ffmpeg") == "/somewhere/ffmpeg" def test_locate_falls_back_to_the_known_directories( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: # A systemd unit's PATH is "/usr/local/bin:/usr/bin" and nothing else. monkeypatch.setattr(media_tracks.shutil, "which", lambda binary: None) (tmp_path / "ffmpeg").write_text("#!/bin/sh\n", encoding="utf-8") monkeypatch.setattr(media_tracks, "FALLBACK_DIRS", (str(tmp_path),)) assert media_tracks.locate("ffmpeg") == str(tmp_path / "ffmpeg") def test_locate_reports_nothing_when_it_is_not_installed( monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: monkeypatch.setattr(media_tracks.shutil, "which", lambda binary: None) monkeypatch.setattr(media_tracks, "FALLBACK_DIRS", (str(tmp_path),)) assert media_tracks.locate("ffmpeg") is None