|
| 1 | +import { webFetch } from './fetch-page'; |
| 2 | + |
| 3 | +const mockFetch = vi.fn<typeof fetch>(); |
| 4 | + |
| 5 | +describe('fetch-page', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.clearAllMocks(); |
| 8 | + vi.stubGlobal('fetch', mockFetch); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('webFetch', () => { |
| 12 | + it('returns error for empty URL', async () => { |
| 13 | + const result = await webFetch(' '); |
| 14 | + expect(result.error).toBe('URL cannot be empty'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns content from Jina Reader on success', async () => { |
| 18 | + mockFetch.mockResolvedValueOnce( |
| 19 | + createFetchResponse('# Page Title\n\nSome content.', 200), |
| 20 | + ); |
| 21 | + |
| 22 | + const result = await webFetch('https://example.com'); |
| 23 | + expect(result.content).toBe('# Page Title\n\nSome content.'); |
| 24 | + expect(result.error).toBeUndefined(); |
| 25 | + const calledUrl = (mockFetch.mock.calls[0] as [string, ...unknown[]])[0]; |
| 26 | + expect(calledUrl).toBe('https://r.jina.ai/https://example.com'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('falls back to direct fetch when Jina Reader fails', async () => { |
| 30 | + mockFetch |
| 31 | + .mockRejectedValueOnce(new Error('rate limited')) |
| 32 | + .mockResolvedValueOnce( |
| 33 | + createFetchResponse( |
| 34 | + '<html><body><p>Direct content</p></body></html>', |
| 35 | + 200, |
| 36 | + ), |
| 37 | + ); |
| 38 | + |
| 39 | + const result = await webFetch('https://example.com'); |
| 40 | + expect(result.content).toContain('Note: Jina Reader unavailable'); |
| 41 | + expect(result.content).toContain('Direct content'); |
| 42 | + expect(result.error).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns error when both Jina and fallback fail', async () => { |
| 46 | + mockFetch |
| 47 | + .mockRejectedValueOnce(new Error('network error')) |
| 48 | + .mockRejectedValueOnce(new Error('also failed')); |
| 49 | + |
| 50 | + const result = await webFetch('https://example.com'); |
| 51 | + expect(result.error).toContain('Failed to fetch page: also failed'); |
| 52 | + expect(result.content).toBe(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns error when both fail with non-Error rejection', async () => { |
| 56 | + mockFetch |
| 57 | + .mockRejectedValueOnce('jina string error') |
| 58 | + .mockRejectedValueOnce('fallback string error'); |
| 59 | + |
| 60 | + const result = await webFetch('https://example.com'); |
| 61 | + expect(result.error).toContain( |
| 62 | + 'Failed to fetch page: fallback string error', |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns error when fallback responds with non-OK status', async () => { |
| 67 | + mockFetch |
| 68 | + .mockRejectedValueOnce(new Error('jina down')) |
| 69 | + .mockResolvedValueOnce(createFetchResponse('bad gateway', 502)); |
| 70 | + |
| 71 | + const result = await webFetch('https://example.com'); |
| 72 | + expect(result.error).toContain('Failed to fetch page: HTTP 502'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('strips HTML tags in fallback content', async () => { |
| 76 | + mockFetch |
| 77 | + .mockRejectedValueOnce(new Error('jina error')) |
| 78 | + .mockResolvedValueOnce( |
| 79 | + createFetchResponse( |
| 80 | + '<html><head><title>Title</title></head><body><h1>Heading</h1><p>Paragraph text.</p></body></html>', |
| 81 | + 200, |
| 82 | + ), |
| 83 | + ); |
| 84 | + |
| 85 | + const result = await webFetch('https://example.com'); |
| 86 | + expect(result.content).toContain('Heading'); |
| 87 | + expect(result.content).toContain('Paragraph text.'); |
| 88 | + expect(result.content).not.toContain('<h1>'); |
| 89 | + expect(result.content).not.toContain('<p>'); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +function createFetchResponse(body: string, status: number): Response { |
| 95 | + return { |
| 96 | + ok: status >= 200 && status < 300, |
| 97 | + status, |
| 98 | + text: vi.fn().mockResolvedValue(body), |
| 99 | + } as unknown as Response; |
| 100 | +} |
0 commit comments