Fix Brave search auth and prompt variables
This commit is contained in:
@@ -72,6 +72,33 @@ class ConfigWebSearchTests(unittest.TestCase):
|
||||
self.assertEqual(config.page_extract.max_chars, 9000)
|
||||
self.assertTrue(config.page_extract.render_dynamic)
|
||||
|
||||
def test_web_search_accepts_brave_search_api_key_env(self) -> None:
|
||||
env = {"BRAVE_SEARCH_API_KEY": "search-env-key"}
|
||||
with patch.dict("os.environ", env, clear=True):
|
||||
config = _parse_ai_config(_raw_ai_config(), root=Path.cwd())
|
||||
|
||||
assert config is not None
|
||||
self.assertEqual(config.web_search.api_key, "search-env-key")
|
||||
|
||||
def test_brave_api_key_takes_precedence_over_search_alias(self) -> None:
|
||||
env = {
|
||||
"BRAVE_API_KEY": "primary-key",
|
||||
"BRAVE_SEARCH_API_KEY": "alias-key",
|
||||
}
|
||||
with patch.dict("os.environ", env, clear=True):
|
||||
config = _parse_ai_config(_raw_ai_config(), root=Path.cwd())
|
||||
|
||||
assert config is not None
|
||||
self.assertEqual(config.web_search.api_key, "primary-key")
|
||||
|
||||
def test_web_search_strips_bearer_prefix(self) -> None:
|
||||
env = {"BRAVE_SEARCH_API_KEY": " Bearer token-value "}
|
||||
with patch.dict("os.environ", env, clear=True):
|
||||
config = _parse_ai_config(_raw_ai_config(), root=Path.cwd())
|
||||
|
||||
assert config is not None
|
||||
self.assertEqual(config.web_search.api_key, "token-value")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
47
tests/test_prompts.py
Normal file
47
tests/test_prompts.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import unittest
|
||||
|
||||
from app.ai.prompts import PromptBook
|
||||
|
||||
|
||||
class PromptBookTests(unittest.TestCase):
|
||||
def test_renders_builtin_time_placeholders(self) -> None:
|
||||
prompt_book = PromptBook(Path("."))
|
||||
rendered = prompt_book._render_template(
|
||||
(
|
||||
"{{current_date}} {{current_time}} {{current_datetime}} "
|
||||
"{{current_weekday}} {{timezone}} {{utc_date}} {{utc_time}} "
|
||||
"{{utc_datetime}} {{unix_timestamp}}"
|
||||
),
|
||||
now=datetime(2026, 5, 19, 12, 34, 56, tzinfo=UTC),
|
||||
)
|
||||
|
||||
self.assertIn("2026-05-19", rendered)
|
||||
self.assertIn("2026-05-19T12:34:56+00:00", rendered)
|
||||
self.assertIn("Tuesday", rendered)
|
||||
self.assertIn("1779194096", rendered)
|
||||
|
||||
def test_unknown_placeholders_are_left_unchanged(self) -> None:
|
||||
prompt_book = PromptBook(Path("."))
|
||||
|
||||
self.assertEqual(
|
||||
prompt_book._render_template("Hello {{unknown_value}}"),
|
||||
"Hello {{unknown_value}}",
|
||||
)
|
||||
|
||||
def test_load_system_prompt_renders_files(self) -> None:
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
root = Path(temp_dir)
|
||||
(root / "system.md").write_text("Today is {{current_date}}", encoding="utf-8")
|
||||
|
||||
rendered = PromptBook(root).load_system_prompt()
|
||||
|
||||
self.assertNotIn("{{current_date}}", rendered)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -6,6 +6,7 @@ import httpx
|
||||
|
||||
from app.ai.web_search import (
|
||||
_format_error_response,
|
||||
BraveWebSearchClient,
|
||||
build_query,
|
||||
format_results,
|
||||
parse_response,
|
||||
@@ -13,7 +14,7 @@ from app.ai.web_search import (
|
||||
from app.config import AIWebSearchConfig
|
||||
|
||||
|
||||
class WebSearchTests(unittest.TestCase):
|
||||
class WebSearchTests(unittest.IsolatedAsyncioTestCase):
|
||||
def test_build_query_clamps_and_defaults(self) -> None:
|
||||
config = AIWebSearchConfig(api_key="key", count=5)
|
||||
|
||||
@@ -79,6 +80,43 @@ class WebSearchTests(unittest.TestCase):
|
||||
|
||||
self.assertIn("Rate limit exceeded", _format_error_response(response))
|
||||
|
||||
async def test_search_sends_subscription_token_header(self) -> None:
|
||||
seen_headers: httpx.Headers | None = None
|
||||
|
||||
async def handler(request: httpx.Request) -> httpx.Response:
|
||||
nonlocal seen_headers
|
||||
seen_headers = request.headers
|
||||
return httpx.Response(
|
||||
200,
|
||||
request=request,
|
||||
json={
|
||||
"query": {"original": "python"},
|
||||
"web": {"results": []},
|
||||
},
|
||||
)
|
||||
|
||||
client = BraveWebSearchClient(
|
||||
AIWebSearchConfig(api_key="test-key"),
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
|
||||
await client.search({"query": "python"})
|
||||
|
||||
assert seen_headers is not None
|
||||
self.assertEqual(seen_headers["x-subscription-token"], "test-key")
|
||||
|
||||
async def test_search_rejects_missing_subscription_token_before_request(self) -> None:
|
||||
async def handler(request: httpx.Request) -> httpx.Response:
|
||||
self.fail("request should not be sent without a subscription token")
|
||||
|
||||
client = BraveWebSearchClient(
|
||||
AIWebSearchConfig(api_key=""),
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "BRAVE_API_KEY or BRAVE_SEARCH_API_KEY"):
|
||||
await client.search({"query": "python"})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user