summaryrefslogtreecommitdiff
path: root/tests/test_tools.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_tools.py')
-rw-r--r--tests/test_tools.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/test_tools.py b/tests/test_tools.py
new file mode 100644
index 0000000..897c597
--- /dev/null
+++ b/tests/test_tools.py
@@ -0,0 +1,100 @@
+"""Tests for tool registry and built-in tools."""
+
+from __future__ import annotations
+
+import json
+from unittest.mock import MagicMock, patch
+
+from ollama_web.tools.registry import ToolRegistry, default_registry
+
+
+def test_registry_register_and_execute():
+ reg = ToolRegistry()
+
+ def add(a: int, b: int) -> int:
+ """Add two numbers.
+
+ Args:
+ a: First number.
+ b: Second number.
+
+ Returns:
+ The sum.
+ """
+ return a + b
+
+ reg.register(add)
+ assert reg.get("add") is add
+ result = reg.execute("add", {"a": 2, "b": 3})
+ assert result == "5"
+
+
+def test_registry_execute_unknown():
+ reg = ToolRegistry()
+ out = reg.execute("nonexistent", {})
+ assert "error" in json.loads(out)
+
+
+def test_registry_execute_invalid_json_args():
+ reg = ToolRegistry()
+
+ def echo(x: str) -> str:
+ return x
+
+ reg.register(echo)
+ out = reg.execute("echo", "{not json")
+ assert "error" in json.loads(out)
+
+
+def test_registry_execute_handles_exceptions():
+ reg = ToolRegistry()
+
+ def boom() -> None:
+ raise ValueError("boom")
+
+ reg.register(boom)
+ out = reg.execute("boom", {})
+ assert "error" in json.loads(out)
+
+
+def test_default_registry_has_tools():
+ reg = default_registry()
+ assert "web_search" in reg.names
+ assert "scrape_url" in reg.names
+
+
+def test_web_search_empty_query():
+ from ollama_web.tools.search import web_search
+
+ assert web_search("") == []
+
+
+def test_web_search_mocked():
+ from ollama_web.tools import search as search_mod
+
+ fake = [{"title": "T", "href": "http://x", "body": "B"}]
+
+ class FakeDDGS:
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *a):
+ return None
+
+ def text(self, q, max_results=5):
+ return fake
+
+ monkey = MagicMock()
+ monkey.DDGS = FakeDDGS
+ with patch.object(search_mod, "DDGS", FakeDDGS):
+ from ollama_web.tools.search import web_search
+
+ result = web_search("test", max_results=1)
+ assert len(result) == 1
+ assert result[0]["title"] == "T"
+
+
+def test_scrape_url_empty():
+ from ollama_web.tools.scrape import scrape_url
+
+ assert scrape_url("") == "Invalid URL." \ No newline at end of file