Spaces:
Running
Running
Download test/services/test_controller_llm.py from kodnuke/youtube-bot: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/spaces/kodnuke/youtube-bot/resolve/main/test/services/test_controller_llm.py
- Command line
-
hf download hf://spaces/kodnuke/youtube-bot/test/services/test_controller_llm.py
-
curl -L -o test_controller_llm.py https://huggingface.co/spaces/kodnuke/youtube-bot/resolve/main/test/services/test_controller_llm.py
3.84 kB
| import unittest | |
| from unittest.mock import patch | |
| from pydantic import ValidationError | |
| from app.controllers.v1 import llm as llm_controller | |
| from app.models.schema import ( | |
| VideoScriptRequest, | |
| VideoSocialMetadataRequest, | |
| VideoTermsRequest, | |
| ) | |
| class TestLlmController(unittest.TestCase): | |
| def test_terms_request_bounds_generated_term_count(self): | |
| """Ordered term prompts must not allocate arbitrary-length examples.""" | |
| self.assertEqual( | |
| VideoTermsRequest(amount=50, match_materials_to_script=True).amount, | |
| 50, | |
| ) | |
| for value in (0, -1, 51, None): | |
| with self.subTest(value=value): | |
| with self.assertRaises(ValidationError): | |
| VideoTermsRequest( | |
| amount=value, match_materials_to_script=True | |
| ) | |
| def test_generate_video_script_forwards_all_prompt_fields(self): | |
| """文案接口不能丢失高级提示词或段落数量。""" | |
| body = VideoScriptRequest( | |
| video_subject="Coffee", | |
| video_language="en", | |
| paragraph_number=2, | |
| video_script_prompt="Friendly tone", | |
| custom_system_prompt="Return narration only.", | |
| ) | |
| with patch.object( | |
| llm_controller.llm, | |
| "generate_script", | |
| return_value="Generated script", | |
| ) as generate: | |
| response = llm_controller.generate_video_script(None, body) | |
| self.assertEqual( | |
| response, | |
| {"status": 200, "data": {"video_script": "Generated script"}}, | |
| ) | |
| generate.assert_called_once_with( | |
| video_subject="Coffee", | |
| language="en", | |
| paragraph_number=2, | |
| video_script_prompt="Friendly tone", | |
| custom_system_prompt="Return narration only.", | |
| ) | |
| def test_generate_video_terms_forwards_order_matching_mode(self): | |
| """素材顺序匹配开关必须继续传递到关键词生成服务。""" | |
| body = VideoTermsRequest( | |
| video_subject="Coffee", | |
| video_script="First beans, then brewing.", | |
| amount=4, | |
| match_materials_to_script=True, | |
| ) | |
| with patch.object( | |
| llm_controller.llm, | |
| "generate_terms", | |
| return_value=["beans", "brewing"], | |
| ) as generate: | |
| response = llm_controller.generate_video_terms(None, body) | |
| self.assertEqual( | |
| response, | |
| {"status": 200, "data": {"video_terms": ["beans", "brewing"]}}, | |
| ) | |
| generate.assert_called_once_with( | |
| video_subject="Coffee", | |
| video_script="First beans, then brewing.", | |
| amount=4, | |
| match_script_order=True, | |
| ) | |
| def test_generate_social_metadata_returns_service_payload(self): | |
| """社交平台元数据接口应保持服务层结果的响应结构。""" | |
| body = VideoSocialMetadataRequest( | |
| video_subject="Coffee", | |
| video_script="Morning coffee.", | |
| language="en", | |
| platform="youtube_shorts", | |
| ) | |
| metadata = { | |
| "title": "Morning Coffee", | |
| "caption": "Start the day.", | |
| "hashtags": ["#coffee"], | |
| } | |
| with patch.object( | |
| llm_controller.llm, | |
| "generate_social_metadata", | |
| return_value=metadata, | |
| ) as generate: | |
| response = llm_controller.generate_video_social_metadata(None, body) | |
| self.assertEqual(response, {"status": 200, "data": metadata}) | |
| generate.assert_called_once_with( | |
| video_subject="Coffee", | |
| video_script="Morning coffee.", | |
| language="en", | |
| platform="youtube_shorts", | |
| ) | |
| if __name__ == "__main__": | |
| unittest.main() | |