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 https://summercms.io/docs now`, ) }) it('links http as well, and only http(s)', () => { expect(linkify('http://a.example')).toContain(' { for (const s of ['https://example.com@bit.ly/', 'https://bit。ly/x']) { expect(linkify(escapeHtml(s))).toBe(escapeHtml(s)) } expect(linkify('https://ok.example:8443/a?b=c#d')).toContain(' { expect(linkify('go to https://a.example/x.')).toBe(`go to https://a.example/x.`) expect(linkify('(https://a.example/x)')).toBe(`(https://a.example/x)`) 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 = ' https://a.example/?a=1&b=' const out = linkify(escapeHtml(input)) expect(out).not.toContain(' 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('skips URLs whose written host is not the host they open', () => { const text = 'a https://bit%2Ely/x b https://ok.example/1 c https://bit.ly/x d https://żółw.example/' expect(messageUrls(text).map((u) => u.host)).toEqual(['ok.example', 'xn--w-uga1v8h.example']) }) 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() }) })