| 1 | import { expect, test, vi } from "vitest"; |
| 2 | import { fetchUrlContentImpl } from "./fetchUrlContent"; |
| 3 | |
| 4 | vi.mock("../../context/providers/URLContextProvider", () => ({ |
| 5 | getUrlContextItems: vi.fn(), |
| 6 | })); |
| 7 | |
| 8 | import { ToolExtras } from "../.."; |
| 9 | import { getUrlContextItems } from "../../context/providers/URLContextProvider"; |
| 10 | const mockExtras = { |
| 11 | fetch: vi.fn() as any, |
| 12 | ide: {} as any, |
| 13 | } as unknown as ToolExtras; |
| 14 | |
| 15 | test("fetchUrlContent should not truncate content under character limit", async () => { |
| 16 | const shortContent = "This is a short content"; |
| 17 | const mockContextItems = [ |
| 18 | { |
| 19 | name: "Test URL", |
| 20 | description: "Test URL description", |
| 21 | content: shortContent, |
| 22 | }, |
| 23 | ]; |
| 24 | |
| 25 | (getUrlContextItems as any).mockResolvedValue(mockContextItems); |
| 26 | |
| 27 | const result = await fetchUrlContentImpl( |
| 28 | { url: "https://example.com" }, |
| 29 | mockExtras, |
| 30 | ); |
| 31 | |
| 32 | expect(result).toHaveLength(1); |
| 33 | expect(result[0].content).toBe(shortContent); |
| 34 | }); |
| 35 | |
| 36 | test("fetchUrlContent should truncate content exceeding character limit", async () => { |
| 37 | const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000; |
| 38 | // Create a string exceeding the character limit |
| 39 | const longContent = "a".repeat(DEFAULT_FETCH_URL_CHAR_LIMIT + 1000); |
| 40 | const mockContextItems = [ |
| 41 | { |
| 42 | name: "Test URL", |
| 43 | description: "Test URL description", |
| 44 | content: longContent, |
| 45 | }, |
| 46 | ]; |
| 47 | |
| 48 | (getUrlContextItems as any).mockResolvedValue(mockContextItems); |
| 49 | |
| 50 | const result = await fetchUrlContentImpl( |
| 51 | { url: "https://example.com" }, |
| 52 | mockExtras, |
| 53 | ); |
| 54 | |
| 55 | expect(result).toHaveLength(2); // Original item + warning |
| 56 | expect(result[0].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT); |
| 57 | expect(result[1].name).toBe("Truncation warning"); |
| 58 | expect(result[1].content).toContain("was truncated"); |
| 59 | expect(result[1].content).toContain("20000 character limit"); |
| 60 | }); |
| 61 | |
| 62 | test("fetchUrlContent should add truncation warning with multiple truncated items", async () => { |
| 63 | const DEFAULT_FETCH_URL_CHAR_LIMIT = 20000; |
| 64 | // Create strings exceeding the character limit |
| 65 | const longContent1 = "a".repeat(DEFAULT_FETCH_URL_CHAR_LIMIT + 500); |
| 66 | const longContent2 = "b".repeat(DEFAULT_FETCH_URL_CHAR_LIMIT + 1000); |
| 67 | const mockContextItems = [ |
| 68 | { |
| 69 | name: "Test URL 1", |
| 70 | description: "Test URL description 1", |
| 71 | content: longContent1, |
| 72 | }, |
| 73 | { |
| 74 | name: "Test URL 2", |
| 75 | description: "Test URL description 2", |
| 76 | content: longContent2, |
| 77 | }, |
| 78 | ]; |
| 79 | |
| 80 | (getUrlContextItems as any).mockResolvedValue(mockContextItems); |
| 81 | |
| 82 | const result = await fetchUrlContentImpl( |
| 83 | { url: "https://example.com" }, |
| 84 | mockExtras, |
| 85 | ); |
| 86 | |
| 87 | expect(result).toHaveLength(3); // Two original items + warning |
| 88 | expect(result[0].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT); |
| 89 | expect(result[1].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT); |
| 90 | expect(result[2].name).toBe("Truncation warning"); |
| 91 | }); |