|
|
@@ -334,5 +334,142 @@ class TestGitTools:
|
|
|
assert "ERROR" in result
|
|
|
|
|
|
|
|
|
+# ════════════════════════════════════════════════════════════
|
|
|
+# A08: Tool Registry
|
|
|
+# ════════════════════════════════════════════════════════════
|
|
|
+
|
|
|
+class TestToolRegistry:
|
|
|
+ def test_all_tools_registered(self):
|
|
|
+ from lambdagent.builtin_tools.registry import BUILTIN_TOOLS
|
|
|
+ expected = {"ReadFile", "EditFile", "WriteFile", "ListFiles", "SearchContent",
|
|
|
+ "Bash", "GitStatus", "GitDiff", "GitLog", "GitCommit", "GitBranch",
|
|
|
+ "terminate"}
|
|
|
+ assert expected.issubset(set(BUILTIN_TOOLS.keys()))
|
|
|
+
|
|
|
+ def test_get_builtin_tool(self):
|
|
|
+ from lambdagent.builtin_tools.registry import get_builtin_tool
|
|
|
+ assert get_builtin_tool("ReadFile") is not None
|
|
|
+ assert get_builtin_tool("nonexistent") is None
|
|
|
+
|
|
|
+ def test_resolve_tools(self):
|
|
|
+ from lambdagent.builtin_tools.registry import resolve_tools
|
|
|
+ result = resolve_tools(["ReadFile", "Bash", "terminate", "fake_tool"])
|
|
|
+ assert "ReadFile" in result
|
|
|
+ assert "Bash" in result
|
|
|
+ assert "terminate" in result
|
|
|
+ assert "fake_tool" not in result
|
|
|
+
|
|
|
+ def test_from_config_integration(self):
|
|
|
+ """Test that from_config resolves built-in tool names."""
|
|
|
+ from lambdagent.builtin_tools.registry import get_builtin_tool
|
|
|
+ tool = get_builtin_tool("ReadFile")
|
|
|
+ assert tool is not None
|
|
|
+ # Tool should work
|
|
|
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
|
|
|
+ f.write("test content\n")
|
|
|
+ path = f.name
|
|
|
+ try:
|
|
|
+ result = tool.apply(json.dumps({"file_path": path}))
|
|
|
+ assert "test content" in result
|
|
|
+ finally:
|
|
|
+ os.unlink(path)
|
|
|
+
|
|
|
+
|
|
|
+# ════════════════════════════════════════════════════════════
|
|
|
+# A09: Terminal UI
|
|
|
+# ════════════════════════════════════════════════════════════
|
|
|
+
|
|
|
+class TestTerminalUI:
|
|
|
+ def test_ui_creation(self):
|
|
|
+ from lambdagent.builtin_tools.terminal_ui import TerminalUI
|
|
|
+ ui = TerminalUI(verbose=True)
|
|
|
+ assert ui.verbose is True
|
|
|
+
|
|
|
+ def test_ui_start(self, capsys):
|
|
|
+ from lambdagent.builtin_tools.terminal_ui import TerminalUI
|
|
|
+ ui = TerminalUI(color=False)
|
|
|
+ ui.start("test-agent", "react")
|
|
|
+ captured = capsys.readouterr()
|
|
|
+ assert "test-agent" in captured.out
|
|
|
+
|
|
|
+ def test_ui_result(self, capsys):
|
|
|
+ from lambdagent.builtin_tools.terminal_ui import TerminalUI
|
|
|
+ ui = TerminalUI(color=False)
|
|
|
+ ui._start_time = time.time()
|
|
|
+ ui.result("Hello world", 3)
|
|
|
+ captured = capsys.readouterr()
|
|
|
+ assert "Hello world" in captured.out
|
|
|
+ assert "3 β-reductions" in captured.out
|
|
|
+
|
|
|
+ def test_ui_tool_cycle(self, capsys):
|
|
|
+ from lambdagent.builtin_tools.terminal_ui import TerminalUI
|
|
|
+ ui = TerminalUI(color=False)
|
|
|
+ ui.tool_start("search", "query text")
|
|
|
+ ui.tool_end("search", "found 5 results", 150.0)
|
|
|
+ captured = capsys.readouterr()
|
|
|
+ assert "search" in captured.out
|
|
|
+ assert "150ms" in captured.out
|
|
|
+
|
|
|
+
|
|
|
+# ════════════════════════════════════════════════════════════
|
|
|
+# A10: Integration — end-to-end with built-in tools
|
|
|
+# ════════════════════════════════════════════════════════════
|
|
|
+
|
|
|
+class TestE2EBuiltinTools:
|
|
|
+ def test_read_edit_verify(self):
|
|
|
+ """End-to-end: ReadFile → EditFile → ReadFile to verify."""
|
|
|
+ from lambdagent.builtin_tools.file_tools import read_file, edit_file
|
|
|
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
|
|
|
+ f.write("def greet():\n return 'hello'\n")
|
|
|
+ path = f.name
|
|
|
+ try:
|
|
|
+ # Read
|
|
|
+ content = read_file({"file_path": path})
|
|
|
+ assert "hello" in content
|
|
|
+ # Edit
|
|
|
+ result = edit_file({"file_path": path, "old_string": "return 'hello'", "new_string": "return 'world'"})
|
|
|
+ assert "OK" in result
|
|
|
+ # Verify
|
|
|
+ content2 = read_file({"file_path": path})
|
|
|
+ assert "world" in content2
|
|
|
+ assert "hello" not in content2
|
|
|
+ finally:
|
|
|
+ os.unlink(path)
|
|
|
+
|
|
|
+ def test_write_list_search(self):
|
|
|
+ """End-to-end: WriteFile → ListFiles → SearchContent."""
|
|
|
+ from lambdagent.builtin_tools.file_tools import write_file, list_files, search_content
|
|
|
+ with tempfile.TemporaryDirectory() as d:
|
|
|
+ # Write
|
|
|
+ path = os.path.join(d, "app.py")
|
|
|
+ write_file({"file_path": path, "content": "def main():\n print('hello')\n"})
|
|
|
+ # List
|
|
|
+ found = list_files({"pattern": "*.py", "path": d})
|
|
|
+ assert "app.py" in found
|
|
|
+ # Search
|
|
|
+ matches = search_content({"pattern": "def main", "path": d})
|
|
|
+ assert "main" in matches
|
|
|
+
|
|
|
+ def test_bash_git_workflow(self):
|
|
|
+ """End-to-end: Bash + Git in temp repo."""
|
|
|
+ from lambdagent.builtin_tools.shell_tools import run_bash, git_status, git_log
|
|
|
+ with tempfile.TemporaryDirectory() as d:
|
|
|
+ run_bash({"command": "git init", "working_dir": d})
|
|
|
+ run_bash({"command": "git config user.email 'test@test.com'", "working_dir": d})
|
|
|
+ run_bash({"command": "git config user.name 'Test'", "working_dir": d})
|
|
|
+ run_bash({"command": "echo hello > test.txt", "working_dir": d})
|
|
|
+ run_bash({"command": "git add . && git commit -m 'init'", "working_dir": d})
|
|
|
+
|
|
|
+ # Status should be clean
|
|
|
+ from lambdagent.builtin_tools.shell_tools import _set_cwd, _get_cwd
|
|
|
+ original_cwd = _get_cwd()
|
|
|
+ _set_cwd(d)
|
|
|
+ try:
|
|
|
+ status = git_status("")
|
|
|
+ assert "nothing to commit" in status.lower() or status.strip() == "[OK] (no output)"
|
|
|
+ finally:
|
|
|
+ _set_cwd(original_cwd)
|
|
|
+
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
pytest.main([__file__, "-v", "--tb=short"])
|