File size: 4,485 Bytes
3cdce90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unit tests for interface module
Tests Gradio interface functionality
"""

import unittest
import uuid
from unittest.mock import MagicMock, Mock, patch

from src.interface import create_interface, respond


class TestInterface(unittest.TestCase):
    """Test cases for interface module"""

    def setUp(self):
        """Set up test fixtures"""
        self.message = "How do I create an account?"
        self.history = [["Previous question", "Previous answer"]]
        self.session_id = str(uuid.uuid4())
        self.mock_intent_classifier = Mock()
        self.mock_retriever = Mock()

    @patch("app.get_context_and_answer")
    def test_respond_with_session_id(self, mock_get_answer):
        """Test respond function with existing session ID"""
        mock_get_answer.return_value = "You can create an account by visiting our website."

        result_msg, result_history = respond(
            self.message,
            self.history.copy(),
            self.session_id,
            self.mock_intent_classifier,
            self.mock_retriever,
        )

        # Verify get_context_and_answer was called
        mock_get_answer.assert_called_once()
        call_args = mock_get_answer.call_args[0]
        self.assertEqual(call_args[0], self.message)
        self.assertEqual(call_args[2], self.session_id)

        # Check return values
        self.assertEqual(result_msg, "")
        self.assertEqual(len(result_history), 2)
        self.assertEqual(result_history[-1][0], self.message)
        self.assertEqual(
            result_history[-1][1],
            "You can create an account by visiting our website.",
        )

    @patch("app.get_context_and_answer")
    def test_respond_without_session_id(self, mock_get_answer):
        """Test respond function generates session ID when none provided"""
        mock_get_answer.return_value = "Response"

        result_msg, result_history = respond(
            self.message,
            [],
            None,
            self.mock_intent_classifier,
            self.mock_retriever,
        )

        # Should have called with a generated session ID
        self.assertEqual(mock_get_answer.call_count, 1)
        call_args = mock_get_answer.call_args[0]
        generated_session_id = call_args[2]

        # Verify it's a valid UUID
        try:
            uuid.UUID(generated_session_id)
            valid_uuid = True
        except ValueError:
            valid_uuid = False

        self.assertTrue(valid_uuid)

        # Check return values
        self.assertEqual(result_msg, "")
        self.assertEqual(len(result_history), 1)

    @patch("app.get_context_and_answer")
    def test_respond_with_empty_history(self, mock_get_answer):
        """Test respond function with empty history"""
        mock_get_answer.return_value = "Test response"

        result_msg, result_history = respond(
            "Test question",
            [],
            self.session_id,
            self.mock_intent_classifier,
            self.mock_retriever,
        )

        # History should have one entry
        self.assertEqual(len(result_history), 1)
        self.assertEqual(result_history[0][0], "Test question")
        self.assertEqual(result_history[0][1], "Test response")

    @patch("app.get_context_and_answer")
    def test_respond_preserves_existing_history(self, mock_get_answer):
        """Test respond function preserves existing chat history"""
        mock_get_answer.return_value = "New response"

        initial_history = [
            ["Question 1", "Answer 1"],
            ["Question 2", "Answer 2"],
        ]

        result_msg, result_history = respond(
            "Question 3",
            initial_history.copy(),
            self.session_id,
            self.mock_intent_classifier,
            self.mock_retriever,
        )

        # Should have 3 entries now
        self.assertEqual(len(result_history), 3)
        self.assertEqual(result_history[0][0], "Question 1")
        self.assertEqual(result_history[1][0], "Question 2")
        self.assertEqual(result_history[2][0], "Question 3")

    def test_create_interface_returns_blocks(self):
        """Test create_interface returns Gradio Blocks interface"""
        result = create_interface(self.mock_intent_classifier, self.mock_retriever)
        
        # Should return a Gradio Blocks object
        import gradio as gr
        self.assertIsInstance(result, gr.Blocks)


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