"""Offline tests for Seedance task polling and result parsing.""" import json import sys import types import unittest from pathlib import Path from unittest.mock import AsyncMock, call, patch PACKAGE_DIR = Path(__file__).resolve().parents[1] sys.path.insert(0, str(PACKAGE_DIR.parent)) # Load only the client module. Importing the plugin-wide registry would make # this offline test depend on the portable build's CUDA availability. package = types.ModuleType("comfyui_o1key") package.__path__ = [str(PACKAGE_DIR)] sys.modules["comfyui_o1key"] = package for child in ("clients", "utils"): module = types.ModuleType(f"comfyui_o1key.{child}") module.__path__ = [str(PACKAGE_DIR / child)] sys.modules[f"comfyui_o1key.{child}"] = module from comfyui_o1key.clients.seedance_client import SeedanceClient # noqa: E402 def make_client(): client = SeedanceClient.__new__(SeedanceClient) client.api_key = "test-token" client.base_url = "https://new-api.example.com" return client def response(payload): value = AsyncMock() value.text = AsyncMock(return_value=json.dumps(payload)) value.release = lambda: None return value class SeedancePollingTests(unittest.IsolatedAsyncioTestCase): async def test_completed_task_reads_metadata_urls(self): request = AsyncMock(side_effect=[ response({ "task_id": "task-1", "status": "queued", "progress": 0, }), response({ "task_id": "task-1", "status": "completed", "progress": 100, "metadata": { "url": "https://cdn.example.com/video.mp4", "last_frame_url": "https://cdn.example.com/frame.png", }, }), ]) with ( patch("comfyui_o1key.clients.seedance_client.async_request_with_retry", request), patch("comfyui_o1key.clients.seedance_client.interruptible_sleep", AsyncMock()), ): video_url, last_frame_url = await make_client().poll_async("task-1", object()) self.assertEqual(video_url, "https://cdn.example.com/video.mp4") self.assertEqual(last_frame_url, "https://cdn.example.com/frame.png") async def test_polling_uses_the_retrying_request_helper(self): request = AsyncMock(return_value=response({ "status": "completed", "result_url": "https://cdn.example.com/video.mp4", })) with patch("comfyui_o1key.clients.seedance_client.async_request_with_retry", request): await make_client().poll_async("task-1", object()) self.assertEqual(request.await_args_list, [ call( unittest.mock.ANY, "GET", "https://new-api.example.com/v1/video/generations/task-1", headers={ "Authorization": "Bearer test-token", "Content-Type": "application/json", }, prefix="Seedance 状态查询: ", ), ]) async def test_get_task_async_reads_one_status_response(self): request = AsyncMock(return_value=response({ "data": { "status": "succeeded", "model": "doubao-seedance-2-5-260628-max", "resolution": "480p", }, })) with patch("comfyui_o1key.clients.seedance_client.async_request_with_retry", request): payload = await make_client().get_task_async( "cgt-1", session=object() ) self.assertEqual(payload["data"]["status"], "succeeded") self.assertEqual(request.await_args.args[2], "https://new-api.example.com/v1/video/generations/cgt-1") self.assertEqual(request.await_args.kwargs["prefix"], "Seedance 任务查询: ") if __name__ == "__main__": unittest.main()