This repository was archived by the owner on Apr 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_plugin.py
More file actions
306 lines (244 loc) · 11 KB
/
Copy pathtest_plugin.py
File metadata and controls
306 lines (244 loc) · 11 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
"""
ELITEA/Pylon Plugin Test Utility
This script provides automated testing for your plugin to ensure it works correctly.
"""
import sys
import json
import requests
import argparse
import time
from pathlib import Path
class PluginTester:
"""Test utility for ELITEA/Pylon plugins"""
def __init__(self, base_url="http://127.0.0.1:8080"):
self.base_url = base_url.rstrip('/')
self.session = requests.Session()
self.session.timeout = 30
def test_health(self):
"""Test the health endpoint"""
print("🏥 Testing health endpoint...")
try:
response = self.session.get(f"{self.base_url}/health")
if response.status_code == 200:
data = response.json()
print(f" ✅ Health check passed")
print(f" 📊 Status: {data.get('status', 'unknown')}")
print(f" ⏱️ Uptime: {data.get('uptime', 0)} seconds")
if 'plugin' in data:
print(f" 🔌 Plugin: {data['plugin']}")
return True
else:
print(f" ❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f" ❌ Health check error: {e}")
return False
def test_descriptor(self):
"""Test the descriptor endpoint"""
print("📋 Testing descriptor endpoint...")
try:
response = self.session.get(f"{self.base_url}/descriptor")
if response.status_code == 200:
data = response.json()
print(f" ✅ Descriptor retrieved successfully")
# Validate structure
if 'provided_toolkits' in data:
toolkits = data['provided_toolkits']
print(f" 🛠️ Toolkits: {len(toolkits)}")
for toolkit in toolkits:
toolkit_name = toolkit.get('name', 'Unknown')
tools = toolkit.get('provided_tools', [])
print(f" 📦 {toolkit_name}: {len(tools)} tools")
for tool in tools:
tool_name = tool.get('name', 'Unknown')
params = tool.get('args_schema', {})
print(f" 🔧 {tool_name}: {len(params)} parameters")
return data
else:
print(f" ❌ Descriptor failed: {response.status_code}")
return None
except Exception as e:
print(f" ❌ Descriptor error: {e}")
return None
def test_tool_invocation(self, toolkit_name, tool_name, parameters=None):
"""Test a tool invocation"""
print(f"🚀 Testing tool: {toolkit_name}/{tool_name}")
if parameters is None:
parameters = {}
try:
url = f"{self.base_url}/tools/{toolkit_name}/{tool_name}/invoke"
payload = {"parameters": parameters}
response = self.session.post(url, json=payload)
if response.status_code == 200:
data = response.json()
print(f" ✅ Tool invocation successful")
print(f" 🆔 Invocation ID: {data.get('invocation_id', 'N/A')}")
print(f" 📊 Status: {data.get('status', 'Unknown')}")
if 'result' in data:
result = data['result']
if isinstance(result, dict):
if 'success' in result:
success = result['success']
print(f" 🎯 Result: {'Success' if success else 'Failed'}")
if not success and 'error' in result:
print(f" ⚠️ Error: {result['error']}")
else:
print(f" 📄 Result keys: {list(result.keys())}")
else:
print(f" 📄 Result type: {type(result).__name__}")
return data
else:
print(f" ❌ Tool invocation failed: {response.status_code}")
try:
error_data = response.json()
print(f" 💬 Error: {error_data.get('message', 'Unknown error')}")
except:
print(f" 💬 Error: {response.text}")
return None
except Exception as e:
print(f" ❌ Tool invocation error: {e}")
return None
def test_invocation_status(self, toolkit_name, tool_name, invocation_id):
"""Test invocation status checking"""
print(f"📊 Testing invocation status: {invocation_id}")
try:
url = f"{self.base_url}/tools/{toolkit_name}/{tool_name}/invocations/{invocation_id}"
response = self.session.get(url)
if response.status_code == 200:
data = response.json()
print(f" ✅ Status check successful")
print(f" 📊 Status: {data.get('status', 'Unknown')}")
return data
else:
print(f" ❌ Status check failed: {response.status_code}")
return None
except Exception as e:
print(f" ❌ Status check error: {e}")
return None
def auto_test_all_tools(self):
"""Automatically test all tools with empty parameters"""
print("🤖 Auto-testing all tools...")
descriptor = self.test_descriptor()
if not descriptor:
print(" ❌ Cannot auto-test without descriptor")
return False
success_count = 0
total_count = 0
for toolkit in descriptor.get('provided_toolkits', []):
toolkit_name = toolkit.get('name')
for tool in toolkit.get('provided_tools', []):
tool_name = tool.get('name')
total_count += 1
# Try with empty parameters first
result = self.test_tool_invocation(toolkit_name, tool_name, {})
if result:
success_count += 1
# Test status if we got an invocation ID
invocation_id = result.get('invocation_id')
if invocation_id:
self.test_invocation_status(toolkit_name, tool_name, invocation_id)
print(f"\n📈 Auto-test results: {success_count}/{total_count} tools passed")
return success_count == total_count
def run_full_test_suite(self):
"""Run the complete test suite"""
print("🧪 Running full plugin test suite...")
print("=" * 50)
tests_passed = 0
total_tests = 3
# Test 1: Health check
if self.test_health():
tests_passed += 1
print()
# Test 2: Descriptor
descriptor = self.test_descriptor()
if descriptor:
tests_passed += 1
print()
# Test 3: Tool auto-testing
if self.auto_test_all_tools():
tests_passed += 1
print()
print("=" * 50)
print(f"🏆 Test Results: {tests_passed}/{total_tests} test suites passed")
if tests_passed == total_tests:
print("🎉 All tests passed! Your plugin is working correctly.")
return True
else:
print("⚠️ Some tests failed. Check the output above for details.")
return False
def create_sample_test_parameters():
"""Create sample test parameters file"""
sample_config = {
"description": "Sample test parameters for your plugin tools",
"tests": [
{
"toolkit_name": "YourToolkitName",
"tool_name": "your_tool_name",
"parameters": {
"param1": "sample_value",
"param2": 42,
"param3": True
},
"description": "Sample test for your_tool_name"
}
]
}
with open('test_parameters.json', 'w') as f:
json.dump(sample_config, f, indent=2)
print("📄 Created test_parameters.json with sample configuration")
print(" Edit this file to add specific test cases for your tools")
def load_custom_tests():
"""Load custom test parameters from file"""
if Path('test_parameters.json').exists():
try:
with open('test_parameters.json', 'r') as f:
return json.load(f)
except Exception as e:
print(f"⚠️ Error loading test_parameters.json: {e}")
return None
def main():
"""Main test function"""
parser = argparse.ArgumentParser(description='Test ELITEA/Pylon plugin')
parser.add_argument('--url', default='http://127.0.0.1:8080',
help='Plugin base URL (default: http://127.0.0.1:8080)')
parser.add_argument('--test', choices=['health', 'descriptor', 'invoke', 'all'],
default='all', help='Which test to run')
parser.add_argument('--toolkit', help='Toolkit name for invoke test')
parser.add_argument('--tool', help='Tool name for invoke test')
parser.add_argument('--create-sample', action='store_true',
help='Create sample test_parameters.json file')
parser.add_argument('--custom-tests', action='store_true',
help='Run custom tests from test_parameters.json')
args = parser.parse_args()
if args.create_sample:
create_sample_test_parameters()
return
tester = PluginTester(args.url)
if args.custom_tests:
print("🎯 Running custom tests...")
custom_config = load_custom_tests()
if custom_config:
for test in custom_config.get('tests', []):
tester.test_tool_invocation(
test['toolkit_name'],
test['tool_name'],
test.get('parameters', {})
)
else:
print("❌ No custom tests found. Use --create-sample first.")
return
if args.test == 'health':
tester.test_health()
elif args.test == 'descriptor':
tester.test_descriptor()
elif args.test == 'invoke':
if not args.toolkit or not args.tool:
print("❌ --toolkit and --tool are required for invoke test")
sys.exit(1)
tester.test_tool_invocation(args.toolkit, args.tool)
elif args.test == 'all':
success = tester.run_full_test_suite()
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()