import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { webFetchTool, _clearCache, _MAX_CONTENT_LENGTH } from './web-fetch.js';
// ---------------------------------------------------------------------------
// Mock global fetch
// ---------------------------------------------------------------------------
const mockFetch = vi.fn();
vi.stubGlobal('fetch', mockFetch);
// ---------------------------------------------------------------------------
// HTML fixture used by extraction tests
// ---------------------------------------------------------------------------
const SAMPLE_HTML = `
Test Page
Article Title
This is the main content of the article. It contains important information.
Second paragraph with more details about the topic.
`;
// ---------------------------------------------------------------------------
// Setup / teardown
// ---------------------------------------------------------------------------
beforeEach(() => {
mockFetch.mockReset();
_clearCache();
});
afterEach(() => {
vi.useRealTimers();
});
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe('web.fetch', () => {
// ---- Metadata ----
it('has correct metadata', () => {
expect(webFetchTool.name).toBe('web.fetch');
expect(webFetchTool.inputSchema.required).toContain('url');
// New format property is advertised in the schema
expect(webFetchTool.inputSchema.properties).toHaveProperty('format');
});
// ---- Default behaviour (markdown extraction) ----
it('fetches a URL and returns markdown by default', async () => {
mockFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => SAMPLE_HTML,
headers: new Headers({ 'content-type': 'text/html' }),
});
const result = await webFetchTool.execute({ url: 'https://example.com' });
expect(result.success).toBe(true);
expect(result.output).toBeTruthy();
// Should contain markdown-ish content (heading or paragraph text)
expect(result.output).toContain('Article Title');
// Should NOT contain raw HTML tags
expect(result.output).not.toContain('