""" 意图分类器单元测试 """ import unittest import sys from pathlib import Path # 添加项目根目录到路径 sys.path.insert(0, str(Path(__file__).parent.parent)) from intent.labels import CHAT, EXECUTION, VALID_LABELS, EXECUTION_CONFIDENCE_THRESHOLD class TestIntentLabels(unittest.TestCase): """意图标签测试""" def test_labels_defined(self): """测试标签已定义""" self.assertEqual(CHAT, "chat") self.assertEqual(EXECUTION, "execution") def test_valid_labels(self): """测试有效标签集合""" self.assertIn(CHAT, VALID_LABELS) self.assertIn(EXECUTION, VALID_LABELS) self.assertEqual(len(VALID_LABELS), 2) def test_confidence_threshold(self): """测试置信度阈值""" self.assertGreater(EXECUTION_CONFIDENCE_THRESHOLD, 0) self.assertLessEqual(EXECUTION_CONFIDENCE_THRESHOLD, 1) class TestIntentClassifierParsing(unittest.TestCase): """意图分类器解析测试(不需要 API)""" def setUp(self): from intent.classifier import IntentClassifier self.classifier = IntentClassifier() def test_parse_valid_chat_response(self): """测试解析有效的 chat 响应""" response = '{"label": "chat", "confidence": 0.95, "reason": "这是一个问答"}' result = self.classifier._parse_response(response) self.assertEqual(result.label, CHAT) self.assertEqual(result.confidence, 0.95) self.assertEqual(result.reason, "这是一个问答") def test_parse_valid_execution_response(self): """测试解析有效的 execution 响应""" response = '{"label": "execution", "confidence": 0.9, "reason": "需要复制文件"}' result = self.classifier._parse_response(response) self.assertEqual(result.label, EXECUTION) self.assertEqual(result.confidence, 0.9) def test_parse_low_confidence_execution(self): """测试低置信度的 execution 降级为 chat""" response = '{"label": "execution", "confidence": 0.5, "reason": "不太确定"}' result = self.classifier._parse_response(response) # 低于阈值应该降级为 chat self.assertEqual(result.label, CHAT) def test_parse_invalid_label(self): """测试无效标签降级为 chat""" response = '{"label": "unknown", "confidence": 0.9, "reason": "测试"}' result = self.classifier._parse_response(response) self.assertEqual(result.label, CHAT) def test_parse_invalid_json(self): """测试无效 JSON 降级为 chat""" response = 'not a json' result = self.classifier._parse_response(response) self.assertEqual(result.label, CHAT) self.assertEqual(result.confidence, 0.0) def test_extract_json_with_prefix(self): """测试从带前缀的文本中提取 JSON""" text = 'Here is the result: {"label": "chat", "confidence": 0.8, "reason": "test"}' json_str = self.classifier._extract_json(text) self.assertTrue(json_str.startswith('{')) self.assertTrue(json_str.endswith('}')) def test_extract_json_with_suffix(self): """测试从带后缀的文本中提取 JSON""" text = '{"label": "chat", "confidence": 0.8, "reason": "test"} That is my answer.' json_str = self.classifier._extract_json(text) self.assertTrue(json_str.startswith('{')) self.assertTrue(json_str.endswith('}')) if __name__ == '__main__': unittest.main()