mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-17 01:26:40 +03:00
fix(channels): ignore confirmations after connect cancellation
This commit is contained in:
@@ -127,9 +127,16 @@ class FeishuConnectStore:
|
||||
session.last_error = str(exc)
|
||||
return _pending_payload(session)
|
||||
|
||||
session.domain = str(result.get("domain") or session.domain)
|
||||
status = result.get("status")
|
||||
if status == "succeeded":
|
||||
if self._sessions.get(session_id) is not session:
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"instance_id": session.instance_id,
|
||||
"status": "cancelled",
|
||||
"message": "Feishu connection cancelled.",
|
||||
}
|
||||
session.domain = str(result.get("domain") or session.domain)
|
||||
session.instance_id = feishu.save_registration_result(
|
||||
result,
|
||||
instance_id=session.instance_id,
|
||||
@@ -145,6 +152,7 @@ class FeishuConnectStore:
|
||||
"app_id": result.get("app_id"),
|
||||
}
|
||||
|
||||
session.domain = str(result.get("domain") or session.domain)
|
||||
if status == "failed":
|
||||
self._sessions.pop(session_id, None)
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.channels.feishu import runtime as feishu
|
||||
from nanobot.channels.feishu.connect import FeishuConnectStore
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_feishu_cancel_wins_over_inflight_confirmation(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
poll_started = threading.Event()
|
||||
release_poll = threading.Event()
|
||||
saved_results: list[dict[str, Any]] = []
|
||||
|
||||
monkeypatch.setattr(feishu, "_init_registration", lambda _domain: None)
|
||||
monkeypatch.setattr(
|
||||
feishu,
|
||||
"_begin_registration",
|
||||
lambda _domain: {
|
||||
"device_code": "device-cancel",
|
||||
"qr_url": "https://qr.example/cancel",
|
||||
"expire_in": 600,
|
||||
"interval": 2,
|
||||
},
|
||||
)
|
||||
|
||||
def fake_poll_registration_once(**_kwargs: Any) -> dict[str, str]:
|
||||
poll_started.set()
|
||||
assert release_poll.wait(timeout=5)
|
||||
return {
|
||||
"status": "succeeded",
|
||||
"domain": "feishu",
|
||||
"app_id": "late-app",
|
||||
"app_secret": "late-secret",
|
||||
}
|
||||
|
||||
def fake_save_registration_result(
|
||||
result: dict[str, Any],
|
||||
**_kwargs: Any,
|
||||
) -> str:
|
||||
saved_results.append(result)
|
||||
return "default"
|
||||
|
||||
monkeypatch.setattr(feishu, "poll_registration_once", fake_poll_registration_once)
|
||||
monkeypatch.setattr(feishu, "save_registration_result", fake_save_registration_result)
|
||||
|
||||
store = FeishuConnectStore()
|
||||
started = await store.handle("start", {})
|
||||
query = {"session_id": [started["session_id"]]}
|
||||
poll_task = asyncio.create_task(store.handle("poll", query))
|
||||
assert await asyncio.to_thread(poll_started.wait, 5)
|
||||
|
||||
cancelled = await store.handle("cancel", query)
|
||||
release_poll.set()
|
||||
completed = await poll_task
|
||||
|
||||
assert cancelled["status"] == "cancelled"
|
||||
assert completed["status"] == "cancelled"
|
||||
assert saved_results == []
|
||||
Reference in New Issue
Block a user