|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { jina } from '../jina'; |
| 4 | + |
| 5 | +describe('jina crawler', () => { |
| 6 | + const mockFetch = vi.fn(); |
| 7 | + global.fetch = mockFetch; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + vi.resetAllMocks(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should crawl url successfully', async () => { |
| 14 | + const mockResponse = { |
| 15 | + ok: true, |
| 16 | + json: () => |
| 17 | + Promise.resolve({ |
| 18 | + code: 200, |
| 19 | + data: { |
| 20 | + content: 'test content', |
| 21 | + description: 'test description', |
| 22 | + siteName: 'test site', |
| 23 | + title: 'test title', |
| 24 | + }, |
| 25 | + }), |
| 26 | + }; |
| 27 | + |
| 28 | + mockFetch.mockResolvedValue(mockResponse); |
| 29 | + |
| 30 | + const result = await jina('https://example.com', { |
| 31 | + apiKey: 'test-key', |
| 32 | + filterOptions: {}, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(mockFetch).toHaveBeenCalledWith('https://r.jina.ai/https://example.com', { |
| 36 | + headers: { |
| 37 | + 'Accept': 'application/json', |
| 38 | + 'Authorization': 'Bearer test-key', |
| 39 | + 'x-send-from': 'LobeChat Community', |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result).toEqual({ |
| 44 | + content: 'test content', |
| 45 | + contentType: 'text', |
| 46 | + description: 'test description', |
| 47 | + length: 12, |
| 48 | + siteName: 'test site', |
| 49 | + title: 'test title', |
| 50 | + url: 'https://example.com', |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should use JINA_READER_API_KEY from env if apiKey not provided', async () => { |
| 55 | + process.env.JINA_READER_API_KEY = 'env-reader-key'; |
| 56 | + |
| 57 | + const mockResponse = { |
| 58 | + ok: true, |
| 59 | + json: () => |
| 60 | + Promise.resolve({ |
| 61 | + code: 200, |
| 62 | + data: { |
| 63 | + content: 'test content', |
| 64 | + }, |
| 65 | + }), |
| 66 | + }; |
| 67 | + |
| 68 | + mockFetch.mockResolvedValue(mockResponse); |
| 69 | + |
| 70 | + await jina('https://example.com', { filterOptions: {} }); |
| 71 | + |
| 72 | + expect(mockFetch).toHaveBeenCalledWith('https://r.jina.ai/https://example.com', { |
| 73 | + headers: { |
| 74 | + 'Accept': 'application/json', |
| 75 | + 'Authorization': 'Bearer env-reader-key', |
| 76 | + 'x-send-from': 'LobeChat Community', |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + delete process.env.JINA_READER_API_KEY; |
| 81 | + }); |
| 82 | + |
| 83 | + it('should use JINA_API_KEY from env if apiKey and JINA_READER_API_KEY not provided', async () => { |
| 84 | + process.env.JINA_API_KEY = 'env-key'; |
| 85 | + |
| 86 | + const mockResponse = { |
| 87 | + ok: true, |
| 88 | + json: () => |
| 89 | + Promise.resolve({ |
| 90 | + code: 200, |
| 91 | + data: { |
| 92 | + content: 'test content', |
| 93 | + }, |
| 94 | + }), |
| 95 | + }; |
| 96 | + |
| 97 | + mockFetch.mockResolvedValue(mockResponse); |
| 98 | + |
| 99 | + await jina('https://example.com', { filterOptions: {} }); |
| 100 | + |
| 101 | + expect(mockFetch).toHaveBeenCalledWith('https://r.jina.ai/https://example.com', { |
| 102 | + headers: { |
| 103 | + 'Accept': 'application/json', |
| 104 | + 'Authorization': 'Bearer env-key', |
| 105 | + 'x-send-from': 'LobeChat Community', |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + delete process.env.JINA_API_KEY; |
| 110 | + }); |
| 111 | + |
| 112 | + it('should send empty Authorization header if no api key provided', async () => { |
| 113 | + const mockResponse = { |
| 114 | + ok: true, |
| 115 | + json: () => |
| 116 | + Promise.resolve({ |
| 117 | + code: 200, |
| 118 | + data: { |
| 119 | + content: 'test content', |
| 120 | + }, |
| 121 | + }), |
| 122 | + }; |
| 123 | + |
| 124 | + mockFetch.mockResolvedValue(mockResponse); |
| 125 | + |
| 126 | + await jina('https://example.com', { filterOptions: {} }); |
| 127 | + |
| 128 | + expect(mockFetch).toHaveBeenCalledWith('https://r.jina.ai/https://example.com', { |
| 129 | + headers: { |
| 130 | + 'Accept': 'application/json', |
| 131 | + 'Authorization': '', |
| 132 | + 'x-send-from': 'LobeChat Community', |
| 133 | + }, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should return undefined if response is not ok', async () => { |
| 138 | + mockFetch.mockResolvedValue({ ok: false }); |
| 139 | + |
| 140 | + const result = await jina('https://example.com', { filterOptions: {} }); |
| 141 | + |
| 142 | + expect(result).toBeUndefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should return undefined if response code is not 200', async () => { |
| 146 | + const mockResponse = { |
| 147 | + ok: true, |
| 148 | + json: () => |
| 149 | + Promise.resolve({ |
| 150 | + code: 400, |
| 151 | + message: 'Bad Request', |
| 152 | + }), |
| 153 | + }; |
| 154 | + |
| 155 | + mockFetch.mockResolvedValue(mockResponse); |
| 156 | + |
| 157 | + const result = await jina('https://example.com', { filterOptions: {} }); |
| 158 | + |
| 159 | + expect(result).toBeUndefined(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should return undefined if fetch throws error', async () => { |
| 163 | + mockFetch.mockRejectedValue(new Error('Network error')); |
| 164 | + |
| 165 | + const result = await jina('https://example.com', { filterOptions: {} }); |
| 166 | + |
| 167 | + expect(result).toBeUndefined(); |
| 168 | + }); |
| 169 | +}); |
0 commit comments