English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet test --solution Chatex.slnxpowershell
git diff --no-index Source/Chatex.Core/Chats D:\Work\Chatex-Reference\Source\Chatex.Core\ChatsCommit:
powershell
git add .powershell
git commit -m "Add chats, permissions, membership and chat events"Common mistakes
Table 10.2. Common mistakes in the chat service
| Symptom | Cause | Fix |
|---|---|---|
| the tests see no events | the recording notifier is registered after AddCore, or AddCore uses AddSingleton instead of TryAddSingleton | register the fake first; keep TryAddSingleton |
a removed member gets not_allowed instead of chat_not_found | the service checks permissions before visibility | always start with ChatAccess |
| two direct chats for one pair, or an exception on a double click | the UniqueConstraintException is not caught in OpenDirectAsync | catch it and load the existing chat |
| a group without an owner | LeaveAsync lets the owner leave, or ChangeRoleAsync does not demote the old owner | check OwnerMustTransfer; set the old owner to Admin on transfer |
CS1503 when passing ChatPermissions.CanEditInfo to RequireAsync | the method signature does not match Func<Chat, ChatMember, bool> | the permission must take (Chat, ChatMember) and return bool |
invalid_role for Member in a group | the role check compares with the wrong regular role | use ChatPermissions.RegularRole(chat.Type) |
Self-check questions
- Why does a private chat of others answer "not found" instead of "forbidden"?
- What do the functions of
ChatPermissionshave in common, and why does that make them easy to test? - What is the difference between
GetVisibleAsyncandGetMembershipAsync? Which operations use which? - How does dependency inversion let Chatex.Core trigger SignalR without referencing Chatex.Api?
- What is the Null Object pattern, and why does Chatex.Core register one with
TryAddSingleton? - Why must events be sent only after the change is committed?
- Why do most events carry only ids, while message events carry the whole message?
- How does
OpenDirectAsynchandle two simultaneous requests for the same direct chat? - Why does
CreateAsyncboth check that the handle is free and catch the unique violation? - Why must the owner hand over ownership before leaving? How does the transfer keep exactly one owner?
- Why are read receipts not sent for channels?
Exercises
- Mute in the list.
SetMutedAsynchas no test. WriteSetMutedAsync_marks_the_chat_as_muted_for_the_caller_only: Bob mutes a group;GetMyChatsAsyncshowsIsMutedfor Bob and not for Alice. - A permission table test. Write a
[Theory]forChatPermissions.CanRemovewith all 16 combinations of actor role and target role, and compare it with the table inDocs/ChatRules.md. - Pinned messages, again. In topic 3 you designed pinned messages. Which method would you add to
IChatService, which permission function toChatPermissions, and which event toIChatNotifier?