File size: 11,228 Bytes
f1ba2d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Tests for session manager functionality.
"""

import unittest
from unittest.mock import patch, MagicMock
import sys
import os
from datetime import datetime

# Add src to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

# Mock streamlit before importing session_manager
sys.modules['streamlit'] = MagicMock()

from src.utils.session_manager import SessionManager

class TestSessionManager(unittest.TestCase):
    """Test cases for SessionManager class."""
    
    def setUp(self):
        """Set up test fixtures."""
        # Mock streamlit session_state
        self.mock_st = MagicMock()
        self.mock_st.session_state = {}
        
        with patch('src.utils.session_manager.st', self.mock_st):
            self.session_manager = SessionManager()
    
    def test_initialization(self):
        """Test SessionManager initialization."""
        with patch('src.utils.session_manager.st', self.mock_st):
            manager = SessionManager()
            
            # Check that session state variables are initialized
            expected_keys = [
                'chat_history', 'processed_videos', 'current_video',
                'qa_chain', 'vectorstore', 'video_metadata', 'conversation_id'
            ]
            
            for key in expected_keys:
                self.assertIn(key, self.mock_st.session_state)
    
    def test_generate_conversation_id(self):
        """Test conversation ID generation."""
        conv_id = self.session_manager.generate_conversation_id()
        
        self.assertIsInstance(conv_id, str)
        self.assertTrue(conv_id.startswith('conv_'))
        self.assertEqual(len(conv_id), 19)  # conv_ + YYYYMMDD_HHMMSS
    
    def test_add_to_chat_history(self):
        """Test adding entries to chat history."""
        with patch('src.utils.session_manager.st', self.mock_st):
            self.mock_st.session_state = {
                'chat_history': [],
                'conversation_id': 'test_conv_123'
            }
            
            manager = SessionManager()
            
            question = "What is this about?"
            answer = "This is a test answer."
            video_id = "test_video_123"
            
            manager.add_to_chat_history(question, answer, video_id)
            
            self.assertEqual(len(self.mock_st.session_state['chat_history']), 1)
            
            entry = self.mock_st.session_state['chat_history'][0]
            self.assertEqual(entry['question'], question)
            self.assertEqual(entry['answer'], answer)
            self.assertEqual(entry['video_id'], video_id)
            self.assertEqual(entry['conversation_id'], 'test_conv_123')
            self.assertIn('timestamp', entry)
    
    def test_get_chat_history_all(self):
        """Test getting all chat history."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'answer': 'A1', 'video_id': 'vid1'},
                {'question': 'Q2', 'answer': 'A2', 'video_id': 'vid2'}
            ]
            self.mock_st.session_state = {'chat_history': test_history}
            
            manager = SessionManager()
            history = manager.get_chat_history()
            
            self.assertEqual(history, test_history)
    
    def test_get_chat_history_filtered(self):
        """Test getting filtered chat history by video ID."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'answer': 'A1', 'video_id': 'vid1'},
                {'question': 'Q2', 'answer': 'A2', 'video_id': 'vid2'},
                {'question': 'Q3', 'answer': 'A3', 'video_id': 'vid1'}
            ]
            self.mock_st.session_state = {'chat_history': test_history}
            
            manager = SessionManager()
            history = manager.get_chat_history('vid1')
            
            self.assertEqual(len(history), 2)
            self.assertEqual(history[0]['video_id'], 'vid1')
            self.assertEqual(history[1]['video_id'], 'vid1')
    
    def test_clear_chat_history_all(self):
        """Test clearing all chat history."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'answer': 'A1', 'video_id': 'vid1'},
                {'question': 'Q2', 'answer': 'A2', 'video_id': 'vid2'}
            ]
            self.mock_st.session_state = {'chat_history': test_history}
            
            manager = SessionManager()
            manager.clear_chat_history()
            
            self.assertEqual(self.mock_st.session_state['chat_history'], [])
    
    def test_clear_chat_history_filtered(self):
        """Test clearing chat history for specific video."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'answer': 'A1', 'video_id': 'vid1'},
                {'question': 'Q2', 'answer': 'A2', 'video_id': 'vid2'},
                {'question': 'Q3', 'answer': 'A3', 'video_id': 'vid1'}
            ]
            self.mock_st.session_state = {'chat_history': test_history}
            
            manager = SessionManager()
            manager.clear_chat_history('vid1')
            
            remaining_history = self.mock_st.session_state['chat_history']
            self.assertEqual(len(remaining_history), 1)
            self.assertEqual(remaining_history[0]['video_id'], 'vid2')
    
    def test_save_processed_video(self):
        """Test saving processed video information."""
        with patch('src.utils.session_manager.st', self.mock_st):
            self.mock_st.session_state = {
                'processed_videos': {},
                'conversation_id': 'test_conv_123'
            }
            
            manager = SessionManager()
            
            video_url = "https://youtube.com/watch?v=test123"
            video_id = "test123"
            metadata = {"title": "Test Video", "author": "Test Author"}
            transcript = "This is a test transcript."
            qa_chain = MagicMock()
            vectorstore = MagicMock()
            
            manager.save_processed_video(
                video_url, video_id, metadata, transcript, qa_chain, vectorstore
            )
            
            # Check processed_videos
            self.assertIn(video_id, self.mock_st.session_state['processed_videos'])
            saved_video = self.mock_st.session_state['processed_videos'][video_id]
            
            self.assertEqual(saved_video['url'], video_url)
            self.assertEqual(saved_video['metadata'], metadata)
            self.assertEqual(saved_video['transcript'], transcript)
            self.assertEqual(saved_video['conversation_id'], 'test_conv_123')
            self.assertIn('processed_at', saved_video)
            
            # Check current session state
            self.assertEqual(self.mock_st.session_state['current_video'], video_id)
            self.assertEqual(self.mock_st.session_state['qa_chain'], qa_chain)
            self.assertEqual(self.mock_st.session_state['vectorstore'], vectorstore)
            self.assertEqual(self.mock_st.session_state['video_metadata'], metadata)
    
    def test_get_processed_videos(self):
        """Test getting processed videos."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_videos = {
                'vid1': {'title': 'Video 1'},
                'vid2': {'title': 'Video 2'}
            }
            self.mock_st.session_state = {'processed_videos': test_videos}
            
            manager = SessionManager()
            videos = manager.get_processed_videos()
            
            self.assertEqual(videos, test_videos)
    
    def test_switch_to_video_success(self):
        """Test successful video switching."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_videos = {
                'vid1': {'title': 'Video 1'},
                'vid2': {'title': 'Video 2'}
            }
            self.mock_st.session_state = {'processed_videos': test_videos}
            
            manager = SessionManager()
            result = manager.switch_to_video('vid1')
            
            self.assertTrue(result)
            self.assertEqual(self.mock_st.session_state['current_video'], 'vid1')
    
    def test_switch_to_video_failure(self):
        """Test video switching failure."""
        with patch('src.utils.session_manager.st', self.mock_st):
            self.mock_st.session_state = {'processed_videos': {}}
            
            manager = SessionManager()
            result = manager.switch_to_video('nonexistent_vid')
            
            self.assertFalse(result)
    
    def test_export_chat_history_json(self):
        """Test exporting chat history as JSON."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'answer': 'A1', 'timestamp': '2024-01-01T12:00:00'}
            ]
            self.mock_st.session_state = {'chat_history': test_history}
            
            manager = SessionManager()
            result = manager.export_chat_history('json')
            
            self.assertIsInstance(result, str)
            self.assertIn('Q1', result)
            self.assertIn('A1', result)
    
    def test_export_chat_history_txt(self):
        """Test exporting chat history as text."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'answer': 'A1', 'timestamp': '2024-01-01T12:00:00'}
            ]
            self.mock_st.session_state = {'chat_history': test_history}
            
            manager = SessionManager()
            result = manager.export_chat_history('txt')
            
            self.assertIsInstance(result, str)
            self.assertIn('Question: Q1', result)
            self.assertIn('Answer: A1', result)
    
    def test_get_session_stats(self):
        """Test getting session statistics."""
        with patch('src.utils.session_manager.st', self.mock_st):
            test_history = [
                {'question': 'Q1', 'timestamp': '2024-01-01T12:00:00'},
                {'question': 'Q2', 'timestamp': '2024-01-01T13:00:00'}
            ]
            test_videos = {'vid1': {}, 'vid2': {}}
            
            self.mock_st.session_state = {
                'chat_history': test_history,
                'processed_videos': test_videos,
                'current_video': 'vid1',
                'conversation_id': 'test_conv_123'
            }
            
            manager = SessionManager()
            stats = manager.get_session_stats()
            
            self.assertEqual(stats['total_questions'], 2)
            self.assertEqual(stats['processed_videos'], 2)
            self.assertEqual(stats['current_video'], 'vid1')
            self.assertEqual(stats['conversation_id'], 'test_conv_123')
            self.assertIn('session_start', stats)

if __name__ == '__main__':
    unittest.main()