| 1 | import asyncio |
| 2 | import tempfile |
| 3 | import unittest |
| 4 | from datetime import datetime, timedelta |
| 5 | from pathlib import Path |
| 6 | |
| 7 | from patchright.async_api import async_playwright |
| 8 | |
| 9 | import sau_cli |
| 10 | from uploader.meta_uploader.facebook_page_uploader.main import MetaBusinessUploader, _is_business_logged_out |
| 11 | |
| 12 | |
| 13 | class MetaBusinessValidationTests(unittest.TestCase): |
| 14 | def test_caption_contains_title_description_and_tags(self): |
| 15 | with tempfile.TemporaryDirectory() as directory: |
| 16 | path = Path(directory) / "clip.mp4" |
| 17 | path.write_bytes(b"placeholder") |
| 18 | uploader = MetaBusinessUploader( |
| 19 | "facebook-page", |
| 20 | Path(directory) / "account.json", |
| 21 | title="标题", |
| 22 | description="正文", |
| 23 | tags=["#一个", "两个"], |
| 24 | video_file=path, |
| 25 | ) |
| 26 | self.assertEqual(uploader._caption(), "标题\n\n正文\n\n#一个 #两个") |
| 27 | |
| 28 | def test_schedule_requires_future_time(self): |
| 29 | with tempfile.TemporaryDirectory() as directory: |
| 30 | path = Path(directory) / "clip.mp4" |
| 31 | path.write_bytes(b"placeholder") |
| 32 | with self.assertRaises(ValueError): |
| 33 | MetaBusinessUploader( |
| 34 | "facebook-page", |
| 35 | Path(directory) / "account.json", |
| 36 | title="标题", |
| 37 | video_file=path, |
| 38 | schedule=datetime.now() + timedelta(minutes=5), |
| 39 | ) |
| 40 | |
| 41 | def test_business_suite_nav_is_positive_login_evidence(self): |
| 42 | async def scenario(): |
| 43 | async with async_playwright() as playwright: |
| 44 | browser = await playwright.chromium.launch(headless=True, channel="chrome") |
| 45 | page = await browser.new_page() |
| 46 | await page.set_content('<nav><a href="/latest/home">Meta Business Suite</a></nav>') |
| 47 | result = await _is_business_logged_out(page) |
| 48 | await browser.close() |
| 49 | return result |
| 50 | |
| 51 | self.assertFalse(asyncio.run(scenario())) |
| 52 | |
| 53 | def test_meta_cli_exposes_schedule_and_images(self): |
| 54 | args = sau_cli.build_parser().parse_args( |
| 55 | [ |
| 56 | "facebook-page", |
| 57 | "upload-note", |
| 58 | "--account", |
| 59 | "creator", |
| 60 | "--images", |
| 61 | __file__, |
| 62 | "--title", |
| 63 | "标题", |
| 64 | "--schedule", |
| 65 | "2026-10-21 10:30", |
| 66 | ] |
| 67 | ) |
| 68 | self.assertEqual(args.platform, "facebook-page") |
| 69 | self.assertEqual(args.images, [Path(__file__)]) |
| 70 | self.assertEqual(args.schedule.year, 2026) |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | unittest.main() |
| 75 |