|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { FarcasterClient } from '../src/client'; |
| 3 | +import { NeynarAPIClient } from '@neynar/nodejs-sdk'; |
| 4 | + |
| 5 | +// Mock dependencies |
| 6 | +vi.mock('@neynar/nodejs-sdk', () => ({ |
| 7 | + NeynarAPIClient: vi.fn().mockImplementation(() => ({ |
| 8 | + publishCast: vi.fn().mockResolvedValue({ |
| 9 | + success: true, |
| 10 | + cast: { |
| 11 | + hash: 'cast-1', |
| 12 | + author: { fid: '123' }, |
| 13 | + text: 'Test cast', |
| 14 | + timestamp: '2025-01-20T20:00:00Z' |
| 15 | + } |
| 16 | + }), |
| 17 | + fetchBulkUsers: vi.fn().mockResolvedValue({ |
| 18 | + users: [{ |
| 19 | + fid: '123', |
| 20 | + username: 'test.farcaster', |
| 21 | + display_name: 'Test User', |
| 22 | + pfp: { |
| 23 | + url: 'https://example.com/pic.jpg' |
| 24 | + } |
| 25 | + }] |
| 26 | + }), |
| 27 | + fetchCastsForUser: vi.fn().mockResolvedValue({ |
| 28 | + casts: [ |
| 29 | + { |
| 30 | + hash: 'cast-1', |
| 31 | + author: { |
| 32 | + fid: '123', |
| 33 | + username: 'test.farcaster', |
| 34 | + display_name: 'Test User' |
| 35 | + }, |
| 36 | + text: 'Test cast', |
| 37 | + timestamp: '2025-01-20T20:00:00Z' |
| 38 | + } |
| 39 | + ] |
| 40 | + }) |
| 41 | + })) |
| 42 | +})); |
| 43 | + |
| 44 | +describe('FarcasterClient', () => { |
| 45 | + let client: FarcasterClient; |
| 46 | + const mockRuntime = { |
| 47 | + name: 'test-runtime', |
| 48 | + memory: new Map(), |
| 49 | + getMemory: vi.fn(), |
| 50 | + setMemory: vi.fn(), |
| 51 | + clearMemory: vi.fn() |
| 52 | + }; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks(); |
| 56 | + client = new FarcasterClient({ |
| 57 | + runtime: mockRuntime, |
| 58 | + url: 'https://api.example.com', |
| 59 | + ssl: true, |
| 60 | + neynar: new NeynarAPIClient({ apiKey: 'test-key' }), |
| 61 | + signerUuid: 'test-signer', |
| 62 | + cache: new Map(), |
| 63 | + farcasterConfig: { |
| 64 | + apiKey: 'test-key', |
| 65 | + signerUuid: 'test-signer' |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('loadCastFromNeynarResponse', () => { |
| 71 | + it('should load cast from Neynar response', async () => { |
| 72 | + const neynarResponse = { |
| 73 | + hash: 'cast-1', |
| 74 | + author: { fid: '123' }, |
| 75 | + text: 'Test cast', |
| 76 | + timestamp: '2025-01-20T20:00:00Z' |
| 77 | + }; |
| 78 | + |
| 79 | + const cast = await client.loadCastFromNeynarResponse(neynarResponse); |
| 80 | + expect(cast).toBeDefined(); |
| 81 | + expect(cast.hash).toBe('cast-1'); |
| 82 | + expect(cast.authorFid).toBe('123'); |
| 83 | + expect(cast.text).toBe('Test cast'); |
| 84 | + expect(cast.profile).toBeDefined(); |
| 85 | + expect(cast.profile.fid).toBe('123'); |
| 86 | + expect(cast.profile.username).toBe('test.farcaster'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle cast with parent', async () => { |
| 90 | + const neynarResponse = { |
| 91 | + hash: 'cast-2', |
| 92 | + author: { fid: '123' }, |
| 93 | + text: 'Reply cast', |
| 94 | + parent_hash: 'cast-1', |
| 95 | + parent_author: { fid: '456' }, |
| 96 | + timestamp: '2025-01-20T20:00:00Z' |
| 97 | + }; |
| 98 | + |
| 99 | + const cast = await client.loadCastFromNeynarResponse(neynarResponse); |
| 100 | + expect(cast.inReplyTo).toBeDefined(); |
| 101 | + expect(cast.inReplyTo?.hash).toBe('cast-1'); |
| 102 | + expect(cast.inReplyTo?.fid).toBe('456'); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('getProfile', () => { |
| 107 | + it('should fetch profile successfully', async () => { |
| 108 | + const profile = await client.getProfile('123'); |
| 109 | + expect(profile).toBeDefined(); |
| 110 | + expect(profile.fid).toBe('123'); |
| 111 | + expect(profile.username).toBe('test.farcaster'); |
| 112 | + expect(profile.name).toBe('Test User'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle profile fetch errors', async () => { |
| 116 | + vi.mocked(client.neynar.fetchBulkUsers).mockRejectedValueOnce(new Error('Profile fetch failed')); |
| 117 | + await expect(client.getProfile('123')).rejects.toThrow('Profile fetch failed'); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('getCastsByFid', () => { |
| 122 | + it('should fetch casts successfully', async () => { |
| 123 | + const casts = await client.getCastsByFid({ fid: '123', pageSize: 10 }); |
| 124 | + expect(casts).toHaveLength(1); |
| 125 | + expect(casts[0].hash).toBe('cast-1'); |
| 126 | + expect(casts[0].authorFid).toBe('123'); |
| 127 | + expect(casts[0].text).toBe('Test cast'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should handle cast fetch errors', async () => { |
| 131 | + vi.mocked(client.neynar.fetchCastsForUser).mockRejectedValueOnce(new Error('Cast fetch failed')); |
| 132 | + await expect(client.getCastsByFid({ fid: '123', pageSize: 10 })).rejects.toThrow('Cast fetch failed'); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments