Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals'
import { acceptGroupInvite, GroupInviteError, previewGroupInvite,} from '../group-invites'import {getServiceAuthToken} from '../service-auth'
jest.mock('../service-auth', () => ({ getServiceAuthToken: jest.fn(),}))
const mockGetServiceAuthToken = jest.mocked(getServiceAuthToken)const fetchMock = jest.fn<typeof fetch>()const CODE = 'a'.repeat(43)const COMMUNITY_DID = 'did:plc:community'
function response(body: unknown, ok = true, status = 200) { return {ok, status, json: () => Promise.resolve(body)} as Response}
describe('group invite client API', () => { beforeEach(() => { global.fetch = fetchMock fetchMock.mockReset() mockGetServiceAuthToken.mockReset() })
afterEach(() => { jest.restoreAllMocks() })
it('previews through the public Acorn tRPC query', async () => { fetchMock.mockResolvedValue( response({ result: { data: { json: { community: {did: COMMUNITY_DID, name: 'Test Community'}, group: {name: 'members', displayName: 'Members'}, expiresAt: new Date().toISOString(), }, }, }, }), )
await expect(previewGroupInvite(CODE)).resolves.toEqual({ community: {did: COMMUNITY_DID, name: 'Test Community'}, group: {name: 'members', displayName: 'Members'}, expiresAt: expect.any(String), }) expect(fetchMock).toHaveBeenCalledWith( expect.not.stringContaining('?input='), expect.objectContaining({ method: 'POST', body: JSON.stringify({code: CODE}), }), ) })
it('requests method-bound service auth for the invite community and sends it', async () => { mockGetServiceAuthToken.mockResolvedValue('service-token') fetchMock.mockResolvedValue( response({ result: { data: { json: { communityDid: COMMUNITY_DID, groupName: 'members', membership: 'joined', feeds: [ { uri: 'at://feed/one', name: 'One', canView: true, canPost: false, }, ], }, }, }, }), ) const agent = {} as Parameters<typeof getServiceAuthToken>[0]['agent']
await expect( acceptGroupInvite({code: CODE, communityDid: COMMUNITY_DID, agent}), ).resolves.toMatchObject({membership: 'joined'}) expect(mockGetServiceAuthToken).toHaveBeenCalledWith( expect.objectContaining({ agent, aud: COMMUNITY_DID, lxm: 'community.blacksky.group.acceptInvite', }), ) expect(fetchMock.mock.calls[0][1]).toEqual( expect.objectContaining({ method: 'POST', headers: expect.objectContaining({ Authorization: 'Bearer service-token', }), }), ) })
it('turns an expired-session service-auth response into reauthentication', async () => { mockGetServiceAuthToken.mockRejectedValue({status: 401})
await expect( acceptGroupInvite({ code: CODE, communityDid: COMMUNITY_DID, agent: {} as Parameters<typeof getServiceAuthToken>[0]['agent'], }), ).rejects.toMatchObject({code: GroupInviteError.AuthenticationRequired}) expect(fetchMock).not.toHaveBeenCalled() })
it('normalizes stable Acorn errors without exposing raw server messages', async () => { fetchMock.mockResolvedValue( response( { error: { json: { data: {code: 'INVITE_UNAVAILABLE'}, message: 'internal row details', }, }, }, false, 410, ), )
await expect(previewGroupInvite(CODE)).rejects.toMatchObject({ code: GroupInviteError.InviteUnavailable, }) })
it('prefers Acorn stable messages over generic tRPC transport codes', async () => { fetchMock.mockResolvedValue( response( { error: { json: { message: 'InviteMisconfigured', data: {code: 'INTERNAL_SERVER_ERROR'}, }, }, }, false, 500, ), )
await expect(previewGroupInvite(CODE)).rejects.toMatchObject({ code: 'InviteMisconfigured', }) })
it('rejects a malformed successful acceptance payload as unavailable', async () => { mockGetServiceAuthToken.mockResolvedValue('service-token') fetchMock.mockResolvedValue( response({ result: { data: { json: {membership: 'joined', feeds: 'not-an-array'}, }, }, }), )
await expect( acceptGroupInvite({ code: CODE, communityDid: COMMUNITY_DID, agent: {} as Parameters<typeof getServiceAuthToken>[0]['agent'], }), ).rejects.toMatchObject({code: GroupInviteError.AuthorizationUnavailable}) })})