summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-06-23 18:37:46 +0900
committertanaka takashi <isazaka@gmail.com>2026-06-23 18:37:46 +0900
commit21b59cc1d0928dc04dcf5eac24470aaa05013b2d (patch)
tree9888aa23aafea3907ce96eb958a06d4369dad86e /tests
parentde0513e7cb0593b1aa141e367ff4da7156d6110f (diff)
基本機能実装
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py12
-rw-r--r--tests/test_tools.py100
2 files changed, 101 insertions, 11 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 53a83b6..2b1df6a 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,11 +1 @@
-</content>
-</write_in_file><write_to_file>
-<path>tests/test_main.py</path>
-<content>"""Tests for ollama_web.main."""
-
-from ollama_web.main import main
-
-
-def test_main() -> None:
- """Test that main returns 0."""
- assert main() == 0 \ No newline at end of file
+"""Test package for ollama-web.""" \ No newline at end of file
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