mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-18 18:16:38 +03:00
test: strengthen user-path coverage and CI gates (#5308)
This commit is contained in:
@@ -84,10 +84,6 @@ class TestToolHintKnownTools:
|
||||
assert '"C:/Program Files/Git/project"' not in result
|
||||
assert '"' in result
|
||||
|
||||
def test_exec_short_command_unchanged(self):
|
||||
result = _hint([_tc("exec", {"command": "npm install typescript"})])
|
||||
assert result == "$ npm install typescript"
|
||||
|
||||
def test_exec_chained_commands_truncated_not_mid_path(self):
|
||||
"""Long chained commands should truncate preserving abbreviated paths."""
|
||||
cmd = "cd D:\\Documents\\GitHub\\project && npm run build && npm test"
|
||||
|
||||
@@ -241,24 +241,6 @@ async def test_file_edit_events_route_to_channel_capability(manager):
|
||||
channel._send_mock.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_typed_file_edit_event_routes_to_channel_capability(manager):
|
||||
channel = manager.channels["mock"]
|
||||
edits = [{"version": 1, "phase": "start", "path": "src/app.py"}]
|
||||
msg = outbound_message_for_event(
|
||||
channel="mock",
|
||||
chat_id="c1",
|
||||
event=ProgressEvent(file_edit_events=edits),
|
||||
)
|
||||
|
||||
await manager._send_once(channel, msg)
|
||||
|
||||
channel._file_edit_mock.assert_awaited_once_with(
|
||||
"c1", edits, msg.metadata
|
||||
)
|
||||
channel._send_mock.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_base_channel_file_edit_events_are_noop_safe():
|
||||
class _Plain(BaseChannel):
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
from nanobot.bus.outbound_events import (
|
||||
StreamDeltaEvent,
|
||||
StreamedResponseEvent,
|
||||
StreamEndEvent,
|
||||
outbound_message_for_event,
|
||||
)
|
||||
from nanobot.cli.commands import app
|
||||
from nanobot.config.schema import Config
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("streamed", [False, True])
|
||||
def test_interactive_agent_routes_a_complete_user_turn(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path,
|
||||
streamed: bool,
|
||||
) -> None:
|
||||
config = Config()
|
||||
config.agents.defaults.workspace = str(tmp_path / "workspace")
|
||||
seen: dict[str, object] = {}
|
||||
renderers: list[object] = []
|
||||
|
||||
class _Renderer:
|
||||
def __init__(self, **_kwargs: object) -> None:
|
||||
self.streamed = False
|
||||
self.header_printed = False
|
||||
self.deltas: list[str] = []
|
||||
self.ends: list[bool] = []
|
||||
self.closed = 0
|
||||
renderers.append(self)
|
||||
|
||||
async def on_delta(self, content: str) -> None:
|
||||
self.streamed = True
|
||||
self.deltas.append(content)
|
||||
|
||||
async def on_end(self, *, resuming: bool = False) -> None:
|
||||
self.ends.append(resuming)
|
||||
|
||||
async def close(self) -> None:
|
||||
self.closed += 1
|
||||
|
||||
def stop_for_input(self) -> None:
|
||||
return None
|
||||
|
||||
class _AgentLoop:
|
||||
channels_config = None
|
||||
|
||||
@classmethod
|
||||
def from_config(cls, _config, bus, **_kwargs):
|
||||
instance = cls(bus)
|
||||
seen["loop"] = instance
|
||||
return instance
|
||||
|
||||
def __init__(self, bus) -> None:
|
||||
self.bus = bus
|
||||
self.stopped = asyncio.Event()
|
||||
self.close_mcp_calls = 0
|
||||
|
||||
async def run(self) -> None:
|
||||
message = await self.bus.consume_inbound()
|
||||
seen["inbound"] = message
|
||||
if streamed:
|
||||
for event in (
|
||||
StreamDeltaEvent(content="hello "),
|
||||
StreamDeltaEvent(content="world"),
|
||||
StreamEndEvent(),
|
||||
StreamedResponseEvent(),
|
||||
):
|
||||
await self.bus.publish_outbound(
|
||||
outbound_message_for_event(
|
||||
channel=message.channel,
|
||||
chat_id=message.chat_id,
|
||||
event=event,
|
||||
content="hello world" if isinstance(event, StreamedResponseEvent) else None,
|
||||
)
|
||||
)
|
||||
else:
|
||||
await self.bus.publish_outbound(
|
||||
OutboundMessage(
|
||||
channel=message.channel,
|
||||
chat_id=message.chat_id,
|
||||
content="hello world",
|
||||
)
|
||||
)
|
||||
await self.stopped.wait()
|
||||
|
||||
def stop(self) -> None:
|
||||
self.stopped.set()
|
||||
|
||||
async def close_mcp(self) -> None:
|
||||
self.close_mcp_calls += 1
|
||||
|
||||
read_input = AsyncMock(side_effect=["hello nanobot", "exit"])
|
||||
print_response = MagicMock()
|
||||
monkeypatch.setattr("nanobot.cli.agent._load_runtime_config", lambda *_args: config)
|
||||
monkeypatch.setattr("nanobot.cli.agent.sync_workspace_templates", lambda *_args: None)
|
||||
monkeypatch.setattr("nanobot.cli.agent.is_default_workspace", lambda *_args: False)
|
||||
monkeypatch.setattr("nanobot.cli.agent._set_nanobot_logs", lambda *_args: None)
|
||||
monkeypatch.setattr("nanobot.cli.agent._model_display", lambda *_args: ("test-model", ""))
|
||||
monkeypatch.setattr("nanobot.cli.agent.consume_restart_notice_from_env", lambda: None)
|
||||
monkeypatch.setattr("nanobot.cli.agent.AgentLoop", _AgentLoop)
|
||||
monkeypatch.setattr("nanobot.cli.agent.StreamRenderer", _Renderer)
|
||||
monkeypatch.setattr("nanobot.providers.factory.make_provider", lambda *_args: object())
|
||||
monkeypatch.setattr(
|
||||
"nanobot.providers.image_generation.image_gen_provider_configs",
|
||||
lambda *_args: [],
|
||||
)
|
||||
monkeypatch.setattr("nanobot.cron.service.CronService", lambda *_args: object())
|
||||
monkeypatch.setattr("nanobot.cli.agent.signal.signal", lambda *_args: None)
|
||||
monkeypatch.setattr("nanobot.cli.terminal._init_prompt_session", lambda: None)
|
||||
monkeypatch.setattr("nanobot.cli.terminal._flush_pending_tty_input", lambda: None)
|
||||
monkeypatch.setattr("nanobot.cli.terminal._restore_terminal", lambda: None)
|
||||
monkeypatch.setattr("nanobot.cli.terminal._read_interactive_input_async", read_input)
|
||||
monkeypatch.setattr("nanobot.cli.terminal._print_agent_response", print_response)
|
||||
|
||||
result = runner.invoke(app, ["agent", "--session", "cli:journey"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
inbound = seen["inbound"]
|
||||
assert isinstance(inbound, InboundMessage)
|
||||
assert (inbound.channel, inbound.chat_id, inbound.content) == (
|
||||
"cli",
|
||||
"journey",
|
||||
"hello nanobot",
|
||||
)
|
||||
assert inbound.metadata == {"_wants_stream": True}
|
||||
loop = seen["loop"]
|
||||
assert isinstance(loop, _AgentLoop)
|
||||
assert loop.close_mcp_calls == 1
|
||||
assert len(renderers) == 1
|
||||
renderer = renderers[0]
|
||||
assert isinstance(renderer, _Renderer)
|
||||
if streamed:
|
||||
assert renderer.deltas == ["hello ", "world"]
|
||||
assert renderer.ends == [False]
|
||||
assert renderer.closed == 0
|
||||
print_response.assert_not_called()
|
||||
else:
|
||||
assert renderer.deltas == []
|
||||
assert renderer.closed == 1
|
||||
print_response.assert_called_once_with(
|
||||
"hello world",
|
||||
render_markdown=True,
|
||||
metadata={},
|
||||
)
|
||||
@@ -1231,27 +1231,6 @@ def test_openai_compat_provider_passes_model_through():
|
||||
assert provider.get_default_model() == "github-copilot/gpt-5.3-codex"
|
||||
|
||||
|
||||
def test_make_provider_uses_github_copilot_backend():
|
||||
from nanobot.config.schema import Config
|
||||
from nanobot.providers.factory import make_provider
|
||||
|
||||
config = Config.model_validate(
|
||||
{
|
||||
"agents": {
|
||||
"defaults": {
|
||||
"provider": "github-copilot",
|
||||
"model": "github-copilot/gpt-4.1",
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
|
||||
provider = make_provider(config)
|
||||
|
||||
assert provider.__class__.__name__ == "GitHubCopilotProvider"
|
||||
|
||||
|
||||
def test_openai_codex_proxy_config_affects_provider_and_signature():
|
||||
def config_with_proxy(proxy: str) -> Config:
|
||||
return Config.model_validate(
|
||||
|
||||
@@ -243,27 +243,6 @@ def test_workspace_override(tmp_path):
|
||||
assert bot._loop.workspace == custom_ws
|
||||
|
||||
|
||||
def test_sdk_make_provider_uses_github_copilot_backend():
|
||||
from nanobot.config.schema import Config
|
||||
from nanobot.providers.factory import make_provider
|
||||
|
||||
config = Config.model_validate(
|
||||
{
|
||||
"agents": {
|
||||
"defaults": {
|
||||
"provider": "github-copilot",
|
||||
"model": "github-copilot/gpt-4.1",
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
|
||||
provider = make_provider(config)
|
||||
|
||||
assert provider.__class__.__name__ == "GitHubCopilotProvider"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_custom_session_key(tmp_path):
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import ANY, AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
import nanobot.webui.forking as forking
|
||||
from nanobot.session.webui_turns import WEBUI_TITLE_METADATA_KEY
|
||||
|
||||
|
||||
def test_create_fork_rebuilds_missing_transcript_and_saves_clean_title(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
forked = SimpleNamespace(messages=[{"role": "user", "content": "hello"}], metadata={})
|
||||
manager = MagicMock()
|
||||
manager.fork_session_before_user_index.return_value = forked
|
||||
rebuild = MagicMock()
|
||||
marker = MagicMock()
|
||||
monkeypatch.setattr(forking.uuid, "uuid4", lambda: "fork-id")
|
||||
monkeypatch.setattr(forking, "fork_transcript_before_user_index", lambda *_args: False)
|
||||
monkeypatch.setattr(forking, "write_session_messages_as_transcript", rebuild)
|
||||
monkeypatch.setattr(forking, "append_fork_marker", marker)
|
||||
|
||||
result = forking.create_webui_chat_fork(
|
||||
manager,
|
||||
source_chat_id="source",
|
||||
before_user_index=2,
|
||||
title=" Useful fork ",
|
||||
)
|
||||
|
||||
assert result == ("fork-id", "websocket:fork-id")
|
||||
manager.fork_session_before_user_index.assert_called_once_with(
|
||||
"websocket:source",
|
||||
"websocket:fork-id",
|
||||
2,
|
||||
)
|
||||
rebuild.assert_called_once_with("websocket:fork-id", forked.messages)
|
||||
marker.assert_called_once_with("websocket:fork-id")
|
||||
assert forked.metadata[WEBUI_TITLE_METADATA_KEY] == "Useful fork"
|
||||
manager.save.assert_called_once_with(forked, fsync=True)
|
||||
|
||||
|
||||
def test_create_fork_rolls_back_session_and_transcript_together(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = MagicMock()
|
||||
manager.fork_session_before_user_index.return_value = SimpleNamespace(
|
||||
messages=[],
|
||||
metadata={},
|
||||
)
|
||||
delete_transcript = MagicMock()
|
||||
monkeypatch.setattr(forking.uuid, "uuid4", lambda: "failed-fork")
|
||||
monkeypatch.setattr(
|
||||
forking,
|
||||
"fork_transcript_before_user_index",
|
||||
MagicMock(side_effect=OSError("disk full")),
|
||||
)
|
||||
monkeypatch.setattr(forking, "delete_webui_transcript", delete_transcript)
|
||||
|
||||
with pytest.raises(OSError, match="disk full"):
|
||||
forking.create_webui_chat_fork(
|
||||
manager,
|
||||
source_chat_id="source",
|
||||
before_user_index=1,
|
||||
)
|
||||
|
||||
delete_transcript.assert_called_once_with("websocket:failed-fork")
|
||||
manager.delete_session.assert_called_once_with("websocket:failed-fork")
|
||||
|
||||
|
||||
def test_create_fork_stops_before_transcript_work_when_source_is_invalid(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = MagicMock()
|
||||
manager.fork_session_before_user_index.return_value = None
|
||||
fork_transcript = MagicMock()
|
||||
monkeypatch.setattr(forking, "fork_transcript_before_user_index", fork_transcript)
|
||||
|
||||
result = forking.create_webui_chat_fork(
|
||||
manager,
|
||||
source_chat_id="missing",
|
||||
before_user_index=1,
|
||||
)
|
||||
|
||||
assert result is None
|
||||
fork_transcript.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("envelope", "detail"),
|
||||
[
|
||||
({"source_chat_id": "bad/id", "before_user_index": 0}, "invalid source_chat_id"),
|
||||
({"source_chat_id": "source", "before_user_index": True}, "invalid before_user_index"),
|
||||
({"source_chat_id": "source", "before_user_index": -1}, "invalid before_user_index"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_fork_handler_rejects_invalid_protocol_input(
|
||||
envelope: dict[str, object],
|
||||
detail: str,
|
||||
) -> None:
|
||||
connection = object()
|
||||
channel = SimpleNamespace(
|
||||
send_webui_protocol_error=AsyncMock(),
|
||||
gateway=SimpleNamespace(session_manager=MagicMock()),
|
||||
)
|
||||
|
||||
await forking.handle_webui_fork_chat(channel, connection, envelope)
|
||||
|
||||
channel.send_webui_protocol_error.assert_awaited_once_with(connection, detail)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fork_handler_reports_unavailable_session_manager() -> None:
|
||||
connection = object()
|
||||
channel = SimpleNamespace(
|
||||
send_webui_protocol_error=AsyncMock(),
|
||||
gateway=SimpleNamespace(session_manager=None),
|
||||
)
|
||||
|
||||
await forking.handle_webui_fork_chat(
|
||||
channel,
|
||||
connection,
|
||||
{"source_chat_id": "source", "before_user_index": 0},
|
||||
)
|
||||
|
||||
channel.send_webui_protocol_error.assert_awaited_once_with(
|
||||
connection,
|
||||
"session_manager_unavailable",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fork_handler_maps_invalid_source_and_internal_failure_to_stable_errors(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
connection = object()
|
||||
channel = SimpleNamespace(
|
||||
send_webui_protocol_error=AsyncMock(),
|
||||
gateway=SimpleNamespace(session_manager=MagicMock()),
|
||||
logger=SimpleNamespace(warning=MagicMock()),
|
||||
)
|
||||
envelope = {"source_chat_id": "source", "before_user_index": 0}
|
||||
monkeypatch.setattr(forking, "create_webui_chat_fork", lambda *_args, **_kwargs: None)
|
||||
|
||||
await forking.handle_webui_fork_chat(channel, connection, envelope)
|
||||
channel.send_webui_protocol_error.assert_awaited_once_with(
|
||||
connection,
|
||||
"invalid fork source or index",
|
||||
)
|
||||
|
||||
channel.send_webui_protocol_error.reset_mock()
|
||||
monkeypatch.setattr(
|
||||
forking,
|
||||
"create_webui_chat_fork",
|
||||
MagicMock(side_effect=RuntimeError("broken transcript")),
|
||||
)
|
||||
await forking.handle_webui_fork_chat(channel, connection, envelope)
|
||||
|
||||
channel.logger.warning.assert_called_once_with("fork_chat failed: {}", ANY)
|
||||
channel.send_webui_protocol_error.assert_awaited_once_with(connection, "fork_chat_failed")
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
from urllib.parse import parse_qs, urlsplit
|
||||
|
||||
import pytest
|
||||
@@ -138,3 +139,62 @@ async def test_model_preset_mutation_routes(
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.body)["routed"] == function_name
|
||||
assert captured["query"] == expected_query
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("update_info", "expected"),
|
||||
[
|
||||
(None, {"updateAvailable": None}),
|
||||
(
|
||||
{
|
||||
"currentVersion": "1.2.0",
|
||||
"latestVersion": "1.3.0",
|
||||
"pypiUrl": "https://pypi.org/project/nanobot-ai/",
|
||||
},
|
||||
{
|
||||
"updateAvailable": {
|
||||
"currentVersion": "1.2.0",
|
||||
"latestVersion": "1.3.0",
|
||||
"pypiUrl": "https://pypi.org/project/nanobot-ai/",
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_version_check_route_returns_stable_payload(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
update_info: dict[str, str] | None,
|
||||
expected: dict[str, object],
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
"nanobot.webui.settings_routes.check_for_update",
|
||||
lambda: update_info,
|
||||
)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
response = await _router().dispatch(None, request, request.path)
|
||||
|
||||
assert response is not None
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.body) == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_version_check_route_enforces_auth_and_bounds_failures(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
check = MagicMock(side_effect=RuntimeError("upstream secret body"))
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.check_for_update", check)
|
||||
request = SimpleNamespace(path="/api/settings/version-check", headers=Headers())
|
||||
|
||||
unauthorized = await _router(authorized=False).dispatch(None, request, request.path)
|
||||
assert unauthorized is not None
|
||||
assert unauthorized.status_code == 401
|
||||
check.assert_not_called()
|
||||
|
||||
failed = await _router().dispatch(None, request, request.path)
|
||||
assert failed is not None
|
||||
assert failed.status_code == 500
|
||||
assert json.loads(failed.body) == {"error": "version check failed"}
|
||||
assert "upstream secret body" not in failed.body.decode()
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
import nanobot.webui.version_check as version_check
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _reset_version_cache(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(version_check, "_cache", (0.0, None))
|
||||
monkeypatch.setattr(version_check.time, "monotonic", lambda: 1_000.0)
|
||||
|
||||
|
||||
def _pypi_response(latest: object) -> MagicMock:
|
||||
response = MagicMock()
|
||||
response.json.return_value = {"info": {"version": latest}}
|
||||
return response
|
||||
|
||||
|
||||
def test_version_check_reports_only_a_newer_release_and_caches_it(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
get = MagicMock(return_value=_pypi_response("1.3.0"))
|
||||
monkeypatch.setattr(version_check, "__version__", "1.2.0")
|
||||
monkeypatch.setattr(version_check.httpx, "get", get)
|
||||
|
||||
expected = {
|
||||
"currentVersion": "1.2.0",
|
||||
"latestVersion": "1.3.0",
|
||||
"pypiUrl": "https://pypi.org/project/nanobot-ai/",
|
||||
}
|
||||
assert version_check.check_for_update() == expected
|
||||
assert version_check.check_for_update() == expected
|
||||
get.assert_called_once_with(
|
||||
"https://pypi.org/pypi/nanobot-ai/json",
|
||||
timeout=5.0,
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("latest", ["1.2.0", "1.1.9", "not-a-version", 42, None])
|
||||
def test_version_check_ignores_non_newer_or_invalid_releases(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
latest: object,
|
||||
) -> None:
|
||||
monkeypatch.setattr(version_check, "__version__", "1.2.0")
|
||||
monkeypatch.setattr(version_check.httpx, "get", lambda *_args, **_kwargs: _pypi_response(latest))
|
||||
|
||||
assert version_check.check_for_update() is None
|
||||
|
||||
|
||||
def test_version_check_treats_network_failure_as_best_effort(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
get = MagicMock(side_effect=TimeoutError("offline"))
|
||||
monkeypatch.setattr(version_check.httpx, "get", get)
|
||||
|
||||
assert version_check.check_for_update() is None
|
||||
|
||||
# Failures are not cached, so a later explicit check can recover.
|
||||
assert version_check.check_for_update() is None
|
||||
assert get.call_count == 2
|
||||
Reference in New Issue
Block a user