diff --git a/tests/sweetistics-client.test.ts b/tests/sweetistics-client.test.ts index 2a5e6e7..03eb11f 100644 --- a/tests/sweetistics-client.test.ts +++ b/tests/sweetistics-client.test.ts @@ -47,4 +47,106 @@ describe('SweetisticsClient', () => { expect(result.success).toBe(false); expect(result.error).toContain('Unauthorized'); }); + + it('reads a tweet', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + status: 200, + json: async () => ({ + success: true, + tweet: { + id: '1', + text: 'hi', + author: { username: 'u', name: 'User' }, + }, + }), + }); + globalThis.fetch = fetchMock as unknown as typeof fetch; + + const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' }); + const result = await client.read('1'); + + expect(result.success).toBe(true); + expect(result.tweet?.id).toBe('1'); + expect(fetchMock).toHaveBeenCalledWith('https://api.example.com/api/twitter/tweet/1', expect.any(Object)); + }); + + it('fetches replies', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + status: 200, + json: async () => ({ + success: true, + tweets: [{ id: '2', text: 'reply', author: { username: 'r', name: 'Reply' } }], + }), + }); + globalThis.fetch = fetchMock as unknown as typeof fetch; + + const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' }); + const result = await client.replies('1'); + + expect(result.success).toBe(true); + expect(result.tweets?.[0].id).toBe('2'); + expect(fetchMock).toHaveBeenCalledWith( + 'https://api.example.com/api/twitter/tweet/1/replies', + expect.any(Object), + ); + }); + + it('fetches thread', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + status: 200, + json: async () => ({ + success: true, + tweets: [{ id: '1', text: 'root', author: { username: 'u', name: 'User' } }], + }), + }); + globalThis.fetch = fetchMock as unknown as typeof fetch; + + const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' }); + const result = await client.thread('1'); + + expect(result.success).toBe(true); + expect(result.tweets?.length).toBe(1); + expect(fetchMock).toHaveBeenCalledWith( + 'https://api.example.com/api/twitter/tweet/1/thread', + expect.any(Object), + ); + }); + + it('searches tweets', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: true, + status: 200, + json: async () => ({ + success: true, + tweets: [{ id: '3', text: 'needle', author: { username: 'n', name: 'Needle' } }], + }), + }); + globalThis.fetch = fetchMock as unknown as typeof fetch; + + const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' }); + const result = await client.search('needle', 5); + + expect(result.success).toBe(true); + expect(result.tweets?.[0].id).toBe('3'); + const [url] = fetchMock.mock.calls[0]; + expect(url).toBe('https://api.example.com/api/twitter/search?q=needle&count=5'); + }); + + it('propagates Sweetistics error message', async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: false, + status: 500, + json: async () => ({ success: false, error: 'boom' }), + }); + globalThis.fetch = fetchMock as unknown as typeof fetch; + + const client = new SweetisticsClient({ baseUrl: 'https://api.example.com', apiKey: 'sweet-test' }); + const result = await client.read('1'); + + expect(result.success).toBe(false); + expect(result.error).toBe('boom'); + }); });