"""Tests for PUT /templates/{id}/editor/clips batch update endpoint.""" from __future__ import annotations import os from unittest.mock import MagicMock, patch import pytest os.environ.setdefault("JWT_SECRET_KEY", "unit-test-secret-key-for-testing") @pytest.fixture def mock_services(): plan_svc = MagicMock() tpl_svc = MagicMock() plan_svc.get_plan_or_raise.return_value = MagicMock(id="plan-1", template_id="tpl-1") plan_svc.delete_all_clips.return_value = 3 plan_svc.mark_clips_ready.return_value = 2 return tpl_svc, plan_svc class TestBatchUpdateClips: def test_batch_update_creates_clips_in_order(self, mock_services): from app.api.routes.templates_editor.draft import batch_update_clips from app.api.routes.templates_editor.schemas import ( EditorClipBatchItem, EditorClipBatchUpdateRequest, ) _, plan_svc = mock_services req = EditorClipBatchUpdateRequest( clips=[ EditorClipBatchItem(asset_id="a1", start_time=0.0, duration=3.0, order=0), EditorClipBatchItem(asset_id="a2", start_time=3.0, duration=5.0, order=1), ] ) result = batch_update_clips( template_id="tpl-1", req=req, plan_id="plan-1", services=mock_services, _=MagicMock(), ) assert result.plan_id == "plan-1" assert result.clip_count == 2 plan_svc.delete_all_clips.assert_called_once_with("plan-1") assert plan_svc.create_clip.call_count == 2 plan_svc.mark_clips_ready.assert_called_once_with("plan-1") def test_batch_update_empty_clips(self, mock_services): from app.api.routes.templates_editor.draft import batch_update_clips from app.api.routes.templates_editor.schemas import EditorClipBatchUpdateRequest _, plan_svc = mock_services req = EditorClipBatchUpdateRequest(clips=[]) result = batch_update_clips( template_id="tpl-1", req=req, plan_id="plan-1", services=mock_services, _=MagicMock(), ) assert result.clip_count == 0 plan_svc.delete_all_clips.assert_called_once() plan_svc.create_clip.assert_not_called() plan_svc.mark_clips_ready.assert_called_once() def test_batch_update_clip_uses_order_field(self, mock_services): from app.api.routes.templates_editor.draft import batch_update_clips from app.api.routes.templates_editor.schemas import ( EditorClipBatchItem, EditorClipBatchUpdateRequest, ) _, plan_svc = mock_services req = EditorClipBatchUpdateRequest( clips=[ EditorClipBatchItem(asset_id="a1", start_time=0.0, duration=3.0, order=5), ] ) batch_update_clips( template_id="tpl-1", req=req, plan_id="plan-1", services=mock_services, _=MagicMock(), ) call_kwargs = plan_svc.create_clip.call_args.kwargs assert call_kwargs["order"] == 5 assert call_kwargs["asset_id"] == "a1" assert call_kwargs["clip_type"] == "main" assert call_kwargs["start_time"] == 0.0 assert call_kwargs["duration"] == 3.0