Files
comfyui_o1key/tests/test_kling_video.py

247 lines
10 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Offline regression tests for the official Kling 4.0 / Flash 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,
canonical_kling_v4_model,
endpoint_for,
extract_kling_v4_error_message,
extract_kling_v4_status,
extract_kling_v4_task_id,
extract_kling_v4_video_url,
is_kling_v4_failure_status,
is_kling_v4_model,
is_kling_v4_success_status,
kling_v4_model_spec,
)
class KlingV4RequestTests(unittest.TestCase):
def test_model_catalog_and_aliases(self):
self.assertEqual(canonical_kling_v4_model("v4"), "v4")
self.assertEqual(canonical_kling_v4_model("Kling 4.0"), "v4")
self.assertEqual(canonical_kling_v4_model("Kling 4.0 Flash"), "v4-flash")
self.assertTrue(is_kling_v4_model("v4-flash"))
self.assertFalse(is_kling_v4_model("v3-omni"))
self.assertEqual(kling_v4_model_spec("v4")["default_resolution"], "1080p")
self.assertEqual(kling_v4_model_spec("v4-flash")["default_resolution"], "720p")
self.assertEqual(kling_v4_model_spec("v4-flash")["max_duration"], 20)
def test_text_mode_uses_official_path_and_settings(self):
body = build_kling_v4_body(
generation_mode="文生视频",
prompt="A slow dolly shot through a rainy market",
duration=30,
resolution="1080p",
aspect_ratio="智能",
)
self.assertNotIn("model", body)
self.assertNotIn("model_name", body)
self.assertEqual(body["prompt"], "A slow dolly shot through a rainy market")
self.assertEqual(body["settings"]["duration"], 30)
self.assertEqual(body["settings"]["resolution"], "1080p")
self.assertEqual(body["settings"]["aspect_ratio"], "16:9")
self.assertEqual(body["options"]["watermark_info"]["enabled"], False)
self.assertNotIn("contents", body)
self.assertEqual(
endpoint_for("文生视频", "v4"),
("/kling/text-to-video/kling-4.0", "/kling/text-to-video/kling-4.0/{task_id}"),
)
self.assertEqual(
endpoint_for("文生视频", "Kling 4.0 Flash")[0],
"/kling/text-to-video/kling-4.0-flash",
)
def test_text_mode_defaults_resolution_by_model(self):
standard = build_kling_v4_body(generation_mode="text", prompt="harbor sunrise")
flash = build_kling_v4_body(
generation_mode="text",
prompt="harbor sunrise",
model="v4-flash",
)
self.assertEqual(standard["settings"]["resolution"], "1080p")
self.assertEqual(flash["settings"]["resolution"], "720p")
def test_flash_rejects_1080p_and_long_duration(self):
with self.assertRaisesRegex(ValueError, "仅支持 720p"):
build_kling_v4_body(
generation_mode="文生视频",
prompt="harbor sunrise",
model="v4-flash",
resolution="1080p",
)
with self.assertRaisesRegex(ValueError, "3~20 秒"):
build_kling_v4_body(
generation_mode="文生视频",
prompt="harbor sunrise",
model="v4-flash",
duration=21,
resolution="720p",
)
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",
aspect_ratio="16:9",
)
self.assertEqual(
[item["type"] for item in body["contents"]],
["prompt", "first_frame", "middle_frame", "middle_frame", "last_frame"],
)
self.assertEqual(body["contents"][1]["url"], "https://cdn.example.invalid/first.png")
self.assertEqual(body["contents"][2]["id"], "middle_1")
self.assertEqual(body["contents"][3]["id"], "middle_2")
self.assertNotIn("aspect_ratio", body["settings"])
self.assertNotIn("audio", body["settings"])
self.assertEqual(
endpoint_for("关键帧")[0],
"/kling/image-to-video/kling-4.0",
)
def test_keyframes_allow_any_single_frame(self):
body = build_kling_v4_body(
generation_mode="关键帧",
prompt="Hold on the ending pose",
last_frame="https://cdn.example.invalid/last.png",
)
self.assertEqual(body["contents"][1]["type"], "last_frame")
def test_omni_mode_uses_official_contents_and_optional_original_audio(self):
body = build_kling_v4_body(
generation_mode="全能参考",
prompt="Use the character from @image_1 and the movement from @video_1",
reference_images=["https://cdn.example.invalid/character.png"],
reference_videos=["https://cdn.example.invalid/motion.mp4"],
audio="保留原声",
aspect_ratio="21:9",
)
types = [item["type"] for item in body["contents"]]
self.assertEqual(types, ["prompt", "refer_image", "feature_video"])
self.assertEqual(body["contents"][1]["id"], "image_1")
self.assertEqual(body["contents"][2]["id"], "video_1")
self.assertEqual(body["settings"]["audio"], "original")
self.assertEqual(body["settings"]["aspect_ratio"], "21:9")
self.assertEqual(endpoint_for("全能参考")[0], "/kling/omni-video/kling-4.0")
self.assertEqual(
endpoint_for("全能参考", "v4-flash")[0],
"/kling/omni-video/kling-4.0-flash",
)
def test_omni_base_video_uses_first_clip(self):
body = build_kling_v4_body(
generation_mode="全能参考",
prompt="Edit @video_1 while following @video_2",
reference_videos=[
"https://cdn.example.invalid/source.mp4",
"https://cdn.example.invalid/motion.mp4",
],
video_reference_type="待编辑视频",
)
self.assertEqual(body["contents"][1]["type"], "base_video")
self.assertEqual(body["contents"][2]["type"], "feature_video")
self.assertNotIn("audio", body["settings"])
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")
with self.assertRaisesRegex(ValueError, "保留原声需要至少一段"):
build_kling_v4_body(
generation_mode="全能参考",
prompt="move",
reference_images=["https://cdn.example.invalid/character.png"],
audio="original",
)
def test_v4_query_parser_reads_official_task_array(self):
created = {
"code": 0,
"message": "",
"request_id": "req_xxxxxxxxxx",
"data": {"id": "task_xxxxxxxxxx", "status": "submitted"},
}
self.assertEqual(extract_kling_v4_task_id(created), "task_xxxxxxxxxx")
succeeded = {
"code": 0,
"message": "",
"request_id": "req_xxxxxxxxxx",
"data": [
{
"id": "task_xxxxxxxxxx",
"status": "succeeded",
"message": "",
"outputs": [
{
"type": "video",
"url": "https://cdn.example.invalid/result.mp4",
"watermark_url": "https://cdn.example.invalid/watermark.mp4",
"duration": "10.2",
}
],
"usage": {"output_duration": "10"},
}
],
}
self.assertEqual(extract_kling_v4_status(succeeded), "succeeded")
self.assertTrue(is_kling_v4_success_status(extract_kling_v4_status(succeeded)))
self.assertEqual(
extract_kling_v4_video_url(succeeded),
"https://cdn.example.invalid/result.mp4",
)
failed = {
"code": 0,
"data": [{"id": "task_xxxxxxxxxx", "status": "failed", "message": "内容审核未通过"}],
}
self.assertTrue(is_kling_v4_failure_status(extract_kling_v4_status(failed), failed))
self.assertEqual(extract_kling_v4_error_message(failed), "内容审核未通过")
# A 3.0 Omni envelope must not be treated as a 4.0 success URL.
legacy = {
"task_id": "task_omni_xxxxxxxxxx",
"status": "SUCCESS",
"video_url": "https://cdn.example.invalid/legacy.mp4",
}
self.assertEqual(extract_kling_v4_status(legacy), "")
self.assertIsNone(extract_kling_v4_video_url(legacy))
if __name__ == "__main__":
unittest.main()