Fix real client chat cache compatibility
This commit is contained in:
parent
414a8645bd
commit
730ea70f78
2 changed files with 77 additions and 7 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from core.protocol import SettingsAction
|
||||
|
|
@ -45,6 +47,18 @@ class FakeAgentApiFactory:
|
|||
return chat_api
|
||||
|
||||
|
||||
class LegacyAgentApi:
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[str] = []
|
||||
self.last_tokens_used = 0
|
||||
|
||||
async def send_message(self, text: str):
|
||||
self.calls.append(text)
|
||||
yield FakeChunk(text[:2])
|
||||
yield FakeChunk(text[2:])
|
||||
self.last_tokens_used = 7
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_platform_client_get_or_create_user_uses_local_state():
|
||||
client = RealPlatformClient(
|
||||
|
|
@ -85,6 +99,26 @@ async def test_real_platform_client_send_message_uses_chat_bound_client():
|
|||
assert agent_api.instances["chat-7"].connect_calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_platform_client_works_with_legacy_agent_api_without_for_chat():
|
||||
legacy_api = LegacyAgentApi()
|
||||
client = RealPlatformClient(
|
||||
agent_api=legacy_api,
|
||||
prototype_state=PrototypeStateStore(),
|
||||
platform="matrix",
|
||||
)
|
||||
|
||||
result = await client.send_message("@alice:example.org", "chat-legacy", "hello")
|
||||
|
||||
assert result == MessageResponse(
|
||||
message_id="@alice:example.org",
|
||||
response="hello",
|
||||
tokens_used=7,
|
||||
finished=True,
|
||||
)
|
||||
assert legacy_api.calls == ["hello"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_platform_client_reuses_cached_chat_client():
|
||||
agent_api = FakeAgentApiFactory()
|
||||
|
|
@ -102,6 +136,26 @@ async def test_real_platform_client_reuses_cached_chat_client():
|
|||
assert agent_api.instances["chat-1"].connect_calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_platform_client_creates_chat_client_atomically_for_concurrent_requests():
|
||||
agent_api = FakeAgentApiFactory()
|
||||
client = RealPlatformClient(
|
||||
agent_api=agent_api,
|
||||
prototype_state=PrototypeStateStore(),
|
||||
platform="matrix",
|
||||
)
|
||||
|
||||
results = await asyncio.gather(
|
||||
client.send_message("@alice:example.org", "chat-1", "hello"),
|
||||
client.send_message("@alice:example.org", "chat-1", "again"),
|
||||
)
|
||||
|
||||
assert [result.response for result in results] == ["hello", "again"]
|
||||
assert agent_api.created_chat_ids == ["chat-1"]
|
||||
assert agent_api.instances["chat-1"].connect_calls == 1
|
||||
assert agent_api.instances["chat-1"].calls == ["hello", "again"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_platform_client_creates_distinct_clients_per_chat():
|
||||
agent_api = FakeAgentApiFactory()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue