-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tool_registry.py
More file actions
217 lines (172 loc) · 6.3 KB
/
test_tool_registry.py
File metadata and controls
217 lines (172 loc) · 6.3 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
#!/usr/bin/env python3
"""
Test Tool Registry
Verifies that:
1. Default tools are registered
2. Builder is aware of tools
3. Generated code uses tools
"""
from dynograph.tools import default_tool_registry
from dynograph.generation.builder import GraphBuilder
def test_default_registry():
"""Test that default tool registry has all tools."""
print("=" * 80)
print("TEST 1: Default Tool Registry")
print("=" * 80)
print(f"\nRegistry: {default_tool_registry}")
print(f"Total tools: {len(default_tool_registry)}")
tools = default_tool_registry.list_tools()
descriptions = default_tool_registry.get_tool_descriptions()
print(f"\n Available Tools ({len(tools)}):")
print("-" * 80)
# Group by category
categories = {
"Search & Research": ["web_search", "wikipedia"],
"Code & Execution": ["code", "python_repl"],
"Math & Analysis": ["calculator", "text_analyzer"],
"File Operations": ["file_reader", "file_writer"],
"Network & API": ["http_request"],
"System": ["shell", "datetime"],
"Data Processing": ["json_parser"],
}
for category, tool_names in categories.items():
print(f"\n{category}:")
for tool_name in tool_names:
if tool_name in descriptions:
print(f" ✓ {tool_name}: {descriptions[tool_name]}")
else:
print(f" ✗ {tool_name}: NOT FOUND")
# Verify expected count
expected = 12
if len(tools) == expected:
print(f"\n✅ All {expected} default tools registered")
return True
else:
print(f"\n❌ Expected {expected} tools, found {len(tools)}")
return False
def test_builder_integration():
"""Test that GraphBuilder uses tool registry."""
print("\n" + "=" * 80)
print("TEST 2: Builder Integration")
print("=" * 80)
# Create builder (should auto-use default registry)
builder = GraphBuilder()
if builder.tool_registry is None:
print("\n❌ Builder has no tool registry!")
return False
tools = builder.tool_registry.list_tools()
print(f"\n✅ Builder has tool registry with {len(tools)} tools")
# Verify it's the default registry
if builder.tool_registry == default_tool_registry:
print("✅ Using default_tool_registry")
else:
print("ℹ️ Using custom registry")
return True
def test_tool_in_prompt():
"""Test that tools are shown in generation prompts."""
print("\n" + "=" * 80)
print("TEST 3: Tools in Prompts")
print("=" * 80)
from dynograph.generation.prompts import build_code_generation_prompt
# Get tools from registry
tools_dict = default_tool_registry.get_tool_descriptions()
# Build a simple prompt
prompt = build_code_generation_prompt(
user_query="Create a graph that searches the web",
examples=[], # Empty for this test
available_tools=tools_dict
)
# Check if tools are mentioned
checks = {
"web_search in prompt": "web_search" in prompt,
"code in prompt": "code" in prompt,
"file_reader in prompt": "file_reader" in prompt,
"Tool count shown": f"{len(tools_dict)} tools" in prompt,
"get_tool mentioned": "get_tool" in prompt,
}
print("\nPrompt checks:")
all_pass = True
for check, result in checks.items():
status = "✅" if result else "❌"
print(f" {status} {check}")
if not result:
all_pass = False
if all_pass:
print("\n✅ Tools properly displayed in prompts")
else:
print("\n❌ Some tools missing from prompts")
# Show snippet of tools section
if "AVAILABLE TOOLS" in prompt:
tools_start = prompt.find("AVAILABLE TOOLS")
tools_section = prompt[tools_start:tools_start+500]
print("\nTools section preview:")
print("-" * 80)
print(tools_section[:400] + "...")
print("-" * 80)
return all_pass
def test_tool_access():
"""Test that we can actually get and use tools."""
print("\n" + "=" * 80)
print("TEST 4: Tool Access")
print("=" * 80)
from dynograph.tools import get_tool
# Test getting a few tools
test_tools = ["text_analyzer", "datetime", "json_parser"]
print("\nTesting tool retrieval:")
all_success = True
for tool_name in test_tools:
tool = get_tool(tool_name)
if tool:
print(f" ✅ {tool_name}: {type(tool).__name__}")
# Try to use it
try:
if tool_name == "text_analyzer":
result = tool.invoke("Hello world test")
print(f" → Result: {result}")
elif tool_name == "datetime":
result = tool.invoke("%Y-%m-%d")
print(f" → Result: {result}")
elif tool_name == "json_parser":
result = tool.invoke('{"test": "value"}')
print(f" → Result: {result}")
except Exception as e:
print(f" ⚠️ Error using tool: {e}")
else:
print(f" ❌ {tool_name}: Could not retrieve")
all_success = False
if all_success:
print("\n✅ All tools accessible and working")
else:
print("\n❌ Some tools failed")
return all_success
def main():
"""Run all tests."""
print("\n" + "╔" + "=" * 78 + "╗")
print("║" + " TOOL REGISTRY TESTS".center(78) + "║")
print("╚" + "=" * 78 + "╝")
results = {
"Default Registry": test_default_registry(),
"Builder Integration": test_builder_integration(),
"Tools in Prompts": test_tool_in_prompt(),
"Tool Access": test_tool_access(),
}
# Summary
print("\n" + "=" * 80)
print("SUMMARY")
print("=" * 80)
for test, passed in results.items():
status = "✅ PASS" if passed else "❌ FAIL"
print(f" {status} {test}")
passed_count = sum(1 for v in results.values() if v)
total = len(results)
print(f"\n{passed_count}/{total} tests passed")
if passed_count == total:
print("\n🎉 All tool registry tests passed!")
return True
else:
print("\n⚠️ Some tests failed")
return False
if __name__ == "__main__":
import sys
success = main()
sys.exit(0 if success else 1)