feat(task-5): scope matrix context state per room
This commit is contained in:
parent
03160a3b37
commit
c11c8ecfbf
7 changed files with 189 additions and 72 deletions
|
|
@ -95,13 +95,18 @@ async def test_update_settings_supports_toggle_skill_and_setters():
|
|||
async def test_add_saved_session_appends_named_entries():
|
||||
store = PrototypeStateStore()
|
||||
|
||||
await store.add_saved_session("usr-matrix-@alice:example.org", "alpha")
|
||||
await store.add_saved_session(
|
||||
"usr-matrix-@alice:example.org",
|
||||
"alpha",
|
||||
source_context_id="ctx-room-1",
|
||||
)
|
||||
await store.add_saved_session("usr-matrix-@alice:example.org", "beta")
|
||||
|
||||
sessions = await store.list_saved_sessions("usr-matrix-@alice:example.org")
|
||||
|
||||
assert [session["name"] for session in sessions] == ["alpha", "beta"]
|
||||
assert all("created_at" in session for session in sessions)
|
||||
assert sessions[0]["source_context_id"] == "ctx-room-1"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
@ -122,24 +127,58 @@ async def test_list_saved_sessions_returns_copy():
|
|||
async def test_get_last_tokens_used_defaults_to_zero():
|
||||
store = PrototypeStateStore()
|
||||
|
||||
assert await store.get_last_tokens_used("usr-matrix-@alice:example.org") == 0
|
||||
assert await store.get_last_tokens_used_for_context("ctx-room-1") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_last_tokens_used_persists_value():
|
||||
async def test_live_tokens_used_are_scoped_per_context():
|
||||
store = PrototypeStateStore()
|
||||
|
||||
await store.set_last_tokens_used("usr-matrix-@alice:example.org", 321)
|
||||
await store.set_last_tokens_used_for_context("ctx-room-1", 321)
|
||||
await store.set_last_tokens_used_for_context("ctx-room-2", 654)
|
||||
|
||||
assert await store.get_last_tokens_used("usr-matrix-@alice:example.org") == 321
|
||||
assert await store.get_last_tokens_used_for_context("ctx-room-1") == 321
|
||||
assert await store.get_last_tokens_used_for_context("ctx-room-2") == 654
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_current_session_roundtrip():
|
||||
async def test_current_session_roundtrip_is_scoped_per_context():
|
||||
store = PrototypeStateStore()
|
||||
|
||||
assert await store.get_current_session("usr-matrix-@alice:example.org") is None
|
||||
assert await store.get_current_session_for_context("ctx-room-1") is None
|
||||
assert await store.get_current_session_for_context("ctx-room-2") is None
|
||||
|
||||
await store.set_current_session("usr-matrix-@alice:example.org", "session-1")
|
||||
await store.set_current_session_for_context("ctx-room-1", "session-1")
|
||||
await store.set_current_session_for_context("ctx-room-2", "session-2")
|
||||
|
||||
assert await store.get_current_session("usr-matrix-@alice:example.org") == "session-1"
|
||||
assert await store.get_current_session_for_context("ctx-room-1") == "session-1"
|
||||
assert await store.get_current_session_for_context("ctx-room-2") == "session-2"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_clear_current_session_removes_only_target_context():
|
||||
store = PrototypeStateStore()
|
||||
|
||||
await store.set_current_session_for_context("ctx-room-1", "session-1")
|
||||
await store.set_current_session_for_context("ctx-room-2", "session-2")
|
||||
|
||||
await store.clear_current_session_for_context("ctx-room-1")
|
||||
|
||||
assert await store.get_current_session_for_context("ctx-room-1") is None
|
||||
assert await store.get_current_session_for_context("ctx-room-2") == "session-2"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_saved_sessions_remain_user_scoped_separate_from_live_context_state():
|
||||
store = PrototypeStateStore()
|
||||
|
||||
await store.set_current_session_for_context("ctx-room-1", "room-session")
|
||||
await store.set_last_tokens_used_for_context("ctx-room-1", 77)
|
||||
await store.add_saved_session("usr-matrix-@alice:example.org", "alpha")
|
||||
|
||||
sessions = await store.list_saved_sessions("usr-matrix-@alice:example.org")
|
||||
|
||||
assert [session["name"] for session in sessions] == ["alpha"]
|
||||
assert all(isinstance(session["created_at"], str) for session in sessions)
|
||||
assert await store.get_current_session_for_context("ctx-room-1") == "room-session"
|
||||
assert await store.get_last_tokens_used_for_context("ctx-room-1") == 77
|
||||
|
|
|
|||
|
|
@ -197,9 +197,10 @@ async def test_real_platform_client_get_or_create_user_uses_local_state():
|
|||
@pytest.mark.asyncio
|
||||
async def test_real_platform_client_send_message_uses_chat_bound_client():
|
||||
agent_api = FakeAgentApiFactory()
|
||||
prototype_state = PrototypeStateStore()
|
||||
client = RealPlatformClient(
|
||||
agent_api=agent_api,
|
||||
prototype_state=PrototypeStateStore(),
|
||||
prototype_state=prototype_state,
|
||||
platform="matrix",
|
||||
)
|
||||
|
||||
|
|
@ -215,6 +216,7 @@ async def test_real_platform_client_send_message_uses_chat_bound_client():
|
|||
assert agent_api.instances["chat-7"].chat_id == "chat-7"
|
||||
assert agent_api.instances["chat-7"].calls == ["hello"]
|
||||
assert agent_api.instances["chat-7"].connect_calls == 1
|
||||
assert await prototype_state.get_last_tokens_used_for_context("chat-7") == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue