Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Test list_all_rooms visibility against real Postgres

Petr Bauchd6004f345309
diff --git a/core/tests/switch_core/bridges/agent/api/test_room_visibility.py b/core/tests/switch_core/bridges/agent/api/test_room_visibility.py
index b83d1f353..eeeba1827 100644
--- a/core/tests/switch_core/bridges/agent/api/test_room_visibility.py
+++ b/core/tests/switch_core/bridges/agent/api/test_room_visibility.py
@@ -3,14 +3,12 @@
from __future__ import annotations
from types import SimpleNamespace
-from typing import Any
from unittest.mock import AsyncMock
import pytest
from fastapi import HTTPException
from switch_core.bridges.agent.api.handlers import list_participants
-from switch_core.bridges.agent.protocol.agent_core import AgentCore
async def test_participants_of_a_room_the_agent_is_not_in_are_refused() -> None:
@@ -24,45 +22,3 @@ async def test_participants_of_a_room_the_agent_is_not_in_are_refused() -> None:
assert exc.value.status_code == 403
protocol.list_participants.assert_not_awaited()
-
-
-class _FakeSession:
- async def __aenter__(self) -> _FakeSession:
- return self
-
- async def __aexit__(self, *exc: object) -> bool:
- return False
-
- async def get(self, model: Any, ident: str) -> Any:
- return SimpleNamespace(id=ident)
-
-
-def _room(room_id: str, *, owner_id: str, read_visibility: str) -> Any:
- return SimpleNamespace(
- id=room_id,
- owner_id=owner_id,
- read_visibility=read_visibility,
- write_visibility="private",
- )
-
-
-async def test_list_all_rooms_hides_private_rooms_of_other_users() -> None:
- own = _room("own", owner_id="alice", read_visibility="private")
- public = _room("public", owner_id="bob", read_visibility="public")
- joined = _room("joined", owner_id="bob", read_visibility="private")
- hidden = _room("hidden", owner_id="bob", read_visibility="private")
-
- svc = object.__new__(AgentCore)
- svc.session_factory = _FakeSession # type: ignore[assignment]
- svc.agent_store = SimpleNamespace( # type: ignore[assignment]
- get=AsyncMock(return_value=SimpleNamespace(id="agent-1", owner_id="alice"))
- )
- svc.user_store = SimpleNamespace(administers=AsyncMock(return_value=False)) # type: ignore[assignment]
- svc.room_store = SimpleNamespace( # type: ignore[assignment]
- get_rooms_for_agent=AsyncMock(return_value=[joined]),
- get_all=AsyncMock(return_value=[own, public, joined, hidden]),
- )
-
- rooms = await svc.list_all_rooms("agent-1")
-
- assert [r.id for r in rooms] == ["own", "public", "joined"]
diff --git a/core/tests/switch_core/bridges/agent/test_list_all_rooms_paging.py b/core/tests/switch_core/bridges/agent/test_list_all_rooms_paging.py
index 5a6349d61..6619e0800 100644
--- a/core/tests/switch_core/bridges/agent/test_list_all_rooms_paging.py
+++ b/core/tests/switch_core/bridges/agent/test_list_all_rooms_paging.py
@@ -7,6 +7,7 @@ import base64
import uuid
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
+from unittest.mock import AsyncMock
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
@@ -40,7 +41,14 @@ async def _rooms(session_factory: async_sessionmaker[AsyncSession]) -> list[str]
def _list(session_factory: async_sessionmaker[AsyncSession], **kwargs: object):
- protocol = SimpleNamespace(session_factory=session_factory, room_store=RoomStore())
+ # The owner administers the workspace, so every room is visible: what is
+ # under test here is the paging; who sees which room is tested in
+ # test_room_store_agent_visibility.py.
+ protocol = SimpleNamespace(
+ session_factory=session_factory,
+ room_store=RoomStore(),
+ _resolve_acting_identity=AsyncMock(return_value=(None, "owner-1", True)),
+ )
defaults: dict[str, object] = {
"include_archived": False,
"name_contains": None,
diff --git a/core/tests/switch_core/db/stores/test_room_store_agent_visibility.py b/core/tests/switch_core/db/stores/test_room_store_agent_visibility.py
new file mode 100644
index 000000000..547262ed8
--- /dev/null
+++ b/core/tests/switch_core/db/stores/test_room_store_agent_visibility.py
@@ -0,0 +1,117 @@
+"""An agent sees the rooms its owner may read and the rooms it is in, and
+no other private room, page by page. Real Postgres."""
+
+from __future__ import annotations
+
+from datetime import UTC, datetime, timedelta
+
+from sqlalchemy import insert
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.db.models import Room, User, room_agents
+from switch_core.db.stores.room_store import RoomStore
+from tests.switch_core.gateway.agent_route_harness import add_agent
+
+_T0 = datetime(2026, 1, 1, tzinfo=UTC)
+
+
+async def _room(
+ session: AsyncSession, name: str, owner_id: str, read_visibility: str, minute: int
+) -> Room:
+ room = Room(
+ transport_room_id=f"!{name}:test",
+ name=name,
+ description="",
+ owner_id=owner_id,
+ read_visibility=read_visibility,
+ write_visibility="private",
+ created_at=_T0 + timedelta(minutes=minute),
+ )
+ session.add(room)
+ await session.flush()
+ return room
+
+
+async def _page_names(
+ store: RoomStore,
+ session: AsyncSession,
+ *,
+ reader_id: str | None,
+ reader_is_admin: bool,
+ agent_id: str,
+ limit: int,
+) -> list[str]:
+ rooms = await store.page_all(
+ session,
+ include_archived=False,
+ name_contains=None,
+ limit=limit,
+ after=None,
+ reader_id=reader_id,
+ reader_is_admin=reader_is_admin,
+ member_agent_id=agent_id,
+ )
+ return [r.name for r in rooms]
+
+
+async def test_an_agent_sees_its_owners_rooms_and_its_own_and_no_other(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ store = RoomStore()
+ async with session_factory() as session:
+ alice = User(name="alice", email="[email protected]", role="user")
+ bob = User(name="bob", email="[email protected]", role="user")
+ session.add_all([alice, bob])
+ await session.flush()
+
+ hidden_first = await _room(session, "hidden-first", bob.id, "private", 0)
+ own = await _room(session, "own", alice.id, "private", 1)
+ public = await _room(session, "public", bob.id, "public", 2)
+ joined = await _room(session, "joined", bob.id, "private", 3)
+ hidden = await _room(session, "hidden", bob.id, "private", 4)
+
+ agent = await add_agent(session, name="alices-agent", owner_id=alice.id)
+ await session.execute(
+ insert(room_agents), [{"room_id": joined.id, "agent_id": agent.id}]
+ )
+ await session.flush()
+
+ # Rooms the agent may not see do not use up a page.
+ assert await _page_names(
+ store,
+ session,
+ reader_id=alice.id,
+ reader_is_admin=False,
+ agent_id=agent.id,
+ limit=3,
+ ) == ["own", "public", "joined"]
+
+ # An agent with no owner sees public rooms and the rooms it is in.
+ assert await _page_names(
+ store,
+ session,
+ reader_id=None,
+ reader_is_admin=False,
+ agent_id=agent.id,
+ limit=10,
+ ) == ["public", "joined"]
+
+ assert await _page_names(
+ store,
+ session,
+ reader_id=alice.id,
+ reader_is_admin=True,
+ agent_id=agent.id,
+ limit=10,
+ ) == ["hidden-first", "own", "public", "joined", "hidden"]
+
+ unpaged = await store.list_visible_to_agent(
+ session,
+ reader_id=alice.id,
+ reader_is_admin=False,
+ member_agent_id=agent.id,
+ include_archived=False,
+ )
+ assert {r.id for r in unpaged} == {own.id, public.id, joined.id}
+ assert hidden_first.id not in {r.id for r in unpaged}
+ assert hidden.id not in {r.id for r in unpaged}