Kling 节点接入官方 4.0 / Flash(文生/图生/Omni)
This commit is contained in:
+171
-42
@@ -1,4 +1,4 @@
|
||||
"""Offline regression tests for the Kling 4.0 request contract."""
|
||||
"""Offline regression tests for the official Kling 4.0 / Flash contract."""
|
||||
|
||||
import sys
|
||||
import types
|
||||
@@ -21,13 +21,31 @@ 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,
|
||||
)
|
||||
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):
|
||||
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",
|
||||
@@ -36,12 +54,49 @@ class KlingV4RequestTests(unittest.TestCase):
|
||||
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")
|
||||
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(
|
||||
@@ -53,41 +108,69 @@ class KlingV4RequestTests(unittest.TestCase):
|
||||
"https://cdn.example.invalid/middle-2.png",
|
||||
],
|
||||
last_frame="https://cdn.example.invalid/last.png",
|
||||
aspect_ratio="16:9",
|
||||
)
|
||||
|
||||
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")
|
||||
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_omni_mode_uses_reference_metadata_and_caps_inputs(self):
|
||||
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 @Image1 and the movement from @Video1",
|
||||
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"],
|
||||
keep_original_sound=True,
|
||||
audio="保留原声",
|
||||
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")
|
||||
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, "关键帧模式必须提供"):
|
||||
with self.assertRaisesRegex(ValueError, "至少需要一张关键帧"):
|
||||
build_kling_v4_body(generation_mode="关键帧", prompt="move")
|
||||
with self.assertRaisesRegex(ValueError, "不能连接图片或视频"):
|
||||
build_kling_v4_body(
|
||||
@@ -97,21 +180,67 @@ class KlingV4RequestTests(unittest.TestCase):
|
||||
)
|
||||
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_output_envelope_is_read_by_shared_parser(self):
|
||||
self.assertEqual(
|
||||
extract_video_url({
|
||||
"data": {
|
||||
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",
|
||||
"output": {
|
||||
"media": [{"type": "video", "url": "https://cdn.example.invalid/result.mp4"}],
|
||||
},
|
||||
},
|
||||
}),
|
||||
"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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user