mirror of
https://github.com/Dasemu/x-space-downloader.git
synced 2026-10-13 21:49:45 +00:00
test: add CLI error cases and cookies option tests
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import click
|
import click
|
||||||
import os
|
import os
|
||||||
|
from click.exceptions import Exit
|
||||||
from src.validator import validate_space_url
|
from src.validator import validate_space_url
|
||||||
from src.downloader import download_space, DownloadError
|
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)
|
is_valid, space_id, error = validate_space_url(url)
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
click.echo(f"Error: {error}", err=True)
|
click.echo(f"Error: {error}", err=True)
|
||||||
raise click.Exit(1)
|
raise Exit(1)
|
||||||
|
|
||||||
# Download the Space
|
# Download the Space
|
||||||
try:
|
try:
|
||||||
@@ -28,7 +29,7 @@ def cli(url, cookies, output):
|
|||||||
click.echo(f"Successfully downloaded to: {output_path}")
|
click.echo(f"Successfully downloaded to: {output_path}")
|
||||||
except DownloadError as e:
|
except DownloadError as e:
|
||||||
click.echo(f"Error: {str(e)}", err=True)
|
click.echo(f"Error: {str(e)}", err=True)
|
||||||
raise click.Exit(2)
|
raise Exit(2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import pytest
|
|||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
from unittest.mock import patch, Mock
|
from unittest.mock import patch, Mock
|
||||||
from src.main import cli
|
from src.main import cli
|
||||||
|
from src.downloader import DownloadError
|
||||||
|
|
||||||
|
|
||||||
def test_cli_valid_url(tmp_path):
|
def test_cli_valid_url(tmp_path):
|
||||||
@@ -19,3 +20,54 @@ def test_cli_valid_url(tmp_path):
|
|||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "Successfully downloaded" in result.output
|
assert "Successfully downloaded" in result.output
|
||||||
assert "space_1ZkKzZWnVRRKv.mp3" 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user