| 1 | import asyncio |
| 2 | import tempfile |
| 3 | import unittest |
| 4 | from argparse import Namespace |
| 5 | from pathlib import Path |
| 6 | from types import SimpleNamespace |
| 7 | from unittest.mock import AsyncMock, patch |
| 8 | |
| 9 | import sau_cli |
| 10 | |
| 11 | |
| 12 | class BilibiliCliTests(unittest.TestCase): |
| 13 | def test_build_parser_accepts_bilibili_login(self): |
| 14 | parser = sau_cli.build_parser() |
| 15 | args = parser.parse_args(["bilibili", "login", "--account", "creator"]) |
| 16 | self.assertEqual(args.platform, "bilibili") |
| 17 | self.assertEqual(args.action, "login") |
| 18 | |
| 19 | def test_build_parser_requires_tid_for_upload_video(self): |
| 20 | parser = sau_cli.build_parser() |
| 21 | with self.assertRaises(SystemExit): |
| 22 | parser.parse_args( |
| 23 | [ |
| 24 | "bilibili", |
| 25 | "upload-video", |
| 26 | "--account", |
| 27 | "creator", |
| 28 | "--file", |
| 29 | "demo.mp4", |
| 30 | "--title", |
| 31 | "hello", |
| 32 | "--desc", |
| 33 | "hello", |
| 34 | ] |
| 35 | ) |
| 36 | |
| 37 | def test_dispatch_bilibili_check_prints_valid(self): |
| 38 | args = Namespace(platform="bilibili", action="check", account="creator") |
| 39 | with patch("sau_cli.check_bilibili_account", new=AsyncMock(return_value=True)): |
| 40 | code = asyncio.run(sau_cli.dispatch(args)) |
| 41 | self.assertEqual(code, 0) |
| 42 | |
| 43 | def test_login_bilibili_account_returns_friendly_message_without_terminal(self): |
| 44 | with patch("sau_cli.has_interactive_terminal", return_value=False): |
| 45 | result = asyncio.run(sau_cli.login_bilibili_account("creator")) |
| 46 | self.assertFalse(result["success"]) |
| 47 | self.assertIn("local interactive terminal", result["message"].lower()) |
| 48 | self.assertIn("qrcode.png", result["message"].lower()) |
| 49 | |
| 50 | def test_upload_bilibili_video_passes_thumbnail_to_biliup(self): |
| 51 | with tempfile.TemporaryDirectory() as temp_dir: |
| 52 | account_file = Path(temp_dir) / "account.json" |
| 53 | account_file.write_text("{}", encoding="utf-8") |
| 54 | request = sau_cli.BilibiliVideoUploadRequest("creator", Path("demo.mp4"), "hello", "hello", 249, ["test"], 0, Path("cover.png")) |
| 55 | with patch("sau_cli.resolve_account_file", return_value=account_file), patch("sau_cli.run_biliup_command", return_value=SimpleNamespace(returncode=0, stdout="", stderr="")) as run_biliup: |
| 56 | asyncio.run(sau_cli.upload_bilibili_video(request)) |
| 57 | self.assertIn("--cover", run_biliup.call_args.args[0]) |
| 58 | self.assertIn("cover.png", run_biliup.call_args.args[0]) |
| 59 |