Something went wrong. Try again.
Explore blocks, labels, lists, and posts on Bluesky. skytrace.aly.town
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136import { describe, expect, it, vi } from 'vitest'import { PublicDataService } from '../src/data/publicData'import { resolveActor } from '../src/data/api/identity'import type { ActorIdentity } from '../src/types'import { createTestQueryClient, jsonResponse as response, repositoryRecord } from './testUtils'
const did = 'did:plc:ewvi7nxzyoun6zhxrhs64oiz'const identity: ActorIdentity = { kind: 'actorIdentity', did, handle: 'atproto.com', pds: 'https://pds.example' }
function stubProfileRecord(body: unknown, status = 200) { vi.stubGlobal( 'fetch', vi.fn(async (input: RequestInfo | URL) => { const url = new URL(String(input)) return url.pathname.endsWith('resolveMiniDoc') ? response({ did, handle: identity.handle, pds: identity.pds, signing_key: 'zQ3test' }) : response(body, status) }), )}
describe('actor data', () => { it('keeps handle.invalid identities and rejects unsafe PDS addresses', async () => { const responseIdentity = { did, handle: 'handle.invalid', pds: 'https://pds.example', signing_key: 'zQ3test' } const fetch = vi .fn() .mockResolvedValueOnce(response(responseIdentity)) .mockResolvedValueOnce(response({ ...responseIdentity, pds: 'http://localhost:3000' })) .mockResolvedValueOnce(response({ ...responseIdentity, pds: 'http://pds.example' })) .mockResolvedValueOnce(response({ ...responseIdentity, pds: 'ftp://localhost' })) vi.stubGlobal('fetch', fetch) await expect(resolveActor(did)).resolves.toMatchObject({ handle: 'handle.invalid' }) await expect(resolveActor(did)).resolves.toMatchObject({ pds: 'http://localhost:3000' }) await expect(resolveActor(did)).rejects.toThrow('unsafe PDS') await expect(resolveActor(did)).rejects.toThrow('unsafe PDS') })
it.each([ ['the exact privacy self-label', [{ val: '!no-unauthenticated' }], true], ['unrelated self-labels', [{ val: 'porn' }, { val: '!NO-UNAUTHENTICATED' }], false], ['no self-labels', undefined, false], ])('reads %s from the profile record', async (_description, labels, expected) => { stubProfileRecord( repositoryRecord(`at://${did}/app.bsky.actor.profile/self`, { $type: 'app.bsky.actor.profile', ...(labels && { labels: { $type: 'com.atproto.label.defs#selfLabels', values: labels, }, }), }), )
const queryClient = createTestQueryClient() const service = new PublicDataService(queryClient) await expect(queryClient.query(service.actors.actorProfileQueryOptions('atproto.com'))).resolves.toMatchObject({ hasNoUnauthenticatedSelfLabel: expected, }) })
it('returns an identity-only profile when the profile record was deleted', async () => { stubProfileRecord({ error: 'NotFound', message: 'Deleted' }, 404) const queryClient = createTestQueryClient() const service = new PublicDataService(queryClient) await expect(queryClient.query(service.actors.actorProfileQueryOptions('atproto.com'))).resolves.toEqual({ kind: 'actorProfile', identity, hasNoUnauthenticatedSelfLabel: false, }) })
it.each([{ displayName: 42 }, { description: ['invalid'] }, { avatar: 'not-a-blob' }])( 'preserves a valid privacy self-label despite invalid profile fields: %j', async (invalidFields) => { stubProfileRecord( repositoryRecord(`at://${did}/app.bsky.actor.profile/self`, { $type: 'app.bsky.actor.profile', ...invalidFields, labels: { $type: 'com.atproto.label.defs#selfLabels', values: [{ val: '!no-unauthenticated' }], }, }), )
const queryClient = createTestQueryClient() const service = new PublicDataService(queryClient) await expect(queryClient.query(service.actors.actorProfileQueryOptions(did))).resolves.toMatchObject({ hasNoUnauthenticatedSelfLabel: true, displayName: undefined, description: undefined, avatarCid: undefined, }) }, )
it('does not read privacy labels from an invalid self-label container', async () => { stubProfileRecord( repositoryRecord(`at://${did}/app.bsky.actor.profile/self`, { $type: 'app.bsky.actor.profile', labels: { $type: 'not-self-labels', values: [{ val: '!no-unauthenticated' }], }, }), )
const queryClient = createTestQueryClient() const service = new PublicDataService(queryClient) await expect(queryClient.query(service.actors.actorProfileQueryOptions(did))).resolves.toMatchObject({ hasNoUnauthenticatedSelfLabel: false, }) })
it('returns an identity-only profile for a malformed profile envelope', async () => { stubProfileRecord({ uri: 'not-an-at-uri', value: {} }) const queryClient = createTestQueryClient() const service = new PublicDataService(queryClient) await expect(queryClient.query(service.actors.actorProfileQueryOptions('atproto.com'))).resolves.toEqual({ kind: 'actorProfile', identity, hasNoUnauthenticatedSelfLabel: false, }) })
it('surfaces transient failures while loading the primary profile', async () => { stubProfileRecord({ error: 'UpstreamFailure', message: 'Try later' }, 503) const queryClient = createTestQueryClient() const service = new PublicDataService(queryClient) await expect(queryClient.query(service.actors.actorProfileQueryOptions('atproto.com'))).rejects.toMatchObject({ status: 503, }) })})