diff --git a/src/main.py b/src/main.py index 2c30f76..54ddc49 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,6 @@ import click import os +from click.exceptions import Exit from src.validator import validate_space_url from src.downloader import download_space, DownloadError @@ -19,7 +20,7 @@ def cli(url, cookies, output): is_valid, space_id, error = validate_space_url(url) if not is_valid: click.echo(f"Error: {error}", err=True) - raise click.Exit(1) + raise Exit(1) # Download the Space try: @@ -28,7 +29,7 @@ def cli(url, cookies, output): click.echo(f"Successfully downloaded to: {output_path}") except DownloadError as e: click.echo(f"Error: {str(e)}", err=True) - raise click.Exit(2) + raise Exit(2) if __name__ == '__main__': diff --git a/tests/test_main.py b/tests/test_main.py index d94bc80..d09aa0a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -2,6 +2,7 @@ import pytest from click.testing import CliRunner from unittest.mock import patch, Mock from src.main import cli +from src.downloader import DownloadError def test_cli_valid_url(tmp_path): @@ -19,3 +20,54 @@ def test_cli_valid_url(tmp_path): assert result.exit_code == 0 assert "Successfully downloaded" in result.output assert "space_1ZkKzZWnVRRKv.mp3" in result.output + + +def test_cli_invalid_url(): + """Test CLI with invalid URL.""" + runner = CliRunner() + + result = runner.invoke(cli, ['https://invalid.com/spaces/123']) + + assert result.exit_code == 1 + assert "Invalid URL format" in result.output + + +def test_cli_download_failure(): + """Test CLI when download fails.""" + runner = CliRunner() + + with patch('src.main.validate_space_url') as mock_validate, \ + patch('src.main.download_space') as mock_download: + + mock_validate.return_value = (True, "1ZkKzZWnVRRKv", None) + mock_download.side_effect = DownloadError("Video unavailable") + + result = runner.invoke(cli, ['https://x.com/i/spaces/1ZkKzZWnVRRKv']) + + assert result.exit_code == 2 + assert "Video unavailable" in result.output + + +def test_cli_with_cookies(tmp_path): + """Test CLI with cookies file.""" + runner = CliRunner() + + # Create a fake cookies file + cookies_file = tmp_path / "cookies.txt" + cookies_file.write_text("# Netscape HTTP Cookie File\n") + + with patch('src.main.validate_space_url') as mock_validate, \ + patch('src.main.download_space') as mock_download: + + mock_validate.return_value = (True, "1ZkKzZWnVRRKv", None) + mock_download.return_value = str(tmp_path / "space_1ZkKzZWnVRRKv.mp3") + + result = runner.invoke(cli, [ + 'https://x.com/i/spaces/1ZkKzZWnVRRKv', + '--cookies', str(cookies_file) + ]) + + assert result.exit_code == 0 + mock_download.assert_called_once() + # Check that cookies file was passed as third positional argument + assert mock_download.call_args[0][2] == str(cookies_file)