feat(usage): add unified provider usage backend

This commit is contained in:
chengyongru
2026-08-25 01:22:25 +08:00
committed by chengyongru
parent 8bb3828487
commit 2ac802b2d5
24 changed files with 1605 additions and 808 deletions
+40
View File
@@ -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,