test: the SPA's chat tests on the port, client, store and link embeds
- vitest runs app/**/*.test.ts and tests/**/*.test.ts; the SPA's chatEvents, chatFormat, chatText, chatUnread and realtime tests sit next to their ports with only import paths changed - linkEmbeds: escaping, http(s) only, rel and target, registry order - client: every endpoint, refresh-and-retry, unauthorized, 422, upload - store: bootstrap, anonymous and non-member starts, lazy joins, DMs, live messages and unread, threads, pins, reactions, slash commands, polling without realtime, onUserEvent, DM-only actions
This commit is contained in:
140
app/lib/chatEvents.test.ts
Normal file
140
app/lib/chatEvents.test.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { ChatConversationRow, ChatMessage } from './types'
|
||||
import { applyChatMessage, applyReaction, needsFetch, parsePublication } from './chatEvents'
|
||||
|
||||
function message(id: number, conversation = 11, sender = 2, created = '2026-10-26T10:00:00Z'): ChatMessage {
|
||||
return {
|
||||
id,
|
||||
conversation_id: conversation,
|
||||
sender_id: sender,
|
||||
sender_name: '',
|
||||
sender_avatar: null,
|
||||
content: `m${id}`,
|
||||
type: 'text',
|
||||
attachments: [],
|
||||
created_at: created,
|
||||
created_at_human: '',
|
||||
edited_at: null,
|
||||
deleted_at: null,
|
||||
is_mine: false,
|
||||
root_id: null,
|
||||
}
|
||||
}
|
||||
|
||||
describe('parsePublication', () => {
|
||||
it('rejects non-objects, unknown events and bad payloads', () => {
|
||||
expect(parsePublication(null)).toBeNull()
|
||||
expect(parsePublication('created.chat.message')).toBeNull()
|
||||
expect(parsePublication([])).toBeNull()
|
||||
expect(parsePublication({ event: 'deleted.chat.message', payload: { id: 1 } })).toBeNull()
|
||||
expect(parsePublication({ event: 'reacted.chat.message', payload: { id: 1, conversation_id: 10, user_id: 2, key: '', action: 'added' } })).toBeNull()
|
||||
expect(parsePublication({ event: 'reacted.chat.message', payload: { id: 1, conversation_id: 10, user_id: 2, key: 'x', action: 'toggled' } })).toBeNull()
|
||||
expect(parsePublication({ event: 'created.chat.message', payload: { id: '7', conversation_id: 10, sender_id: 2 } })).toBeNull()
|
||||
expect(parsePublication({ event: 'typing.chat.message', payload: { conversation_id: 10, user_id: 2, action: 'typing-stop' } })).toBeNull()
|
||||
expect(parsePublication({ event: 'chat:message', payload: { conversation_id: 10, message: { id: 1 } } })).toBeNull()
|
||||
expect(parsePublication({ event: 'chat:message', payload: { conversation_id: 12, message: message(1, 11) } })).toBeNull()
|
||||
expect(parsePublication({ event: 'chat:unread', payload: { count: -1 } })).toBeNull()
|
||||
})
|
||||
|
||||
it('reads the ids-only created event and never its content', () => {
|
||||
const ev = parsePublication({
|
||||
event: 'created.chat.message',
|
||||
payload: { id: 7, conversation_id: 10, sender_id: 2, root_id: null, type: 'text', action: 'created', created_at: 'x', content: 'spoofed' },
|
||||
timestamp: 1,
|
||||
})
|
||||
expect(ev).toEqual({ kind: 'created', id: 7, conversationId: 10, senderId: 2 })
|
||||
})
|
||||
|
||||
it('reads chat:message, typing, read, unread and mentions', () => {
|
||||
expect(parsePublication({ event: 'chat:message', payload: { conversation_id: 11, message: message(3) } })).toEqual({
|
||||
kind: 'chatMessage',
|
||||
conversationId: 11,
|
||||
message: message(3),
|
||||
})
|
||||
expect(parsePublication({ event: 'typing.chat.message', payload: { conversation_id: 10, user_id: 2, action: 'typing-start' } })).toEqual({
|
||||
kind: 'typing',
|
||||
conversationId: 10,
|
||||
userId: 2,
|
||||
})
|
||||
expect(parsePublication({ event: 'chat:read', payload: { conversation_id: 10, user_id: 2, read_at: 'x' } })).toEqual({ kind: 'read', conversationId: 10, userId: 2 })
|
||||
expect(parsePublication({ event: 'chat:unread', payload: { count: 4 } })).toEqual({ kind: 'unread', count: 4 })
|
||||
expect(parsePublication({ event: 'mentions.changed', payload: { action: 'mentions' } })).toEqual({ kind: 'mentions' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('needsFetch', () => {
|
||||
const created = { kind: 'created', id: 7, conversationId: 10, senderId: 2 } as const
|
||||
it('asks once per id, only for a loaded conversation', () => {
|
||||
expect(needsFetch(created, {})).toBe(false)
|
||||
expect(needsFetch(created, { 10: [message(6, 10)] })).toBe(true)
|
||||
expect(needsFetch(created, { 10: [message(6, 10), message(7, 10)] })).toBe(false)
|
||||
expect(needsFetch({ kind: 'mentions' }, { 10: [] })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('applyChatMessage', () => {
|
||||
const rows: ChatConversationRow[] = [
|
||||
{ id: 11, type: 'dm', other_user_id: 2, unread: 1, last_message: { id: 1, sender_id: 2, content: 'a', created_at: '2026-10-25T10:00:00Z' } },
|
||||
{ id: 12, type: 'dm', other_user_id: 3, unread: 0, last_message: null },
|
||||
]
|
||||
|
||||
it('bumps unread and the preview of a known DM', () => {
|
||||
const next = applyChatMessage(rows, message(5, 11), { seen: false })
|
||||
expect(next[0]).toMatchObject({ id: 11, unread: 2, last_message: { id: 5, content: 'm5' } })
|
||||
expect(rows[0]?.unread).toBe(1)
|
||||
// The same message twice changes nothing.
|
||||
expect(applyChatMessage(next, message(5, 11), { seen: false })).toBe(next)
|
||||
})
|
||||
|
||||
it('does not count a message the user is looking at', () => {
|
||||
expect(applyChatMessage(rows, message(5, 12, 3), { seen: true })[1]).toMatchObject({ unread: 0, last_message: { id: 5 } })
|
||||
})
|
||||
|
||||
it('adds a new DM row first', () => {
|
||||
const next = applyChatMessage(rows, message(9, 20, 4), { seen: false })
|
||||
expect(next).toHaveLength(3)
|
||||
expect(next[0]).toMatchObject({ id: 20, type: 'dm', other_user_id: 4, unread: 1, last_message: { id: 9 } })
|
||||
})
|
||||
})
|
||||
|
||||
describe('message events (reacted, updated, deleted)', () => {
|
||||
it('reads the reacted, updated and deleted payloads', () => {
|
||||
expect(parsePublication({ event: 'reacted.chat.message', payload: { id: 1, conversation_id: 10, user_id: 2, key: '👍', action: 'added' } })).toEqual({
|
||||
kind: 'reacted',
|
||||
id: 1,
|
||||
conversationId: 10,
|
||||
userId: 2,
|
||||
key: '👍',
|
||||
added: true,
|
||||
})
|
||||
expect(parsePublication({ event: 'reacted.chat.message', payload: { id: 1, conversation_id: 10, user_id: 2, key: '👍', action: 'removed' } })).toMatchObject({ added: false })
|
||||
expect(parsePublication({ event: 'updated.chat.message', payload: { id: 4, conversation_id: 10, action: 'updated', edited_at: '2026-01-15T12:05:00Z' } })).toEqual({
|
||||
kind: 'updated',
|
||||
id: 4,
|
||||
conversationId: 10,
|
||||
})
|
||||
expect(parsePublication({ event: 'deleted.chat.message', payload: { id: 5, conversation_id: 10, action: 'deleted' } })).toEqual({ kind: 'deleted', id: 5, conversationId: 10 })
|
||||
})
|
||||
})
|
||||
|
||||
describe('applyReaction', () => {
|
||||
it('adds and removes one user idempotently', () => {
|
||||
const one = applyReaction(undefined, 'a', 1, true)
|
||||
expect(one).toEqual([{ key: 'a', count: 1, user_ids: [1] }])
|
||||
expect(applyReaction(one, 'a', 1, true)).toBe(one)
|
||||
const two = applyReaction(one, 'a', 2, true)
|
||||
expect(two).toEqual([{ key: 'a', count: 2, user_ids: [1, 2] }])
|
||||
expect(applyReaction(two, 'a', 3, false)).toBe(two)
|
||||
expect(applyReaction(two, 'a', 1, false)).toEqual([{ key: 'a', count: 1, user_ids: [2] }])
|
||||
expect(applyReaction(one, 'a', 1, false)).toEqual([])
|
||||
})
|
||||
|
||||
it('places a new key in the allowed order', () => {
|
||||
const list = [
|
||||
{ key: 'a', count: 1, user_ids: [1] },
|
||||
{ key: 'c', count: 1, user_ids: [1] },
|
||||
]
|
||||
expect(applyReaction(list, 'b', 2, true, ['a', 'b', 'c']).map((r) => r.key)).toEqual(['a', 'b', 'c'])
|
||||
expect(applyReaction(list, 'z', 2, true, ['a', 'b', 'c']).map((r) => r.key)).toEqual(['a', 'c', 'z'])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user