| 1 | import type { ContextItem } from "core/index.js"; |
| 2 | import { fetchUrlContentImpl } from "core/tools/implementations/fetchUrlContent.js"; |
| 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | |
| 5 | import { fetchTool } from "./fetch.js"; |
| 6 | |
| 7 | // Mock the core fetchUrlContent implementation |
| 8 | vi.mock("core/tools/implementations/fetchUrlContent.js", () => ({ |
| 9 | fetchUrlContentImpl: vi.fn(), |
| 10 | })); |
| 11 | |
| 12 | const mockFetchUrlContentImpl = vi.mocked(fetchUrlContentImpl); |
| 13 | |
| 14 | describe("fetchTool", () => { |
| 15 | let originalConsoleError: typeof console.error; |
| 16 | |
| 17 | beforeEach(() => { |
| 18 | originalConsoleError = console.error; |
| 19 | console.error = vi.fn(); |
| 20 | mockFetchUrlContentImpl.mockReset(); |
| 21 | }); |
| 22 | |
| 23 | afterEach(() => { |
| 24 | console.error = originalConsoleError; |
| 25 | }); |
| 26 | |
| 27 | it("should return clean content from successful fetch", async () => { |
| 28 | const mockContextItems: ContextItem[] = [ |
| 29 | { |
| 30 | name: "Example Page", |
| 31 | description: "https://example.com", |
| 32 | content: |
| 33 | "# Example Page\n\nThis is some example content from a webpage.", |
| 34 | uri: { type: "url", value: "https://example.com" }, |
| 35 | }, |
| 36 | ]; |
| 37 | |
| 38 | mockFetchUrlContentImpl.mockResolvedValue(mockContextItems); |
| 39 | |
| 40 | const result = await fetchTool.run({ url: "https://example.com" }); |
| 41 | |
| 42 | expect(result).toBe( |
| 43 | "# Example Page\n\nThis is some example content from a webpage.", |
| 44 | ); |
| 45 | expect(mockFetchUrlContentImpl).toHaveBeenCalledWith( |
| 46 | { url: "https://example.com" }, |
| 47 | { fetch }, |
| 48 | ); |
| 49 | }); |
| 50 | |
| 51 | it("should handle truncation warnings from core implementation", async () => { |
| 52 | const mockContextItems: ContextItem[] = [ |
| 53 | { |
| 54 | name: "Long Page", |
| 55 | description: "https://example.com", |
| 56 | content: "This is the main content that was truncated.", |
| 57 | uri: { type: "url", value: "https://example.com" }, |
| 58 | }, |
| 59 | { |
| 60 | name: "Truncation warning", |
| 61 | description: "", |
| 62 | content: |
| 63 | "The content from https://example.com was truncated because it exceeded the 20000 character limit.", |
| 64 | }, |
| 65 | ]; |
| 66 | |
| 67 | mockFetchUrlContentImpl.mockResolvedValue(mockContextItems); |
| 68 | |
| 69 | const result = await fetchTool.run({ url: "https://example.com" }); |
| 70 | |
| 71 | expect(result).toBe( |
| 72 | "This is the main content that was truncated.\n\nThe content from https://example.com was truncated because it exceeded the 20000 character limit.", |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | it("should handle multiple content items", async () => { |
| 77 | const mockContextItems: ContextItem[] = [ |
| 78 | { |
| 79 | name: "Page 1", |
| 80 | description: "https://example.com", |
| 81 | content: "Content from page 1", |
| 82 | uri: { type: "url", value: "https://example.com" }, |
| 83 | }, |
| 84 | { |
| 85 | name: "Page 2", |
| 86 | description: "https://example.com", |
| 87 | content: "Content from page 2", |
| 88 | uri: { type: "url", value: "https://example.com" }, |
| 89 | }, |
| 90 | ]; |
| 91 | |
| 92 | mockFetchUrlContentImpl.mockResolvedValue(mockContextItems); |
| 93 | |
| 94 | const result = await fetchTool.run({ url: "https://example.com" }); |
| 95 | |
| 96 | expect(result).toBe("Content from page 1\n\nContent from page 2"); |
| 97 | }); |
| 98 | |
| 99 | it("should return error message when no content items returned", async () => { |
| 100 | mockFetchUrlContentImpl.mockResolvedValue([]); |
| 101 | |
| 102 | const result = await fetchTool.run({ url: "https://example.com" }); |
| 103 | |
| 104 | expect(result).toBe( |
| 105 | "Error: Could not fetch content from https://example.com", |
| 106 | ); |
| 107 | }); |
| 108 | |
| 109 | it("should handle errors from core implementation", async () => { |
| 110 | const error = new Error("Network error"); |
| 111 | mockFetchUrlContentImpl.mockRejectedValue(error); |
| 112 | |
| 113 | const result = await fetchTool.run({ url: "https://example.com" }); |
| 114 | |
| 115 | expect(result).toBe("Error: Network error"); |
| 116 | }); |
| 117 | |
| 118 | it("should call fetchUrlContentImpl with correct arguments", async () => { |
| 119 | const mockContextItems: ContextItem[] = [ |
| 120 | { |
| 121 | name: "Test Page", |
| 122 | description: "https://example.com", |
| 123 | content: "Test content", |
| 124 | uri: { type: "url", value: "https://example.com" }, |
| 125 | }, |
| 126 | ]; |
| 127 | |
| 128 | mockFetchUrlContentImpl.mockResolvedValue(mockContextItems); |
| 129 | |
| 130 | await fetchTool.run({ url: "https://example.com" }); |
| 131 | |
| 132 | expect(mockFetchUrlContentImpl).toHaveBeenCalledWith( |
| 133 | { url: "https://example.com" }, |
| 134 | { fetch }, |
| 135 | ); |
| 136 | }); |
| 137 | |
| 138 | it("should have correct tool metadata", () => { |
| 139 | expect(fetchTool.name).toBe("Fetch"); |
| 140 | expect(fetchTool.displayName).toBe("Fetch"); |
| 141 | expect(fetchTool.description).toBe( |
| 142 | "Fetches content from a URL, converts to markdown, and handles long content with truncation", |
| 143 | ); |
| 144 | expect(fetchTool.readonly).toBe(true); |
| 145 | expect(fetchTool.isBuiltIn).toBe(true); |
| 146 | expect(fetchTool.parameters).toEqual({ |
| 147 | type: "object", |
| 148 | required: ["url"], |
| 149 | properties: { |
| 150 | url: { |
| 151 | type: "string", |
| 152 | description: "The URL to fetch content from", |
| 153 | }, |
| 154 | }, |
| 155 | }); |
| 156 | }); |
| 157 | |
| 158 | it("should provide correct preview in preprocess", async () => { |
| 159 | const args = { url: "https://example.com" }; |
| 160 | const result = await fetchTool.preprocess?.(args); |
| 161 | |
| 162 | expect(result).toEqual({ |
| 163 | preview: [ |
| 164 | { |
| 165 | type: "text", |
| 166 | content: "Will fetch: https://example.com", |
| 167 | }, |
| 168 | ], |
| 169 | args, |
| 170 | }); |
| 171 | }); |
| 172 | }); |