PRINTFILM: AI short-video marketing and AI short-drama creation platform
fix(kepu): 修复视频用量记录的项目变量作用域
yi1bb9bdcff272
diff --git a/backend/app/services/pipeline.py b/backend/app/services/pipeline.pyindex ae096ac..51229e9 100644--- a/backend/app/services/pipeline.py+++ b/backend/app/services/pipeline.py@@ -1150,6 +1150,7 @@ async def _parallel_videos(project_id: int) -> None:.options(selectinload(Project.shots), selectinload(Project.template)))project = result.scalar_one()+ project_user_id = int(project.user_id)tpl = project.template# 科普 full:默认 Seedance 内置口播;回退模式才 ambient_only + 后期 TTSgenerate_audio = _kepu_seedance_generate_audio(project)@@ -1330,7 +1331,7 @@ async def _parallel_videos(project_id: int) -> None:from app.services.media_models.runtime import current_snapshotif (current_snapshot.get() or {}).get("mode") == "fixed_media":from app.services.drama.billing_util import record_seedance_video_usage- await record_seedance_video_usage(db, user_id=project.user_id, billing_key=billing_key,+ await record_seedance_video_usage(db, user_id=project_user_id, billing_key=billing_key,model=video_model or get_settings().model_video, domain="kepu", task_result=task_result,fallback_duration_sec=dur, resolution=resolution, provider_task_id=task_result.provider_task_id,project_id=project_id, shot_id=shot.id)diff --git a/backend/tests/test_kepu_video_workflow.py b/backend/tests/test_kepu_video_workflow.pyindex 2614918..c3a2950 100644--- a/backend/tests/test_kepu_video_workflow.py+++ b/backend/tests/test_kepu_video_workflow.py@@ -187,8 +187,9 @@ def test_full_create_contract_preserves_user_media_choices():("https://media.example/start-1.png", "https://media.example/start-1.png"),("https://www.tokenfree.com/v1/tasks/t1/artifacts/image-0/content", "/static/shot.png"),])[email protected]("billing_mode", ["legacy", "fixed_media"])async def test_full_video_generation_uses_own_image_and_previous_tail_with_native_audio(- monkeypatch, upstream_image, expected_first_frame,+ monkeypatch, upstream_image, expected_first_frame, billing_mode,):"""图生视频沿用本镜首帧、前镜尾帧、脚本时长和项目模型,完整模式保留内置口播。"""shot1 = _shot(image_ark_url=upstream_image, duration=3,@@ -211,10 +212,11 @@ async def test_full_video_generation_uses_own_image_and_previous_tail_with_nativdb.get = AsyncMock(side_effect=get_row)generate_video = AsyncMock(side_effect=[- ("/static/video-1.mp4", SimpleNamespace(last_frame_url="/static/tail-1.png")),- ("/static/video-2.mp4", SimpleNamespace(last_frame_url="/static/tail-2.png")),+ ("/static/video-1.mp4", SimpleNamespace(last_frame_url="/static/tail-1.png", provider_task_id="video-task-1")),+ ("/static/video-2.mp4", SimpleNamespace(last_frame_url="/static/tail-2.png", provider_task_id="video-task-2")),])record_usage = AsyncMock()+ fixed_record_usage = AsyncMock()monkeypatch.setattr(get_settings(), "kepu_seedance_native_audio", True)monkeypatch.setattr(pipeline, "AsyncSessionLocal", session)monkeypatch.setattr(pipeline, "get_ark", lambda: SimpleNamespace(gen_and_wait_video=generate_video))@@ -222,9 +224,15 @@ async def test_full_video_generation_uses_own_image_and_previous_tail_with_nativmonkeypatch.setattr(pipeline, "_set_status", AsyncMock())monkeypatch.setattr(pipeline, "publish_progress", AsyncMock())monkeypatch.setattr(pipeline, "_record_seedance_usage", record_usage)+ monkeypatch.setattr("app.services.drama.billing_util.record_seedance_video_usage", fixed_record_usage)monkeypatch.setattr(pipeline, "persist_last_frame_from_video", lambda *args, **kwargs: kwargs.get("preferred_url"))- await pipeline._parallel_videos(project.id)+ from app.services.media_models.runtime import current_snapshot+ token = current_snapshot.set({"mode": "fixed_media"} if billing_mode == "fixed_media" else None)+ try:+ await pipeline._parallel_videos(project.id)+ finally:+ current_snapshot.reset(token)first, second = generate_video.await_args_listassert first.args[0] == expected_first_frame@@ -240,8 +248,15 @@ async def test_full_video_generation_uses_own_image_and_previous_tail_with_nativassert "三维卡通" in call.args[1]assert shot1.video_url == "/static/video-1.mp4" and shot2.video_url == "/static/video-2.mp4"assert project.status == ProjectStatus.VIDEO_READY and project.progress == 88- assert [call.kwargs["fallback_duration_sec"] for call in record_usage.await_args_list] == [8, 6]- assert all(call.kwargs["billing_key"] == "seedance2:video0" for call in record_usage.await_args_list)+ selected_usage = fixed_record_usage if billing_mode == "fixed_media" else record_usage+ unused_usage = record_usage if billing_mode == "fixed_media" else fixed_record_usage+ assert [call.kwargs["fallback_duration_sec"] for call in selected_usage.await_args_list] == [8, 6]+ assert all(call.kwargs["billing_key"] == "seedance2:video0" for call in selected_usage.await_args_list)+ unused_usage.assert_not_awaited()+ if billing_mode == "fixed_media":+ assert all(call.kwargs["user_id"] == project.user_id for call in selected_usage.await_args_list)+ assert [call.kwargs["shot_id"] for call in selected_usage.await_args_list] == [shot1.id, shot2.id]+ assert [call.kwargs["provider_task_id"] for call in selected_usage.await_args_list] == ["video-task-1", "video-task-2"]async def test_full_project_video_admission_reserves_only_remaining_video_cost(db_session, monkeypatch):