Skip to content

Commit 0589260

Browse files
🧪 Add tests for get_tool logic
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent efd7cfd commit 0589260

2 files changed

Lines changed: 53 additions & 51 deletions

File tree

patch_pr.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/test_tools.py

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
find_tools,
1212
execute_tool,
1313
render_tool_index,
14-
PORTED_TOOLS
14+
PORTED_TOOLS,
1515
)
1616
from src.models import PortingBacklog, PortingModule
1717
from src.permissions import ToolPermissionContext
@@ -24,12 +24,12 @@ def test_load_tool_snapshot(self) -> None:
2424
self.assertTrue(len(tools) > 0)
2525
for tool in tools:
2626
self.assertIsInstance(tool, PortingModule)
27-
self.assertEqual(tool.status, 'mirrored')
27+
self.assertEqual(tool.status, "mirrored")
2828

2929
def test_build_tool_backlog(self) -> None:
3030
backlog = build_tool_backlog()
3131
self.assertIsInstance(backlog, PortingBacklog)
32-
self.assertEqual(backlog.title, 'Tool surface')
32+
self.assertEqual(backlog.title, "Tool surface")
3333
self.assertEqual(len(backlog.modules), len(PORTED_TOOLS))
3434
self.assertEqual(backlog.modules, list(PORTED_TOOLS))
3535

@@ -49,9 +49,46 @@ def test_get_tool(self) -> None:
4949
# Case-insensitive match
5050
self.assertEqual(get_tool(first_tool.name.lower()), first_tool)
5151
self.assertEqual(get_tool(first_tool.name.upper()), first_tool)
52+
# Mixed casing
53+
mixed_case_name = "".join(
54+
c.upper() if i % 2 == 0 else c.lower()
55+
for i, c in enumerate(first_tool.name)
56+
)
57+
self.assertEqual(get_tool(mixed_case_name), first_tool)
58+
59+
# Edge cases
60+
self.assertIsNone(get_tool(""))
61+
self.assertIsNone(get_tool(" "))
62+
self.assertIsNone(get_tool("\n"))
63+
5264
# Unknown tool
5365
self.assertIsNone(get_tool("NonExistentToolNamexyz123"))
5466

67+
# First-match priority on duplicates
68+
# Find a name with duplicates in PORTED_TOOLS
69+
names = [t.name for t in PORTED_TOOLS]
70+
dupes = [x for x in set(names) if names.count(x) > 1]
71+
72+
if dupes:
73+
dupe_name = dupes[0]
74+
# Find the actual first module in PORTED_TOOLS with this name
75+
expected_first_module = next(
76+
t for t in PORTED_TOOLS if t.name.lower() == dupe_name.lower()
77+
)
78+
79+
# get_tool should return this exact expected_first_module
80+
self.assertEqual(get_tool(dupe_name), expected_first_module)
81+
self.assertEqual(get_tool(dupe_name.upper()), expected_first_module)
82+
83+
# verify there are other modules with the same name but different source_hint that were not returned
84+
other_modules = [
85+
t
86+
for t in PORTED_TOOLS
87+
if t.name.lower() == dupe_name.lower() and t != expected_first_module
88+
]
89+
self.assertTrue(len(other_modules) > 0)
90+
self.assertNotEqual(get_tool(dupe_name), other_modules[0])
91+
5592
def test_filter_tools_by_permission_context(self) -> None:
5693
tools = PORTED_TOOLS[:5]
5794
# No context
@@ -70,21 +107,27 @@ def test_get_tools(self) -> None:
70107
self.assertEqual(len(all_tools), len(PORTED_TOOLS))
71108

72109
# simple_mode
73-
simple_mode_names = {'BashTool', 'FileReadTool', 'FileEditTool'}
74-
expected_simple_names = {t.name for t in PORTED_TOOLS if t.name in simple_mode_names}
110+
simple_mode_names = {"BashTool", "FileReadTool", "FileEditTool"}
111+
expected_simple_names = {
112+
t.name for t in PORTED_TOOLS if t.name in simple_mode_names
113+
}
75114
simple_tools = get_tools(simple_mode=True)
76115
simple_tool_names = {tool.name for tool in simple_tools}
77116
self.assertEqual(simple_tool_names, expected_simple_names)
78117

79118
# include_mcp=False
80119
# First, find if there are any MCP tools to test the filter
81-
mcp_tools = [t for t in PORTED_TOOLS if 'mcp' in t.name.lower() or 'mcp' in t.source_hint.lower()]
120+
mcp_tools = [
121+
t
122+
for t in PORTED_TOOLS
123+
if "mcp" in t.name.lower() or "mcp" in t.source_hint.lower()
124+
]
82125
if mcp_tools:
83126
no_mcp_tools = get_tools(include_mcp=False)
84127
self.assertTrue(len(no_mcp_tools) < len(PORTED_TOOLS))
85128
for tool in no_mcp_tools:
86-
self.assertNotIn('mcp', tool.name.lower())
87-
self.assertNotIn('mcp', tool.source_hint.lower())
129+
self.assertNotIn("mcp", tool.name.lower())
130+
self.assertNotIn("mcp", tool.source_hint.lower())
88131

89132
# With permission context
90133
if len(PORTED_TOOLS) > 0:
@@ -146,5 +189,6 @@ def test_render_tool_index(self) -> None:
146189
self.assertIn(f"Filtered by: {tool.name}", output)
147190
self.assertIn(tool.name, output)
148191

149-
if __name__ == '__main__':
192+
193+
if __name__ == "__main__":
150194
unittest.main()

0 commit comments

Comments
 (0)