Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306import {type Client} from '@atproto/lex'import {type QueryClient} from '@tanstack/react-query'
import {post, quotedSpace, resolveReply} from '../index'import {postToSpace} from '../space-post'
// `post()`'s module graph reaches the image picker, which pulls in native UI// modules that cannot load under jest. Only the write routing is under test.jest.mock('react-native-uuid', () => ({ __esModule: true, default: {v4: () => 'test-device-id'},}))jest.mock('#/state/gallery', () => ({compressImage: jest.fn()}))jest.mock('#/state/queries/resolve-link', () => ({ fetchResolveGifQuery: jest.fn(), fetchResolveLinkQuery: jest.fn(),}))jest.mock('#/state/queries/threadgate', () => ({ createThreadgateRecord: jest.fn(), threadgateAllowUISettingToAllowRecordValue: jest.fn(() => []),}))
// The public write path hashes the record through ESM-only IPLD packages// that jest cannot load; the CID value is not under test here.jest.mock( '@ipld/dag-cbor', () => ({encode: (v: unknown) => Buffer.from(JSON.stringify(v))}), {virtual: true},)jest.mock('multiformats/cid', () => ({ CID: {createV1: () => ({toString: () => 'bafyreifake'})},}))
jest.mock('../space-post', () => ({ postToSpace: jest.fn(() => Promise.resolve({uris: ['at://space/post']})),}))
const SPACE = 'at://did:plc:community/space/community.blacksky.feed/private'
const feedConfig = (space?: string) => ({ $type: 'community.blacksky.feed.config', contentType: 'communityRecord', visibility: 'gated', group: 'at://did:plc:community/community.blacksky.group/community', createdAt: '2026-08-09T12:00:00.000Z', ...(space ? {space} : {}),})
const threadWith = (space?: string) => ({ posts: [], postgate: {}, threadgate: [], blackskyOnly: false, communityFeed: { feed: 'at://did:plc:community/app.bsky.feed.generator/3m2feed', name: 'Private', serviceDid: 'did:web:feeds.example.com', config: feedConfig(space), }, }) as never
function mockClient() { const fetchHandler = jest.fn() const applyWrites = jest.fn((_input: unknown) => Promise.resolve({})) const client = { did: 'did:plc:alice', assertDid: 'did:plc:alice', agent: {fetchHandler}, call: (_method: unknown, input: unknown) => applyWrites(input), } as unknown as Client return { client, clients: {appviewClient: client, chatClient: client, pdsClient: client}, fetchHandler, applyWrites, }}
const queryClient = {invalidateQueries: jest.fn()} as unknown as QueryClient
describe('post routing', () => { beforeEach(() => jest.clearAllMocks())
it('sends a space-backed feed down the space write path', async () => { const {clients, fetchHandler, applyWrites} = mockClient()
await post(queryClient, {...clients, thread: threadWith(SPACE)})
expect(postToSpace).toHaveBeenCalledWith(queryClient, SPACE, { ...clients, thread: threadWith(SPACE), }) // Nothing may reach the appview or the public repo on this path. expect(fetchHandler).not.toHaveBeenCalled() expect(applyWrites).not.toHaveBeenCalled() })
it('leaves a feed without a space on the community path', async () => { const {clients, applyWrites} = mockClient()
await post(queryClient, {...clients, thread: threadWith(undefined)})
expect(postToSpace).not.toHaveBeenCalled() expect(applyWrites).toHaveBeenCalled() })})
describe('self labels', () => { beforeEach(() => jest.clearAllMocks())
it('publishes a text-only post with its self labels', async () => { const {clients, applyWrites} = mockClient()
await post(queryClient, { ...clients, thread: { posts: [ { richtext: {text: 'words only', facets: []}, shortenedGraphemeLength: 10, labels: ['porn'], embed: {}, }, ], postgate: {}, threadgate: [], blackskyOnly: false, } as never, })
const [{writes}] = applyWrites.mock.calls[0] as unknown as [ {writes: {value: {labels?: unknown}}[]}, ] expect(writes[0].value.labels).toEqual({ $type: 'com.atproto.label.defs#selfLabels', values: [{val: 'porn'}], }) })})
describe('quoting a space post from outside the space', () => { const SPACE_POST = `${SPACE}/did:plc:bob/app.bsky.feed.post/3kabc` const permalink = `https://blacksky.community/profile/did:plc:bob/post/3kabc?space=${encodeURIComponent( SPACE, )}`
const threadQuoting = (uri: string, space?: string) => ({ ...(threadWith(space) as object), posts: [ { richtext: {text: '', facets: []}, shortenedGraphemeLength: 0, labels: [], embed: {quote: {type: 'link', uri}}, }, ], }) as never
beforeEach(() => jest.clearAllMocks())
// The link form is what the composer actually holds: it is only expanded to // the at:// form at publish, after routing has already decided. it.each([ ['a pasted permalink', permalink], ['an at:// space record uri', SPACE_POST], ])( 'refuses %s rather than writing it to the public repo', async (_n, uri) => { const {clients, applyWrites, fetchHandler} = mockClient()
await expect( post(queryClient, {...clients, thread: threadQuoting(uri)}), ).rejects.toThrow(/private post/)
expect(applyWrites).not.toHaveBeenCalled() expect(fetchHandler).not.toHaveBeenCalled() expect(postToSpace).not.toHaveBeenCalled() }, )
it.each([ ['opened from the space post', {communitySpaceUri: SPACE}], ['composing into the space-backed feed', {}], ])( 'allows the quote when the post goes into that space (%s)', async (_n, extra) => { const {clients} = mockClient()
await post(queryClient, { ...clients, thread: { ...(threadQuoting(permalink, SPACE) as object), ...extra, } as never, })
expect(postToSpace).toHaveBeenCalled() }, )})
describe('quotedSpace', () => { const OTHER = 'at://did:plc:community/space/community.blacksky.feed/other'
it('reads the space out of either form', () => { expect(quotedSpace(`${SPACE}/did:plc:bob/app.bsky.feed.post/3kabc`)).toBe( SPACE, ) expect( quotedSpace( `/profile/did:plc:bob/post/3kabc?space=${encodeURIComponent(OTHER)}`, ), ).toBe(OTHER) })
it.each([ ['a public post', 'at://did:plc:bob/app.bsky.feed.post/3kabc'], ['a community stub', 'at://did:plc:bob/community.blacksky.feed.post/3kabc'], ['a bare space uri with no record', SPACE], ['nothing', undefined], ])('returns null for %s', (_n, uri) => { expect(quotedSpace(uri)).toBeNull() })})
describe('space replies', () => { it('passes the reply target down to the space write path', async () => { const {clients} = mockClient() const parent = `${SPACE}/did:plc:bob/app.bsky.feed.post/3kparent`
await post(queryClient, { ...clients, thread: threadWith(SPACE), replyTo: parent, })
// postToSpace resolves the parent itself; what matters here is that the // reply is routed into the space at all rather than posted publicly. expect(postToSpace).toHaveBeenCalledWith(queryClient, SPACE, { ...clients, thread: threadWith(SPACE), replyTo: parent, }) })
it('reconstructs the space target when the reply view omitted it', async () => { const {clients} = mockClient() const parent = `${SPACE}/did:plc:bob/app.bsky.feed.post/3kparent` const thread = threadWith(undefined)
await post(queryClient, {...clients, thread, replyTo: parent})
expect(postToSpace).toHaveBeenCalledWith(queryClient, SPACE, { ...clients, thread, replyTo: parent, }) })})
describe('resolveReply public-path guard', () => { const publicParent = 'at://did:plc:bob/app.bsky.feed.post/3kparent' const spaceRecord = `${SPACE}/did:plc:bob/app.bsky.feed.post/3kroot`
const clientReturningRoot = (rootUri: string) => ({ call: jest.fn(() => Promise.resolve({ posts: [ { uri: publicParent, cid: 'bafyreiparent', record: { $type: 'app.bsky.feed.post', text: 'hi', createdAt: '2026-08-09T12:00:00.000Z', reply: { root: {uri: rootUri, cid: 'bafyreiroot'}, parent: {uri: publicParent, cid: 'bafyreiparent'}, }, }, }, ], }), ), }) as unknown as Client
it('refuses to hand a space thread root to the public write path', async () => { await expect( resolveReply(clientReturningRoot(spaceRecord), publicParent), ).rejects.toThrow(/private post/i) })
it('passes an ordinary thread root through', async () => { const root = 'at://did:plc:bob/app.bsky.feed.post/3kroot' await expect( resolveReply(clientReturningRoot(root), publicParent), ).resolves.toEqual({ root: {uri: root, cid: 'bafyreiroot'}, parent: {uri: publicParent, cid: 'bafyreiparent'}, }) })})