From 937e85a61f69e36f5a88e44308d47836a8d5d523 Mon Sep 17 00:00:00 2001 From: Sampson Date: Tue, 11 Aug 2026 15:13:10 -0500 Subject: [PATCH] test: consolidate MCP client/fetch-stub setup into useTestClient helper (#333) --- src/testUtils/mcpTestHarness.ts | 24 ++++++ src/tools/images/index.test.ts | 99 ++++++++-------------- src/tools/llm_context/index.test.ts | 87 +++++++------------ src/tools/local/index.test.ts | 120 +++++++++------------------ src/tools/news/index.test.ts | 56 +++++-------- src/tools/place_search/index.test.ts | 28 ++----- src/tools/summarizer/index.test.ts | 36 +++----- src/tools/videos/index.test.ts | 60 +++++--------- src/tools/web/index.test.ts | 52 +++++------- 9 files changed, 211 insertions(+), 351 deletions(-) diff --git a/src/testUtils/mcpTestHarness.ts b/src/testUtils/mcpTestHarness.ts index 15cc505..f691a49 100644 --- a/src/testUtils/mcpTestHarness.ts +++ b/src/testUtils/mcpTestHarness.ts @@ -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; + let restoreFetch: () => void; + + before(async () => { + restoreFetch = stubFetch(responder); + ({ client, close } = await connectTestClient()); + }); + + after(async () => { + restoreFetch(); + await close(); + }); + + return () => client; +} diff --git a/src/tools/images/index.test.ts b/src/tools/images/index.test.ts index fbe1756..a31f4c2 100644 --- a/src/tools/images/index.test.ts +++ b/src/tools/images/index.test.ts @@ -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; - 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; - 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( diff --git a/src/tools/llm_context/index.test.ts b/src/tools/llm_context/index.test.ts index b32032a..3b25bb2 100644 --- a/src/tools/llm_context/index.test.ts +++ b/src/tools/llm_context/index.test.ts @@ -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; - 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; - 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)); diff --git a/src/tools/local/index.test.ts b/src/tools/local/index.test.ts index 8649e6a..204d8da 100644 --- a/src/tools/local/index.test.ts +++ b/src/tools/local/index.test.ts @@ -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; - 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; - 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; - 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' }, }); diff --git a/src/tools/news/index.test.ts b/src/tools/news/index.test.ts index da2c401..447cf07 100644 --- a/src/tools/news/index.test.ts +++ b/src/tools/news/index.test.ts @@ -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; - 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); diff --git a/src/tools/place_search/index.test.ts b/src/tools/place_search/index.test.ts index 51677a3..710e053 100644 --- a/src/tools/place_search/index.test.ts +++ b/src/tools/place_search/index.test.ts @@ -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; - 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' }, }); diff --git a/src/tools/summarizer/index.test.ts b/src/tools/summarizer/index.test.ts index 5b83fbf..c63a7b9 100644 --- a/src/tools/summarizer/index.test.ts +++ b/src/tools/summarizer/index.test.ts @@ -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; - 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); diff --git a/src/tools/videos/index.test.ts b/src/tools/videos/index.test.ts index 92ef43f..a69537f 100644 --- a/src/tools/videos/index.test.ts +++ b/src/tools/videos/index.test.ts @@ -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; - 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); diff --git a/src/tools/web/index.test.ts b/src/tools/web/index.test.ts index d71a792..2692926 100644 --- a/src/tools/web/index.test.ts +++ b/src/tools/web/index.test.ts @@ -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; - 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);