import { 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() }) })