File size: 1,887 Bytes
68e8f6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# tests/test_travel_assistant.py
import pytest
from unittest.mock import Mock, patch
from modules.travel_assistant import TravelAssistant

class TestTravelAssistant:
    
    @pytest.fixture
    def mock_travel_assistant(self):
        """创建模拟的旅游助手"""
        with patch('modules.travel_assistant.ConfigLoader') as mock_config, \
             patch('modules.travel_assistant.KnowledgeBase') as mock_kb, \
             patch('modules.travel_assistant.AIModel') as mock_ai:
            
            # 设置模拟对象
            mock_config.return_value.cities = {"巴黎": {"name": "巴黎", "country": "法国"}}
            mock_config.return_value.personas = {"planner": {"name": "规划型"}}
            
            assistant = TravelAssistant()
            yield assistant
    
    def test_chat_basic_flow(self, mock_travel_assistant):
        """测试基本聊天流程"""
        reply, session_id, status_info, history = mock_travel_assistant.chat(
            message="我想去巴黎旅游",
            session_id=None,
            history=[]
        )
        
        assert isinstance(reply, str)
        assert len(session_id) == 8  # UUID前8位
        assert isinstance(status_info, str)
        assert len(history) == 1
        assert history[0][0] == "我想去巴黎旅游"
    
    def test_session_persistence(self, mock_travel_assistant):
        """测试会话持久性"""
        # 第一次对话
        _, session_id, _, _ = mock_travel_assistant.chat(
            message="我想去巴黎",
            session_id=None,
            history=[]
        )
        
        # 第二次对话使用相同session_id
        reply, same_session_id, _, _ = mock_travel_assistant.chat(
            message="3天行程",
            session_id=session_id,
            history=[]
        )
        
        assert session_id == same_session_id