import tempfile import unittest from pathlib import Path from unittest.mock import patch from ffmpeg_handling import build_parser, main ASSET_DIR = Path(__file__).resolve().parents[2] / "testassets" / "ffmpeg_handling" class CliTests(unittest.TestCase): def test_test_command_is_not_registered(self): with self.assertRaises(SystemExit) as error: build_parser().parse_args(["test"]) self.assertEqual(error.exception.code, 2) def test_short_command_alias_resolves_to_canonical_name(self): args = build_parser().parse_args(["i", str(ASSET_DIR / "test_auto.mp4")]) self.assertEqual(args.command, "info") def test_missing_ffmpeg_logs_error_and_returns_failure(self): with patch("ffmpeg_handling.cli.shutil.which", return_value=None), \ patch("ffmpeg_handling.cli.setup_logging"), \ self.assertLogs("ffmpeg_handling", level="ERROR") as captured: result = main(["info", str(ASSET_DIR / "test_auto_no_subs.mkv")]) self.assertEqual(result, 1) self.assertIn("FFmpeg is not installed", captured.output[0]) def test_non_output_command_returns_success(self): with patch("ffmpeg_handling.cli.setup_logging"), \ self.assertLogs("ffmpeg_handling", level="INFO"): self.assertEqual( main(["info", str(ASSET_DIR / "test_auto_no_subs.mkv")]), 0 ) def test_cli_dry_run_uses_requested_output_and_creates_nothing(self): with tempfile.TemporaryDirectory() as temp_dir: output = Path(temp_dir) / "dry_run.mkv" with patch("ffmpeg_handling.cli.setup_logging"), \ self.assertLogs("ffmpeg_handling", level="INFO"): result = main([ "downsize", str(ASSET_DIR / "test_auto_no_subs.mkv"), "-o", str(output), "--dry-run", "-w", "320", "-he", "180", "-c", "30", ]) self.assertEqual(result, 0) self.assertFalse(output.exists()) def test_cli_overwrite_replaces_input_and_creates_backup(self): with tempfile.TemporaryDirectory() as temp_dir: input_path = Path(temp_dir) / "media.mkv" input_path.write_text("original", encoding="utf-8") def create_ffmpeg_output(args, command): Path(command[-1]).write_text("converted", encoding="utf-8") return True, "success" with patch("ffmpeg_handling.cli.shutil.which", return_value="/usr/bin/ffmpeg"), \ patch("ffmpeg_handling.cli.setup_logging"), \ patch( "ffmpeg_handling.video_commands.run_ffmpeg_for_args", side_effect=create_ffmpeg_output, ), \ self.assertLogs("ffmpeg_handling", level="INFO"): result = main([ "downsize", str(input_path), "--overwrite", "--backup", "-w", "320", "-he", "180", "-c", "30", ]) self.assertEqual(result, 0) self.assertEqual(input_path.read_text(encoding="utf-8"), "converted") backup = input_path.with_name("media_backup.mkv") self.assertEqual(backup.read_text(encoding="utf-8"), "original") self.assertFalse(input_path.with_name("media_overwrite.mkv").exists()) def test_cli_overwrite_replaces_input_after_successful_command(self): with tempfile.TemporaryDirectory() as temp_dir: input_path = Path(temp_dir) / "media.mkv" input_path.write_text("original", encoding="utf-8") def create_ffmpeg_output(args, command): Path(command[-1]).write_text("converted", encoding="utf-8") return True, "success" with patch("ffmpeg_handling.cli.shutil.which", return_value="/usr/bin/ffmpeg"), \ patch("ffmpeg_handling.cli.setup_logging"), \ patch( "ffmpeg_handling.video_commands.run_ffmpeg_for_args", side_effect=create_ffmpeg_output, ), \ self.assertLogs("ffmpeg_handling", level="INFO"): result = main([ "downsize", str(input_path), "--overwrite", "-w", "320", "-he", "180", "-c", "30", ]) self.assertEqual(result, 0) self.assertEqual(input_path.read_text(encoding="utf-8"), "converted") self.assertFalse(input_path.with_name("media_overwrite.mkv").exists()) self.assertFalse(input_path.with_name("media_backup.mkv").exists()) if __name__ == "__main__": unittest.main()