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

+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));