| 1 | import unittest |
| 2 | from unittest.mock import patch |
| 3 | |
| 4 | from tools.video_generator_omni_yunwu_api import ( |
| 5 | VideoGeneratorOmniYunwuAPI, |
| 6 | VideoGeneratorOminiYunwuAPI, |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | class _FakeResponse: |
| 11 | def __init__(self, payload): |
| 12 | self.payload = payload |
| 13 | |
| 14 | async def __aenter__(self): |
| 15 | return self |
| 16 | |
| 17 | async def __aexit__(self, exc_type, exc, tb): |
| 18 | return False |
| 19 | |
| 20 | async def json(self): |
| 21 | return self.payload |
| 22 | |
| 23 | |
| 24 | class _FakeSession: |
| 25 | def __init__(self, payload): |
| 26 | self.payload = payload |
| 27 | self.calls = [] |
| 28 | |
| 29 | async def __aenter__(self): |
| 30 | return self |
| 31 | |
| 32 | async def __aexit__(self, exc_type, exc, tb): |
| 33 | return False |
| 34 | |
| 35 | def get(self, url, **kwargs): |
| 36 | self.calls.append(("get", url, kwargs)) |
| 37 | return _FakeResponse(self.payload) |
| 38 | |
| 39 | |
| 40 | class TestVideoGeneratorOmniYunwuAPI(unittest.IsolatedAsyncioTestCase): |
| 41 | def test_text_to_video_payload(self): |
| 42 | generator = VideoGeneratorOmniYunwuAPI(api_key="test-key") |
| 43 | |
| 44 | payload = generator._build_payload( |
| 45 | prompt="hello world", |
| 46 | reference_image_paths=[], |
| 47 | aspect_ratio="16:9", |
| 48 | seconds=8, |
| 49 | size=None, |
| 50 | enable_upsample=False, |
| 51 | enable_sample=None, |
| 52 | ) |
| 53 | |
| 54 | self.assertEqual(payload["model"], "omni-flash") |
| 55 | self.assertEqual(payload["type"], 1) |
| 56 | self.assertEqual(payload["seconds"], "8") |
| 57 | self.assertEqual(payload["aspect_ratio"], "16:9") |
| 58 | self.assertFalse(payload["enable_upsample"]) |
| 59 | self.assertNotIn("images", payload) |
| 60 | |
| 61 | def test_first_last_frame_payload(self): |
| 62 | generator = VideoGeneratorOmniYunwuAPI(api_key="test-key") |
| 63 | |
| 64 | payload = generator._build_payload( |
| 65 | prompt="transition", |
| 66 | reference_image_paths=["https://example.com/first.png", "https://example.com/last.png"], |
| 67 | aspect_ratio="9:16", |
| 68 | seconds=6, |
| 69 | size=None, |
| 70 | enable_upsample=None, |
| 71 | enable_sample=True, |
| 72 | ) |
| 73 | |
| 74 | self.assertEqual(payload["type"], 2) |
| 75 | self.assertEqual(payload["images"], ["https://example.com/first.png", "https://example.com/last.png"]) |
| 76 | self.assertEqual(payload["seconds"], "6") |
| 77 | self.assertTrue(payload["enable_sample"]) |
| 78 | |
| 79 | def test_three_reference_images_use_reference_mode(self): |
| 80 | generator = VideoGeneratorOmniYunwuAPI(api_key="test-key") |
| 81 | |
| 82 | payload = generator._build_payload( |
| 83 | prompt="blend references", |
| 84 | reference_image_paths=[ |
| 85 | "https://example.com/1.png", |
| 86 | "https://example.com/2.png", |
| 87 | "https://example.com/3.png", |
| 88 | ], |
| 89 | aspect_ratio="16:9", |
| 90 | seconds=None, |
| 91 | size=None, |
| 92 | enable_upsample=None, |
| 93 | enable_sample=None, |
| 94 | ) |
| 95 | |
| 96 | self.assertEqual(payload["type"], 3) |
| 97 | self.assertEqual(len(payload["images"]), 3) |
| 98 | |
| 99 | def test_too_many_reference_images_raise(self): |
| 100 | generator = VideoGeneratorOmniYunwuAPI(api_key="test-key") |
| 101 | |
| 102 | with self.assertRaises(ValueError): |
| 103 | generator._build_payload( |
| 104 | prompt="too many", |
| 105 | reference_image_paths=["1", "2", "3", "4"], |
| 106 | aspect_ratio="16:9", |
| 107 | seconds=None, |
| 108 | size=None, |
| 109 | enable_upsample=None, |
| 110 | enable_sample=None, |
| 111 | ) |
| 112 | |
| 113 | async def test_query_completed_uses_top_level_video_url(self): |
| 114 | generator = VideoGeneratorOmniYunwuAPI(api_key="test-key", poll_interval=0, max_poll_attempts=1) |
| 115 | session = _FakeSession({"status": "completed", "video_url": "https://example.com/out.mp4"}) |
| 116 | |
| 117 | with patch("tools.video_generator_omni_yunwu_api.aiohttp.ClientSession", return_value=session): |
| 118 | video_url = await generator.query_video_generation_task("task-1", "omni-flash") |
| 119 | |
| 120 | self.assertEqual(video_url, "https://example.com/out.mp4") |
| 121 | self.assertEqual(session.calls[0][2]["params"], {"id": "task-1", "model": "omni-flash"}) |
| 122 | |
| 123 | async def test_query_failed_raises(self): |
| 124 | generator = VideoGeneratorOmniYunwuAPI(api_key="test-key", poll_interval=0, max_poll_attempts=1) |
| 125 | session = _FakeSession({"status": "failed", "error": "视频生成失败"}) |
| 126 | |
| 127 | with patch("tools.video_generator_omni_yunwu_api.aiohttp.ClientSession", return_value=session): |
| 128 | with self.assertRaises(RuntimeError): |
| 129 | await generator.query_video_generation_task("task-1", "omni-flash") |
| 130 | |
| 131 | def test_omini_alias(self): |
| 132 | self.assertTrue(issubclass(VideoGeneratorOminiYunwuAPI, VideoGeneratorOmniYunwuAPI)) |
| 133 | |
| 134 | |
| 135 | if __name__ == "__main__": |
| 136 | unittest.main() |
| 137 |