diff --git a/tests/unit/test_preview_generation_fixes.py b/tests/unit/test_preview_generation_fixes.py index 23781db72..9705a8edf 100644 --- a/tests/unit/test_preview_generation_fixes.py +++ b/tests/unit/test_preview_generation_fixes.py @@ -7,7 +7,6 @@ from unittest.mock import MagicMock, patch import pytest - # ── Fix 1: URL 签名 ────────────────────────────────────────────────────────── @@ -78,8 +77,8 @@ class TestLoadTemplateSegmentDurations: def test_empty_template_id_returns_empty(self): """空 template_id 返回空列表。""" - import sys import os + import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "apps", "worker")) from worker_app.tasks.generation import _load_template_segment_durations @@ -155,11 +154,11 @@ class TestDurationCappingInBuildPlan: def test_clips_capped_by_segment_max(self): """clip 时长超过 segment duration_max 时应被截断。""" - from worker_app.tasks.generation import _build_plan_and_clips_from_task - # 创建临时假视频文件 import tempfile + from worker_app.tasks.generation import _build_plan_and_clips_from_task + with tempfile.TemporaryDirectory() as tmpdir: paths = [] for i in range(3): @@ -193,10 +192,10 @@ class TestDurationCappingInBuildPlan: def test_clips_not_capped_when_under_max(self): """clip 时长小于 segment duration_max 时不截断。""" - from worker_app.tasks.generation import _build_plan_and_clips_from_task - import tempfile + from worker_app.tasks.generation import _build_plan_and_clips_from_task + with tempfile.TemporaryDirectory() as tmpdir: paths = [Path(tmpdir) / "video_0.mp4"] paths[0].write_bytes(b"\x00" * 100) @@ -222,10 +221,10 @@ class TestDurationCappingInBuildPlan: def test_no_capping_without_template(self): """无 template_id 时不截断。""" - from worker_app.tasks.generation import _build_plan_and_clips_from_task - import tempfile + from worker_app.tasks.generation import _build_plan_and_clips_from_task + with tempfile.TemporaryDirectory() as tmpdir: paths = [Path(tmpdir) / "video_0.mp4"] paths[0].write_bytes(b"\x00" * 100) @@ -250,14 +249,14 @@ class TestPreviewResolution: def test_preview_uses_passed_resolution(self): """is_preview=True 且有 resolution 参数时,使用传入的分辨率。""" + import tempfile + from worker_app.tasks.generation import _render_video # 验证逻辑:检查 _render_video 在 is_preview + resolution 时的行为 # 由于 _render_video 内部会调用 RenderAdapter,这里只验证分辨率配置逻辑 # 通过 mock 掉渲染部分,检查 export_cfg - import tempfile - with tempfile.TemporaryDirectory() as tmpdir: temp_path = Path(tmpdir) video_path = temp_path / "input.mp4" @@ -322,10 +321,10 @@ class TestPreviewResolution: def test_preview_defaults_to_landscape_when_no_resolution(self): """is_preview=True 且无 resolution 参数时,默认 854x480。""" - from worker_app.tasks.generation import _render_video - import tempfile + from worker_app.tasks.generation import _render_video + with tempfile.TemporaryDirectory() as tmpdir: temp_path = Path(tmpdir) video_path = temp_path / "input.mp4"