|
| 1 | +jest.mock('@/actions/tmgr/chats', () => ({ |
| 2 | + getChat: jest.fn(), |
| 3 | + getMessages: jest.fn(), |
| 4 | + getMessage: jest.fn(), |
| 5 | + getReplies: jest.fn(), |
| 6 | + sendMessage: jest.fn(), |
| 7 | + editMessage: jest.fn(), |
| 8 | + deleteMessage: jest.fn(), |
| 9 | + listChats: jest.fn(), |
| 10 | + markRead: jest.fn(), |
| 11 | +})); |
| 12 | +jest.mock('@/composable/usePusher', () => ({ usePusher: jest.fn() })); |
| 13 | + |
| 14 | +import type { Chat, ChatMessage, ChatMessageHit } from '@/actions/tmgr/chats'; |
| 15 | +import * as api from '@/actions/tmgr/chats'; |
| 16 | +import { applyChatEvent, readableHits, useChats } from '@/composable/useChats'; |
| 17 | + |
| 18 | +const mocked = api as jest.Mocked<typeof api>; |
| 19 | + |
| 20 | +const msg = (id: number, chatId: number, over: Partial<ChatMessage> = {}) => |
| 21 | + ({ |
| 22 | + id, |
| 23 | + chat_id: chatId, |
| 24 | + author: { kind: 'user', id: 2, name: 'A' }, |
| 25 | + body: `m${id}`, |
| 26 | + mentions: [], |
| 27 | + parent_message_id: null, |
| 28 | + reply_count: 0, |
| 29 | + last_reply_at: null, |
| 30 | + reactions: [], |
| 31 | + files: [], |
| 32 | + edited_at: null, |
| 33 | + deleted: false, |
| 34 | + created_at: '2026-01-01T00:00:00Z', |
| 35 | + ...over, |
| 36 | + }) as ChatMessage; |
| 37 | + |
| 38 | +const chat = (id: number, over: Partial<Chat> = {}) => |
| 39 | + ({ |
| 40 | + id, |
| 41 | + workspace_id: 1, |
| 42 | + kind: 'channel', |
| 43 | + visibility: 'members', |
| 44 | + title: `c${id}`, |
| 45 | + topic: null, |
| 46 | + joined: true, |
| 47 | + can_read: true, |
| 48 | + can_write: true, |
| 49 | + can_moderate: false, |
| 50 | + can_delete: false, |
| 51 | + archived_at: null, |
| 52 | + last_message_at: null, |
| 53 | + last_message: null, |
| 54 | + unread: 0, |
| 55 | + member_count: 2, |
| 56 | + members: null, |
| 57 | + created_at: null, |
| 58 | + notify_level: null, |
| 59 | + muted_until: null, |
| 60 | + ...over, |
| 61 | + }) as Chat; |
| 62 | + |
| 63 | +const hit = (messageId: number, chatId: number) => |
| 64 | + ({ |
| 65 | + message_id: messageId, |
| 66 | + chat_id: chatId, |
| 67 | + chat_title: `c${chatId}`, |
| 68 | + chat_kind: 'channel', |
| 69 | + parent_message_id: null, |
| 70 | + author: { kind: 'user', id: 2, name: 'A' }, |
| 71 | + snippet: 'secret plan', |
| 72 | + created_at: '2026-01-01T00:00:00Z', |
| 73 | + }) as ChatMessageHit; |
| 74 | + |
| 75 | +const deferred = <T>() => { |
| 76 | + let resolve!: (v: T) => void; |
| 77 | + const promise = new Promise<T>((r) => (resolve = r)); |
| 78 | + return { promise, resolve }; |
| 79 | +}; |
| 80 | + |
| 81 | +describe('readableHits', () => { |
| 82 | + it('keeps hits only from chats the user can still read', () => { |
| 83 | + const hits = [hit(1, 1), hit(2, 2), hit(3, 3)]; |
| 84 | + const chats = [chat(1), chat(2, { can_read: false, joined: false })]; |
| 85 | + expect(readableHits(hits, chats).map((h) => h.message_id)).toEqual([1]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('drops a hit once its chat leaves the list', () => { |
| 89 | + const hits = [hit(1, 1)]; |
| 90 | + expect(readableHits(hits, [chat(1)])).toHaveLength(1); |
| 91 | + expect(readableHits(hits, [])).toHaveLength(0); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('chat switches', () => { |
| 96 | + const chats = useChats(); |
| 97 | + |
| 98 | + beforeEach(() => { |
| 99 | + jest.resetAllMocks(); |
| 100 | + chats.chats.value = [chat(1), chat(2)]; |
| 101 | + mocked.getMessages.mockImplementation(async (id: number) => [ |
| 102 | + msg(id * 100, id), |
| 103 | + ]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('sends to the chat the composer belongs to and keeps it out of the open one', async () => { |
| 107 | + await chats.openChat(1); |
| 108 | + const pending = deferred<ChatMessage>(); |
| 109 | + mocked.sendMessage.mockReturnValue(pending.promise); |
| 110 | + const sending = chats.send(1, { body: 'draft for A' }); |
| 111 | + await chats.openChat(2); |
| 112 | + pending.resolve(msg(150, 1)); |
| 113 | + await sending; |
| 114 | + expect(mocked.sendMessage).toHaveBeenCalledWith(1, { body: 'draft for A' }); |
| 115 | + expect(chats.messages.value.map((m) => m.id)).toEqual([200]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('ignores an edit response from a chat that is no longer open', async () => { |
| 119 | + await chats.openChat(1); |
| 120 | + const pending = deferred<ChatMessage>(); |
| 121 | + mocked.editMessage.mockReturnValue(pending.promise); |
| 122 | + const saving = chats.saveEdit(msg(100, 1), 'edited', []); |
| 123 | + await chats.openChat(2); |
| 124 | + pending.resolve(msg(100, 1, { body: 'edited' })); |
| 125 | + await saving; |
| 126 | + expect(chats.messages.value.map((m) => m.chat_id)).toEqual([2]); |
| 127 | + }); |
| 128 | + |
| 129 | + it('ignores a delete response from a chat that is no longer open', async () => { |
| 130 | + await chats.openChat(1); |
| 131 | + const pending = deferred<void>(); |
| 132 | + mocked.deleteMessage.mockReturnValue(pending.promise); |
| 133 | + const removing = chats.removeMessage(msg(100, 1)); |
| 134 | + await chats.openChat(2); |
| 135 | + chats.messages.value = [msg(100, 2)]; |
| 136 | + pending.resolve(); |
| 137 | + await removing; |
| 138 | + expect(chats.messages.value[0].deleted).toBe(false); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('unread from thread replies', () => { |
| 143 | + it('does not count a reply, matching the server count of top-level messages', () => { |
| 144 | + const result = applyChatEvent( |
| 145 | + [ |
| 146 | + chat(1, { |
| 147 | + last_message: { id: 5, author_id: 2, preview: 'x', deleted: false }, |
| 148 | + }), |
| 149 | + ], |
| 150 | + 'chat.message.created', |
| 151 | + { chat_id: 1, message_id: 9, parent_message_id: 5, author_id: 3 }, |
| 152 | + { openChatId: null, selfId: 2 }, |
| 153 | + ); |
| 154 | + expect(result.chats[0].unread).toBe(0); |
| 155 | + expect(result.chats[0].last_message?.id).toBe(5); |
| 156 | + }); |
| 157 | +}); |
0 commit comments