|
| 1 | +import { describe, it, expect } from "bun:test"; |
| 2 | +import { createWebFetchTool } from "./web_fetch"; |
| 3 | +import type { WebFetchToolArgs, WebFetchToolResult } from "@/common/types/tools"; |
| 4 | +import { WEB_FETCH_MAX_OUTPUT_BYTES } from "@/common/constants/toolLimits"; |
| 5 | +import { TestTempDir, createTestToolConfig } from "./testHelpers"; |
| 6 | +import * as fs from "fs/promises"; |
| 7 | +import * as path from "path"; |
| 8 | + |
| 9 | +import type { ToolCallOptions } from "ai"; |
| 10 | + |
| 11 | +// ToolCallOptions stub for testing |
| 12 | +const toolCallOptions: ToolCallOptions = { |
| 13 | + toolCallId: "test-call-id", |
| 14 | + messages: [], |
| 15 | +}; |
| 16 | + |
| 17 | +// Helper to create web_fetch tool with real LocalRuntime |
| 18 | +function createTestWebFetchTool() { |
| 19 | + const tempDir = new TestTempDir("test-web-fetch"); |
| 20 | + const config = createTestToolConfig(tempDir.path); |
| 21 | + const tool = createWebFetchTool(config); |
| 22 | + |
| 23 | + return { |
| 24 | + tool, |
| 25 | + tempDir, |
| 26 | + [Symbol.dispose]() { |
| 27 | + tempDir[Symbol.dispose](); |
| 28 | + }, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe("web_fetch tool", () => { |
| 33 | + // Integration test: fetch a real public URL |
| 34 | + it("should fetch and convert a real web page to markdown", async () => { |
| 35 | + using testEnv = createTestWebFetchTool(); |
| 36 | + const args: WebFetchToolArgs = { |
| 37 | + // example.com is a stable, simple HTML page maintained by IANA |
| 38 | + url: "https://example.com", |
| 39 | + }; |
| 40 | + |
| 41 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 42 | + |
| 43 | + expect(result.success).toBe(true); |
| 44 | + if (result.success) { |
| 45 | + expect(result.title).toContain("Example Domain"); |
| 46 | + expect(result.url).toBe("https://example.com"); |
| 47 | + // example.com mentions documentation examples |
| 48 | + expect(result.content).toContain("documentation"); |
| 49 | + expect(result.length).toBeGreaterThan(0); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return plain text when format is 'text'", async () => { |
| 54 | + using testEnv = createTestWebFetchTool(); |
| 55 | + const args: WebFetchToolArgs = { |
| 56 | + url: "https://example.com", |
| 57 | + format: "text", |
| 58 | + }; |
| 59 | + |
| 60 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 61 | + |
| 62 | + expect(result.success).toBe(true); |
| 63 | + if (result.success) { |
| 64 | + // Plain text should not have markdown formatting |
| 65 | + expect(result.content).not.toContain("#"); |
| 66 | + expect(result.content).not.toContain("**"); |
| 67 | + // example.com content mentions documentation |
| 68 | + expect(result.content).toContain("documentation"); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it("should handle DNS failure gracefully", async () => { |
| 73 | + using testEnv = createTestWebFetchTool(); |
| 74 | + const args: WebFetchToolArgs = { |
| 75 | + // .invalid TLD is reserved and guaranteed to never resolve |
| 76 | + url: "https://this-domain-does-not-exist.invalid/page", |
| 77 | + }; |
| 78 | + |
| 79 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 80 | + |
| 81 | + expect(result.success).toBe(false); |
| 82 | + if (!result.success) { |
| 83 | + expect(result.error).toContain("Failed to fetch URL"); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it("should handle connection refused gracefully", async () => { |
| 88 | + using testEnv = createTestWebFetchTool(); |
| 89 | + const args: WebFetchToolArgs = { |
| 90 | + // localhost on a random high port should refuse connection |
| 91 | + url: "http://127.0.0.1:59999/page", |
| 92 | + }; |
| 93 | + |
| 94 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 95 | + |
| 96 | + expect(result.success).toBe(false); |
| 97 | + if (!result.success) { |
| 98 | + expect(result.error).toContain("Failed to fetch URL"); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + // Test with a local file served via file:// - tests HTML parsing without network |
| 103 | + it("should handle local HTML content via file:// URL", async () => { |
| 104 | + using testEnv = createTestWebFetchTool(); |
| 105 | + |
| 106 | + // Create a test HTML file |
| 107 | + const htmlContent = ` |
| 108 | +<!DOCTYPE html> |
| 109 | +<html> |
| 110 | +<head><title>Local Test Page</title></head> |
| 111 | +<body> |
| 112 | + <article> |
| 113 | + <h1>Test Heading</h1> |
| 114 | + <p>This is test content with <strong>bold</strong> and <em>italic</em> text.</p> |
| 115 | + </article> |
| 116 | +</body> |
| 117 | +</html>`; |
| 118 | + const htmlPath = path.join(testEnv.tempDir.path, "test.html"); |
| 119 | + await fs.writeFile(htmlPath, htmlContent); |
| 120 | + |
| 121 | + const args: WebFetchToolArgs = { |
| 122 | + url: `file://${htmlPath}`, |
| 123 | + }; |
| 124 | + |
| 125 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 126 | + |
| 127 | + expect(result.success).toBe(true); |
| 128 | + if (result.success) { |
| 129 | + expect(result.title).toBe("Local Test Page"); |
| 130 | + expect(result.content).toContain("Test Heading"); |
| 131 | + expect(result.content).toContain("**bold**"); |
| 132 | + expect(result.content).toContain("_italic_"); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + it("should truncate oversized output from local file", async () => { |
| 137 | + using testEnv = createTestWebFetchTool(); |
| 138 | + |
| 139 | + // Create HTML that will produce content larger than WEB_FETCH_MAX_OUTPUT_BYTES |
| 140 | + const largeContent = "x".repeat(WEB_FETCH_MAX_OUTPUT_BYTES + 1000); |
| 141 | + const htmlContent = ` |
| 142 | +<!DOCTYPE html> |
| 143 | +<html> |
| 144 | +<head><title>Large Page</title></head> |
| 145 | +<body><article><p>${largeContent}</p></article></body> |
| 146 | +</html>`; |
| 147 | + const htmlPath = path.join(testEnv.tempDir.path, "large.html"); |
| 148 | + await fs.writeFile(htmlPath, htmlContent); |
| 149 | + |
| 150 | + const args: WebFetchToolArgs = { |
| 151 | + url: `file://${htmlPath}`, |
| 152 | + }; |
| 153 | + |
| 154 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 155 | + |
| 156 | + expect(result.success).toBe(true); |
| 157 | + if (result.success) { |
| 158 | + expect(result.content.length).toBeLessThanOrEqual( |
| 159 | + WEB_FETCH_MAX_OUTPUT_BYTES + 100 // Allow for truncation message |
| 160 | + ); |
| 161 | + expect(result.content).toContain("[Content truncated]"); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + it("should handle non-article HTML gracefully", async () => { |
| 166 | + using testEnv = createTestWebFetchTool(); |
| 167 | + |
| 168 | + // Minimal HTML that Readability may not parse as an article |
| 169 | + const htmlContent = "<html><body><p>Just some text</p></body></html>"; |
| 170 | + const htmlPath = path.join(testEnv.tempDir.path, "minimal.html"); |
| 171 | + await fs.writeFile(htmlPath, htmlContent); |
| 172 | + |
| 173 | + const args: WebFetchToolArgs = { |
| 174 | + url: `file://${htmlPath}`, |
| 175 | + }; |
| 176 | + |
| 177 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 178 | + |
| 179 | + // Readability may or may not parse this - the important thing is we don't crash |
| 180 | + expect(typeof result.success).toBe("boolean"); |
| 181 | + }); |
| 182 | + |
| 183 | + it("should handle empty file", async () => { |
| 184 | + using testEnv = createTestWebFetchTool(); |
| 185 | + |
| 186 | + const htmlPath = path.join(testEnv.tempDir.path, "empty.html"); |
| 187 | + await fs.writeFile(htmlPath, ""); |
| 188 | + |
| 189 | + const args: WebFetchToolArgs = { |
| 190 | + url: `file://${htmlPath}`, |
| 191 | + }; |
| 192 | + |
| 193 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 194 | + |
| 195 | + expect(result.success).toBe(false); |
| 196 | + if (!result.success) { |
| 197 | + expect(result.error).toContain("Empty response"); |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + it("should handle missing file", async () => { |
| 202 | + using testEnv = createTestWebFetchTool(); |
| 203 | + |
| 204 | + const args: WebFetchToolArgs = { |
| 205 | + url: `file://${testEnv.tempDir.path}/nonexistent.html`, |
| 206 | + }; |
| 207 | + |
| 208 | + const result = (await testEnv.tool.execute!(args, toolCallOptions)) as WebFetchToolResult; |
| 209 | + |
| 210 | + expect(result.success).toBe(false); |
| 211 | + if (!result.success) { |
| 212 | + expect(result.error).toContain("Failed to fetch URL"); |
| 213 | + } |
| 214 | + }); |
| 215 | +}); |
0 commit comments