-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_coordinator_content_routing.py
More file actions
210 lines (171 loc) · 6.89 KB
/
test_coordinator_content_routing.py
File metadata and controls
210 lines (171 loc) · 6.89 KB
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
"""测试 LLM Coordinator 的内容路由功能"""
import pytest
from unittest.mock import Mock, MagicMock, patch
from video_transcript_api.llm.coordinator import LLMCoordinator
@pytest.fixture
def mock_config_dict():
"""Mock configuration dictionary"""
return {
"llm": {
"api_key": "test-key",
"base_url": "http://test",
"calibrate_model": "test-model",
"summary_model": "test-model",
"max_retries": 3,
"retry_delay": 5,
"min_calibrate_ratio": 0.8,
"min_summary_threshold": 500,
"quality_validation": {
"score_weights": {
"accuracy": 0.4,
"completeness": 0.3,
"fluency": 0.2,
"format": 0.1,
},
"quality_threshold": {
"overall_score": 8.0,
"minimum_single_score": 7.0,
},
},
"segmentation": {
"enable_threshold": 5000,
"segment_size": 2000,
"max_segment_size": 3000,
"concurrent_workers": 10,
},
"structured_calibration": {
"min_chunk_length": 300,
"max_chunk_length": 1500,
"preferred_chunk_length": 800,
"max_calibration_retries": 2,
"calibration_concurrent_limit": 3,
"quality_validation": {"enabled": True, "fallback_strategy": "best_quality"},
},
}
}
@pytest.fixture
def coordinator(mock_config_dict, tmp_path):
"""Create a coordinator instance with mocked dependencies"""
with patch(
"video_transcript_api.llm.coordinator.PlainTextProcessor"
) as MockPlainProcessor, patch(
"video_transcript_api.llm.coordinator.SpeakerAwareProcessor"
) as MockSpeakerProcessor, patch(
"video_transcript_api.llm.coordinator.SummaryProcessor"
):
coordinator = LLMCoordinator(
config_dict=mock_config_dict, cache_dir=str(tmp_path)
)
# Mock processor results
coordinator.plain_text_processor.process = Mock(
return_value={
"calibrated_text": "Calibrated plain text",
"key_info": {},
"stats": {"original_length": 100, "calibrated_length": 100},
}
)
coordinator.speaker_aware_processor.process = Mock(
return_value={
"calibrated_text": "Calibrated dialog text",
"key_info": {},
"stats": {"original_length": 100, "calibrated_length": 100},
"structured_data": {"speaker_mapping": {"Speaker1": "Alice"}},
}
)
coordinator.summary_processor.process = Mock(return_value=None)
yield coordinator
def test_route_plain_text_to_plain_processor(coordinator):
"""Test routing plain text (str) to PlainTextProcessor"""
content = "This is a plain text transcript"
coordinator.process(
content=content,
title="Test Video",
author="Test Author",
description="Test Description",
)
# Verify PlainTextProcessor was called
coordinator.plain_text_processor.process.assert_called_once()
coordinator.speaker_aware_processor.process.assert_not_called()
def test_route_dialog_list_to_speaker_processor(coordinator):
"""Test routing dialog list to SpeakerAwareProcessor"""
content = [
{"speaker": "Speaker1", "text": "Hello", "start_time": 0.0},
{"speaker": "Speaker2", "text": "Hi there", "start_time": 1.5},
]
coordinator.process(
content=content,
title="Test Video",
author="Test Author",
description="Test Description",
)
# Verify SpeakerAwareProcessor was called
coordinator.speaker_aware_processor.process.assert_called_once()
coordinator.plain_text_processor.process.assert_not_called()
# Verify correct dialogs were passed
call_args = coordinator.speaker_aware_processor.process.call_args
assert call_args.kwargs["dialogs"] == content
def test_route_dict_with_segments_to_speaker_processor(coordinator):
"""Test routing dict with 'segments' key to SpeakerAwareProcessor"""
segments = [
{"speaker": "Speaker1", "text": "Hello", "start_time": 0.0},
{"speaker": "Speaker2", "text": "Hi there", "start_time": 1.5},
]
content = {"segments": segments, "speakers": ["Speaker1", "Speaker2"]}
coordinator.process(
content=content,
title="Test Video",
author="Test Author",
description="Test Description",
)
# Verify SpeakerAwareProcessor was called
coordinator.speaker_aware_processor.process.assert_called_once()
# Verify segments were extracted correctly
call_args = coordinator.speaker_aware_processor.process.call_args
assert call_args.kwargs["dialogs"] == segments
def test_route_dict_without_segments_raises_error(coordinator):
"""Test that dict without 'segments' key raises ValueError"""
content = {"invalid_key": "value"}
with pytest.raises(ValueError, match="dict without 'segments' key"):
coordinator.process(
content=content,
title="Test Video",
author="Test Author",
description="Test Description",
)
def test_route_invalid_type_raises_error(coordinator):
"""Test that invalid content type raises ValueError"""
content = 123 # Invalid type
with pytest.raises(
ValueError, match="Unsupported content type.*Expected str.*or list"
):
coordinator.process(
content=content,
title="Test Video",
author="Test Author",
description="Test Description",
)
def test_extract_speaker_count_from_plain_text(coordinator):
"""Test speaker count extraction from plain text"""
content = "Plain text"
calibration_result = {
"calibrated_text": "Calibrated",
"key_info": {},
"stats": {},
}
speaker_count = coordinator._extract_speaker_count(content, calibration_result)
assert speaker_count == 0
def test_extract_speaker_count_from_dialogs(coordinator):
"""Test speaker count extraction from dialog result"""
content = [{"speaker": "Speaker1", "text": "Hello"}]
calibration_result = {
"calibrated_text": "Calibrated",
"key_info": {},
"stats": {},
"structured_data": {
"speaker_mapping": {"Speaker1": "Alice", "Speaker2": "Bob"}
},
}
speaker_count = coordinator._extract_speaker_count(content, calibration_result)
assert speaker_count == 2
if __name__ == "__main__":
pytest.main([__file__, "-v"])