Files
DocsGPT/tests/agents/test_brave_tool.py
T
Alex 574f96341e refactor: rename the application package to docsgpt
The backend import package is now docsgpt, the name it will carry on PyPI;
application was far too generic to install into anyone's site-packages.
git mv plus a mechanical rewrite of every import, dotted string and path
reference: 734 Python files, the compose files, Dockerfile, workflows, docs,
setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage
config, .gitignore. Behaviour is unchanged.

Kept for one release:
- A top-level application package whose meta-path finder resolves
  application.x.y to the already-imported docsgpt.x.y object, so old imports
  and entry points (celery -A application.app.celery,
  uvicorn application.asgi:asgi_app) keep working with a FutureWarning.
- Celery registers every application.* task name as an alias of its
  docsgpt.* task on start-up, so messages queued by the previous release still
  run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries
  the previous release wrote are left unread instead of firing twice.

The backend image builds from the repository root (docker build -f
docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore
allow-lists docsgpt/ and application/ and keeps caches, local data, .env
files, the sample index files and the Dockerfile out. Compose and the image
workflows point at the new context.
2026-09-07 10:20:43 +01:00

133 lines
4.4 KiB
Python

"""Tests for docsgpt/agents/tools/brave.py"""
from unittest.mock import MagicMock, patch
import pytest
from docsgpt.agents.tools.brave import BraveSearchTool
@pytest.fixture
def tool():
return BraveSearchTool(config={"token": "test_api_key"})
@pytest.mark.unit
class TestBraveExecuteAction:
def test_unknown_action_raises(self, tool):
with pytest.raises(ValueError, match="Unknown action"):
tool.execute_action("invalid")
@patch("docsgpt.agents.tools.brave.requests.get")
def test_web_search_success(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {"web": {"results": [{"title": "Result"}]}}
mock_get.return_value = mock_resp
result = tool.execute_action("brave_web_search", query="python")
assert result["status_code"] == 200
assert "results" in result
assert "successfully" in result["message"]
call_kwargs = mock_get.call_args
assert call_kwargs[1]["headers"]["X-Subscription-Token"] == "test_api_key"
@patch("docsgpt.agents.tools.brave.requests.get")
def test_web_search_failure(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 429
mock_get.return_value = mock_resp
result = tool.execute_action("brave_web_search", query="test")
assert result["status_code"] == 429
assert "failed" in result["message"].lower()
@patch("docsgpt.agents.tools.brave.requests.get")
def test_image_search_success(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {"results": [{"url": "https://img.com/1.jpg"}]}
mock_get.return_value = mock_resp
result = tool.execute_action("brave_image_search", query="cats")
assert result["status_code"] == 200
assert "results" in result
@patch("docsgpt.agents.tools.brave.requests.get")
def test_image_search_failure(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 500
mock_get.return_value = mock_resp
result = tool.execute_action("brave_image_search", query="cats")
assert result["status_code"] == 500
@patch("docsgpt.agents.tools.brave.requests.get")
def test_count_capped_at_20(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {}
mock_get.return_value = mock_resp
tool.execute_action("brave_web_search", query="test", count=100)
params = mock_get.call_args[1]["params"]
assert params["count"] == 20
@patch("docsgpt.agents.tools.brave.requests.get")
def test_image_count_capped_at_100(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {}
mock_get.return_value = mock_resp
tool.execute_action("brave_image_search", query="test", count=500)
params = mock_get.call_args[1]["params"]
assert params["count"] == 100
@patch("docsgpt.agents.tools.brave.requests.get")
def test_freshness_param(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {}
mock_get.return_value = mock_resp
tool.execute_action("brave_web_search", query="news", freshness="pd")
params = mock_get.call_args[1]["params"]
assert params["freshness"] == "pd"
@patch("docsgpt.agents.tools.brave.requests.get")
def test_offset_capped(self, mock_get, tool):
mock_resp = MagicMock()
mock_resp.status_code = 200
mock_resp.json.return_value = {}
mock_get.return_value = mock_resp
tool.execute_action("brave_web_search", query="test", offset=100)
params = mock_get.call_args[1]["params"]
assert params["offset"] == 9
@pytest.mark.unit
class TestBraveMetadata:
def test_actions_metadata(self, tool):
meta = tool.get_actions_metadata()
assert len(meta) == 2
names = {a["name"] for a in meta}
assert "brave_web_search" in names
assert "brave_image_search" in names
def test_config_requirements(self, tool):
reqs = tool.get_config_requirements()
assert "token" in reqs
assert reqs["token"]["secret"] is True
assert reqs["token"]["required"] is True