Files
roboco/tests/unit/agent_sdk/test_secretary_driver.py
T
607e13f3dd feat(secretary): wire task name→id search as a Secretary tool (#304)
The GET /secretary/tasks?q= route (name→id resolution) shipped in wave 1
but no tool called it, so the Secretary could read a task only by UUID —
yet the CEO always refers to tasks by name. This adds search_tasks to
both runtimes (Claude SDK build_secretary_options + the grok
roboco-secretary MCP server) over a shared _do_search_tasks helper, so
'the task about X' resolves to concrete ids the CEO can then act on via
read_task or a control_task directive.

_call_backend gains query-param support and now returns the decoded JSON
(object or list) so the search route's list response flows through;
_do_search_tasks wraps matches under 'tasks' and passes error envelopes
straight through. Persona + RAG role doc updated (the RAG doc had
explicitly flagged this gap). Tests cover the helper and both wrappers.

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-03 20:07:28 +02:00

126 lines
3.9 KiB
Python

"""roboco.agent_sdk.secretary_driver — the backend-calling tool helpers."""
from __future__ import annotations
import json
from collections.abc import Callable
import httpx
import pytest
from roboco.agent_sdk import secretary_driver as sd
Handler = Callable[[httpx.Request], httpx.Response]
def _client(handler: Handler) -> httpx.AsyncClient:
return httpx.AsyncClient(transport=httpx.MockTransport(handler))
def _env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("ROBOCO_API_URL", "http://x:8000")
monkeypatch.setenv("ROBOCO_AGENT_ID", "secretary-uuid")
monkeypatch.setenv("ROBOCO_AGENT_ROLE", "secretary")
monkeypatch.setenv("ROBOCO_AGENT_TOKEN", "tok")
@pytest.mark.asyncio
async def test_read_state_calls_backend_with_auth(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_env(monkeypatch)
def handler(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/api/secretary/state"
assert request.headers["X-Agent-Token"] == "tok"
assert request.headers["X-Agent-Role"] == "secretary"
return httpx.Response(200, json={"goals": {}})
out = await sd._do_read_state(client=_client(handler))
assert out == {"goals": {}}
@pytest.mark.asyncio
async def test_read_task_calls_backend(monkeypatch: pytest.MonkeyPatch) -> None:
_env(monkeypatch)
def handler(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/api/secretary/tasks/abc"
return httpx.Response(200, json={"id": "abc"})
out = await sd._do_read_task("abc", client=_client(handler))
assert out["id"] == "abc"
@pytest.mark.asyncio
async def test_search_tasks_sends_query_and_wraps_list(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_env(monkeypatch)
def handler(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/api/secretary/tasks"
assert request.url.params["q"] == "x account"
assert request.url.params["limit"] == "20"
return httpx.Response(200, json=[{"id": "abc", "title": "X account"}])
out = await sd._do_search_tasks("x account", client=_client(handler))
assert out == {"tasks": [{"id": "abc", "title": "X account"}]}
@pytest.mark.asyncio
async def test_search_tasks_passes_error_through(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_env(monkeypatch)
out = await sd._do_search_tasks(
"q", client=_client(lambda _r: httpx.Response(422, text="too short"))
)
assert "tasks" not in out
assert out["error"] == "http_422"
@pytest.mark.asyncio
async def test_submit_directive_posts_kind_and_payload(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_env(monkeypatch)
def handler(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/api/secretary/directives"
body = json.loads(request.content)
assert body == {"kind": "announce", "payload": {"text": "hi"}}
return httpx.Response(201, json={"status": "pending"})
out = await sd._do_submit_directive(
"announce", {"text": "hi"}, client=_client(handler)
)
assert out["status"] == "pending"
@pytest.mark.asyncio
async def test_non_2xx_returns_error_dict(monkeypatch: pytest.MonkeyPatch) -> None:
_env(monkeypatch)
out = await sd._do_read_state(
client=_client(lambda _r: httpx.Response(500, text="boom"))
)
assert "error" in out
@pytest.mark.asyncio
async def test_network_error_returns_error_dict(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_env(monkeypatch)
def handler(_request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("down")
out = await sd._do_read_state(client=_client(handler))
assert out["error"] == "request_failed"
def test_text_result_shape() -> None:
result = sd._text_result({"a": 1})
assert result["content"][0]["type"] == "text"
assert json.loads(result["content"][0]["text"]) == {"a": 1}