-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mcpserver_1.py
More file actions
198 lines (158 loc) · 7.18 KB
/
Copy pathtest_mcpserver_1.py
File metadata and controls
198 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
import json
import os
import tempfile
import unittest
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch
import mcpserver_opt_1 as mod
class TestValidateRtbrickUrl(unittest.TestCase):
def test_allows_valid_https_rtbrick_domain(self) -> None:
ok, msg = mod.validate_rtbrick_url("https://www.rtbrick.com/some/page")
self.assertTrue(ok)
self.assertEqual(msg, "")
def test_rejects_non_https_scheme(self) -> None:
ok, msg = mod.validate_rtbrick_url("http://www.rtbrick.com/")
self.assertFalse(ok)
self.assertIn("Only https:// URLs are allowed", msg)
def test_rejects_unknown_domain(self) -> None:
ok, msg = mod.validate_rtbrick_url("https://example.com/")
self.assertFalse(ok)
self.assertIn("allowlist", msg)
class TestExtractTextFromHtml(unittest.TestCase):
def test_extracts_readable_text_and_skips_script(self) -> None:
html = """
<html>
<head><title>Test</title></head>
<body>
<h1>Title</h1>
<p>Hello <b>world</b></p>
<script>console.log("should be skipped")</script>
</body>
</html>
"""
text = mod.extract_text_from_html(html)
self.assertIn("Title", text)
self.assertIn("Hello world", text)
self.assertNotIn("should be skipped", text)
class TestCacheEntryAndDocumentCache(unittest.TestCase):
def test_cache_entry_expiry(self) -> None:
now = datetime.now()
old_ts = now - timedelta(seconds=5)
entry = mod.CacheEntry(content="x", timestamp=old_ts, url="u")
self.assertTrue(entry.is_expired(ttl_seconds=1))
self.assertFalse(entry.is_expired(ttl_seconds=10))
def test_document_cache_put_get_and_evict(self) -> None:
cache = mod.DocumentCache(max_size=1, ttl_seconds=60)
cache.put("k1", "v1", "url1")
self.assertEqual(cache.get("k1"), "v1")
stats = cache.stats()
self.assertEqual(stats["entries"], 1)
cache.put("k2", "v2", "url2")
self.assertIsNone(cache.get("k1"))
self.assertEqual(cache.get("k2"), "v2")
class TestSearchDocuments(unittest.TestCase):
def test_search_documents_ranks_and_returns_context(self) -> None:
docs = {
"doc1.html": "This is a RTBrick document about BGP and routing.",
"doc2.html": "Unrelated content with no useful keywords.",
"bgp_intro.html": "BGP basics. BGP is a routing protocol. More about BGP here.",
}
hits = mod.search_documents("BGP", docs, context_lines=1, max_results=5)
self.assertGreaterEqual(len(hits), 1)
top = hits[0]
self.assertEqual(top.doc_path, "bgp_intro.html")
self.assertIn("BGP", top.context)
class TestLoadConfig(unittest.TestCase):
def test_load_config_uses_config_path_env_when_present(self) -> None:
data = {
"doc_paths": ["custom1.html", "custom2.html"],
"base_url": "https://documents.rtbrick.com/custom/",
}
with tempfile.TemporaryDirectory() as tmpdir:
cfg_path = os.path.join(tmpdir, "config.json")
with open(cfg_path, "w", encoding="utf-8") as fh:
json.dump(data, fh)
with patch.dict(os.environ, {"CONFIG_PATH": cfg_path}, clear=False):
loaded = mod.load_config()
self.assertEqual(loaded.get("doc_paths"), data["doc_paths"])
self.assertEqual(loaded.get("base_url"), data["base_url"])
class TestMCPServerBasics(unittest.TestCase):
@patch.object(mod, "load_config", return_value={})
def test_server_initialization_uses_defaults(self, _mock_load_config: MagicMock) -> None:
server = mod.MCPServer()
self.assertEqual(server.tech_doc_paths, mod.DEFAULT_TECH_DOC_PATHS)
self.assertEqual(server.tech_docs_base_url, mod.RTBRICK_TECHDOCS_BASE_URL)
@patch.object(mod, "load_config", return_value={})
def test_tools_schema_contains_expected_tools(self, _mock_load_config: MagicMock) -> None:
server = mod.MCPServer()
tools = server._tools_schema()
names = {t["name"] for t in tools}
self.assertIn("fetch_rtbrick_page", names)
self.assertIn("search_tech_docs", names)
@patch.object(mod, "load_config", return_value={})
def test_handle_initialize_and_tools_list(self, _mock_load_config: MagicMock) -> None:
server = mod.MCPServer()
init_req = {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}
init_resp = server.handle_request(init_req)
self.assertIsNotNone(init_resp)
self.assertEqual(init_resp["result"]["serverInfo"]["name"], "rtbrick-mcp-server")
list_req = {"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}
list_resp = server.handle_request(list_req)
self.assertIsNotNone(list_resp)
self.assertIn("tools", list_resp["result"])
@patch.object(mod, "load_config", return_value={})
def test_handle_tools_call_validation_and_unknown_tool(
self,
_mock_load_config: MagicMock,
) -> None:
server = mod.MCPServer()
bad_fetch_req = {
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {"name": "fetch_rtbrick_page", "arguments": {}},
}
bad_resp = server.handle_request(bad_fetch_req)
self.assertIsNotNone(bad_resp)
self.assertEqual(bad_resp["error"]["code"], server.INVALID_PARAMS)
unknown_req = {
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {"name": "nonexistent_tool", "arguments": {}},
}
unknown_resp = server.handle_request(unknown_req)
self.assertIsNotNone(unknown_resp)
self.assertEqual(unknown_resp["error"]["code"], server.METHOD_NOT_FOUND)
class TestMCPServerToolImplementations(unittest.TestCase):
@patch.object(mod, "load_config", return_value={})
def test_tool_list_tech_docs_outputs_expected_header(self, _mock_load_config: MagicMock) -> None:
server = mod.MCPServer()
output = server.tool_list_tech_docs()
self.assertIn("RTBrick Technical Documentation Pages", output)
self.assertIn(server.tech_docs_base_url, output)
@patch.object(mod, "load_config", return_value={})
def test_fetch_url_uses_cache_before_http(
self,
_mock_load_config: MagicMock,
) -> None:
server = mod.MCPServer()
url = "https://www.rtbrick.com/some/page"
server.cache.put(url, "cached content", url=url)
with patch.object(server.http, "get") as mock_get:
content, err = server._fetch_url(url)
self.assertIsNone(err)
self.assertEqual(content, "cached content")
mock_get.assert_not_called()
@patch.object(mod, "load_config", return_value={})
def test_fetch_url_rejects_invalid_domain(
self,
_mock_load_config: MagicMock,
) -> None:
server = mod.MCPServer()
content, err = server._fetch_url("https://example.com/not-allowed")
self.assertIsNone(content)
self.assertIsInstance(err, str)
self.assertIn("allowlist", err)
if __name__ == "__main__":
unittest.main()