test: consolidate MCP client/fetch-stub setup into useTestClient helper (#333)

This commit is contained in:
Sampson authored and GitHub committed 2026-08-11 15:13:10 -05:00
1 parent ad288a4ef9
commit 937e85a61f
9 files changed
+211 -351

No files matched your search

+24
View File
@@ -1,5 +1,6 @@
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { after, before } from 'node:test';
import createMcpServer from '../server.js';
/**
@@ -45,3 +46,26 @@ export function stubFetch(handler: (url: URL) => unknown): () => void {
globalThis.fetch = originalFetch;
};
}
/**
* Registers before/after hooks on the enclosing `describe` block that stub
* the Brave API via `responder` and connect a fresh test client, tearing
* both down afterward. Returns a getter for the connected client.
*/
export function useTestClient(responder: (url: URL) => unknown): () => Client {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch(responder);
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
});
return () => client;
}
+36 -63
View File
@@ -1,49 +1,35 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/images/search') {
return {
type: 'images',
query: { original: 'brave browser' },
results: [
{
type: 'image_result',
title: 'Brave Browser Logo',
url: 'https://example.com/logo.png',
page_fetched: '2024-01-01T00:00:00Z',
confidence: 'high',
properties: {
url: 'https://example.com/logo-full.png',
width: 512,
height: 512,
},
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/images/search') {
return {
type: 'images',
query: { original: 'brave browser' },
results: [
{
type: 'image_result',
title: 'Brave Browser Logo',
url: 'https://example.com/logo.png',
page_fetched: '2024-01-01T00:00:00Z',
confidence: 'high',
properties: {
url: 'https://example.com/logo-full.png',
width: 512,
height: 512,
},
],
extra: { might_be_offensive: false },
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
},
],
extra: { might_be_offensive: false },
};
}
});
it('returns image results for a simple query', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
assert.deepEqual(result.structuredContent, {
@@ -68,34 +54,21 @@ describe(name, () => {
});
describe(`${name} (malformed upstream response)`, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/images/search') {
return {
type: 'images',
query: { original: 'brave browser' },
results: [],
// `might_be_offensive` is required by the output schema; omitting it
// simulates an upstream response that fails validation.
extra: {},
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/images/search') {
return {
type: 'images',
query: { original: 'brave browser' },
results: [],
// `might_be_offensive` is required by the output schema; omitting it
// simulates an upstream response that fails validation.
extra: {},
};
}
});
it('surfaces the validation failure as an error result', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError, true);
assert.ok(
+30 -57
View File
@@ -1,45 +1,31 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/llm/context') {
return {
grounding: {
generic: [
{
url: 'https://search.brave.com/',
title: 'Brave Search',
snippets: ['Brave is a privacy-focused browser and search engine.'],
},
],
map: [],
},
sources: {
'https://search.brave.com/': { title: 'Brave Search' },
},
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/llm/context') {
return {
grounding: {
generic: [
{
url: 'https://search.brave.com/',
title: 'Brave Search',
snippets: ['Brave is a privacy-focused browser and search engine.'],
},
],
map: [],
},
sources: {
'https://search.brave.com/': { title: 'Brave Search' },
},
};
}
});
it('returns grounding content for a simple query', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
assert.deepEqual(result.structuredContent, {
@@ -61,31 +47,18 @@ describe(name, () => {
});
describe(`${name} (malformed upstream response)`, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/llm/context') {
// `sources` is required by the output schema; omitting it simulates
// an upstream response that fails validation.
return {
grounding: { generic: [], map: [] },
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/llm/context') {
// `sources` is required by the output schema; omitting it simulates
// an upstream response that fails validation.
return {
grounding: { generic: [], map: [] },
};
}
});
it('surfaces the validation failure as an error result', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError, true);
assert.ok(JSON.stringify(result.content).includes('sources'), JSON.stringify(result.content));
+40 -80
View File
@@ -1,43 +1,29 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/web/search') {
return {
type: 'search',
locations: {
results: [{ id: 'poi-1', title: 'Brave Coffee Shop' }],
},
};
}
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/web/search') {
return {
type: 'search',
locations: {
results: [{ id: 'poi-1', title: 'Brave Coffee Shop' }],
},
};
}
if (url.pathname === '/res/v1/local/descriptions') {
return {
type: 'local_descriptions',
results: [{ type: 'local_description', id: 'poi-1', description: 'A cozy coffee shop.' }],
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
if (url.pathname === '/res/v1/local/descriptions') {
return {
type: 'local_descriptions',
results: [{ type: 'local_description', id: 'poi-1', description: 'A cozy coffee shop.' }],
};
}
});
it('returns enriched location results for a simple query', async () => {
const result = await client.callTool({
const result = await getClient().callTool({
name,
arguments: { query: 'coffee shops in san francisco' },
});
@@ -48,39 +34,26 @@ describe(name, () => {
});
describe(`${name} (no locations, web fallback available)`, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/web/search') {
return {
type: 'search',
web: {
results: [
{
url: 'https://example.com/cafe',
title: 'Local Cafe',
description: 'A nice cafe.',
extra_snippets: [],
},
],
},
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/web/search') {
return {
type: 'search',
web: {
results: [
{
url: 'https://example.com/cafe',
title: 'Local Cafe',
description: 'A nice cafe.',
extra_snippets: [],
},
],
},
};
}
});
it('falls back to formatted web results', async () => {
const result = await client.callTool({
const result = await getClient().callTool({
name,
arguments: { query: 'coffee shops in san francisco' },
});
@@ -95,27 +68,14 @@ describe(`${name} (no locations, web fallback available)`, () => {
});
describe(`${name} (no locations, no web results)`, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/web/search') {
return { type: 'search' };
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/web/search') {
return { type: 'search' };
}
});
it('reports that no location data was found', async () => {
const result = await client.callTool({
const result = await getClient().callTool({
name,
arguments: { query: 'coffee shops in san francisco' },
});
+21 -35
View File
@@ -1,45 +1,31 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/news/search') {
return {
results: [
{
url: 'https://example.com/news',
title: 'Brave ships a new release',
description: 'News about Brave.',
age: '1 hour ago',
page_age: '2024-01-01T00:00:00Z',
breaking: false,
is_live: false,
extra_snippets: [],
thumbnail: { src: 'https://example.com/news-thumb.jpg' },
},
],
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/news/search') {
return {
results: [
{
url: 'https://example.com/news',
title: 'Brave ships a new release',
description: 'News about Brave.',
age: '1 hour ago',
page_age: '2024-01-01T00:00:00Z',
breaking: false,
is_live: false,
extra_snippets: [],
thumbnail: { src: 'https://example.com/news-thumb.jpg' },
},
],
};
}
});
it('returns news results for a simple query', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
assert.ok(Array.isArray(result.content) && result.content.length > 0);
+7 -21
View File
@@ -1,31 +1,17 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/local/place_search') {
return { type: 'locations' };
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/local/place_search') {
return { type: 'locations' };
}
});
it('returns a locations response for a simple location query', async () => {
const result = await client.callTool({
const result = await getClient().callTool({
name,
arguments: { location: 'san francisco ca united states' },
});
+11 -25
View File
@@ -1,35 +1,21 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/summarizer/search') {
return {
type: 'summarizer',
status: 'complete',
summary: [{ type: 'token', data: 'Brave is a privacy-focused browser.' }],
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/summarizer/search') {
return {
type: 'summarizer',
status: 'complete',
summary: [{ type: 'token', data: 'Brave is a privacy-focused browser.' }],
};
}
});
it('returns a summary for a completed key', async () => {
const result = await client.callTool({ name, arguments: { key: 'test-key' } });
const result = await getClient().callTool({ name, arguments: { key: 'test-key' } });
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
assert.ok(Array.isArray(result.content) && result.content.length > 0);
+23 -37
View File
@@ -1,48 +1,34 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/videos/search') {
return {
results: [
{
url: 'https://example.com/video',
title: 'Introducing Brave',
description: 'A video about the Brave browser.',
age: '1 day ago',
video: {
duration: '5:00',
views: '1000',
creator: 'Brave Software',
publisher: 'Brave Software',
tags: ['browser'],
},
thumbnail: { src: 'https://example.com/thumb.jpg' },
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/videos/search') {
return {
results: [
{
url: 'https://example.com/video',
title: 'Introducing Brave',
description: 'A video about the Brave browser.',
age: '1 day ago',
video: {
duration: '5:00',
views: '1000',
creator: 'Brave Software',
publisher: 'Brave Software',
tags: ['browser'],
},
],
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
thumbnail: { src: 'https://example.com/thumb.jpg' },
},
],
};
}
});
it('returns video results for a simple query', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
assert.ok(Array.isArray(result.content) && result.content.length > 0);
+19 -33
View File
@@ -1,43 +1,29 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { connectTestClient, stubFetch } from '../../testUtils/mcpTestHarness.js';
import { describe, it } from 'node:test';
import { useTestClient } from '../../testUtils/mcpTestHarness.js';
import { name } from './index.js';
describe(name, () => {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch((url) => {
if (url.pathname === '/res/v1/web/search') {
return {
type: 'search',
web: {
results: [
{
url: 'https://search.brave.com/',
title: 'Brave Search',
description: 'A privacy-focused search engine.',
extra_snippets: [],
},
],
},
};
}
});
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
const getClient = useTestClient((url) => {
if (url.pathname === '/res/v1/web/search') {
return {
type: 'search',
web: {
results: [
{
url: 'https://search.brave.com/',
title: 'Brave Search',
description: 'A privacy-focused search engine.',
extra_snippets: [],
},
],
},
};
}
});
it('returns web results for a simple query', async () => {
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
const result = await getClient().callTool({ name, arguments: { query: 'brave browser' } });
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
assert.ok(Array.isArray(result.content) && result.content.length > 0);