forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_integrations.py
More file actions
206 lines (158 loc) 路 7.18 KB
/
Copy pathtest_integrations.py
File metadata and controls
206 lines (158 loc) 路 7.18 KB
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
"""Tests for LangChain and LlamaIndex integrations."""
import json
import os
import sys
from unittest.mock import MagicMock, patch
import pytest
# Add repo root to path for imports
sys.path.insert(0, str(os.path.dirname(os.path.dirname(__file__))))
class TestLangChainIntegration:
"""Tests for LangChain MisakaNet tool."""
@pytest.fixture(autouse=True)
def skip_if_no_langchain(self):
"""Skip LangChain tests if langchain is not installed."""
pytest.importorskip("langchain")
def test_tool_import(self):
"""Test that LangChain tool can be imported."""
from integrations.langchain.misakanet_tool import MisakaNetSearchTool
tool = MisakaNetSearchTool()
assert tool.name == "misakanet_search"
assert "MisakaNet" in tool.description
def test_tool_input_schema(self):
"""Test tool input schema is properly defined."""
from integrations.langchain.misakanet_tool import (
MisakaNetSearchInput,
MisakaNetSearchTool,
)
tool = MisakaNetSearchTool()
schema = tool.args_schema.schema()
assert "query" in schema["properties"]
assert "max_results" in schema["properties"]
assert schema["properties"]["query"]["type"] == "string"
@patch("urllib.request.urlopen")
def test_search_rest_success(self, mock_urlopen):
"""Test REST API search with successful response."""
from integrations.langchain.misakanet_tool import MisakaNetSearchTool
# Mock response
mock_response = MagicMock()
mock_response.read.return_value = json.dumps(
{
"results": [
{
"title": "Test Lesson",
"type": "error",
"score": 0.95,
"problem": "Test problem",
}
]
}
).encode("utf-8")
mock_response.__enter__ = lambda s: s
mock_response.__exit__ = MagicMock(return_value=False)
mock_urlopen.return_value = mock_response
tool = MisakaNetSearchTool()
result = tool._run("test query")
assert "Found 1 relevant lessons" in result
assert "Test Lesson" in result
@patch("urllib.request.urlopen")
def test_search_rest_empty(self, mock_urlopen):
"""Test REST API search with no results."""
from integrations.langchain.misakanet_tool import MisakaNetSearchTool
mock_response = MagicMock()
mock_response.read.return_value = json.dumps({"results": []}).encode("utf-8")
mock_response.__enter__ = lambda s: s
mock_response.__exit__ = MagicMock(return_value=False)
mock_urlopen.return_value = mock_response
tool = MisakaNetSearchTool()
result = tool._run("nonexistent query")
assert "No matching lessons found" in result
@patch("urllib.request.urlopen")
def test_search_rest_error(self, mock_urlopen):
"""Test REST API search with network error."""
from integrations.langchain.misakanet_tool import MisakaNetSearchTool
mock_urlopen.side_effect = Exception("Connection failed")
tool = MisakaNetSearchTool()
result = tool._run("test query")
assert "Search failed" in result
def test_get_misakanet_tool(self):
"""Test convenience function."""
from integrations.langchain.misakanet_tool import get_misakanet_tool
tool = get_misakanet_tool()
assert tool.name == "misakanet_search"
class TestLlamaIndexIntegration:
"""Tests for LlamaIndex MisakaNet tool."""
def test_function_import(self):
"""Test that LlamaIndex function can be imported."""
from integrations.llamaindex.misakanet_tool import misakanet_search
assert callable(misakanet_search)
@patch("urllib.request.urlopen")
def test_search_success(self, mock_urlopen):
"""Test search with successful response."""
from integrations.llamaindex.misakanet_tool import misakanet_search
mock_response = MagicMock()
mock_response.read.return_value = json.dumps(
{
"results": [
{
"title": "Test Lesson",
"type": "error",
"score": 0.95,
"problem": "Test problem",
}
]
}
).encode("utf-8")
mock_response.__enter__ = lambda s: s
mock_response.__exit__ = MagicMock(return_value=False)
mock_urlopen.return_value = mock_response
result = misakanet_search("test query")
assert "Found 1 relevant lessons" in result
assert "Test Lesson" in result
@patch("urllib.request.urlopen")
def test_search_empty(self, mock_urlopen):
"""Test search with no results."""
from integrations.llamaindex.misakanet_tool import misakanet_search
mock_response = MagicMock()
mock_response.read.return_value = json.dumps({"results": []}).encode("utf-8")
mock_response.__enter__ = lambda s: s
mock_response.__exit__ = MagicMock(return_value=False)
mock_urlopen.return_value = mock_response
result = misakanet_search("nonexistent query")
assert "No matching lessons found" in result
@patch("urllib.request.urlopen")
def test_search_error(self, mock_urlopen):
"""Test search with network error."""
from integrations.llamaindex.misakanet_tool import misakanet_search
mock_urlopen.side_effect = Exception("Connection failed")
result = misakanet_search("test query")
assert "Search failed" in result
def test_max_results_clamping(self):
"""Test that max_results is clamped to valid range."""
from integrations.llamaindex.misakanet_tool import misakanet_search
# Mock to avoid actual API call
with patch("urllib.request.urlopen") as mock_urlopen:
mock_response = MagicMock()
mock_response.read.return_value = json.dumps({"results": []}).encode(
"utf-8"
)
mock_response.__enter__ = lambda s: s
mock_response.__exit__ = MagicMock(return_value=False)
mock_urlopen.return_value = mock_response
# Test clamping
misakanet_search("test", max_results=0) # Should become 1
misakanet_search("test", max_results=100) # Should become 10
@patch.dict(os.environ, {"MISAKANET_API_KEY": "test-key"})
@patch("urllib.request.urlopen")
def test_api_key_from_env(self, mock_urlopen):
"""Test that API key is read from environment."""
from integrations.llamaindex.misakanet_tool import misakanet_search
mock_response = MagicMock()
mock_response.read.return_value = json.dumps({"results": []}).encode("utf-8")
mock_response.__enter__ = lambda s: s
mock_response.__exit__ = MagicMock(return_value=False)
mock_urlopen.return_value = mock_response
misakanet_search("test query")
# Verify API key was passed
call_args = mock_urlopen.call_args
req = call_args[0][0]
assert req.get_header("Authorization") == "Bearer test-key"