File size: 10,884 Bytes
b7681c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Unit tests for script_generator tool.
"""

import os
import json
import pytest
from unittest.mock import Mock, patch, MagicMock
import sys

# Add src to path to import modules
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))

from app.tools.script_generator import script_generator


class TestScriptGenerator:
    """Test cases for script_generator function."""

    def test_script_generator_with_multiple_videos(self, temp_video_file):
        """Test script_generator with multiple video inputs."""
        with (
            patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture,
            patch("app.tools.script_generator.genai.Client") as mock_client,
        ):

            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            mock_genai_client = Mock()
            mock_response = Mock()
            mock_response.text = """Here's a comprehensive video script:

```json
{
  "concept": "Energetic travel montage",
  "target_duration": 30.0,
  "total_duration": 30.0,
  "scenes": [
    {
      "scene_id": 1,
      "source_video": 0,
      "start_time": 0.0,
      "end_time": 10.0,
      "duration": 10.0,
      "description": "Opening scene",
      "transition_in": "fade",
      "transition_out": "crossfade"
    }
  ],
  "audio": {
    "mood": "energetic",
    "style": "upbeat",
    "bpm": 120,
    "volume": 0.7
  },
  "text_overlays": [],
  "visual_effects": ["color_grading"],
  "call_to_action": "Subscribe for more"
}
```

This is a narrative description of the script."""
            mock_genai_client.models.generate_content.return_value = mock_response
            mock_client.return_value = mock_genai_client

            video_inputs = [temp_video_file, temp_video_file]

            with patch.dict(os.environ, {"GOOGLE_API_KEY": "test_key"}):
                result = script_generator(
                    video_inputs, user_prompt="Create an energetic video"
                )

            result_json = json.loads(result)
            assert "videos_analyzed" in result_json
            assert "script_narrative" in result_json
            assert len(result_json["videos_analyzed"]) == 2

    def test_script_generator_without_prompt(self, temp_video_file):
        """Test script_generator without user prompt."""
        with (
            patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture,
            patch("app.tools.script_generator.genai.Client") as mock_client,
        ):

            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            mock_genai_client = Mock()
            mock_response = Mock()
            mock_response.text = "Auto-generated script based on video analysis."
            mock_genai_client.models.generate_content.return_value = mock_response
            mock_client.return_value = mock_genai_client

            video_inputs = [temp_video_file]

            with patch.dict(os.environ, {"GOOGLE_API_KEY": "test_key"}):
                result = script_generator(video_inputs)

            result_json = json.loads(result)
            assert "videos_analyzed" in result_json
            assert "script_narrative" in result_json
            assert result_json["user_prompt"] == "Auto-generated based on materials"

    def test_script_generator_with_string_input(self, temp_video_file):
        """Test script_generator with single string video input."""
        with (
            patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture,
            patch("app.tools.script_generator.genai.Client") as mock_client,
        ):

            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            mock_genai_client = Mock()
            mock_response = Mock()
            mock_response.text = "Generated script."
            mock_genai_client.models.generate_content.return_value = mock_response
            mock_client.return_value = mock_genai_client

            with patch.dict(os.environ, {"GOOGLE_API_KEY": "test_key"}):
                result = script_generator(temp_video_file)

            result_json = json.loads(result)
            assert "videos_analyzed" in result_json
            assert len(result_json["videos_analyzed"]) == 1

    def test_script_generator_with_tuple_input(self, temp_video_file):
        """Test script_generator with tuple input (Gradio format)."""
        with (
            patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture,
            patch("app.tools.script_generator.genai.Client") as mock_client,
        ):

            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            mock_genai_client = Mock()
            mock_response = Mock()
            mock_response.text = "Generated script."
            mock_genai_client.models.generate_content.return_value = mock_response
            mock_client.return_value = mock_genai_client

            video_input = (temp_video_file, "subtitle.srt")

            with patch.dict(os.environ, {"GOOGLE_API_KEY": "test_key"}):
                result = script_generator(video_input)

            result_json = json.loads(result)
            assert "videos_analyzed" in result_json

    def test_script_generator_with_empty_input(self):
        """Test script_generator with no video input."""
        result = script_generator([])
        result_json = json.loads(result)
        assert "error" in result_json
        assert result_json["error"] == "No video files provided"

    def test_script_generator_with_nonexistent_file(self):
        """Test script_generator with nonexistent video file."""
        result = script_generator(["/nonexistent/video.mp4"])
        result_json = json.loads(result)
        assert "error" in result_json
        assert "not found" in result_json["error"]

    def test_script_generator_without_api_key(self, temp_video_file):
        """Test script_generator without GOOGLE_API_KEY."""
        with patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture:
            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            with patch.dict(os.environ, {}, clear=True):
                result = script_generator([temp_video_file])

            result_json = json.loads(result)
            assert "error" in result_json
            assert "GOOGLE_API_KEY" in result_json["error"]
            assert "videos_analyzed" in result_json

    def test_script_generator_with_invalid_video(self, temp_video_file):
        """Test script_generator with video that cannot be opened."""
        with patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture:
            mock_cap = Mock()
            mock_cap.isOpened.return_value = False
            mock_capture.return_value = mock_cap

            result = script_generator([temp_video_file])
            result_json = json.loads(result)
            assert "error" in result_json
            assert "Could not open video file" in result_json["error"]

    def test_script_generator_structured_script_parsing(self, temp_video_file):
        """Test that structured JSON is properly extracted and parsed."""
        with (
            patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture,
            patch("app.tools.script_generator.genai.Client") as mock_client,
        ):

            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            mock_genai_client = Mock()
            mock_response = Mock()
            mock_response.text = """Here's the script:

```json
{
  "concept": "Test concept",
  "target_duration": 30.0,
  "scenes": []
}
```

Narrative description."""
            mock_genai_client.models.generate_content.return_value = mock_response
            mock_client.return_value = mock_genai_client

            with patch.dict(os.environ, {"GOOGLE_API_KEY": "test_key"}):
                result = script_generator([temp_video_file])

            result_json = json.loads(result)
            assert "structured_script" in result_json
            assert result_json["structured_script"]["concept"] == "Test concept"
            assert result_json["structured_script"]["target_duration"] == 30.0

    def test_script_generator_with_custom_prompt(self, temp_video_file):
        """Test script_generator with custom user prompt."""
        with (
            patch("app.tools.script_generator.cv2.VideoCapture") as mock_capture,
            patch("app.tools.script_generator.genai.Client") as mock_client,
        ):

            mock_cap = Mock()
            mock_cap.isOpened.return_value = True
            mock_cap.get.side_effect = lambda prop: {
                5: 30.0,
                7: 900,
                3: 1920,
                4: 1080,
            }.get(prop, 0)
            mock_capture.return_value = mock_cap

            mock_genai_client = Mock()
            mock_response = Mock()
            mock_response.text = "Custom prompt response."
            mock_genai_client.models.generate_content.return_value = mock_response
            mock_client.return_value = mock_genai_client

            custom_prompt = "Create a dramatic product reveal"

            with patch.dict(os.environ, {"GOOGLE_API_KEY": "test_key"}):
                result = script_generator([temp_video_file], user_prompt=custom_prompt)

            result_json = json.loads(result)
            assert result_json["user_prompt"] == custom_prompt
            assert "script_narrative" in result_json