|
| 1 | +// Copyright (c) Mapbox, Inc. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | +import { BatchGetDocumentsTool } from '../../../src/tools/batch-get-documents-tool/BatchGetDocumentsTool.js'; |
| 6 | +import { docCache } from '../../../src/utils/docCache.js'; |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + docCache.clear(); |
| 10 | +}); |
| 11 | + |
| 12 | +function makeResponse(body: string, status = 200): Response { |
| 13 | + return new Response(body, { |
| 14 | + status, |
| 15 | + headers: { |
| 16 | + 'content-type': 'text/plain', |
| 17 | + 'content-length': String(Buffer.byteLength(body, 'utf8')) |
| 18 | + } |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +describe('BatchGetDocumentsTool', () => { |
| 23 | + describe('intra-batch URL deduplication', () => { |
| 24 | + it('issues only one HTTP request for multiple URLs with the same normalized key', async () => { |
| 25 | + const httpRequest = vi |
| 26 | + .fn() |
| 27 | + .mockResolvedValue(makeResponse('page content')); |
| 28 | + const tool = new BatchGetDocumentsTool({ httpRequest }); |
| 29 | + |
| 30 | + const urls = [ |
| 31 | + 'https://docs.mapbox.com/page?bust=1', |
| 32 | + 'https://docs.mapbox.com/page?bust=2', |
| 33 | + 'https://docs.mapbox.com/page?bust=3' |
| 34 | + ]; |
| 35 | + |
| 36 | + const result = await tool.run({ urls }); |
| 37 | + |
| 38 | + expect(httpRequest).toHaveBeenCalledTimes(1); |
| 39 | + expect(result.isError).toBe(false); |
| 40 | + |
| 41 | + const output = JSON.parse((result.content[0] as { text: string }).text); |
| 42 | + expect(output).toHaveLength(3); |
| 43 | + expect( |
| 44 | + output.every((r: { content: string }) => r.content === 'page content') |
| 45 | + ).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('issues one request per distinct normalized URL', async () => { |
| 49 | + const httpRequest = vi |
| 50 | + .fn() |
| 51 | + .mockResolvedValueOnce(makeResponse('page A')) |
| 52 | + .mockResolvedValueOnce(makeResponse('page B')); |
| 53 | + const tool = new BatchGetDocumentsTool({ httpRequest }); |
| 54 | + |
| 55 | + const urls = [ |
| 56 | + 'https://docs.mapbox.com/a?x=1', |
| 57 | + 'https://docs.mapbox.com/a?x=2', |
| 58 | + 'https://docs.mapbox.com/b?x=1' |
| 59 | + ]; |
| 60 | + |
| 61 | + const result = await tool.run({ urls }); |
| 62 | + |
| 63 | + expect(httpRequest).toHaveBeenCalledTimes(2); |
| 64 | + expect(result.isError).toBe(false); |
| 65 | + |
| 66 | + const output = JSON.parse((result.content[0] as { text: string }).text); |
| 67 | + expect(output[0].content).toBe('page A'); |
| 68 | + expect(output[1].content).toBe('page A'); |
| 69 | + expect(output[2].content).toBe('page B'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('uses cached content and skips fetch for already-cached normalized URL', async () => { |
| 73 | + docCache.set('https://docs.mapbox.com/page', 'cached content'); |
| 74 | + const httpRequest = vi.fn(); |
| 75 | + const tool = new BatchGetDocumentsTool({ httpRequest }); |
| 76 | + |
| 77 | + const result = await tool.run({ |
| 78 | + urls: [ |
| 79 | + 'https://docs.mapbox.com/page?bust=1', |
| 80 | + 'https://docs.mapbox.com/page?bust=2' |
| 81 | + ] |
| 82 | + }); |
| 83 | + |
| 84 | + expect(httpRequest).not.toHaveBeenCalled(); |
| 85 | + const output = JSON.parse((result.content[0] as { text: string }).text); |
| 86 | + expect( |
| 87 | + output.every((r: { content: string }) => r.content === 'cached content') |
| 88 | + ).toBe(true); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('response body size limit', () => { |
| 93 | + it('returns an error for a URL whose Content-Length exceeds the limit', async () => { |
| 94 | + const oversizeHeaders = new Headers({ |
| 95 | + 'content-type': 'text/plain', |
| 96 | + 'content-length': String(3 * 1024 * 1024) // 3 MB > 2 MB limit |
| 97 | + }); |
| 98 | + const httpRequest = vi |
| 99 | + .fn() |
| 100 | + .mockResolvedValue( |
| 101 | + new Response('x', { status: 200, headers: oversizeHeaders }) |
| 102 | + ); |
| 103 | + const tool = new BatchGetDocumentsTool({ httpRequest }); |
| 104 | + |
| 105 | + const result = await tool.run({ |
| 106 | + urls: ['https://docs.mapbox.com/page'] |
| 107 | + }); |
| 108 | + |
| 109 | + expect(result.isError).toBe(false); // batch doesn't fail entirely |
| 110 | + const output = JSON.parse((result.content[0] as { text: string }).text); |
| 111 | + expect(output[0].error).toMatch(/too large/i); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('invalid URLs', () => { |
| 116 | + it('rejects non-mapbox URLs', async () => { |
| 117 | + const httpRequest = vi.fn(); |
| 118 | + const tool = new BatchGetDocumentsTool({ httpRequest }); |
| 119 | + |
| 120 | + const result = await tool.run({ |
| 121 | + urls: ['https://evil.com/page'] |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result.isError).toBe(true); |
| 125 | + expect(httpRequest).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('HTTP errors', () => { |
| 130 | + it('returns per-URL error on non-ok response', async () => { |
| 131 | + const httpRequest = vi |
| 132 | + .fn() |
| 133 | + .mockResolvedValue( |
| 134 | + new Response('Not Found', { status: 404, statusText: 'Not Found' }) |
| 135 | + ); |
| 136 | + const tool = new BatchGetDocumentsTool({ httpRequest }); |
| 137 | + |
| 138 | + const result = await tool.run({ |
| 139 | + urls: ['https://docs.mapbox.com/missing'] |
| 140 | + }); |
| 141 | + |
| 142 | + expect(result.isError).toBe(false); |
| 143 | + const output = JSON.parse((result.content[0] as { text: string }).text); |
| 144 | + expect(output[0].error).toBe('404 Not Found'); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments