agenthub/backend/tests/test_driver_nanobot.py

149 lines
5.8 KiB
Python

"""nanobot is stricter than a generic OpenAI server, so the driver obeys it.
The rules come from soucecode/nanobot/nanobot/api/server.py, and tests/fake_nanobot.py
enforces them: one user message per request, and only the model the server
reports. A driver that drifts away from real nanobot fails here.
"""
from __future__ import annotations
import pytest
from agenthub.adapters.base import AgentTarget, DriverError, Invocation
from agenthub.adapters.http_openai import HttpOpenAIDriver, NanobotDriver
from tests.conftest import BASE, add_agent, task_status, wait_for
from tests.fake_nanobot import FakeNanobot
def target(base_url: str, model: str = "nanobot", **config) -> AgentTarget:
return AgentTarget(
id="agt_1",
name="nano",
driver="nanobot",
base_url=base_url,
model=model,
config=dict(config),
)
async def ask(driver, where: AgentTarget, prompt: str = "ship it", context=None):
events: list[tuple[str, dict]] = []
async def emit(kind: str, payload: dict) -> None:
events.append((kind, payload))
invocation = Invocation(
task_id="task-1",
prompt=prompt,
target=where,
session_key="run-9",
context=dict(context or {}),
)
try:
return await driver.invoke(invocation, emit), events
finally:
await driver.close()
async def test_instructions_never_become_a_second_message():
"""nanobot keeps the conversation itself, so it rejects a messages array."""
with FakeNanobot(reply="done") as bot:
result, events = await ask(
NanobotDriver(),
target(bot.base_url),
context={
"instructions": "be terse",
"history": [{"role": "user", "content": "earlier question"}],
},
)
assert result.ok, result.error
messages = bot.last_body["messages"]
assert len(messages) == 1 and messages[0]["role"] == "user"
folded = messages[0]["content"]
assert "be terse" in folded and "earlier question" in folded
assert folded.rstrip().endswith("ship it")
assert any(kind == "delta" for kind, _ in events), "the answer must stream"
async def test_model_comes_from_the_server_never_from_a_guess():
with FakeNanobot(model="claude-sonnet-4.5") as bot:
result, _ = await ask(NanobotDriver(), target(bot.base_url)) # the row says nanobot
assert result.ok, result.error
assert bot.model != "nanobot"
assert bot.last_body["model"] == "claude-sonnet-4.5"
async def test_the_model_key_is_dropped_when_it_cannot_be_verified():
"""Better to omit the key than to send an id the server would 400 on."""
with FakeNanobot(models_endpoint=False) as bot:
result, _ = await ask(NanobotDriver(), target(bot.base_url))
assert result.ok, result.error
assert "model" not in bot.last_body
async def test_the_conversation_is_pinned_on_the_session_key():
with FakeNanobot(reply="ok") as bot:
result, _ = await ask(NanobotDriver(), target(bot.base_url))
assert result.ok, result.error
assert bot.last_body["session_id"] == "run-9"
assert bot.sessions == ["run-9"]
async def test_either_base_url_spelling_reaches_the_same_endpoint():
"""nanobot hands you a base ending in /v1; rows may hold either spelling."""
with FakeNanobot(reply="ok") as bot:
origin = bot.base_url.rsplit("/v1", 1)[0]
with_v1, _ = await ask(NanobotDriver(), target(bot.base_url))
plain, _ = await ask(NanobotDriver(), target(origin))
probed = await NanobotDriver().probe(target(origin))
assert with_v1.ok and plain.ok
assert len(bot.bodies) == 2 and all("messages" in body for body in bot.bodies)
assert probed["ok"] is True and probed["models"] == [bot.model]
async def test_the_api_key_travels_as_a_bearer_token():
with FakeNanobot(api_key="s3cret", reply="ok") as bot:
where = target(bot.base_url)
where.auth = {"token": "s3cret"}
granted, _ = await ask(NanobotDriver(), where)
with pytest.raises(DriverError) as refused:
await ask(NanobotDriver(), target(bot.base_url))
assert granted.ok, granted.error
assert "401" in str(refused.value) and refused.value.retryable is False
async def test_a_plain_openai_compatible_agent_keeps_its_own_convention():
"""The nanobot quirks must not leak into the generic driver."""
with FakeNanobot(model="gpt-4o-mini") as bot:
where = target(bot.base_url, model="gpt-4o-mini")
where.driver = "openai_compatible"
result, _ = await ask(HttpOpenAIDriver(), where)
assert result.ok, result.error
assert bot.last_body["model"] == "gpt-4o-mini"
assert bot.last_body["stream_options"] == {"include_usage": True}
def test_a_registered_nanobot_answers_a_collaboration_step(hub):
"""The whole plane: task pump, then driver, then a nanobot-shaped endpoint.
The context carries member instructions, which is what used to build a
two-message request that real nanobot answers with 400.
"""
with FakeNanobot(model="kimi-k2", reply="nanobot says") as bot:
add_agent(hub, "nano-live", driver="nanobot", base_url=bot.base_url, model="nanobot")
task = hub.post(
f"{BASE}/tasks",
json={
"agent_id": "nano-live",
"prompt": "summarise the plan",
"context": {"instructions": "answer in one line"},
},
).json()
wait_for(lambda: task_status(hub, task["id"]) in ("completed", "failed"), timeout=20)
done = hub.get(f"{BASE}/tasks/" + task['id']).json()
assert done["status"] == "completed", done["error"]
assert done["result"].startswith("nanobot says")
assert len(bot.last_body["messages"]) == 1
assert "answer in one line" in bot.last_body["messages"][0]["content"]
assert bot.last_body["model"] == "kimi-k2"