|
| 1 | +import { GetMapboxDocSourceTool } from './GetMapboxDocSourceTool.js'; |
| 2 | + |
| 3 | +// Mock fetch for testing |
| 4 | +global.fetch = jest.fn(); |
| 5 | + |
| 6 | +describe('GetMapboxDocSourceTool', () => { |
| 7 | + let tool: GetMapboxDocSourceTool; |
| 8 | + const mockFetch = global.fetch as jest.MockedFunction<typeof fetch>; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + tool = new GetMapboxDocSourceTool(); |
| 12 | + mockFetch.mockClear(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should have correct name and description', () => { |
| 16 | + expect(tool.name).toBe('get_mapbox_doc_source_tool'); |
| 17 | + expect(tool.description).toContain('Fetch and return Mapbox documentation'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should successfully fetch documentation content', async () => { |
| 21 | + const mockContent = `# Mapbox Documentation |
| 22 | + |
| 23 | +This is the Mapbox developer documentation for LLMs. |
| 24 | +
|
| 25 | +## Web SDKs |
| 26 | +- Mapbox GL JS for interactive maps |
| 27 | +- Mobile SDKs for iOS and Android |
| 28 | +
|
| 29 | +## APIs |
| 30 | +- Geocoding API for address search |
| 31 | +- Directions API for routing`; |
| 32 | + |
| 33 | + mockFetch.mockResolvedValueOnce({ |
| 34 | + ok: true, |
| 35 | + status: 200, |
| 36 | + text: () => Promise.resolve(mockContent) |
| 37 | + } as Response); |
| 38 | + |
| 39 | + const result = await tool.run({}); |
| 40 | + |
| 41 | + expect(mockFetch).toHaveBeenCalledWith('https://docs.mapbox.com/llms.txt'); |
| 42 | + expect(result.content).toHaveLength(1); |
| 43 | + expect(result.content[0].type).toBe('text'); |
| 44 | + |
| 45 | + if (result.content[0].type === 'text') { |
| 46 | + expect(result.content[0].text).toBe(mockContent); |
| 47 | + } |
| 48 | + expect(result.isError).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle HTTP errors', async () => { |
| 52 | + mockFetch.mockResolvedValueOnce({ |
| 53 | + ok: false, |
| 54 | + status: 404 |
| 55 | + } as Response); |
| 56 | + |
| 57 | + const result = await tool.run({}); |
| 58 | + |
| 59 | + expect(result.isError).toBe(true); |
| 60 | + expect(result.content[0].type).toBe('text'); |
| 61 | + |
| 62 | + if (result.content[0].type === 'text') { |
| 63 | + expect(result.content[0].text).toContain( |
| 64 | + 'Failed to fetch Mapbox documentation' |
| 65 | + ); |
| 66 | + expect(result.content[0].text).toContain('HTTP error! status: 404'); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle network errors', async () => { |
| 71 | + mockFetch.mockRejectedValueOnce(new Error('Network error')); |
| 72 | + |
| 73 | + const result = await tool.run({}); |
| 74 | + |
| 75 | + expect(result.isError).toBe(true); |
| 76 | + expect(result.content[0].type).toBe('text'); |
| 77 | + |
| 78 | + if (result.content[0].type === 'text') { |
| 79 | + expect(result.content[0].text).toContain( |
| 80 | + 'Failed to fetch Mapbox documentation' |
| 81 | + ); |
| 82 | + expect(result.content[0].text).toContain('Network error'); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle unknown errors', async () => { |
| 87 | + mockFetch.mockRejectedValueOnce('Unknown error'); |
| 88 | + |
| 89 | + const result = await tool.run({}); |
| 90 | + |
| 91 | + expect(result.isError).toBe(true); |
| 92 | + expect(result.content[0].type).toBe('text'); |
| 93 | + |
| 94 | + if (result.content[0].type === 'text') { |
| 95 | + expect(result.content[0].text).toContain( |
| 96 | + 'Failed to fetch Mapbox documentation' |
| 97 | + ); |
| 98 | + expect(result.content[0].text).toContain('Unknown error occurred'); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + it('should work with empty input object', async () => { |
| 103 | + const mockContent = 'Test documentation content'; |
| 104 | + |
| 105 | + mockFetch.mockResolvedValueOnce({ |
| 106 | + ok: true, |
| 107 | + status: 200, |
| 108 | + text: () => Promise.resolve(mockContent) |
| 109 | + } as Response); |
| 110 | + |
| 111 | + const result = await tool.run({}); |
| 112 | + |
| 113 | + expect(result.isError).toBe(false); |
| 114 | + expect(result.content).toHaveLength(1); |
| 115 | + expect(result.content[0].type).toBe('text'); |
| 116 | + }); |
| 117 | +}); |
0 commit comments