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(' { expect(renderChatText('**a** _b_ `c`')).toBe('a b c') expect(renderChatText('`**x**`')).toBe('<b>**x**</b>') }) 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 https://example.org/x?y=1', ) expect(renderChatText('(https://example.org/a).')).toBe( '(https://example.org/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('plan') expect(renderChatText('[x](https://example.org)')).toBe( 'x', ) const bad = renderChatText('[x](javascript:alert(1))') expect(bad).not.toContain(' { const html = renderChatText('# h\n- a\n> q\n![i](https://example.org/i.png)') for (const tag of [' { expect(renderChatText('a\nb')).toBe('a
b') expect(renderChatText('a\r\nb')).toBe('a
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('@Ola K. hi') expect(renderChatText('@nobody hi', members)).toBe('@nobody hi') expect(renderChatText('dzięki @ola_k.', members)).toBe('dzięki @Ola K..') expect(renderChatText('mail@ola_k', members)).toBe('mail@ola_k') expect(renderChatText('@asystent', members)).toContain('>@Asystent') expect(renderChatText('`@ola_k`', members)).toBe('@ola_k') }) 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('kod') 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/a') const link = renderChatText('[tu](https://x.pl/`a`)') expect(link).not.toContain('a') 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 }) }) })