| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223 |
- """Tests for ChattoClient.
- Since the client now goes through generated ConnectRPC service stubs, tests
- mock at the service-client method level with ``AsyncMock`` rather than at the
- HTTP transport layer. This exercises the request-building and response-parsing
- paths without any real network traffic.
- """
- from __future__ import annotations
- import datetime as dt
- from unittest.mock import AsyncMock
- import pytest
- from chattolib._pb.chatto.admin.v1 import (
- diagnostics_pb2,
- room_layout_pb2,
- )
- from chattolib._pb.chatto.admin.v1 import (
- server_pb2 as admin_server_pb2,
- )
- from chattolib._pb.chatto.api.v1 import (
- account_pb2,
- asset_uploads_pb2,
- attachments_pb2,
- message_search_pb2,
- messages_pb2,
- notification_preferences_pb2,
- notifications_pb2,
- presence_pb2,
- push_notifications_pb2,
- reactions_pb2,
- read_state_pb2,
- roles_pb2,
- room_directory_pb2,
- room_timeline_pb2,
- rooms_pb2,
- threads_pb2,
- user_service_pb2,
- viewer_pb2,
- voice_calls_pb2,
- )
- from chattolib._pb.chatto.discovery.v1 import server_pb2 as discovery_server_pb2
- from chattolib.client import ChattoClient
- from chattolib.types import (
- ImageFitMode,
- ImageTransformOptions,
- MessageSearchGroupBy,
- MessageSearchOrder,
- MessageSearchScope,
- MessageSearchState,
- NotificationLevel,
- PresenceStatus,
- RoomKind,
- ThreadRootAuthorFilter,
- TimeFormat,
- )
- BASE = "https://chat.chatto.run"
- @pytest.fixture
- def client():
- return ChattoClient(token="test-token", base_url=BASE)
- def _mock_method(client: ChattoClient, service: str, method: str, response):
- """Replace a service-client method with an AsyncMock returning ``response``."""
- svc = getattr(client._svc, service)
- setattr(svc, method, AsyncMock(return_value=response))
- return getattr(svc, method)
- # --- Server discovery ---------------------------------------------------
- async def test_get_server(client):
- resp = discovery_server_pb2.GetServerResponse()
- resp.profile.name = "Chatto HQ"
- resp.profile.version = "0.4.2"
- resp.login.direct_registration_enabled = True
- resp.login.authorize_url = "/oauth"
- _mock_method(client, "server_discovery", "get_server", resp)
- async with client:
- profile, login = await client.get_server()
- assert profile.name == "Chatto HQ"
- assert profile.version == "0.4.2"
- assert login.direct_registration_enabled is True
- assert login.authorize_url == "/oauth"
- async def test_list_neighborhood_servers(client):
- resp = discovery_server_pb2.ListNeighborhoodServersResponse()
- s1 = resp.servers.add()
- s1.origin = "https://neighbor.example"
- s1.profile.name = "Neighbor"
- s1.profile.version = "0.5.0"
- s1.profile.logo_url = "/logos/neighbor.png"
- s1.direct_neighbor = True
- s2 = resp.servers.add()
- s2.origin = "https://referred.example"
- s2.profile.name = "Referred"
- s2.direct_neighbor = False
- s2.recommended_by_origins.append("https://neighbor.example")
- resp.refreshed_at.FromJsonString("2026-09-26T12:00:00Z")
- mocked = _mock_method(client, "server_discovery", "list_neighborhood_servers", resp)
- async with client:
- page = await client.list_neighborhood_servers()
- mocked.assert_awaited_once()
- assert [s.origin for s in page.servers] == [
- "https://neighbor.example",
- "https://referred.example",
- ]
- assert page.servers[0].profile is not None
- assert page.servers[0].profile.name == "Neighbor"
- assert page.servers[0].profile.logo_url == "/logos/neighbor.png"
- assert page.servers[0].direct_neighbor is True
- assert page.servers[1].direct_neighbor is False
- assert page.servers[1].recommended_by_origins == ["https://neighbor.example"]
- assert page.refreshed_at == dt.datetime(2026, 9, 26, 12, 0, tzinfo=dt.UTC)
- # --- Viewer -------------------------------------------------------------
- async def test_me(client):
- resp = viewer_pb2.GetViewerResponse()
- resp.user.profile.id = "u1"
- resp.user.profile.login = "alice"
- resp.user.profile.display_name = "Alice"
- resp.user.profile.presence_status = "PRESENCE_STATUS_ONLINE"
- resp.user.has_verified_email = True
- resp.user.settings.timezone = "UTC"
- resp.user.settings.time_format = "TIME_FORMAT_24_HOUR"
- resp.user.has_password = True
- _mock_method(client, "viewer", "get_viewer", resp)
- async with client:
- user = await client.me()
- assert user.id == "u1"
- assert user.login == "alice"
- assert user.display_name == "Alice"
- assert user.presence_status is PresenceStatus.ONLINE
- # --- Room directory ----------------------------------------------------
- async def test_list_rooms(client):
- resp = room_directory_pb2.ListRoomsResponse()
- entry = resp.rooms.add()
- entry.room.id = "r1"
- entry.room.kind = "ROOM_KIND_CHANNEL"
- entry.room.name = "general"
- entry.room.group_id = "g1"
- entry.viewer_state.is_member = True
- entry.viewer_state.has_unread = False
- _mock_method(client, "room_directory", "list_rooms", resp)
- async with client:
- rooms = await client.list_rooms()
- assert len(rooms) == 1
- assert rooms[0].room is not None
- assert rooms[0].room.name == "general"
- assert rooms[0].room.kind is RoomKind.CHANNEL
- assert rooms[0].viewer_state.is_member is True
- async def test_list_rooms_dm_keeps_deleted_participants(client):
- # Since Chatto 0.5.0-beta.10, a DM's member_user_ids keeps participants
- # whose accounts were deleted; the client must surface the full list
- # (resolution to a User then legitimately fails for the deleted one).
- resp = room_directory_pb2.ListRoomsResponse()
- entry = resp.rooms.add()
- entry.room.id = "r-dm"
- entry.room.kind = "ROOM_KIND_DM"
- entry.room.name = "me & u2"
- entry.viewer_state.is_member = True
- entry.member_user_ids.extend(["u1", "u-deleted"])
- _mock_method(client, "room_directory", "list_rooms", resp)
- async with client:
- rooms = await client.list_rooms()
- assert len(rooms) == 1
- assert rooms[0].room is not None
- assert rooms[0].room.kind is RoomKind.DM
- assert rooms[0].member_user_ids == ["u1", "u-deleted"]
- # --- Messages ----------------------------------------------------------
- async def test_post_message(client):
- resp = messages_pb2.CreateMessageResponse()
- resp.message.id = "e1"
- resp.message.room_id = "r1"
- resp.message.actor_id = "u1"
- resp.message.body = "Hello!"
- _mock_method(client, "messages", "create_message", resp)
- async with client:
- message = await client.post_message("r1", "Hello!")
- assert message.id == "e1"
- assert message.body == "Hello!"
- assert message.room_id == "r1"
- async def test_update_message(client):
- resp = messages_pb2.UpdateMessageResponse()
- resp.message.id = "e1"
- resp.message.room_id = "r1"
- resp.message.actor_id = "u1"
- resp.message.body = "edited"
- resp.message.updated_at.FromJsonString("2026-01-01T00:01:00Z")
- m = _mock_method(client, "messages", "update_message", resp)
- async with client:
- msg = await client.update_message("r1", "e1", body="edited")
- assert msg.body == "edited"
- assert msg.updated_at is not None
- # verify the request had the optional body field explicitly set
- call_args = m.call_args
- req = call_args.args[0]
- assert isinstance(req, messages_pb2.UpdateMessageRequest)
- assert req.HasField("body")
- assert req.body == "edited"
- async def test_delete_message(client):
- _mock_method(client, "messages", "delete_message", messages_pb2.DeleteMessageResponse())
- async with client:
- assert await client.delete_message("r1", "e1") is None
- async def test_add_reaction(client):
- resp = reactions_pb2.AddReactionResponse(added=True)
- _mock_method(client, "messages", "add_reaction", resp)
- async with client:
- assert await client.add_reaction("r1", "e1", "thumbsup") is True
- async def test_remove_reaction(client):
- resp = reactions_pb2.RemoveReactionResponse(removed=True)
- _mock_method(client, "messages", "remove_reaction", resp)
- async with client:
- assert await client.remove_reaction("r1", "e1", "thumbsup") is True
- # --- Room lifecycle ---------------------------------------------------
- async def test_create_room(client):
- resp = rooms_pb2.CreateRoomResponse()
- resp.room.id = "r1"
- resp.room.kind = "ROOM_KIND_CHANNEL"
- resp.room.name = "general"
- resp.room.group_id = "g1"
- _mock_method(client, "rooms", "create_room", resp)
- async with client:
- room = await client.create_room("general", "g1")
- assert room.id == "r1"
- assert room.name == "general"
- assert room.group_id == "g1"
- async def test_archive_room(client):
- resp = rooms_pb2.ArchiveRoomResponse()
- resp.room.id = "r1"
- resp.room.name = "general"
- resp.room.archived = True
- _mock_method(client, "rooms", "archive_room", resp)
- async with client:
- room = await client.archive_room("r1")
- assert room.archived is True
- async def test_remove_user(client):
- resp = rooms_pb2.RemoveUserResponse()
- mock = _mock_method(client, "rooms", "remove_user", resp)
- async with client:
- await client.remove_user("r1", "u2", "spam")
- await client.remove_user(
- "r1", "u2", "spam", suspension_expires_at=dt.datetime.fromtimestamp(0, dt.UTC)
- )
- await client.remove_user("r1", "u2", "spam", suspend_indefinitely=True)
- calls = [c.args[0] for c in mock.call_args_list]
- assert calls[0].WhichOneof("suspension") is None
- assert calls[1].WhichOneof("suspension") == "suspension_expires_at"
- assert calls[2].WhichOneof("suspension") == "suspend_indefinitely"
- async def test_remove_user_rejects_both_suspensions(client):
- _mock_method(client, "rooms", "remove_user", rooms_pb2.RemoveUserResponse())
- async with client:
- with pytest.raises(ValueError, match="not both"):
- await client.remove_user(
- "r1",
- "u2",
- "spam",
- suspension_expires_at=dt.datetime.fromtimestamp(0, dt.UTC),
- suspend_indefinitely=True,
- )
- async def test_lift_suspension(client):
- _mock_method(client, "rooms", "lift_suspension", rooms_pb2.LiftSuspensionResponse())
- async with client:
- assert await client.lift_suspension("r1", "u2", "reinstated") is None
- async def test_list_suspensions(client):
- resp = rooms_pb2.ListSuspensionsResponse()
- s = resp.suspensions.add()
- s.id = "s1"
- s.room_id = "r1"
- s.user_id = "u2"
- _mock_method(client, "rooms", "list_suspensions", resp)
- async with client:
- suspensions, _ = await client.list_suspensions(room_id="r1")
- assert [x.id for x in suspensions] == ["s1"]
- async def test_start_dm(client):
- resp = rooms_pb2.StartDMResponse()
- resp.room.id = "r1"
- resp.room.kind = "ROOM_KIND_DM"
- _mock_method(client, "rooms", "start_dm", resp)
- async with client:
- room = await client.start_dm(["u1", "u2"])
- assert room.id == "r1"
- assert room.kind is RoomKind.DM
- # --- Room timeline ----------------------------------------------------
- async def test_get_room_events(client):
- resp = room_timeline_pb2.GetRoomEventsResponse()
- event = resp.page.events.add()
- event.id = "e1"
- event.actor_id = "u1"
- event.created_at.FromJsonString("2026-01-01T00:00:00Z")
- event.message_posted.message.id = "e1"
- event.message_posted.message.room_id = "r1"
- event.message_posted.message.actor_id = "u1"
- event.message_posted.message.body = "hi"
- event.message_posted.message.created_at.FromJsonString("2026-01-01T00:00:00Z")
- resp.page.start_cursor = "cA"
- resp.page.end_cursor = "cB"
- resp.page.includes.users["u1"].id = "u1"
- resp.page.includes.users["u1"].login = "alice"
- resp.page.includes.users["u1"].display_name = "Alice"
- _mock_method(client, "rooms", "get_room_events", resp)
- async with client:
- page = await client.get_room_events("r1", limit=50)
- assert len(page.events) == 1
- assert page.events[0].kind == "message_posted"
- assert page.events[0].message is not None
- assert page.events[0].message.body == "hi"
- assert page.users_by_id["u1"].login == "alice"
- async def test_mark_room_as_read(client):
- resp = read_state_pb2.MarkRoomAsReadResponse()
- resp.last_read_at.FromJsonString("2026-01-01T00:00:00Z")
- resp.previous_last_read_at.FromJsonString("2025-12-31T00:00:00Z")
- _mock_method(client, "rooms", "mark_room_as_read", resp)
- async with client:
- last, previous = await client.mark_room_as_read("r1", up_to_event_id="e1")
- assert last is not None
- assert previous is not None
- # --- Profile / account ------------------------------------------------
- async def test_update_user_profile(client):
- resp = user_service_pb2.UpdateUserProfileResponse()
- resp.user.id = "u1"
- resp.user.login = "newname"
- resp.user.display_name = "New Name"
- resp.user.bio = "# Hi"
- resp.user.presence_status = "PRESENCE_STATUS_ONLINE"
- _mock_method(client, "user", "update_user_profile", resp)
- async with client:
- user = await client.update_user_profile(
- "u1", login="newname", display_name="New Name", bio="# Hi"
- )
- assert user.id == "u1"
- assert user.login == "newname"
- assert user.display_name == "New Name"
- assert user.bio == "# Hi"
- async def test_update_profile(client):
- """Self-service update resolves the caller id via the viewer, then
- delegates to the new UserService.UpdateUserProfile wire call."""
- viewer = viewer_pb2.GetViewerResponse()
- viewer.user.profile.id = "u1"
- viewer.user.profile.login = "alice"
- viewer.user.profile.display_name = "Alice"
- _mock_method(client, "viewer", "get_viewer", viewer)
- resp = user_service_pb2.UpdateUserProfileResponse()
- resp.user.id = "u1"
- resp.user.login = "newname"
- resp.user.display_name = "New Name"
- resp.user.presence_status = "PRESENCE_STATUS_ONLINE"
- update = _mock_method(client, "user", "update_user_profile", resp)
- async with client:
- user = await client.update_profile(login="newname", display_name="New Name")
- assert user.login == "newname"
- assert user.display_name == "New Name"
- (req,) = update.await_args.args
- assert req.user_id == "u1"
- async def test_set_presence(client):
- resp = presence_pb2.SetPresenceResponse()
- resp.status = "PRESENCE_STATUS_ONLINE"
- _mock_method(client, "account", "set_presence", resp)
- async with client:
- result = await client.set_presence(PresenceStatus.ONLINE)
- assert result is PresenceStatus.ONLINE
- async def test_set_presence_offline_rejected(client):
- async with client:
- with pytest.raises(ValueError):
- await client.set_presence(PresenceStatus.OFFLINE)
- async def test_get_presence_preference(client):
- resp = presence_pb2.GetPresencePreferenceResponse()
- resp.preference.status = "PRESENCE_STATUS_OFFLINE"
- resp.preference.revision = "rev-1"
- _mock_method(client, "account", "get_presence_preference", resp)
- async with client:
- pref = await client.get_presence_preference()
- assert pref is not None
- assert pref.status is PresenceStatus.OFFLINE
- assert pref.revision == "rev-1"
- async def test_get_presence_preference_absent(client):
- # No preference saved yet: the response carries no sub-message.
- _mock_method(
- client, "account", "get_presence_preference", presence_pb2.GetPresencePreferenceResponse()
- )
- async with client:
- assert await client.get_presence_preference() is None
- async def test_set_presence_preference(client):
- resp = presence_pb2.SetPresencePreferenceResponse()
- resp.preference.status = "PRESENCE_STATUS_DO_NOT_DISTURB"
- resp.preference.revision = "rev-2"
- m = _mock_method(client, "account", "set_presence_preference", resp)
- async with client:
- pref = await client.set_presence_preference(
- PresenceStatus.DO_NOT_DISTURB, expected_revision="rev-1"
- )
- assert pref.status is PresenceStatus.DO_NOT_DISTURB
- assert pref.revision == "rev-2"
- req = m.await_args.args[0]
- assert req.status == presence_pb2.PresenceStatus.Value("PRESENCE_STATUS_DO_NOT_DISTURB")
- assert req.expected_revision == "rev-1"
- async def test_set_presence_preference_offline_allowed(client):
- # Unlike set_presence, OFFLINE is a valid saved choice ("Appear Offline").
- resp = presence_pb2.SetPresencePreferenceResponse()
- resp.preference.status = "PRESENCE_STATUS_OFFLINE"
- _mock_method(client, "account", "set_presence_preference", resp)
- async with client:
- assert (await client.set_presence_preference(PresenceStatus.OFFLINE)).status is (
- PresenceStatus.OFFLINE
- )
- async def test_set_presence_preference_unspecified_rejected(client):
- async with client:
- with pytest.raises(ValueError):
- await client.set_presence_preference(PresenceStatus.UNSPECIFIED)
- async def test_refresh_presence(client):
- resp = presence_pb2.RefreshPresenceResponse()
- resp.preference.status = "PRESENCE_STATUS_ONLINE"
- resp.preference.revision = "rev-3"
- _mock_method(client, "account", "refresh_presence", resp)
- async with client:
- pref = await client.refresh_presence()
- assert pref is not None
- assert pref.status is PresenceStatus.ONLINE
- assert pref.revision == "rev-3"
- async def test_update_settings(client):
- resp = account_pb2.UpdateSettingsResponse()
- resp.settings.timezone = "Europe/Rome"
- resp.settings.time_format = "TIME_FORMAT_24_HOUR"
- _mock_method(client, "account", "update_settings", resp)
- async with client:
- settings = await client.update_settings(
- timezone="Europe/Rome", time_format=TimeFormat.HOUR_24
- )
- assert settings.timezone == "Europe/Rome"
- assert settings.time_format is TimeFormat.HOUR_24
- # --- Threads ----------------------------------------------------------
- async def test_list_followed_threads(client):
- resp = threads_pb2.ListFollowedThreadsResponse()
- ft = resp.threads.add()
- ft.room.id = "r1"
- ft.room.name = "general"
- ft.root_message.id = "e1"
- ft.root_message.room_id = "r1"
- ft.root_message.actor_id = "u1"
- ft.root_message.body = "root"
- ft.root_message.created_at.FromJsonString("2026-01-01T00:00:00Z")
- ft.thread.thread_root_event_id = "e1"
- ft.thread.reply_count = 3
- ft.thread.last_reply_at.FromJsonString("2026-01-02T00:00:00Z")
- ft.thread.viewer_state.is_following = True
- ft.thread.viewer_state.has_unread_replies = False
- resp.page.total_count = 1
- resp.page.has_more = False
- mocked = _mock_method(client, "threads", "list_followed_threads", resp)
- async with client:
- page = await client.list_followed_threads()
- await client.list_followed_threads(
- limit=5,
- offset=5,
- include_direct_message_threads=True,
- unread_only=True,
- root_author_filter=ThreadRootAuthorFilter.VIEWER,
- )
- await client.list_followed_threads(
- root_author_filter=ThreadRootAuthorFilter.OTHERS,
- )
- assert page.page.total_count == 1
- assert page.threads[0].thread is not None
- assert page.threads[0].thread.reply_count == 3
- assert page.threads[0].thread.is_following is True
- calls = [c.args[0] for c in mocked.call_args_list]
- assert calls[0].include_direct_message_threads is False
- assert calls[0].unread_only is False
- assert calls[0].root_author_filter == 0
- assert calls[1].include_direct_message_threads is True
- assert calls[1].unread_only is True
- assert calls[1].root_author_filter == threads_pb2.ThreadRootAuthorFilter.Value(
- "THREAD_ROOT_AUTHOR_FILTER_VIEWER"
- )
- assert calls[2].root_author_filter == threads_pb2.ThreadRootAuthorFilter.Value(
- "THREAD_ROOT_AUTHOR_FILTER_OTHERS"
- )
- assert calls[1].page.limit == 5
- assert calls[1].page.offset == 5
- async def test_follow_and_unfollow_thread(client):
- _mock_method(client, "threads", "follow_thread", threads_pb2.FollowThreadResponse())
- _mock_method(client, "threads", "unfollow_thread", threads_pb2.UnfollowThreadResponse())
- async with client:
- assert await client.follow_thread("r1", "e1") is None
- assert await client.unfollow_thread("r1", "e1") is None
- # --- Notifications ----------------------------------------------------
- async def test_list_notification_occurrences(client):
- resp = notifications_pb2.ListNotificationOccurrencesResponse()
- occ = resp.occurrences.add()
- occ.id = "n1"
- occ.created_at.FromJsonString("2026-01-01T00:00:00Z")
- occ.actor.id = "u1"
- occ.actor.login = "alice"
- occ.actor.display_name = "Alice"
- occ.unread = True
- sig = occ.signal.direct_mention_received
- sig.message.room.id = "r1"
- sig.message.room.name = "general"
- sig.message.event_id = "e1"
- resp.page.total_count = 1
- resp.unread_count = 1
- _mock_method(client, "notifications", "list_notification_occurrences", resp)
- async with client:
- page = await client.list_notification_occurrences()
- assert page.page.total_count == 1
- assert page.unread_count == 1
- assert page.occurrences[0].signal is not None
- assert page.occurrences[0].signal.kind == "direct_mention_received"
- assert page.occurrences[0].signal.message is not None
- assert page.occurrences[0].signal.message.event_id == "e1"
- assert page.occurrences[0].signal.message.room is not None
- assert page.occurrences[0].signal.message.room.name == "general"
- async def test_mark_and_delete_notification_occurrence(client):
- read_resp = notifications_pb2.MarkNotificationReadResponse()
- read_resp.occurrence.id = "n1"
- read_resp.occurrence.unread = False
- _mock_method(client, "notifications", "mark_notification_read", read_resp)
- del_resp = notifications_pb2.DeleteNotificationOccurrenceResponse(deleted=True)
- _mock_method(client, "notifications", "delete_notification_occurrence", del_resp)
- all_resp = notifications_pb2.DeleteAllNotificationOccurrencesResponse(deleted_count=3)
- _mock_method(client, "notifications", "delete_all_notification_occurrences", all_resp)
- async with client:
- occ = await client.mark_notification_read("n1")
- assert occ is not None and occ.unread is False
- assert await client.delete_notification_occurrence("n1") is True
- assert await client.delete_all_notification_occurrences() == 3
- async def test_notification_preference(client):
- resp = notification_preferences_pb2.UpdateNotificationPreferenceResponse()
- resp.preference.level = "NOTIFICATION_LEVEL_MUTED"
- resp.preference.effective_level = "NOTIFICATION_LEVEL_MUTED"
- _mock_method(
- client,
- "notification_prefs",
- "update_room_notification_preference",
- resp,
- )
- async with client:
- pref = await client.update_room_notification_preference("r1", NotificationLevel.MUTED)
- assert pref.level is NotificationLevel.MUTED
- assert pref.effective_level is NotificationLevel.MUTED
- # --- Push -------------------------------------------------------------
- async def test_subscribe_push(client):
- _mock_method(client, "push", "subscribe", push_notifications_pb2.SubscribeResponse())
- async with client:
- assert await client.subscribe_push("https://push", "k", "a") is True
- # --- Voice calls ------------------------------------------------------
- async def test_active_calls(client):
- resp = voice_calls_pb2.ListActiveCallsResponse()
- call = resp.calls.add()
- call.call_id = "c1"
- call.room.id = "r1"
- call.room.name = "voice"
- p = call.participants.add()
- p.user.id = "u1"
- p.user.login = "alice"
- p.user.display_name = "Alice"
- _mock_method(client, "voice_calls", "list_active_calls", resp)
- async with client:
- calls = await client.list_active_calls()
- assert len(calls) == 1
- assert calls[0].call_id == "c1"
- assert calls[0].room is not None
- assert calls[0].room.name == "voice"
- # --- Assets -----------------------------------------------------------
- async def test_get_asset_with_thumbnail(client):
- resp = attachments_pb2.GetAssetResponse()
- resp.asset.id = "a1"
- resp.asset.filename = "pic.png"
- resp.asset.content_type = "image/png"
- resp.asset.size = 1024
- resp.asset.asset_url.url = "https://cdn/pic.png"
- mock = _mock_method(client, "assets", "get_asset", resp)
- async with client:
- asset = await client.get_asset(
- "r1",
- "a1",
- thumbnail=ImageTransformOptions(128, 128, ImageFitMode.COVER),
- )
- assert asset is not None
- assert asset.filename == "pic.png"
- assert asset.asset_url is not None
- assert asset.asset_url.url == "https://cdn/pic.png"
- # verify the request encoded thumbnail options
- req = mock.call_args.args[0]
- assert req.thumbnail.width == 128
- assert req.thumbnail.height == 128
- # --- Roles ------------------------------------------------------------
- async def test_list_roles(client):
- resp = roles_pb2.ListRolesResponse()
- r1 = resp.roles.add()
- r1.name = "everyone"
- r1.display_name = "Everyone"
- r1.is_system = True
- r1.position = 0
- r2 = resp.roles.add()
- r2.name = "admin"
- r2.display_name = "Admin"
- r2.position = 10
- _mock_method(client, "roles", "list_roles", resp)
- async with client:
- roles = await client.list_roles()
- assert [r.name for r in roles] == ["everyone", "admin"]
- assert roles[0].is_system is True
- # --- Asset uploads ---------------------------------------------------
- async def test_asset_upload_end_to_end(client, tmp_path):
- import hashlib
- payload = b"hello world" * 100
- file = tmp_path / "greet.txt"
- file.write_bytes(payload)
- sha = hashlib.sha256(payload).hexdigest()
- create_resp = asset_uploads_pb2.CreateUploadResponse()
- create_resp.upload.upload_id = "up1"
- create_resp.upload.room_id = "r1"
- create_resp.upload.status = "ASSET_UPLOAD_STATUS_OPEN"
- create_resp.upload.committed_offset = 0
- create_resp.upload.size = len(payload)
- create_resp.upload.max_chunk_size = 4096
- create_resp.upload.sha256 = sha
- create_mock = _mock_method(client, "asset_uploads", "create_upload", create_resp)
- chunk_resp = asset_uploads_pb2.UploadChunkResponse()
- chunk_resp.upload.upload_id = "up1"
- chunk_resp.upload.room_id = "r1"
- chunk_resp.upload.status = "ASSET_UPLOAD_STATUS_OPEN"
- chunk_resp.upload.committed_offset = len(payload)
- chunk_resp.upload.size = len(payload)
- chunk_resp.upload.max_chunk_size = 4096
- chunk_resp.upload.sha256 = sha
- _mock_method(client, "asset_uploads", "upload_chunk", chunk_resp)
- complete_resp = asset_uploads_pb2.CompleteUploadResponse()
- complete_resp.upload.upload_id = "up1"
- complete_resp.upload.room_id = "r1"
- complete_resp.upload.status = "ASSET_UPLOAD_STATUS_COMPLETED"
- complete_resp.upload.committed_offset = len(payload)
- complete_resp.upload.size = len(payload)
- complete_resp.upload.sha256 = sha
- complete_resp.upload.asset_id = "a1"
- complete_resp.asset.id = "a1"
- complete_resp.asset.filename = "greet.txt"
- complete_resp.asset.content_type = "text/plain"
- complete_resp.asset.size = len(payload)
- _mock_method(client, "asset_uploads", "complete_upload", complete_resp)
- async with client:
- asset = await client.upload_attachment("r1", file, content_type="text/plain")
- assert asset.id == "a1"
- assert asset.filename == "greet.txt"
- create_req = create_mock.call_args.args[0]
- assert create_req.sha256 == sha
- assert create_req.size == len(payload)
- # --- External identities --------------------------------------------
- # --- Admin (structural smoke tests) ----------------------------------
- async def test_admin_list_room_groups(client):
- resp = room_layout_pb2.ListRoomGroupsResponse()
- g = resp.groups.add()
- g.id = "g1"
- g.name = "General"
- room_item = g.items.add()
- room_item.room.id = "r1"
- room_item.room.kind = "ROOM_KIND_CHANNEL"
- room_item.room.name = "chat"
- link_item = g.items.add()
- link_item.sidebar_link.id = "sl1"
- link_item.sidebar_link.label = "Docs"
- link_item.sidebar_link.url = "https://x"
- g.can_create_room = True
- _mock_method(client, "admin_room_layout", "list_room_groups", resp)
- async with client:
- groups = await client.admin_list_room_groups()
- assert len(groups) == 1
- assert groups[0].name == "General"
- assert groups[0].rooms[0].id == "r1"
- assert groups[0].sidebar_links[0].label == "Docs"
- assert groups[0].can_create_room is True
- async def test_admin_create_sidebar_link(client):
- resp = room_layout_pb2.CreateSidebarLinkResponse()
- resp.sidebar_link.id = "sl1"
- resp.sidebar_link.label = "Docs"
- resp.sidebar_link.url = "https://x"
- _mock_method(client, "admin_room_layout", "create_sidebar_link", resp)
- async with client:
- result = await client.admin_create_sidebar_link("g1", "Docs", "https://x")
- assert result == {"id": "sl1", "label": "Docs", "url": "https://x"}
- async def test_admin_move_room_group(client):
- resp = room_layout_pb2.MoveRoomGroupResponse()
- g = resp.groups.add()
- g.id = "g1"
- g.name = "General"
- mock = _mock_method(client, "admin_room_layout", "move_room_group", resp)
- async with client:
- groups = await client.admin_move_room_group("g1", before_group_id="g2")
- assert [g.id for g in groups] == ["g1"]
- sent = mock.call_args.args[0]
- assert sent.group_id == "g1"
- assert sent.before_group_id == "g2"
- async def test_admin_move_room_group_to_end(client):
- resp = room_layout_pb2.MoveRoomGroupResponse()
- mock = _mock_method(client, "admin_room_layout", "move_room_group", resp)
- async with client:
- await client.admin_move_room_group("g1")
- sent = mock.call_args.args[0]
- assert sent.group_id == "g1"
- assert not sent.HasField("before_group_id")
- async def test_admin_move_sidebar_item(client):
- resp = room_layout_pb2.MoveSidebarItemResponse()
- resp.group.id = "g1"
- resp.group.name = "General"
- mock = _mock_method(client, "admin_room_layout", "move_sidebar_item", resp)
- async with client:
- group = await client.admin_move_sidebar_item(
- ("sidebar_link", "sl1"),
- "g1",
- before=("room", "r1"),
- )
- assert group.id == "g1"
- sent = mock.call_args.args[0]
- assert sent.item.sidebar_link_id == "sl1"
- assert sent.before.room_id == "r1"
- assert sent.group_id == "g1"
- async def test_admin_get_system_info_livekit(client):
- resp = diagnostics_pb2.GetSystemInfoResponse()
- resp.projections_available = True
- resp.livekit.enabled = True
- resp.livekit.configured = True
- resp.livekit.url = "wss://livekit.example"
- resp.livekit.api_key = "AP1234"
- resp.livekit.separate_webhook_key = True
- resp.livekit.webhook_url = "https://chat.example/api/admin/livekit/webhook"
- resp.livekit.connection_state = diagnostics_pb2.ADMIN_LIVE_KIT_CONNECTION_STATE_OK
- resp.livekit.insecure_url = False
- _mock_method(client, "admin_diagnostics", "get_system_info", resp)
- async with client:
- data = await client.admin_get_system_info()
- lk = data["livekit"]
- assert lk["enabled"] is True
- assert lk["configured"] is True
- assert lk["url"] == "wss://livekit.example"
- assert lk["apiKey"] == "AP1234"
- assert lk["separateWebhookKey"] is True
- # enums render as their full name in Connect JSON (use_integers_for_enums=False)
- assert lk["connectionState"] == "ADMIN_LIVE_KIT_CONNECTION_STATE_OK"
- assert (
- diagnostics_pb2.AdminLiveKitConnectionState.Value(
- "ADMIN_LIVE_KIT_CONNECTION_STATE_UNREACHABLE"
- )
- == 3
- )
- assert "connectionError" not in lk # empty string omitted by MessageToDict
- async def test_admin_update_server_config(client):
- resp = admin_server_pb2.UpdateServerConfigResponse()
- resp.config.server_name = "MyServer"
- resp.config.motd = "hi"
- resp.public_profile.name = "MyServer"
- resp.public_profile.version = "0.4.2"
- _mock_method(client, "admin_server", "update_server_config", resp)
- async with client:
- config, profile = await client.admin_update_server_config(server_name="MyServer", motd="hi")
- assert config.server_name == "MyServer"
- assert profile.version == "0.4.2"
- # --- Neighbors (0.5.0-alpha.3) -----------------------------------------
- async def test_list_neighbors_public(client):
- resp = discovery_server_pb2.ListNeighborsResponse()
- resp.origins.extend(["https://a.example", "https://b.example"])
- _mock_method(client, "server_discovery", "list_neighbors", resp)
- async with client:
- origins = await client.list_neighbors()
- assert origins == ["https://a.example", "https://b.example"]
- async def test_admin_neighbor_crud(client):
- list_resp = admin_server_pb2.ListNeighborsResponse()
- n = list_resp.neighbors.add()
- n.id = "n1"
- n.origin = "https://a.example"
- n.revision = "rev1"
- _mock_method(client, "admin_server", "list_neighbors", list_resp)
- get_resp = admin_server_pb2.GetNeighborResponse()
- get_resp.neighbor.id = "n1"
- get_resp.neighbor.origin = "https://a.example"
- get_resp.neighbor.revision = "rev1"
- _mock_method(client, "admin_server", "get_neighbor", get_resp)
- create_resp = admin_server_pb2.CreateNeighborResponse()
- create_resp.neighbor.id = "n2"
- create_resp.neighbor.origin = "https://b.example"
- create_resp.neighbor.revision = "rev2"
- _mock_method(client, "admin_server", "create_neighbor", create_resp)
- update_resp = admin_server_pb2.UpdateNeighborResponse()
- update_resp.neighbor.id = "n2"
- update_resp.neighbor.origin = "https://b2.example"
- update_resp.neighbor.revision = "rev3"
- _mock_method(client, "admin_server", "update_neighbor", update_resp)
- del_resp = admin_server_pb2.DeleteNeighborResponse()
- _mock_method(client, "admin_server", "delete_neighbor", del_resp)
- async with client:
- listed = await client.admin_list_neighbors()
- assert [x.origin for x in listed] == ["https://a.example"]
- got = await client.admin_get_neighbor("n1")
- assert got is not None and got.revision == "rev1"
- created = await client.admin_create_neighbor("https://b.example")
- assert created.id == "n2" and created.revision == "rev2"
- updated = await client.admin_update_neighbor("n2", "https://b2.example", "rev2")
- assert updated.origin == "https://b2.example" and updated.revision == "rev3"
- await client.admin_delete_neighbor("n2", "rev3")
- async def test_update_settings_share_timezone(client):
- resp = account_pb2.UpdateSettingsResponse()
- resp.settings.timezone = "Europe/Berlin"
- resp.settings.share_timezone = True
- _mock_method(client, "account", "update_settings", resp)
- async with client:
- settings = await client.update_settings(timezone="Europe/Berlin", share_timezone=True)
- assert settings.timezone == "Europe/Berlin"
- assert settings.share_timezone is True
- def test_snake_case_is_acronym_aware():
- """``types._snake_case`` must not chop acronym runs (same bug class as
- the protoc plugin's old ``StartDM`` -> ``start_d_m``)."""
- from chattolib.types import _snake_case
- assert _snake_case("messagePosted") == "message_posted"
- assert _snake_case("directMessageReceived") == "direct_message_received"
- # hypothetical acronym field: DM stays whole, boundary before the word after it
- assert _snake_case("startDMReceived") == "start_dm_received"
- assert _snake_case("getDMStats") == "get_dm_stats"
- # --- Message search -----------------------------------------------------
- async def test_get_search_status_reports_disabled_with_no_delay(client):
- """Search is off by default, so DISABLED is a state a client has to be
- able to read — not an error it has to decode from an exception."""
- resp = message_search_pb2.GetStatusResponse()
- resp.state = message_search_pb2.MessageSearchState.MESSAGE_SEARCH_STATE_DISABLED
- _mock_method(client, "message_search", "get_status", resp)
- async with client:
- status = await client.get_search_status()
- assert status.state is MessageSearchState.DISABLED
- assert status.retry_after_seconds is None
- assert status.is_ready is False
- async def test_get_search_status_carries_the_servers_retry_delay(client):
- """INDEXING is transient and the server says how long to wait; dropping
- that number would leave the caller to invent a backoff of its own."""
- resp = message_search_pb2.GetStatusResponse()
- resp.state = message_search_pb2.MessageSearchState.MESSAGE_SEARCH_STATE_INDEXING
- resp.retry_after.FromJsonString("3.5s")
- _mock_method(client, "message_search", "get_status", resp)
- async with client:
- status = await client.get_search_status()
- assert status.state is MessageSearchState.INDEXING
- assert status.retry_after_seconds == pytest.approx(3.5)
- assert status.is_ready is False
- async def test_degraded_search_still_counts_as_ready(client):
- """DEGRADED is documented as "can answer with a known non-fatal
- limitation", so a readiness check that excludes it refuses working search."""
- resp = message_search_pb2.GetStatusResponse()
- resp.state = message_search_pb2.MessageSearchState.MESSAGE_SEARCH_STATE_DEGRADED
- _mock_method(client, "message_search", "get_status", resp)
- async with client:
- status = await client.get_search_status()
- assert status.is_ready is True
- async def test_search_messages_parses_results_and_page_state(client):
- resp = message_search_pb2.SearchMessagesResponse()
- result = resp.results.add()
- result.message.id = "e1"
- result.message.room_id = "r1"
- result.message.body = "the migration runs on friday"
- result.relevance_score = 0.8
- resp.next_cursor = "cursor-2"
- resp.includes.users["u1"].login = "alice"
- _mock_method(client, "message_search", "search_messages", resp)
- async with client:
- page = await client.search_messages("migration")
- assert len(page.results) == 1
- assert page.results[0].message is not None
- assert page.results[0].message.body == "the migration runs on friday"
- assert page.results[0].relevance_score == pytest.approx(0.8)
- assert page.results[0].thread_context is None
- assert page.next_cursor == "cursor-2"
- assert page.thread_total_count is None
- assert page.users_by_id["u1"].login == "alice"
- async def test_search_messages_parses_thread_grouping(client):
- """With THREAD grouping a result stands for a whole thread: the root and
- the latest reply live in thread_context, and the message is only its best
- match. Reading them as if they were one message misreports the thread."""
- resp = message_search_pb2.SearchMessagesResponse()
- result = resp.results.add()
- result.message.id = "e2"
- result.message.body = "a reply that matched"
- result.relevance_score = 0.5
- result.thread_context.root_message.id = "e1"
- result.thread_context.root_message.body = "the root, which did not match"
- result.thread_context.room.id = "r1"
- result.thread_context.thread.thread_root_event_id = "e1"
- result.thread_context.thread.reply_count = 4
- result.thread_context.latest_reply.id = "e9"
- result.thread_context.latest_reply.body = "newest reply"
- resp.thread_total_count = 1
- _mock_method(client, "message_search", "search_messages", resp)
- async with client:
- page = await client.search_messages("reply", group_by=MessageSearchGroupBy.THREAD)
- context = page.results[0].thread_context
- assert context is not None
- assert context.root_message is not None
- assert context.root_message.id == "e1"
- assert context.room is not None
- assert context.room.id == "r1"
- assert context.thread is not None
- assert context.thread.reply_count == 4
- assert context.latest_reply is not None
- assert context.latest_reply.id == "e9"
- assert page.thread_total_count == 1
- async def test_search_messages_sends_the_full_query_shape(client):
- """Every optional field has to reach the wire as the protobuf value the
- server validates — enums as their pb2 number, timestamps as Timestamp
- messages. A silently dropped scope or filter turns a search into a
- different, broader one."""
- resp = message_search_pb2.SearchMessagesResponse()
- mocked = _mock_method(client, "message_search", "search_messages", resp)
- async with client:
- await client.search_messages(
- "deploy plan",
- room_id="r1",
- author_id="u1",
- created_after=dt.datetime(2026, 1, 1, tzinfo=dt.UTC),
- created_before=dt.datetime(2026, 2, 1, tzinfo=dt.UTC),
- has_attachments=True,
- order=MessageSearchOrder.NEWEST,
- page_size=25,
- cursor="cursor-1",
- scope=MessageSearchScope.FOLLOWED_THREADS,
- group_by=MessageSearchGroupBy.THREAD,
- root_author_filter=ThreadRootAuthorFilter.VIEWER,
- )
- req = mocked.call_args.args[0]
- assert req.query == "deploy plan"
- assert req.room_id == "r1"
- assert req.author_id == "u1"
- assert req.created_after.ToJsonString() == "2026-01-01T00:00:00Z"
- assert req.created_before.ToJsonString() == "2026-02-01T00:00:00Z"
- assert req.has_attachments is True
- assert req.order == message_search_pb2.MessageSearchOrder.MESSAGE_SEARCH_ORDER_NEWEST
- assert req.page_size == 25
- assert req.cursor == "cursor-1"
- assert req.scope == message_search_pb2.MessageSearchScope.MESSAGE_SEARCH_SCOPE_FOLLOWED_THREADS
- assert req.group_by == message_search_pb2.MessageSearchGroupBy.MESSAGE_SEARCH_GROUP_BY_THREAD
- assert req.root_author_filter == threads_pb2.ThreadRootAuthorFilter.Value(
- "THREAD_ROOT_AUTHOR_FILTER_VIEWER"
- )
- async def test_search_messages_leaves_unset_fields_at_their_defaults(client):
- """Optional filters must stay *unset*, not be written as empty strings or
- zero timestamps: the server intersects what it receives with what the caller
- can read, and an empty filter is not the same as no filter."""
- resp = message_search_pb2.SearchMessagesResponse()
- mocked = _mock_method(client, "message_search", "search_messages", resp)
- async with client:
- await client.search_messages("anything")
- req = mocked.call_args.args[0]
- assert req.query == "anything"
- assert req.room_id == ""
- assert req.author_id == ""
- assert req.has_attachments is False
- assert req.page_size == 0
- assert req.cursor == ""
- assert req.order == message_search_pb2.MessageSearchOrder.MESSAGE_SEARCH_ORDER_UNSPECIFIED
- assert req.HasField("created_after") is False
|