Sifa professional network API (Fastify, AT Protocol, Jetstream) sifa.id/
at main 49 lines 1.5 kB view raw
1import { describe, it, expect, vi, beforeEach } from 'vitest'; 2 3const mockGetProfile = vi.fn(); 4vi.mock('@atproto/api', () => ({ 5 Agent: class MockAgent { 6 getProfile = mockGetProfile; 7 constructor(_url: string) {} 8 }, 9})); 10 11import { Agent } from '@atproto/api'; 12 13describe('POST /api/profile/refresh-pds', () => { 14 beforeEach(() => { 15 vi.clearAllMocks(); 16 }); 17 18 it('should call Bluesky public API and return updated fields', async () => { 19 mockGetProfile.mockResolvedValueOnce({ 20 data: { 21 handle: 'alice.bsky.social', 22 displayName: 'Alice Updated', 23 avatar: 'https://cdn.bsky.app/img/avatar/new.jpg', 24 }, 25 }); 26 27 const agent = new Agent('https://public.api.bsky.app'); 28 const result = await agent.getProfile( 29 { actor: 'did:plc:abc123' }, 30 { signal: AbortSignal.timeout(5000) }, 31 ); 32 33 expect(result.data.displayName).toBe('Alice Updated'); 34 expect(result.data.avatar).toBe('https://cdn.bsky.app/img/avatar/new.jpg'); 35 expect(mockGetProfile).toHaveBeenCalledWith( 36 { actor: 'did:plc:abc123' }, 37 expect.objectContaining({ signal: expect.any(AbortSignal) }), 38 ); 39 }); 40 41 it('should handle Bluesky API timeout gracefully', async () => { 42 mockGetProfile.mockRejectedValueOnce(new Error('AbortError')); 43 44 const agent = new Agent('https://public.api.bsky.app'); 45 await expect( 46 agent.getProfile({ actor: 'did:plc:abc123' }, { signal: AbortSignal.timeout(5000) }), 47 ).rejects.toThrow('AbortError'); 48 }); 49});