mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-31 08:13:11 +03:00
feat(usage): add unified provider usage backend
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import threading
|
||||
from collections.abc import Awaitable, Callable, Mapping
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
@@ -98,6 +99,45 @@ async def test_mcp_list_serializes_local_runtime_failure_snapshot(tmp_path) -> N
|
||||
assert snapshot_calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_usage_query_runs_off_the_event_loop(monkeypatch) -> None:
|
||||
calling_thread = threading.get_ident()
|
||||
worker_threads: list[int] = []
|
||||
|
||||
def usage_payload(**_kwargs):
|
||||
worker_threads.append(threading.get_ident())
|
||||
return {"days": []}
|
||||
|
||||
monkeypatch.setattr("nanobot.webui.settings_routes.settings_usage_payload", usage_payload)
|
||||
request = SimpleNamespace(path="/api/settings/usage", headers=Headers())
|
||||
|
||||
response = await _router().dispatch(None, request, request.path)
|
||||
|
||||
assert response is not None
|
||||
assert response.status_code == 200
|
||||
assert worker_threads and worker_threads[0] != calling_thread
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_settings_query_runs_off_the_event_loop(monkeypatch) -> None:
|
||||
calling_thread = threading.get_ident()
|
||||
worker_threads: list[int] = []
|
||||
router = _router()
|
||||
|
||||
def settings_response():
|
||||
worker_threads.append(threading.get_ident())
|
||||
return http_json_response({"ok": True})
|
||||
|
||||
monkeypatch.setattr(router, "_handle_settings", settings_response)
|
||||
request = SimpleNamespace(path="/api/settings", headers=Headers())
|
||||
|
||||
response = await router.dispatch(None, request, request.path)
|
||||
|
||||
assert response is not None
|
||||
assert response.status_code == 200
|
||||
assert worker_threads and worker_threads[0] != calling_thread
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mcp_reload_callback_is_bounded(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
|
||||
Reference in New Issue
Block a user