返回 Social Auto Upload
test_sau_browser_cli.py
根目录 / tests / test_sau_browser_cli.py
1 import sau_commands.douyin
2 import sau_commands.tencent
3 import sau_commands.xiaohongshu
4 import asyncio
5 import tempfile
6 import unittest
7 from argparse import Namespace
8 from pathlib import Path
9 from unittest.mock import AsyncMock, patch
10
11 import sau_cli
12
13
14 class BrowserCliParserTests(unittest.TestCase):
15 def test_build_parser_accepts_xiaohongshu_login(self):
16 parser = sau_cli.build_parser()
17 args = parser.parse_args(["xiaohongshu", "login", "--account", "creator"])
18 self.assertEqual(args.platform, "xiaohongshu")
19 self.assertEqual(args.action, "login")
20
21 def test_douyin_upload_video_accepts_desc(self):
22 with tempfile.TemporaryDirectory() as tmp_dir:
23 video_path = Path(tmp_dir) / "demo.mp4"
24 video_path.write_bytes(b"video")
25
26 parser = sau_cli.build_parser()
27 args = parser.parse_args(
28 [
29 "douyin",
30 "upload-video",
31 "--account",
32 "creator",
33 "--file",
34 str(video_path),
35 "--title",
36 "标题",
37 "--desc",
38 "视频简介",
39 ]
40 )
41
42 self.assertEqual(args.desc, "视频简介")
43
44 def test_douyin_upload_video_accepts_dual_thumbnail_aspects(self):
45 with tempfile.TemporaryDirectory() as tmp_dir:
46 video_path = Path(tmp_dir) / "demo.mp4"
47 landscape_path = Path(tmp_dir) / "landscape.png"
48 portrait_path = Path(tmp_dir) / "portrait.png"
49 video_path.write_bytes(b"video")
50 landscape_path.write_bytes(b"image")
51 portrait_path.write_bytes(b"image")
52
53 parser = sau_cli.build_parser()
54 args = parser.parse_args(
55 [
56 "douyin",
57 "upload-video",
58 "--account",
59 "creator",
60 "--file",
61 str(video_path),
62 "--title",
63 "标题",
64 "--thumbnail-landscape",
65 str(landscape_path),
66 "--thumbnail-portrait",
67 str(portrait_path),
68 ]
69 )
70
71 self.assertEqual(args.thumbnail_landscape, landscape_path)
72 self.assertEqual(args.thumbnail_portrait, portrait_path)
73
74 def test_douyin_upload_video_accepts_explicit_declaration(self):
75 with tempfile.TemporaryDirectory() as tmp_dir:
76 video_path = Path(tmp_dir) / "demo.mp4"
77 video_path.write_bytes(b"video")
78 parser = sau_cli.build_parser()
79 args = parser.parse_args([
80 "douyin", "upload-video", "--account", "creator",
81 "--file", str(video_path), "--title", "标题",
82 "--declaration", "已确认声明原文",
83 ])
84 self.assertEqual(args.declaration, "已确认声明原文")
85
86 def test_douyin_upload_video_has_no_implicit_declaration(self):
87 with tempfile.TemporaryDirectory() as tmp_dir:
88 video_path = Path(tmp_dir) / "demo.mp4"
89 video_path.write_bytes(b"video")
90 args = sau_cli.build_parser().parse_args([
91 "douyin", "upload-video", "--account", "creator",
92 "--file", str(video_path), "--title", "标题",
93 ])
94 self.assertIsNone(args.declaration)
95
96 def test_douyin_request_legacy_positional_runtime_flags_keep_their_meaning(self):
97 request = sau_commands.douyin.DouyinVideoUploadRequest(
98 "creator", Path("demo.mp4"), "标题", "简介", [], 0,
99 None, None, None, "", "", "scheduled", False, False,
100 )
101 self.assertEqual(request.publish_strategy, "scheduled")
102 self.assertFalse(request.debug)
103 self.assertFalse(request.headless)
104 self.assertIsNone(request.declaration)
105
106 def test_tencent_upload_video_accepts_dual_thumbnail_aspects(self):
107 with tempfile.TemporaryDirectory() as tmp_dir:
108 video_path = Path(tmp_dir) / "demo.mp4"
109 landscape_path = Path(tmp_dir) / "landscape.png"
110 portrait_path = Path(tmp_dir) / "portrait.png"
111 video_path.write_bytes(b"video")
112 landscape_path.write_bytes(b"image")
113 portrait_path.write_bytes(b"image")
114
115 parser = sau_cli.build_parser()
116 args = parser.parse_args(
117 [
118 "tencent",
119 "upload-video",
120 "--account",
121 "creator",
122 "--file",
123 str(video_path),
124 "--title",
125 "标题",
126 "--thumbnail-landscape",
127 str(landscape_path),
128 "--thumbnail-portrait",
129 str(portrait_path),
130 ]
131 )
132
133 self.assertEqual(args.thumbnail_landscape, landscape_path)
134 self.assertEqual(args.thumbnail_portrait, portrait_path)
135
136 def test_kuaishou_upload_note_accepts_title_and_note(self):
137 with tempfile.TemporaryDirectory() as tmp_dir:
138 image_path = Path(tmp_dir) / "1.png"
139 image_path.write_bytes(b"image")
140
141 parser = sau_cli.build_parser()
142 args = parser.parse_args(
143 [
144 "kuaishou",
145 "upload-note",
146 "--account",
147 "creator",
148 "--images",
149 str(image_path),
150 "--title",
151 "图文标题",
152 "--note",
153 "图文正文",
154 ]
155 )
156
157 self.assertEqual(args.title, "图文标题")
158 self.assertEqual(args.note, "图文正文")
159
160 def test_xiaohongshu_upload_video_defaults_to_headless(self):
161 with tempfile.TemporaryDirectory() as tmp_dir:
162 video_path = Path(tmp_dir) / "demo.mp4"
163 video_path.write_bytes(b"video")
164
165 parser = sau_cli.build_parser()
166 args = parser.parse_args(
167 [
168 "xiaohongshu",
169 "upload-video",
170 "--account",
171 "creator",
172 "--file",
173 str(video_path),
174 "--title",
175 "视频标题",
176 ]
177 )
178
179 self.assertTrue(args.headless)
180
181 def test_xiaohongshu_upload_note_accepts_headed(self):
182 with tempfile.TemporaryDirectory() as tmp_dir:
183 image_path = Path(tmp_dir) / "1.png"
184 image_path.write_bytes(b"image")
185
186 parser = sau_cli.build_parser()
187 args = parser.parse_args(
188 [
189 "xiaohongshu",
190 "upload-note",
191 "--account",
192 "creator",
193 "--images",
194 str(image_path),
195 "--title",
196 "图文标题",
197 "--note",
198 "图文正文",
199 "--headed",
200 ]
201 )
202
203 self.assertFalse(args.headless)
204
205
206 class BrowserCliDispatchTests(unittest.TestCase):
207 def test_upload_tencent_video_forwards_collection_name_to_uploader(self):
208 request = sau_commands.tencent.TencentVideoUploadRequest(
209 account_name="creator",
210 video_file=Path("demo.mp4"),
211 title="视频标题",
212 description="视频简介",
213 tags=["测试"],
214 publish_date=0,
215 collection_name="我的合集",
216 )
217
218 with (
219 patch("sau_commands.tencent.tencent_setup", new=AsyncMock(return_value=True)),
220 patch.object(sau_commands.tencent.TencentVideo, "tencent_upload_video", new=AsyncMock()) as mock_upload,
221 ):
222 asyncio.run(sau_commands.tencent.upload_tencent_video(request))
223
224 mock_upload.assert_awaited_once()
225
226 def test_dispatch_xiaohongshu_check_prints_valid(self):
227 args = Namespace(platform="xiaohongshu", action="check", account="creator")
228 with patch("sau_commands.xiaohongshu.check_xiaohongshu_account", new=AsyncMock(return_value=True)):
229 code = asyncio.run(sau_cli.dispatch(args))
230 self.assertEqual(code, 0)
231
232 def test_dispatch_douyin_upload_note_uses_new_request_fields(self):
233 args = Namespace(
234 platform="douyin",
235 action="upload-note",
236 account="creator",
237 images=[Path("1.png")],
238 title="图文标题",
239 note="图文正文",
240 notef="",
241 bgm="",
242 tags="测试,图文",
243 schedule=0,
244 debug=False,
245 headless=True,
246 )
247 with patch("sau_commands.douyin.upload_note", new=AsyncMock()) as mock_upload:
248 asyncio.run(sau_cli.dispatch(args))
249
250 request = mock_upload.await_args.args[0]
251 self.assertEqual(request.title, "图文标题")
252 self.assertEqual(request.note, "图文正文")
253
254 def test_dispatch_douyin_upload_video_uses_dual_thumbnail_request_fields(self):
255 args = Namespace(
256 platform="douyin",
257 action="upload-video",
258 account="creator",
259 file=Path("demo.mp4"),
260 title="视频标题",
261 desc="视频简介",
262 tags="测试,视频",
263 schedule=0,
264 thumbnail=None,
265 thumbnail_landscape=Path("landscape.png"),
266 thumbnail_portrait=Path("portrait.png"),
267 product_link="",
268 product_title="",
269 declaration="已确认声明原文",
270 collection=None,
271 debug=False,
272 headless=True,
273 )
274 with patch("sau_commands.douyin.upload_video", new=AsyncMock()) as mock_upload:
275 asyncio.run(sau_cli.dispatch(args))
276
277 request = mock_upload.await_args.args[0]
278 self.assertEqual(request.thumbnail_landscape_file, Path("landscape.png"))
279 self.assertEqual(request.thumbnail_portrait_file, Path("portrait.png"))
280 self.assertEqual(request.declaration, "已确认声明原文")
281
282 def test_dispatch_tencent_upload_video_uses_dual_thumbnail_request_fields(self):
283 args = Namespace(
284 platform="tencent",
285 action="upload-video",
286 account="creator",
287 file=Path("demo.mp4"),
288 title="视频标题",
289 desc="视频简介",
290 tags="测试,视频",
291 schedule=0,
292 thumbnail=None,
293 thumbnail_landscape=Path("landscape.png"),
294 thumbnail_portrait=Path("portrait.png"),
295 short_title=None,
296 category=None,
297 collection=None,
298 draft=False,
299 debug=False,
300 headless=True,
301 )
302 with patch("sau_commands.tencent.upload_tencent_video", new=AsyncMock()) as mock_upload:
303 asyncio.run(sau_cli.dispatch(args))
304
305 request = mock_upload.await_args.args[0]
306 self.assertEqual(request.thumbnail_landscape_file, Path("landscape.png"))
307 self.assertEqual(request.thumbnail_portrait_file, Path("portrait.png"))
308
309 def test_dispatch_xiaohongshu_upload_video_uses_headed_request(self):
310 args = Namespace(
311 platform="xiaohongshu",
312 action="upload-video",
313 account="creator",
314 file=Path("demo.mp4"),
315 title="视频标题",
316 desc="视频简介",
317 tags="测试,视频",
318 schedule=0,
319 thumbnail=None,
320 debug=False,
321 headless=False,
322 )
323 with patch("sau_commands.xiaohongshu.upload_xiaohongshu_video", new=AsyncMock()) as mock_upload:
324 asyncio.run(sau_cli.dispatch(args))
325
326 request = mock_upload.await_args.args[0]
327 self.assertEqual(request.title, "视频标题")
328 self.assertEqual(request.description, "视频简介")
329 self.assertFalse(request.headless)
330
331 def test_dispatch_xiaohongshu_upload_note_uses_headless_request(self):
332 args = Namespace(
333 platform="xiaohongshu",
334 action="upload-note",
335 account="creator",
336 images=[Path("1.png"), Path("2.png")],
337 title="图文标题",
338 note="图文正文",
339 tags="测试,图文",
340 schedule=0,
341 debug=False,
342 headless=True,
343 )
344 with patch("sau_commands.xiaohongshu.upload_xiaohongshu_note", new=AsyncMock()) as mock_upload:
345 asyncio.run(sau_cli.dispatch(args))
346
347 request = mock_upload.await_args.args[0]
348 self.assertEqual(request.title, "图文标题")
349 self.assertEqual(request.note, "图文正文")
350 self.assertTrue(request.headless)
351 self.assertEqual(len(request.image_files), 2)
352
353
354 if __name__ == "__main__":
355 unittest.main()
356
356 lines PYTHON