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:
110
app/lib/chatText.test.ts
Normal file
110
app/lib/chatText.test.ts
Normal file
@@ -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('<script>x</script> <img src=x onerror=alert(1)> & "q"')
|
||||
expect(html).not.toContain('<script')
|
||||
expect(html).not.toContain('<img')
|
||||
expect(html).toContain('<script>x</script>')
|
||||
expect(html).toContain('& "q"')
|
||||
})
|
||||
|
||||
it('renders bold, italic and code spans', () => {
|
||||
expect(renderChatText('**a** _b_ `c`')).toBe('<strong>a</strong> <em>b</em> <code>c</code>')
|
||||
expect(renderChatText('`<b>**x**</b>`')).toBe('<code><b>**x**</b></code>')
|
||||
})
|
||||
|
||||
it('autolinks bare http(s) URLs for a new tab, leaving trailing punctuation outside', () => {
|
||||
expect(renderChatText('see https://example.org/x?y=1')).toBe(
|
||||
'see <a href="https://example.org/x?y=1" target="_blank" rel="noopener noreferrer nofollow">https://example.org/x?y=1</a>',
|
||||
)
|
||||
expect(renderChatText('(https://example.org/a).')).toBe(
|
||||
'(<a href="https://example.org/a" target="_blank" rel="noopener noreferrer nofollow">https://example.org/a</a>).',
|
||||
)
|
||||
expect(renderChatText('https://pl.wikipedia.org/wiki/Foo_(bar)')).toContain('href="https://pl.wikipedia.org/wiki/Foo_(bar)"')
|
||||
})
|
||||
|
||||
it('renders [label](url) through safeUrl and a javascript: link as text', () => {
|
||||
expect(renderChatText('[plan](/plan)')).toBe('<a href="/plan">plan</a>')
|
||||
expect(renderChatText('[x](https://example.org)')).toBe(
|
||||
'<a href="https://example.org" target="_blank" rel="noopener noreferrer nofollow">x</a>',
|
||||
)
|
||||
const bad = renderChatText('[x](javascript:alert(1))')
|
||||
expect(bad).not.toContain('<a')
|
||||
expect(bad).not.toContain('href')
|
||||
expect(bad.startsWith('x')).toBe(true)
|
||||
})
|
||||
|
||||
it('never produces headings, lists, quotes or images', () => {
|
||||
const html = renderChatText('# h\n- a\n> q\n')
|
||||
for (const tag of ['<h', '<ul', '<li', '<blockquote', '<img', '<p']) expect(html).not.toContain(tag)
|
||||
})
|
||||
|
||||
it('turns line breaks into br', () => {
|
||||
expect(renderChatText('a\nb')).toBe('a<br>b')
|
||||
expect(renderChatText('a\r\nb')).toBe('a<br>b')
|
||||
})
|
||||
|
||||
it('renders a known @handle as a mention span and leaves unknown ones as text (D-12)', () => {
|
||||
expect(renderChatText('@ola_k hi', members)).toBe('<span class="mention" title="@ola_k">@Ola K.</span> hi')
|
||||
expect(renderChatText('@nobody hi', members)).toBe('@nobody hi')
|
||||
expect(renderChatText('dzięki @ola_k.', members)).toBe('dzięki <span class="mention" title="@ola_k">@Ola K.</span>.')
|
||||
expect(renderChatText('mail@ola_k', members)).toBe('mail@ola_k')
|
||||
expect(renderChatText('@asystent', members)).toContain('>@Asystent</span>')
|
||||
expect(renderChatText('`@ola_k`', members)).toBe('<code>@ola_k</code>')
|
||||
})
|
||||
|
||||
it('handles empty input', () => {
|
||||
expect(renderChatText('')).toBe('')
|
||||
expect(renderChatText(null)).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('nested code spans (WR-07)', () => {
|
||||
it('renders a code span inside a link label', () => {
|
||||
const html = renderChatText('[`kod`](https://x.pl)')
|
||||
expect(html).toBe('<a href="https://x.pl" target="_blank" rel="noopener noreferrer nofollow"><code>kod</code></a>')
|
||||
expect(html).not.toContain('\u0000')
|
||||
})
|
||||
|
||||
it('does not link a URL that holds a code span', () => {
|
||||
const bare = renderChatText('https://x.pl/`a`')
|
||||
expect(bare).toBe('https://x.pl/<code>a</code>')
|
||||
const link = renderChatText('[tu](https://x.pl/`a`)')
|
||||
expect(link).not.toContain('<a ')
|
||||
expect(link).toContain('<code>a</code>')
|
||||
expect(link).not.toContain('\u0000')
|
||||
})
|
||||
})
|
||||
|
||||
describe('mention helpers', () => {
|
||||
it('mentionsHandle finds a handle as its own token', () => {
|
||||
expect(mentionsHandle('hi @ola_k', 'ola_k')).toBe(true)
|
||||
expect(mentionsHandle('hi @ola_k.', 'ola_k')).toBe(true)
|
||||
expect(mentionsHandle('hi @ola_kx', 'ola_k')).toBe(false)
|
||||
expect(mentionsHandle('mail@ola_k', 'ola_k')).toBe(false)
|
||||
expect(mentionsHandle('`@ola_k`', 'ola_k')).toBe(false)
|
||||
expect(mentionsHandle(null, 'ola_k')).toBe(false)
|
||||
expect(mentionsHandle('hi @ola_k', '')).toBe(false)
|
||||
})
|
||||
|
||||
it('mentionQuery reads an @ at a word start before the caret', () => {
|
||||
expect(mentionQuery('hej @ol', 7)).toEqual({ start: 4, query: 'ol' })
|
||||
expect(mentionQuery('@', 1)).toEqual({ start: 0, query: '' })
|
||||
expect(mentionQuery('hej\n@Łu', 7)).toEqual({ start: 4, query: 'Łu' })
|
||||
expect(mentionQuery('mail@x', 6)).toBeNull()
|
||||
expect(mentionQuery('hej @ol x', 9)).toBeNull()
|
||||
expect(mentionQuery('hej @ol', 3)).toBeNull()
|
||||
})
|
||||
|
||||
it('insertMention puts @handle and a space in place of the query', () => {
|
||||
expect(insertMention('hej @ol', { start: 4, end: 7 }, 'ola_k')).toEqual({ text: 'hej @ola_k ', caret: 11 })
|
||||
expect(insertMention('@ol jak tam', { start: 0, end: 3 }, 'ola_k')).toEqual({ text: '@ola_k jak tam', caret: 7 })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user