Skip to content

Commit 3c31ddc

Browse files
committed
test: refactor tests to reduce duplication
1 parent 76ab9fa commit 3c31ddc

1 file changed

Lines changed: 58 additions & 90 deletions

File tree

tests/index.test.ts

Lines changed: 58 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import {Client} from '@modelcontextprotocol/sdk/client/index.js';
1212
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
1313
import {executablePath} from 'puppeteer';
1414

15-
import {
16-
OFF_BY_DEFAULT_CATEGORIES,
17-
ToolCategory,
18-
} from '../src/tools/categories.js';
15+
import {OFF_BY_DEFAULT_CATEGORIES} from '../src/tools/categories.js';
1916
import type {ToolDefinition} from '../src/tools/ToolDefinition.js';
2017

2118
describe('e2e', () => {
@@ -80,44 +77,7 @@ describe('e2e', () => {
8077
async client => {
8178
const {tools} = await client.listTools();
8279
const exposedNames = tools.map(t => t.name).sort();
83-
const files = fs.readdirSync('build/src/tools');
84-
const definedNames = [];
85-
for (const file of files) {
86-
if (
87-
file === 'ToolDefinition.js' ||
88-
file === 'tools.js' ||
89-
file === 'slim'
90-
) {
91-
continue;
92-
}
93-
const fileTools = await import(`../src/tools/${file}`);
94-
for (const maybeTool of Object.values<unknown>(fileTools)) {
95-
if (typeof maybeTool === 'function') {
96-
const tool = (maybeTool as (val: boolean) => ToolDefinition)(
97-
false,
98-
);
99-
if (tool && typeof tool === 'object' && 'name' in tool) {
100-
if (tool.annotations?.conditions) {
101-
continue;
102-
}
103-
definedNames.push(tool.name);
104-
}
105-
continue;
106-
}
107-
if (
108-
typeof maybeTool === 'object' &&
109-
maybeTool !== null &&
110-
'name' in maybeTool
111-
) {
112-
const tool = maybeTool as ToolDefinition;
113-
if (tool.annotations?.conditions) {
114-
continue;
115-
}
116-
definedNames.push(tool.name);
117-
}
118-
}
119-
}
120-
80+
const definedNames = await getToolsWithFilteredCategories();
12181
definedNames.sort();
12282
assert.deepStrictEqual(exposedNames, definedNames);
12383
},
@@ -129,54 +89,9 @@ describe('e2e', () => {
12989
await withClient(async client => {
13090
const {tools} = await client.listTools();
13191
const exposedNames = tools.map(t => t.name).sort();
132-
const files = fs.readdirSync('build/src/tools');
133-
const definedNames = [];
134-
for (const file of files) {
135-
if (
136-
file === 'ToolDefinition.js' ||
137-
file === 'tools.js' ||
138-
file === 'slim'
139-
) {
140-
continue;
141-
}
142-
const fileTools = await import(`../src/tools/${file}`);
143-
for (const maybeTool of Object.values<unknown>(fileTools)) {
144-
if (typeof maybeTool === 'function') {
145-
const tool = (maybeTool as (val: boolean) => ToolDefinition)(false);
146-
if (tool && typeof tool === 'object' && 'name' in tool) {
147-
if (tool.annotations?.conditions) {
148-
continue;
149-
}
150-
if (
151-
tool.annotations?.category &&
152-
tool.annotations?.category === ToolCategory.EXTENSIONS
153-
) {
154-
continue;
155-
}
156-
definedNames.push(tool.name);
157-
}
158-
continue;
159-
}
160-
if (
161-
typeof maybeTool === 'object' &&
162-
maybeTool !== null &&
163-
'name' in maybeTool
164-
) {
165-
const tool = maybeTool as ToolDefinition;
166-
if (tool.annotations?.conditions) {
167-
continue;
168-
}
169-
if (
170-
tool.annotations?.category &&
171-
tool.annotations?.category === ToolCategory.EXTENSIONS
172-
) {
173-
continue;
174-
}
175-
definedNames.push(tool.name);
176-
}
177-
}
178-
}
179-
92+
const definedNames = await getToolsWithFilteredCategories(
93+
OFF_BY_DEFAULT_CATEGORIES,
94+
);
18095
definedNames.sort();
18196
assert.deepStrictEqual(exposedNames, definedNames);
18297
});
@@ -245,3 +160,56 @@ describe('e2e', () => {
245160
);
246161
});
247162
});
163+
164+
async function getToolsWithFilteredCategories(
165+
filterOutCategories: string[] = [],
166+
): Promise<string[]> {
167+
const files = fs.readdirSync('build/src/tools');
168+
const definedNames = [];
169+
for (const file of files) {
170+
if (
171+
file === 'ToolDefinition.js' ||
172+
file === 'tools.js' ||
173+
file === 'slim'
174+
) {
175+
continue;
176+
}
177+
const fileTools = await import(`../src/tools/${file}`);
178+
for (const maybeTool of Object.values<unknown>(fileTools)) {
179+
if (typeof maybeTool === 'function') {
180+
const tool = (maybeTool as (val: boolean) => ToolDefinition)(false);
181+
if (tool && typeof tool === 'object' && 'name' in tool) {
182+
if (tool.annotations?.conditions) {
183+
continue;
184+
}
185+
if (
186+
tool.annotations?.category &&
187+
filterOutCategories.includes(tool.annotations?.category)
188+
) {
189+
continue;
190+
}
191+
definedNames.push(tool.name);
192+
}
193+
continue;
194+
}
195+
if (
196+
typeof maybeTool === 'object' &&
197+
maybeTool !== null &&
198+
'name' in maybeTool
199+
) {
200+
const tool = maybeTool as ToolDefinition;
201+
if (tool.annotations?.conditions) {
202+
continue;
203+
}
204+
if (
205+
tool.annotations?.category &&
206+
filterOutCategories.includes(tool.annotations?.category)
207+
) {
208+
continue;
209+
}
210+
definedNames.push(tool.name);
211+
}
212+
}
213+
}
214+
return definedNames;
215+
}

0 commit comments

Comments
 (0)