Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205import { afterEach, describe, expect, it, vi } from 'vitest'import { bskySearchUrl, canonicalArticleUrl, mentionUrl, searchMentions, searchMentionsUrl,} from './mentions'import type { DocInfo, PubInfo } from './types'
const pub: PubInfo = { uri: 'at://did:plc:publisher/site.standard.publication/self', did: 'did:plc:publisher', handle: 'pub.example.com', pds: 'https://pds.example.com', record: { $type: 'site.standard.publication', url: 'https://pub.example.com/blog', name: 'A publication', }, verified: true,}
const doc: DocInfo = { uri: 'at://did:plc:publisher/site.standard.document/post', record: { $type: 'site.standard.document', site: pub.uri, title: 'A post', publishedAt: '2026-08-20T00:00:00.000Z', path: '/posts/a-post', },}
const POST_URI = 'at://did:plc:writer/app.bsky.feed.post/3kabc'
afterEach(() => { vi.unstubAllGlobals()})
describe('canonicalArticleUrl', () => { it('joins the publication url and the document path', () => { expect(canonicalArticleUrl(pub, doc)).toBe('https://pub.example.com/blog/posts/a-post') })
it('adds the missing leading slash', () => { expect( canonicalArticleUrl(pub, { ...doc, record: { ...doc.record, path: 'posts/a-post' } }), ).toBe('https://pub.example.com/blog/posts/a-post') })
it('accepts an absolute path and ignores a non-http one', () => { expect( canonicalArticleUrl(pub, { ...doc, record: { ...doc.record, path: 'https://elsewhere.example/a-post' }, }), ).toBe('https://elsewhere.example/a-post') expect( canonicalArticleUrl(pub, { ...doc, record: { ...doc.record, path: 'ftp://elsewhere/a' } }), ).toBeNull() })
it('uses the page when it is on the publication’s origin and the document has no path', () => { const page = 'https://pub.example.com/blog/from-the-page' expect( canonicalArticleUrl(pub, { ...doc, record: { ...doc.record, path: undefined } }, page), ).toBe(page) })
it('falls back to the publication home for another origin or no page', () => { const noPath = { ...doc, record: { ...doc.record, path: undefined } } expect(canonicalArticleUrl(pub, noPath, 'https://reader.example/a')).toBe( 'https://pub.example.com/blog', ) expect(canonicalArticleUrl(pub, noPath)).toBe('https://pub.example.com/blog') })
it('is null when the publication has no linkable url', () => { expect( canonicalArticleUrl( { ...pub, record: { ...pub.record, url: 'javascript:alert(1)' } }, doc, ), ).toBeNull() })})
describe('searchMentionsUrl', () => { it('filters by url and asks the appview for the most relevant posts', () => { const u = new URL(searchMentionsUrl('https://pub.example.com/blog/posts/a-post')) expect(u.origin + u.pathname).toBe('https://api.bsky.app/xrpc/app.bsky.feed.searchPosts') expect(u.searchParams.get('q')).toBe('*') expect(u.searchParams.get('url')).toBe('https://pub.example.com/blog/posts/a-post') expect(u.searchParams.get('sort')).toBe('top') expect(u.searchParams.get('limit')).toBe('10') })})
describe('bskySearchUrl', () => { it('opens bsky.app’s search for the article url', () => { expect(bskySearchUrl('https://pub.example.com/blog/posts/a-post')).toBe( 'https://bsky.app/search?q=https%3A%2F%2Fpub.example.com%2Fblog%2Fposts%2Fa-post', ) })})
describe('mentionUrl', () => { it('opens the post on bsky.app', () => { expect( mentionUrl({ uri: POST_URI, author: { did: 'did:plc:writer' }, text: 'hello', likeCount: 0, replyCount: 0, }), ).toBe('https://bsky.app/profile/did:plc:writer/post/3kabc') })
it('is undefined for a uri that is not a post', () => { expect( mentionUrl({ uri: 'at://did:plc:writer/app.bsky.feed.like/3kabc', author: { did: 'did:plc:writer' }, text: '', likeCount: 0, replyCount: 0, }), ).toBeUndefined() })})
describe('searchMentions', () => { const post = (overrides: Record<string, unknown> = {}) => ({ uri: POST_URI, author: { did: 'did:plc:writer', handle: 'writer.example.com', displayName: ' A Writer ', avatar: 'https://cdn.example.com/avatar.png', }, record: { text: ' niceee atproto.com/blog ', createdAt: '2026-08-21T00:00:00.000Z' }, likeCount: 4, replyCount: 2, ...overrides, })
const stub = (body: unknown, status = 200) => { const fetchMock = vi.fn( async (_input: string) => new Response(JSON.stringify(body), { status }), ) vi.stubGlobal('fetch', fetchMock) return fetchMock }
it('maps post views and keeps the cursor as “more”', async () => { stub({ posts: [post()], cursor: 'next' }) await expect(searchMentions('https://pub.example.com/blog/posts/a-post')).resolves.toEqual({ posts: [ { uri: POST_URI, author: { did: 'did:plc:writer', handle: 'writer.example.com', displayName: 'A Writer', avatarUrl: 'https://cdn.example.com/avatar.png', }, text: 'niceee atproto.com/blog', likeCount: 4, replyCount: 2, }, ], more: true, }) })
it('drops results that are not post views', async () => { stub({ posts: [ post(), { uri: 'https://not-a-post' }, post({ author: { handle: 'no-did.example.com' } }), post({ record: {} }), ], }) const result = await searchMentions('https://pub.example.com/blog/posts/a-post') expect(result.posts).toHaveLength(1) expect(result.more).toBe(false) })
it('refuses an avatar that is not https', async () => { stub({ posts: [post({ author: { did: 'did:plc:writer', avatar: 'http://cdn.example.com/a.png' } })], }) const result = await searchMentions('https://pub.example.com/blog/posts/a-post') expect(result.posts[0]?.author.avatarUrl).toBeUndefined() })
it('throws when the appview refuses the search', async () => { stub({ error: 'Forbidden' }, 403) await expect(searchMentions('https://pub.example.com/blog/posts/a-post')).rejects.toThrow() })})