diff --git a/README.md b/README.md
index 6fd97b6..81bdd6c 100644
--- a/README.md
+++ b/README.md
@@ -197,7 +197,7 @@ Typing `/` at the start of the composer opens the commands of `GET {botsApiBase}
| `ChatReactionBar`, `ChatStagingChips`, `ChatDeleteDialog`, `ChatMemberPicker`, `ChatMentionPopup`, `ChatCommandPicker`, `ChatLinkEmbed` | Parts of the above |
| `ChatAvatar`, `ChatIcon`, `ChatNotices` | Initials avatar, stroke icons, short notices |
-Composables: `useChat()` (the store, one per app, state in `useState` keys prefixed `chat:`), `useChatApi()` (the HTTP client bound to runtime config and the hook), `useChatNotice()`. The pure modules in `app/lib/` are imported explicitly, never auto-imported: `client.ts` (`createChatClient`, `ChatApiError`, `userIdFromToken`), `realtime.ts`, `linkEmbeds.ts`, `chatEvents.ts`, `chatFormat.ts`, `chatText.ts`, `chatUnread.ts`, `format.ts`, `attachments.ts`.
+Composables: `useChat()` (the store, one per app, state in `useState` keys prefixed `chat:`), `useChatApi()` (the HTTP client bound to runtime config and the hook), `useChatNotice()`. The pure modules in `app/lib/` are imported explicitly, never auto-imported: `client.ts` (`createChatClient`, `ChatApiError`, `userIdFromToken`), `realtime.ts`, `linkEmbeds.ts`, `chatEvents.ts`, `chatFormat.ts`, `chatText.ts`, `chatUnread.ts`, `format.ts`, `attachments.ts`, `slots.ts` (`dmActionsTarget`, the DM-only rule of the conversation-actions slot).
## Tokens
@@ -225,7 +225,12 @@ pnpm build:playground
## Testing
```sh
-pnpm test # vitest: the HTTP client (tests/smoke.test.ts)
+pnpm test # vitest in Node: app/**/*.test.ts and tests/**/*.test.ts
```
+- `app/lib/chatEvents.test.ts`, `chatFormat.test.ts`, `chatText.test.ts`, `chatUnread.test.ts` and `realtime.test.ts` are the Golem15 SPA's tests of the modules ported here, with only their import paths changed (`vitest.config.ts` maps the SPA's `@/api/http` to `tests/support/http.ts`).
+- `app/lib/linkEmbeds.test.ts`: `linkify` (escaped input, http(s) only, `target` and `rel`), `messageUrls` and the renderer registry.
+- `tests/client.test.ts`: every endpoint's URL, method and body, the 401 refresh-and-retry, `unauthorized`, the 422 mapping and the multipart upload.
+- `tests/store.test.ts`: `useChat` with stubbed Nuxt composables, a fake client and a fake realtime client: the bootstrap (and the anonymous, non-member and no-host cases), lazy channel joins, DMs, live messages and unread counts, threads, pins, reactions, slash commands, polling without realtime, `onUserEvent` and the DM-only `conversation-actions` rule.
+
The consuming application covers the components with browser checks.
diff --git a/app/lib/chatEvents.test.ts b/app/lib/chatEvents.test.ts
new file mode 100644
index 0000000..4e5a0db
--- /dev/null
+++ b/app/lib/chatEvents.test.ts
@@ -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'])
+ })
+})
diff --git a/app/lib/chatFormat.test.ts b/app/lib/chatFormat.test.ts
new file mode 100644
index 0000000..83a64fb
--- /dev/null
+++ b/app/lib/chatFormat.test.ts
@@ -0,0 +1,131 @@
+import { describe, expect, it } from 'vitest'
+import type { ChatMessage } from './types'
+import { avatarIndex, dayLabel, filterMembers, fullName, groupMessages, initials, reactorsTitle, shortName } from './chatFormat'
+
+const ola = { id: 3, name: 'Ola', surname: 'Kowalska', handle: 'ola', is_bot: false }
+
+function msg(id: number, sender: number, at: string): ChatMessage {
+ return {
+ id,
+ conversation_id: 1,
+ sender_id: sender,
+ sender_name: '',
+ sender_avatar: null,
+ content: `m${id}`,
+ type: 'text',
+ attachments: [],
+ created_at: at,
+ created_at_human: '',
+ edited_at: null,
+ deleted_at: null,
+ is_mine: false,
+ root_id: null,
+ }
+}
+
+describe('shortName', () => {
+ it('is the name and the initial of the surname', () => {
+ expect(shortName(ola)).toBe('Ola K.')
+ })
+ it('falls back to @handle, then to the e-mail local part', () => {
+ expect(shortName({ name: '', surname: '', handle: 'olak' })).toBe('@olak')
+ expect(shortName(undefined, 'ola.k@uwr.edu.pl')).toBe('ola.k')
+ })
+ it('is the name only for a bot', () => {
+ expect(shortName({ name: 'Asystent', surname: 'Bot', handle: 'bot', is_bot: true })).toBe('Asystent')
+ })
+ it('gives the full name for picker rows', () => {
+ expect(fullName(ola)).toBe('Ola Kowalska')
+ })
+})
+
+describe('initials', () => {
+ it('takes name and surname', () => {
+ expect(initials(ola)).toBe('OK')
+ })
+ it('falls back to the first two letters of the handle', () => {
+ expect(initials({ name: '', surname: '', handle: 'łukasz' })).toBe('ŁU')
+ })
+})
+
+describe('avatarIndex', () => {
+ it('maps user ids onto the 8 palette slots', () => {
+ expect(avatarIndex(9)).toBe(2)
+ expect(avatarIndex(8)).toBe(1)
+ expect(avatarIndex(7)).toBe(8)
+ })
+})
+
+describe('dayLabel', () => {
+ const now = new Date('2026-10-26T12:00:00+01:00')
+ it('says DZIŚ and WCZORAJ', () => {
+ expect(dayLabel('2026-10-26T08:00:00+01:00', now)).toBe('DZIŚ')
+ expect(dayLabel('2026-10-25T23:30:00+01:00', now)).toBe('WCZORAJ')
+ })
+ it('uppercases older days and adds a different year', () => {
+ expect(dayLabel('2026-10-20T10:00:00+02:00', now)).toBe('WTOREK, 20 PAŹDZIERNIKA')
+ expect(dayLabel('2025-12-31T10:00:00+01:00', now)).toBe('ŚRODA, 31 GRUDNIA 2025')
+ })
+})
+
+describe('groupMessages', () => {
+ const now = new Date('2026-10-26T12:00:00+01:00')
+ it('collapses one author within 5 minutes and starts a new group after that', () => {
+ const items = groupMessages(
+ [
+ msg(1, 3, '2026-10-26T10:00:00+01:00'),
+ msg(2, 3, '2026-10-26T10:04:00+01:00'),
+ msg(3, 3, '2026-10-26T10:10:00+01:00'),
+ msg(4, 5, '2026-10-26T10:11:00+01:00'),
+ ],
+ now,
+ )
+ expect(items.map((i) => (i.kind === 'day' ? i.label : `${i.message.id}${i.followUp ? '+' : ''}`))).toEqual([
+ 'DZIŚ',
+ '1',
+ '2+',
+ '3',
+ '4',
+ ])
+ })
+ it('never collapses across a day separator', () => {
+ const items = groupMessages([msg(1, 3, '2026-10-25T23:58:00+01:00'), msg(2, 3, '2026-10-26T00:01:00+01:00')], now)
+ expect(items.map((i) => (i.kind === 'day' ? i.label : `${i.message.id}${i.followUp ? '+' : ''}`))).toEqual([
+ 'WCZORAJ',
+ '1',
+ 'DZIŚ',
+ '2',
+ ])
+ })
+})
+
+describe('filterMembers', () => {
+ const people = [
+ { id: 1, name: 'Jakub', surname: 'Zych', handle: 'jakub', is_bot: false },
+ { id: 2, name: 'Łukasz', surname: 'Nowak', handle: 'lukasz', is_bot: false },
+ { id: 3, name: 'Ola', surname: 'Łuczak', handle: 'ola', is_bot: false },
+ { id: 4, name: 'Michał', surname: 'Wiśniewski', handle: 'michal', is_bot: false },
+ ]
+ it('folds case and diacritics (ł = l) and matches a name or surname prefix', () => {
+ expect(filterMembers(people, 'łu', 1).map((m) => m.id)).toEqual([2, 3])
+ expect(filterMembers(people, 'LU', 1).map((m) => m.id)).toEqual([2, 3])
+ expect(filterMembers(people, 'wisn', 1).map((m) => m.id)).toEqual([4])
+ expect(filterMembers(people, 'ichał', 1)).toEqual([])
+ })
+ it('excludes yourself and lists everyone else for an empty query, by name', () => {
+ expect(filterMembers(people, '', 1).map((m) => m.id)).toEqual([2, 4, 3])
+ expect(filterMembers(people, 'jak', 1)).toEqual([])
+ })
+})
+
+describe('reactorsTitle', () => {
+ const m = (id: number, name: string, surname: string) => ({ id, name, surname, handle: `${name}_${id}`, is_bot: false })
+ it('lists short names, at most ten, then the rest as a count', () => {
+ const members = new Map([1, 2, 3].map((id) => [id, m(id, 'Ola', 'Kowalska')]))
+ expect(reactorsTitle([1, 2], members)).toBe('Ola K., Ola K.')
+ const many = Array.from({ length: 13 }, (_, i) => i + 1)
+ const title = reactorsTitle(many, members)
+ expect(title.endsWith(' i 3 innych')).toBe(true)
+ expect(title.split(', ')).toHaveLength(10)
+ })
+})
diff --git a/app/lib/chatText.test.ts b/app/lib/chatText.test.ts
new file mode 100644
index 0000000..491a42c
--- /dev/null
+++ b/app/lib/chatText.test.ts
@@ -0,0 +1,110 @@
+import { describe, expect, it } from 'vitest'
+import type { ChatMember } from './types'
+import { insertMention, mentionQuery, mentionsHandle, renderChatText } from './chatText'
+
+const ola: ChatMember = { id: 2, name: 'Ola', surname: 'Kowalska', handle: 'ola_k', is_bot: false }
+const bot: ChatMember = { id: 9, name: 'Asystent', surname: 'Roku', handle: 'asystent', is_bot: true }
+const members = new Map([ola, bot].map((m) => [m.handle, m]))
+
+describe('renderChatText (D-13)', () => {
+ it('escapes everything it does not produce itself', () => {
+ const html = renderChatText('
& "q"')
+ expect(html).not.toContain(' https://a.example/?a=1&b='
+ const out = linkify(escapeHtml(input))
+ expect(out).not.toContain('