Skip to content

Commit 639edf0

Browse files
committed
unmock the tests
1 parent 822e6a8 commit 639edf0

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed

packages/mcp/src/docs-search/doc-fetch.test.ts

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1-
import { describe, it, expect, vi } from 'vitest';
1+
import { describe, it, expect, vi, afterEach } from 'vitest';
22
import { DocFetchTool, normalizeDocUrl } from './doc-fetch.js';
33

4+
const createMockResponse = ({
5+
content,
6+
contentType = 'text/html',
7+
status = 200,
8+
statusText = 'OK',
9+
}: {
10+
content: string;
11+
contentType?: string;
12+
status?: number;
13+
statusText?: string;
14+
}) =>
15+
new Response(content, {
16+
status,
17+
statusText,
18+
headers: { 'content-type': contentType },
19+
});
20+
21+
const stubFetch = (factory: () => Response) => {
22+
const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(factory()));
23+
vi.stubGlobal('fetch', fetchMock);
24+
return fetchMock;
25+
};
26+
427
describe('DocFetchTool', () => {
528
const tool = new DocFetchTool();
629

30+
afterEach(() => {
31+
vi.clearAllMocks();
32+
vi.unstubAllGlobals();
33+
});
34+
735
describe('URL validation', () => {
836
it('should accept valid HF and Gradio docs URLs', () => {
937
const validUrls = [
@@ -38,13 +66,30 @@ describe('DocFetchTool', () => {
3866
});
3967

4068
describe('document chunking', () => {
69+
it('uses markdown content from host when available', async () => {
70+
const markdown = '# Heading\nBody content';
71+
const fetchMock = stubFetch(() =>
72+
createMockResponse({
73+
content: markdown,
74+
contentType: 'text/markdown',
75+
}),
76+
);
77+
78+
const result = await tool.fetch({ doc_url: 'https://huggingface.co/docs/test' });
79+
expect(fetchMock).toHaveBeenCalledWith('https://huggingface.co/docs/test', {
80+
headers: { accept: 'text/markdown' },
81+
});
82+
expect(result).toBe(markdown);
83+
});
84+
4185
it('should return small documents without chunking', async () => {
4286

4387
// Mock fetch to return HTML that converts to short markdown
44-
global.fetch = vi.fn().mockResolvedValue({
45-
ok: true,
46-
text: () => Promise.resolve('<h1>Short Document</h1><p>This is a short document.</p>'),
47-
});
88+
stubFetch(() =>
89+
createMockResponse({
90+
content: '<h1>Short Document</h1><p>This is a short document.</p>',
91+
}),
92+
);
4893

4994
const result = await tool.fetch({ doc_url: 'https://huggingface.co/docs/test' });
5095

@@ -57,10 +102,11 @@ describe('DocFetchTool', () => {
57102
// Mock fetch to return HTML that converts to long markdown
58103
const longHtml = '<h1>Long Document</h1>' + '<p>This is a very long sentence that will be repeated many times to create a document that exceeds the 7500 token limit for testing chunking functionality.</p>'.repeat(200);
59104

60-
global.fetch = vi.fn().mockResolvedValue({
61-
ok: true,
62-
text: () => Promise.resolve(longHtml),
63-
});
105+
stubFetch(() =>
106+
createMockResponse({
107+
content: longHtml,
108+
}),
109+
);
64110

65111
const result = await tool.fetch({ doc_url: 'https://huggingface.co/docs/test' });
66112

@@ -85,10 +131,11 @@ describe('DocFetchTool', () => {
85131
// Mock fetch to return the same long HTML
86132
const longHtml = '<h1>Long Document</h1>' + '<p>This is a very long sentence that will be repeated many times to create a document that exceeds the 7500 token limit for testing chunking functionality.</p>'.repeat(200);
87133

88-
global.fetch = vi.fn().mockResolvedValue({
89-
ok: true,
90-
text: () => Promise.resolve(longHtml),
91-
});
134+
stubFetch(() =>
135+
createMockResponse({
136+
content: longHtml,
137+
}),
138+
);
92139

93140
// Get first chunk
94141
const firstChunk = await tool.fetch({ doc_url: 'https://huggingface.co/docs/test' });
@@ -106,10 +153,11 @@ describe('DocFetchTool', () => {
106153
});
107154

108155
it('should handle offset beyond document length', async () => {
109-
global.fetch = vi.fn().mockResolvedValue({
110-
ok: true,
111-
text: () => Promise.resolve('<h1>Short Document</h1><p>This is short.</p>'),
112-
});
156+
stubFetch(() =>
157+
createMockResponse({
158+
content: '<h1>Short Document</h1><p>This is short.</p>',
159+
}),
160+
);
113161

114162
const result = await tool.fetch({ doc_url: 'https://huggingface.co/docs/test', offset: 10000 });
115163

0 commit comments

Comments
 (0)