- 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
106 lines
4.6 KiB
TypeScript
106 lines
4.6 KiB
TypeScript
import { afterEach, describe, expect, it } from 'vitest'
|
|
import { embedRendererFor, linkify, messageUrls, registerEmbedRenderer } from './linkEmbeds'
|
|
import { escapeHtml } from './markdown'
|
|
|
|
const EXTERNAL = 'target="_blank" rel="noopener noreferrer nofollow"'
|
|
|
|
describe('linkify', () => {
|
|
it('links a bare https URL with target and rel set', () => {
|
|
expect(linkify('see https://summercms.io/docs now')).toBe(
|
|
`see <a href="https://summercms.io/docs" ${EXTERNAL}>https://summercms.io/docs</a> now`,
|
|
)
|
|
})
|
|
|
|
it('links http as well, and only http(s)', () => {
|
|
expect(linkify('http://a.example')).toContain('<a href="http://a.example"')
|
|
for (const s of ['javascript:alert(1)', 'ftp://a.example/x', 'mailto:a@b.example', 'data:text/html,hi', '//a.example/x']) {
|
|
expect(linkify(escapeHtml(s))).toBe(escapeHtml(s))
|
|
}
|
|
})
|
|
|
|
it('keeps trailing punctuation outside the link', () => {
|
|
expect(linkify('go to https://a.example/x.')).toBe(`go to <a href="https://a.example/x" ${EXTERNAL}>https://a.example/x</a>.`)
|
|
expect(linkify('(https://a.example/x)')).toBe(`(<a href="https://a.example/x" ${EXTERNAL}>https://a.example/x</a>)`)
|
|
expect(linkify('https://en.wikipedia.org/wiki/Go_(game)')).toContain('href="https://en.wikipedia.org/wiki/Go_(game)"')
|
|
expect(linkify('https://a.example/?q=1, then')).toContain('href="https://a.example/?q=1"')
|
|
})
|
|
|
|
it('works on escaped text and adds no markup of the input', () => {
|
|
const input = '<script>alert(1)</script> https://a.example/?a=1&b=<x>'
|
|
const out = linkify(escapeHtml(input))
|
|
expect(out).not.toContain('<script')
|
|
expect(out).toContain('<script>')
|
|
// The URL's & and <> stay escaped once in the href and the text.
|
|
expect(out).toContain('href="https://a.example/?a=1&b=<x>"')
|
|
expect(out).not.toMatch(/<(?!a |\/a>)/)
|
|
})
|
|
|
|
it('cannot break out of the href attribute', () => {
|
|
const out = linkify(escapeHtml('https://a.example/"onmouseover="alert(1)'))
|
|
expect(out).not.toContain('" onmouseover')
|
|
expect(out).not.toMatch(/href="[^"]*"[^>]*onmouseover/)
|
|
})
|
|
|
|
it('leaves text without URLs alone', () => {
|
|
expect(linkify('no links here')).toBe('no links here')
|
|
expect(linkify('')).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('messageUrls', () => {
|
|
it('lists distinct http(s) URLs in order', () => {
|
|
const urls = messageUrls('a https://x.example/1 b http://y.example c https://x.example/1 d https://z.example/2.')
|
|
expect(urls.map((u) => u.href)).toEqual(['https://x.example/1', 'http://y.example/', 'https://z.example/2'])
|
|
})
|
|
|
|
it('skips code spans, other schemes and empty text', () => {
|
|
expect(messageUrls('`https://code.example` and ftp://f.example and https://ok.example').map((u) => u.host)).toEqual(['ok.example'])
|
|
expect(messageUrls('')).toEqual([])
|
|
expect(messageUrls(null)).toEqual([])
|
|
expect(messageUrls(undefined)).toEqual([])
|
|
})
|
|
|
|
it('counts the URL of a [label](url) link and ignores a URL glued to a word', () => {
|
|
expect(messageUrls('[docs](https://docs.example/a)').map((u) => u.href)).toEqual(['https://docs.example/a'])
|
|
expect(messageUrls('xhttps://a.example user@https://b.example')).toEqual([])
|
|
})
|
|
|
|
it('gives the cards limits.max_embeds takes the first of', () => {
|
|
const urls = messageUrls('https://1.example https://2.example https://3.example https://4.example')
|
|
expect(urls.slice(0, 3).map((u) => u.host)).toEqual(['1.example', '2.example', '3.example'])
|
|
})
|
|
})
|
|
|
|
describe('registerEmbedRenderer', () => {
|
|
const cleanups: Array<() => void> = []
|
|
afterEach(() => {
|
|
while (cleanups.length) cleanups.pop()?.()
|
|
})
|
|
|
|
it('returns null without a matching renderer (the generic card)', () => {
|
|
expect(embedRendererFor(new URL('https://a.example/'))).toBeNull()
|
|
})
|
|
|
|
it('picks the first registered renderer that matches', () => {
|
|
const blog = { name: 'BlogCard' }
|
|
const any = { name: 'AnyCard' }
|
|
cleanups.push(registerEmbedRenderer((u) => u.pathname.startsWith('/blog/'), blog))
|
|
cleanups.push(registerEmbedRenderer(() => true, any))
|
|
expect(embedRendererFor(new URL('https://a.example/blog/post'))).toBe(blog)
|
|
expect(embedRendererFor(new URL('https://a.example/other'))).toBe(any)
|
|
})
|
|
|
|
it('removes a registration with the returned function, and a throwing matcher matches nothing', () => {
|
|
const card = { name: 'Card' }
|
|
const off = registerEmbedRenderer(() => {
|
|
throw new Error('broken matcher')
|
|
}, { name: 'Broken' })
|
|
cleanups.push(off)
|
|
const remove = registerEmbedRenderer((u) => u.host === 'a.example', card)
|
|
expect(embedRendererFor(new URL('https://a.example/'))).toBe(card)
|
|
remove()
|
|
remove()
|
|
expect(embedRendererFor(new URL('https://a.example/'))).toBeNull()
|
|
})
|
|
})
|