"""Offline regression tests for the Kling 4.0 request contract.""" import sys import types import unittest from pathlib import Path PLUGIN_ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(PLUGIN_ROOT.parent)) package = types.ModuleType("comfyui_o1key") package.__path__ = [str(PLUGIN_ROOT)] sys.modules.setdefault("comfyui_o1key", package) clients = types.ModuleType("comfyui_o1key.clients") clients.__path__ = [str(PLUGIN_ROOT / "clients")] sys.modules.setdefault("comfyui_o1key.clients", clients) utils = types.ModuleType("comfyui_o1key.utils") utils.__path__ = [str(PLUGIN_ROOT / "utils")] sys.modules.setdefault("comfyui_o1key.utils", utils) from comfyui_o1key.clients.kling_video_client import ( # noqa: E402 build_kling_v4_body, endpoint_for, ) from comfyui_o1key.utils.video_task import extract_video_url # noqa: E402 class KlingV4RequestTests(unittest.TestCase): def test_text_mode_uses_resolution_mode_and_adaptive_ratio(self): body = build_kling_v4_body( generation_mode="文生视频", prompt="A slow dolly shot through a rainy market", duration=30, resolution="1080p", aspect_ratio="智能", ) self.assertEqual(body["model_name"], "kling-v4") self.assertEqual(body["duration"], 30) self.assertEqual(body["mode"], "pro") self.assertEqual(body["aspect_ratio"], "adaptive") self.assertNotIn("metadata", body) self.assertEqual(endpoint_for("文生视频")[0], "/kling/v1/videos/text2video") def test_keyframes_keep_frame_order_and_omit_aspect_ratio(self): body = build_kling_v4_body( generation_mode="关键帧", prompt="Move continuously between the keyframes", first_frame="https://cdn.example.invalid/first.png", middle_frames=[ "https://cdn.example.invalid/middle-1.png", "https://cdn.example.invalid/middle-2.png", ], last_frame="https://cdn.example.invalid/last.png", ) self.assertEqual(body["metadata"], { "first_frame": "https://cdn.example.invalid/first.png", "middle_frames": [ "https://cdn.example.invalid/middle-1.png", "https://cdn.example.invalid/middle-2.png", ], "last_frame": "https://cdn.example.invalid/last.png", }) self.assertNotIn("aspect_ratio", body) self.assertEqual(endpoint_for("关键帧")[0], "/kling/v1/videos/image2video") def test_omni_mode_uses_reference_metadata_and_caps_inputs(self): body = build_kling_v4_body( generation_mode="全能参考", prompt="Use the character from @Image1 and the movement from @Video1", reference_images=["https://cdn.example.invalid/character.png"], reference_videos=["https://cdn.example.invalid/motion.mp4"], keep_original_sound=True, aspect_ratio="21:9", ) self.assertEqual(body["metadata"]["refer_images"], [ "https://cdn.example.invalid/character.png", ]) self.assertEqual(body["metadata"]["videos"], [ "https://cdn.example.invalid/motion.mp4", ]) self.assertTrue(body["keep_original_sound"]) self.assertEqual(body["aspect_ratio"], "21:9") self.assertEqual(endpoint_for("全能参考")[0], "/kling/v1/videos/omni-video") def test_modes_reject_invalid_combinations_before_upload(self): with self.assertRaisesRegex(ValueError, "关键帧模式必须提供"): build_kling_v4_body(generation_mode="关键帧", prompt="move") with self.assertRaisesRegex(ValueError, "不能连接图片或视频"): build_kling_v4_body( generation_mode="文生视频", prompt="move", first_frame="https://cdn.example.invalid/frame.png", ) with self.assertRaisesRegex(ValueError, "至少需要一张参考图"): build_kling_v4_body(generation_mode="全能参考", prompt="move") def test_v4_output_envelope_is_read_by_shared_parser(self): self.assertEqual( extract_video_url({ "data": { "status": "succeeded", "output": { "media": [{"type": "video", "url": "https://cdn.example.invalid/result.mp4"}], }, }, }), "https://cdn.example.invalid/result.mp4", ) if __name__ == "__main__": unittest.main()