test: consolidate MCP client/fetch-stub setup into useTestClient helper (#333)
This commit is contained in:
1 parent
ad288a4ef9
commit
937e85a61f
9 files changed
+211
-351
No files matched your search
+19
-33
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user