Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import { afterEach, describe, expect, it, vi } from 'vitest'import { bskyPostRef, bskyThreadUrl, discussionCounts, discussionSummary,} from './discussion'import type { DocumentRecord } from './types'
const POST_URI = 'at://did:plc:author123/app.bsky.feed.post/3kabc'
const doc = { $type: 'site.standard.document', site: 'at://did:plc:author123/site.standard.publication/self', title: 'A post', publishedAt: '2026-01-01T00:00:00.000Z', bskyPostRef: { uri: POST_URI, cid: 'bafcid' },} satisfies DocumentRecord
afterEach(() => { vi.unstubAllGlobals()})
describe('bskyPostRef', () => { it('reads a reference to a Bluesky post', () => { expect(bskyPostRef(doc)).toEqual({ uri: POST_URI }) })
it('ignores a reference to another collection', () => { expect( bskyPostRef({ ...doc, bskyPostRef: { uri: 'at://did:plc:a/app.bsky.feed.like/3k' } }), ).toBeUndefined() })
it('ignores anything malformed', () => { expect(bskyPostRef({ ...doc, bskyPostRef: undefined })).toBeUndefined() expect(bskyPostRef({ ...doc, bskyPostRef: 'nope' })).toBeUndefined() expect(bskyPostRef({ ...doc, bskyPostRef: { uri: 3 } })).toBeUndefined() expect(bskyPostRef({ ...doc, bskyPostRef: { uri: 'https://bsky.app/x' } })).toBeUndefined() })})
describe('bskyThreadUrl', () => { it('opens the post on bsky.app', () => { expect(bskyThreadUrl({ uri: POST_URI })).toBe( 'https://bsky.app/profile/did:plc:author123/post/3kabc', ) })})
describe('discussionSummary', () => { it('names each non-zero count with the right plural', () => { expect(discussionSummary({ replies: 1, reposts: 2, likes: 0 })).toBe('1 reply · 2 reposts') })
it('is empty when nobody has interacted', () => { expect(discussionSummary({ replies: 0, reposts: 0, likes: 0 })).toBe('') })})
describe('discussionCounts', () => { const stub = (posts: unknown[]) => { const fetchMock = vi.fn( async (_input: string) => new Response(JSON.stringify({ posts }), { status: 200 }), ) vi.stubGlobal('fetch', fetchMock) return fetchMock }
it('asks the public appview for the post and reads its counts', async () => { const fetchMock = stub([ { uri: POST_URI, replyCount: 12, repostCount: 3, likeCount: 40 }, ]) await expect(discussionCounts({ uri: POST_URI })).resolves.toEqual({ replies: 12, reposts: 3, likes: 40, }) const called = String(fetchMock.mock.calls[0]?.[0]) expect(called).toContain('public.api.bsky.app/xrpc/app.bsky.feed.getPosts') expect(decodeURIComponent(called)).toContain(`uris=${POST_URI}`) })
it('treats missing or malformed counts as zero', async () => { stub([{ uri: POST_URI, replyCount: -2, repostCount: '3' }]) await expect(discussionCounts({ uri: POST_URI })).resolves.toEqual({ replies: 0, reposts: 0, likes: 0, }) })
it('answers null when the appview no longer has the post', async () => { stub([]) await expect(discussionCounts({ uri: POST_URI })).resolves.toBeNull() })
it('answers null when the appview cannot be reached', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => new Response('down', { status: 503, statusText: 'Unavailable' })), ) await expect(discussionCounts({ uri: POST_URI })).resolves.toBeNull() })})