import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { JSDOM } from "jsdom"; import { ReadWebpageTool } from "./ReadWebpageTool"; // Mock fetch globally const mockFetch = vi.fn(); global.fetch = mockFetch; describe("ReadWebpageTool", () => { let tool: ReadWebpageTool; let domParser: DOMParser; beforeEach(() => { // Create a JSDOM instance for testing const jsdom = new JSDOM(); domParser = new jsdom.window.DOMParser(); tool = new ReadWebpageTool(domParser); mockFetch.mockClear(); }); afterEach(() => { vi.restoreAllMocks(); }); describe("inputSchema", () => { it("should validate valid URL", () => { const result = tool.inputSchema.safeParse({ url: "https://example.com", }); expect(result.success).toBe(true); }); it("should reject invalid URL", () => { const result = tool.inputSchema.safeParse({ url: "not-a-url", }); expect(result.success).toBe(false); }); }); describe("execute", () => { it("should extract and convert article content to markdown", async () => { const mockHtml = ` Test Article

Main Article Title

This is the main content of the article.

It has multiple paragraphs with bold text and italic text.

Subsection

`; const mockResponse = { ok: true, headers: { get: vi.fn().mockReturnValue("text/html; charset=utf-8"), }, text: vi.fn().mockResolvedValue(mockHtml), }; mockFetch.mockResolvedValue(mockResponse); const result = await tool.execute({ url: "https://example.com/article", }); expect(result).toContain("# Test Article"); expect(result).toContain("**Source:** https://example.com/article"); expect(result).toContain("Main Article Title"); expect(result).toContain("**bold text**"); expect(result).toContain("*italic text*"); expect(result).toContain("## Subsection"); expect(result).toContain("* First item"); expect(result).toContain("* Second item"); }); it("should handle articles with metadata", async () => { const mockHtml = ` Article with Metadata

Article with Metadata

Main article content goes here.

`; const mockResponse = { ok: true, headers: { get: vi.fn().mockReturnValue("text/html"), }, text: vi.fn().mockResolvedValue(mockHtml), }; mockFetch.mockResolvedValue(mockResponse); const result = await tool.execute({ url: "https://example.com/article-with-metadata", }); expect(result).toContain("# Article with Metadata"); expect(result).toContain("**Source:** https://example.com/article-with-metadata"); }); it("should handle code blocks properly", async () => { const mockHtml = `

Code Example

Here's some code:


function hello() {
  console.log("Hello, world!");
}
            

And some inline code too.

`; const mockResponse = { ok: true, headers: { get: vi.fn().mockReturnValue("text/html"), }, text: vi.fn().mockResolvedValue(mockHtml), }; mockFetch.mockResolvedValue(mockResponse); const result = await tool.execute({ url: "https://example.com/code-example", }); expect(result).toContain("```"); expect(result).toContain("function hello()"); expect(result).toContain("`code`"); }); it("should handle HTTP errors", async () => { const mockResponse = { ok: false, status: 404, statusText: "Not Found", }; mockFetch.mockResolvedValue(mockResponse); await expect(tool.execute({ url: "https://example.com/not-found", })).rejects.toThrow("HTTP 404: Not Found"); }); it("should handle non-HTML content", async () => { const mockResponse = { ok: true, headers: { get: vi.fn().mockReturnValue("application/json"), }, text: vi.fn().mockResolvedValue('{"data": "json"}'), }; mockFetch.mockResolvedValue(mockResponse); await expect(tool.execute({ url: "https://api.example.com/data.json", })).rejects.toThrow("Expected HTML content, but received: application/json"); }); it("should handle network errors", async () => { mockFetch.mockRejectedValue(new TypeError("Failed to fetch")); await expect(tool.execute({ url: "https://example.com", })).rejects.toThrow("Network error: Unable to fetch https://example.com"); }); it("should handle pages with minimal content", async () => { const mockHtml = ` `; const mockResponse = { ok: true, headers: { get: vi.fn().mockReturnValue("text/html"), }, text: vi.fn().mockResolvedValue(mockHtml), }; mockFetch.mockResolvedValue(mockResponse); const result = await tool.execute({ url: "https://example.com/minimal-content", }); expect(result).toContain("# Untitled"); expect(result).toContain("**Source:** https://example.com/minimal-content"); }); it("should handle invalid URLs", async () => { await expect(tool.execute({ url: "invalid-url", })).rejects.toThrow("Failed to read webpage invalid-url"); }); }); });