diff --git a/tests/unit/domain/test_edit_plan_clip.py b/tests/unit/domain/test_edit_plan_clip.py new file mode 100644 index 000000000..e1361cdd2 --- /dev/null +++ b/tests/unit/domain/test_edit_plan_clip.py @@ -0,0 +1,362 @@ +"""edit_plan_clip 单测. + +domain 层剪辑计划片段纯逻辑模块,0 外部依赖。 +覆盖:枚举常量、create工厂/校验、素材分配、状态流转、属性计算。 +""" + +from __future__ import annotations + +from packages.domain.edit_plan_clip import EditPlanClip, EditPlanClipStatus + + +class TestEditPlanClipStatus: + """EditPlanClipStatus 枚举测试.""" + + def test_four_statuses(self): + """四种状态.""" + assert len(EditPlanClipStatus) == 4 + + def test_pending(self): + """pending 状态.""" + assert EditPlanClipStatus.PENDING == "pending" + + def test_ready(self): + """ready 状态.""" + assert EditPlanClipStatus.READY == "ready" + + def test_rendered(self): + """rendered 状态.""" + assert EditPlanClipStatus.RENDERED == "rendered" + + def test_failed(self): + """failed 状态.""" + assert EditPlanClipStatus.FAILED == "failed" + + def test_is_string(self): + """枚举值是字符串.""" + for status in EditPlanClipStatus: + assert isinstance(status.value, str) + assert len(status.value) > 0 + + def test_str_compatible(self): + """StrEnum 可与字符串比较.""" + assert EditPlanClipStatus.PENDING == "pending" + assert EditPlanClipStatus.READY + "" == "ready" + + +class TestEditPlanClipCreate: + """EditPlanClip.create 工厂方法测试.""" + + def test_create_minimal(self): + """最简创建.""" + clip = EditPlanClip.create(plan_id="plan1", clip_type="video", order=0) + assert clip.plan_id == "plan1" + assert clip.clip_type == "video" + assert clip.order == 0 + assert clip.status == EditPlanClipStatus.PENDING + assert clip.template_clip_config_id == "" + assert clip.asset_id == "" + assert clip.text_content == "" + assert clip.start_time == 0.0 + assert clip.duration == 0.0 + assert clip.transition_effect == "cut" + assert clip.config == {} + assert isinstance(clip.id, str) + assert len(clip.id) > 0 + + def test_create_with_all_fields(self): + """带全部字段创建.""" + clip = EditPlanClip.create( + plan_id="plan1", + clip_type="video", + order=2, + template_clip_config_id="tpl1", + asset_id="asset1", + text_content=" 你好世界 ", + start_time=10.5, + duration=5.0, + transition_effect="fade", + config={"key": "value"}, + ) + assert clip.order == 2 + assert clip.template_clip_config_id == "tpl1" + assert clip.asset_id == "asset1" + assert clip.text_content == "你好世界" # strip了 + assert clip.start_time == 10.5 + assert clip.duration == 5.0 + assert clip.transition_effect == "fade" + assert clip.config == {"key": "value"} + + def test_create_strips_ids(self): + """plan_id 和 clip_type 会 strip.""" + clip = EditPlanClip.create(plan_id=" plan1 ", clip_type=" video ", order=0) + assert clip.plan_id == "plan1" + assert clip.clip_type == "video" + + def test_create_empty_plan_id(self): + """空 plan_id 无效.""" + try: + EditPlanClip.create(plan_id="", clip_type="video", order=0) + assert False + except ValueError as e: + assert "plan_id" in str(e) + + def test_create_whitespace_plan_id(self): + """纯空白 plan_id 无效.""" + try: + EditPlanClip.create(plan_id=" ", clip_type="video", order=0) + assert False + except ValueError as e: + assert "plan_id" in str(e) + + def test_create_empty_clip_type(self): + """空 clip_type 无效.""" + try: + EditPlanClip.create(plan_id="p1", clip_type="", order=0) + assert False + except ValueError as e: + assert "clip_type" in str(e) + + def test_create_whitespace_clip_type(self): + """纯空白 clip_type 无效.""" + try: + EditPlanClip.create(plan_id="p1", clip_type=" ", order=0) + assert False + except ValueError as e: + assert "clip_type" in str(e) + + def test_create_negative_start_time(self): + """start_time 为负无效.""" + try: + EditPlanClip.create(plan_id="p1", clip_type="v", order=0, start_time=-1.0) + assert False + except ValueError as e: + assert "start_time" in str(e) + + def test_create_negative_duration(self): + """duration 为负无效.""" + try: + EditPlanClip.create(plan_id="p1", clip_type="v", order=0, duration=-1.0) + assert False + except ValueError as e: + assert "duration" in str(e) + + def test_create_zero_duration_valid(self): + """duration 为 0 合法.""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, duration=0.0) + assert clip.duration == 0.0 + + def test_create_empty_transition_defaults_to_cut(self): + """空 transition_effect 默认 cut.""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, transition_effect="") + assert clip.transition_effect == "cut" + + def test_create_whitespace_transition_defaults_to_cut(self): + """空白 transition_effect 默认 cut.""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, transition_effect=" ") + assert clip.transition_effect == "cut" + + def test_create_config_none_defaults_empty_dict(self): + """config=None 默认为空 dict.""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, config=None) + assert clip.config == {} + + def test_create_unique_id(self): + """不同 clip id 不同.""" + c1 = EditPlanClip.create("p1", "v", 0) + c2 = EditPlanClip.create("p1", "v", 0) + assert c1.id != c2.id + + def test_create_has_timestamps(self): + """有创建和更新时间.""" + clip = EditPlanClip.create("p1", "v", 0) + assert clip.created_at is not None + assert clip.updated_at is not None + + def test_create_negative_order_valid(self): + """order 可以为负(表示排序位置).""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=-1) + assert clip.order == -1 + + +class TestEditPlanClipAssignAsset: + """素材分配测试.""" + + def _make_clip(self): + return EditPlanClip.create(plan_id="p1", clip_type="video", order=0) + + def test_assign_asset(self): + """正常分配素材.""" + clip = self._make_clip() + old_updated = clip.updated_at + clip.assign_asset("asset123") + assert clip.asset_id == "asset123" + assert clip.updated_at >= old_updated + + def test_assign_asset_strips(self): + """asset_id 会 strip.""" + clip = self._make_clip() + clip.assign_asset(" asset123 ") + assert clip.asset_id == "asset123" + + def test_assign_asset_empty(self): + """空 asset_id 无效.""" + clip = self._make_clip() + try: + clip.assign_asset("") + assert False + except ValueError as e: + assert "asset_id" in str(e) + + def test_assign_asset_whitespace(self): + """纯空白 asset_id 无效.""" + clip = self._make_clip() + try: + clip.assign_asset(" ") + assert False + except ValueError as e: + assert "asset_id" in str(e) + + def test_has_asset_false_initially(self): + """初始无素材.""" + clip = self._make_clip() + assert clip.has_asset is False + + def test_has_asset_true_after_assign(self): + """分配后有素材.""" + clip = self._make_clip() + clip.assign_asset("a1") + assert clip.has_asset is True + + +class TestEditPlanClipStatusFlow: + """状态流转测试.""" + + def _make_clip(self): + return EditPlanClip.create(plan_id="p1", clip_type="video", order=0) + + def test_initial_status_pending(self): + """初始状态 pending.""" + clip = self._make_clip() + assert clip.status == EditPlanClipStatus.PENDING + + def test_pending_to_ready(self): + """pending -> ready.""" + clip = self._make_clip() + clip.mark_ready() + assert clip.status == EditPlanClipStatus.READY + + def test_ready_to_rendered(self): + """ready -> rendered.""" + clip = self._make_clip() + clip.mark_ready() + clip.mark_rendered() + assert clip.status == EditPlanClipStatus.RENDERED + + def test_ready_to_failed(self): + """ready -> failed.""" + clip = self._make_clip() + clip.mark_ready() + clip.mark_failed() + assert clip.status == EditPlanClipStatus.FAILED + + def test_cannot_ready_from_rendered(self): + """rendered 状态不能再 mark_ready.""" + clip = self._make_clip() + clip.mark_ready() + clip.mark_rendered() + try: + clip.mark_ready() + assert False + except ValueError as e: + assert "pending" in str(e).lower() + + def test_cannot_ready_from_failed(self): + """failed 状态不能 mark_ready.""" + clip = self._make_clip() + clip.mark_ready() + clip.mark_failed() + try: + clip.mark_ready() + assert False + except ValueError as e: + assert "pending" in str(e).lower() + + def test_cannot_render_from_pending(self): + """pending 不能直接 mark_rendered.""" + clip = self._make_clip() + try: + clip.mark_rendered() + assert False + except ValueError as e: + assert "ready" in str(e).lower() + + def test_cannot_failed_from_pending(self): + """pending 不能直接 mark_failed.""" + clip = self._make_clip() + try: + clip.mark_failed() + assert False + except ValueError as e: + assert "ready" in str(e).lower() + + def test_status_change_updates_timestamp(self): + """状态变更更新 updated_at.""" + clip = self._make_clip() + old_updated = clip.updated_at + clip.mark_ready() + assert clip.updated_at >= old_updated + + +class TestEditPlanClipProperties: + """属性计算测试.""" + + def test_end_time(self): + """end_time = start_time + duration.""" + clip = EditPlanClip.create( + plan_id="p1", + clip_type="v", + order=0, + start_time=10.0, + duration=5.5, + ) + assert clip.end_time == 15.5 + + def test_end_time_zero_duration(self): + """零时长 end_time = start_time.""" + clip = EditPlanClip.create( + plan_id="p1", + clip_type="v", + order=0, + start_time=10.0, + duration=0.0, + ) + assert clip.end_time == 10.0 + + def test_end_time_zero_start(self): + """零起点 end_time = duration.""" + clip = EditPlanClip.create( + plan_id="p1", + clip_type="v", + order=0, + start_time=0.0, + duration=7.0, + ) + assert clip.end_time == 7.0 + + def test_has_asset_empty_string(self): + """空字符串无素材.""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, asset_id="") + assert clip.has_asset is False + + def test_has_asset_with_value(self): + """有值则有素材.""" + clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, asset_id="a1") + assert clip.has_asset is True + + def test_config_independent_between_clips(self): + """不同 clip 的 config 独立.""" + c1 = EditPlanClip.create(plan_id="p1", clip_type="v", order=0) + c2 = EditPlanClip.create(plan_id="p1", clip_type="v", order=1) + c1.config["key"] = "value" + assert "key" not in c2.config