import { describe, expect, it } from 'vitest' import { blobUrl, listCollection, listRepoDids, listRecordsUrl, listReposByCollectionUrl, normalizeAtUri, requestJson, resolveDid, } from './relay.mjs' function json(body, status = 200) { return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' }, }) } describe('URL builders', () => { it('escapes the collection and keeps the cursor opaque', () => { const url = new URL(listReposByCollectionUrl('https://relay.test', 'site.standard.document', 'a+b/c', 2000)) expect(url.pathname).toBe('/xrpc/com.atproto.sync.listReposByCollection') expect(url.searchParams.get('collection')).toBe('site.standard.document') expect(url.searchParams.get('cursor')).toBe('a+b/c') expect(url.searchParams.get('limit')).toBe('2000') }) it('builds a listRecords walk for one repository', () => { const url = new URL(listRecordsUrl('https://pds.test', 'did:plc:abc', 'site.standard.document')) expect(url.pathname).toBe('/xrpc/com.atproto.repo.listRecords') expect(url.searchParams.get('repo')).toBe('did:plc:abc') expect(url.searchParams.get('collection')).toBe('site.standard.document') expect(url.searchParams.get('limit')).toBe('100') }) it('builds a blob URL from a CID', () => { const url = new URL(blobUrl('https://pds.test', 'did:plc:abc', { ref: { $link: 'bafy' } })) expect(url.pathname).toBe('/xrpc/com.atproto.sync.getBlob') expect(url.searchParams.get('did')).toBe('did:plc:abc') expect(url.searchParams.get('cid')).toBe('bafy') }) it('has no blob URL without a CID', () => { expect(blobUrl('https://pds.test', 'did:plc:abc', 'not-a-blob')).toBeUndefined() }) }) describe('listRepoDids', () => { it('follows the cursor and yields each page', async () => { const calls = [] const fetchImpl = async (url) => { calls.push(String(url)) const cursor = new URL(url).searchParams.get('cursor') return cursor ? json({ repos: [{ did: 'did:plc:three' }] }) : json({ repos: [{ did: 'did:plc:one' }, { did: 'did:plc:two' }], cursor: 'next' }) } const pages = [] for await (const page of listRepoDids('site.standard.publication', { fetchImpl })) { pages.push(page) } expect(pages).toEqual([['did:plc:one', 'did:plc:two'], ['did:plc:three']]) expect(calls).toHaveLength(2) expect(calls[1]).toContain('cursor=next') }) }) describe('listCollection', () => { it('follows the cursor until the repository ends', async () => { const fetchImpl = async (url) => new URL(url).searchParams.get('cursor') ? json({ records: [{ uri: 'at://a/3' }] }) : json({ records: [{ uri: 'at://a/1' }, { uri: 'at://a/2' }], cursor: 'next' }) const { records, truncated } = await listCollection('https://pds.test', 'did:plc:a', 'x', { fetchImpl, }) expect(records.map((record) => record.uri)).toEqual(['at://a/1', 'at://a/2', 'at://a/3']) expect(truncated).toBe(false) }) it('says when it stopped at the limit', async () => { const fetchImpl = async () => json({ records: [{ uri: 'at://a/1' }, { uri: 'at://a/2' }], cursor: 'next' }) const { records, truncated } = await listCollection('https://pds.test', 'did:plc:a', 'x', { fetchImpl, limit: 2, }) expect(records).toHaveLength(2) expect(truncated).toBe(true) }) }) describe('requestJson', () => { it('retries a 503 and returns the next answer', async () => { let calls = 0 const fetchImpl = async () => { calls++ return calls === 1 ? json({ error: 'busy' }, 503) : json({ ok: true }) } expect(await requestJson('https://example.test', { fetchImpl, retries: 1 })).toEqual({ ok: true, }) expect(calls).toBe(2) }) it('does not retry a 400', async () => { let calls = 0 const fetchImpl = async () => { calls++ return json({ error: 'no' }, 400) } await expect(requestJson('https://example.test', { fetchImpl, retries: 3 })).rejects.toThrow( /400/, ) expect(calls).toBe(1) }) }) describe('resolveDid', () => { it('reads the PDS endpoint and handle from a did:plc document', async () => { const fetchImpl = async (url) => { expect(url).toBe('https://plc.directory/did:plc:abc') return json({ alsoKnownAs: ['at://writer.test'], service: [ { id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'https://pds.test/' }, ], }) } expect(await resolveDid('did:plc:abc', { fetchImpl })).toEqual({ did: 'did:plc:abc', pds: 'https://pds.test', handle: 'writer.test', }) }) it('reads a did:web document from its host', async () => { const fetchImpl = async (url) => { expect(url).toBe('https://writer.test/.well-known/did.json') return json({ service: [{ id: '#atproto_pds', serviceEndpoint: 'https://pds.test' }] }) } expect((await resolveDid('did:web:writer.test', { fetchImpl })).pds).toBe('https://pds.test') }) it('refuses a non-https PDS endpoint', async () => { const fetchImpl = async () => json({ service: [{ id: '#atproto_pds', serviceEndpoint: 'http://pds.test' }] }) await expect(resolveDid('did:plc:abc', { fetchImpl })).rejects.toThrow(/non-https/) }) }) describe('normalizeAtUri', () => { it('leaves a DID authority alone', async () => { expect(await normalizeAtUri('at://did:plc:abc/site.standard.document/1')).toBe( 'at://did:plc:abc/site.standard.document/1', ) }) it('uses a cached handle resolution', async () => { const cache = new Map([['writer.test', 'did:plc:abc']]) expect(await normalizeAtUri('at://writer.test/site.standard.document/1', { handleCache: cache })).toBe( 'at://did:plc:abc/site.standard.document/1', ) }) it('resolves a handle and remembers it', async () => { const cache = new Map() const fetchImpl = async (url) => { expect(String(url)).toContain('resolveHandle') return json({ did: 'did:plc:abc' }) } expect(await normalizeAtUri('at://writer.test/site.standard.document/1', { fetchImpl, handleCache: cache })).toBe( 'at://did:plc:abc/site.standard.document/1', ) expect(cache.get('writer.test')).toBe('did:plc:abc') }) it('returns null for an https site, which is a loose document', async () => { expect(await normalizeAtUri('https://loose.test/post')).toBeNull() }) })