fix(webui): bound history replay work

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-12 17:09:51 +08:00
1 parent 0c5d8443ec
commit 7ba60c2ef0
6 files changed
+784 -80

No files matched your search

@@ -5537,6 +5537,77 @@ def test_handle_webui_thread_get_returns_json(tmp_path, monkeypatch) -> None:
assert body["has_pending_tool_calls"] is False
@pytest.mark.asyncio
async def test_webui_thread_dispatch_runs_replay_off_event_loop(tmp_path, monkeypatch) -> None:
from urllib.parse import quote
from websockets.http11 import Request
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
key = "websocket:threaded-history"
append_transcript_object(
key,
{"event": "user", "chat_id": "threaded-history", "text": "hi"},
)
gateway = _basic_handler(MagicMock(), workspace_path=tmp_path)
gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0
encoded = quote(key, safe="")
request = Request(
f"/api/sessions/{encoded}/webui-thread",
Headers([("Authorization", "Bearer tok")]),
)
original = gateway.http._handle_webui_thread_get
def slow_replay(*args: Any, **kwargs: Any):
time.sleep(0.15)
return original(*args, **kwargs)
monkeypatch.setattr(gateway.http, "_handle_webui_thread_get", slow_replay)
request_task = asyncio.create_task(
gateway.http._dispatch_session_routes(request, request.path)
)
await asyncio.sleep(0.03)
assert request_task.done() is False
response = await request_task
assert response is not None
assert response.status_code == 200
@pytest.mark.asyncio
async def test_webui_thread_diagnostics_hash_session_key(tmp_path, monkeypatch) -> None:
from urllib.parse import quote
from websockets.http11 import Request
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_PAGE_RECORDS", 1)
key = "websocket:private-session-name"
for event in (
{"event": "user", "chat_id": "private-session-name", "text": "hi"},
{"event": "message", "chat_id": "private-session-name", "text": "hello"},
{"event": "turn_end", "chat_id": "private-session-name"},
):
append_transcript_object(key, event)
gateway = _basic_handler(MagicMock(), workspace_path=tmp_path)
gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0
gateway.http._log = MagicMock()
encoded = quote(key, safe="")
request = Request(
f"/api/sessions/{encoded}/webui-thread",
Headers([("Authorization", "Bearer tok")]),
)
response = await gateway.http._dispatch_session_routes(request, request.path)
assert response is not None
assert response.status_code == 200
call = gateway.http._log.warning.call_args
assert call is not None
assert key not in " ".join(str(value) for value in call.args)
@pytest.mark.asyncio
async def test_handle_session_context_get_reads_detached_session() -> None:
from urllib.parse import quote