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:
131
app/lib/chatFormat.test.ts
Normal file
131
app/lib/chatFormat.test.ts
Normal file
@@ -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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user