|
| 1 | +import { z } from 'zod'; |
1 | 2 | import { resolveExternalCreators } from '../server.toolsHostCreator'; |
2 | | -import { type McpToolCreator } from '../server'; |
| 3 | +import { isZodSchema } from '../server.schema'; |
3 | 4 |
|
4 | | -// Mock dependencies |
5 | | -jest.mock('../logger', () => ({ |
6 | | - log: { |
7 | | - warn: jest.fn(), |
8 | | - error: jest.fn(), |
9 | | - info: jest.fn(), |
10 | | - debug: jest.fn() |
11 | | - }, |
12 | | - formatUnknownError: jest.fn((error: unknown) => String(error)) |
13 | | -})); |
14 | | - |
15 | | -// plugin-object support removed in streamlined implementation |
16 | | - |
17 | | -// plugin-object adapters removed; tests eliminated |
18 | | - |
19 | | -describe('resolveExternalCreators (streamlined)', () => { |
| 5 | +describe('resolveExternalCreators', () => { |
20 | 6 | it.each([ |
21 | 7 | { |
22 | | - description: 'function export returning a realized tuple (cached)', |
23 | | - moduleExports: { default: () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()] }, |
24 | | - expectedLength: 1 |
25 | | - }, |
26 | | - { |
27 | | - description: 'tool creator as default export', |
| 8 | + description: 'valid format, default export with function, tuple', |
28 | 9 | moduleExports: { |
29 | 10 | default: () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()] |
30 | 11 | }, |
31 | | - expectedLength: 1 |
| 12 | + isValid: true |
32 | 13 | }, |
33 | 14 | { |
34 | | - description: 'array of tool creators', |
35 | | - moduleExports: [ |
36 | | - () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()], |
37 | | - () => ['Tool2', { description: 'Tool 2', inputSchema: {} }, jest.fn()] |
38 | | - ], |
39 | | - expectedLength: 2 |
| 15 | + description: 'valid format, default export with function, array of functions with tuple return', |
| 16 | + moduleExports: { |
| 17 | + default: () => [ |
| 18 | + () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()], |
| 19 | + () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()] |
| 20 | + ] |
| 21 | + }, |
| 22 | + isValid: true |
40 | 23 | }, |
41 | 24 | { |
42 | | - description: 'array of tool creators as default export', |
| 25 | + description: 'valid format, default export with array of functions with tuple return', |
43 | 26 | moduleExports: { |
44 | 27 | default: [ |
| 28 | + () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()], |
45 | 29 | () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()] |
46 | 30 | ] |
47 | 31 | }, |
48 | | - expectedLength: 1 |
| 32 | + isValid: true |
| 33 | + }, |
| 34 | + { |
| 35 | + description: 'invalid format, default export with function, array of tuples', |
| 36 | + moduleExports: { |
| 37 | + default: () => [ |
| 38 | + ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()], |
| 39 | + ['Tool2', { description: 'Tool 2', inputSchema: {} }, jest.fn()] |
| 40 | + ] |
| 41 | + }, |
| 42 | + isValid: false |
| 43 | + }, |
| 44 | + { |
| 45 | + description: 'invalid format, default export with tuple', |
| 46 | + moduleExports: { |
| 47 | + default: ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()] |
| 48 | + }, |
| 49 | + isValid: false |
| 50 | + }, |
| 51 | + { |
| 52 | + description: 'invalid format, default export with array of tuples', |
| 53 | + moduleExports: { |
| 54 | + default: [ |
| 55 | + ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()], |
| 56 | + ['Tool2', { description: 'Tool 2', inputSchema: {} }, jest.fn()] |
| 57 | + ] |
| 58 | + }, |
| 59 | + isValid: false |
| 60 | + }, |
| 61 | + { |
| 62 | + description: 'invalid format, default export function that returns empty', |
| 63 | + moduleExports: { |
| 64 | + default: () => {} |
| 65 | + }, |
| 66 | + isValid: false |
49 | 67 | }, |
50 | 68 | { |
51 | | - description: 'empty module', |
| 69 | + description: 'invalid format, empty module', |
52 | 70 | moduleExports: {}, |
53 | | - expectedLength: 0 |
| 71 | + isValid: false |
54 | 72 | }, |
55 | 73 | { |
56 | | - description: 'null', |
| 74 | + description: 'invalid format, default export function that returns null', |
| 75 | + moduleExports: { |
| 76 | + default: () => null |
| 77 | + }, |
| 78 | + isValid: false |
| 79 | + }, |
| 80 | + { |
| 81 | + description: 'invalid format, null', |
57 | 82 | moduleExports: null, |
58 | | - expectedLength: 0 |
| 83 | + isValid: false |
| 84 | + }, |
| 85 | + { |
| 86 | + description: 'invalid format, default export function that returns undefined', |
| 87 | + moduleExports: { |
| 88 | + default: () => undefined |
| 89 | + }, |
| 90 | + isValid: false |
59 | 91 | }, |
60 | 92 | { |
61 | | - description: 'undefined', |
| 93 | + description: 'invalid format, undefined', |
62 | 94 | moduleExports: undefined, |
63 | | - expectedLength: 0 |
| 95 | + isValid: false |
64 | 96 | }, |
65 | 97 | { |
66 | | - description: 'function that throws', |
| 98 | + description: 'invalid format, default export function that throws', |
| 99 | + moduleExports: { |
| 100 | + default: () => { |
| 101 | + throw new Error('Function error'); |
| 102 | + } |
| 103 | + }, |
| 104 | + isValid: false |
| 105 | + }, |
| 106 | + { |
| 107 | + description: 'invalid format, function that throws', |
67 | 108 | moduleExports: () => { |
68 | 109 | throw new Error('Function error'); |
69 | 110 | }, |
70 | | - expectedLength: 0 |
| 111 | + isValid: false |
71 | 112 | }, |
72 | 113 | { |
73 | | - description: 'function returning unsupported shape', |
| 114 | + description: 'invalid format, function returning unsupported shape', |
74 | 115 | moduleExports: () => 'not a tool or creators[]', |
75 | | - expectedLength: 0 |
| 116 | + isValid: false |
76 | 117 | }, |
77 | 118 | { |
78 | | - description: 'array with non-function elements', |
| 119 | + description: 'invalid format, array with non-function elements', |
79 | 120 | moduleExports: ['not a function', 123, {}], |
80 | | - expectedLength: 0 |
| 121 | + isValid: false |
81 | 122 | }, |
82 | 123 | { |
83 | | - description: 'function returning non-tuple array (should not pass)', |
84 | | - moduleExports: () => ['not a tool tuple', 123], |
85 | | - expectedLength: 0 |
| 124 | + description: 'invalid format, named exports only', |
| 125 | + moduleExports: { |
| 126 | + named1: () => ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()], |
| 127 | + named2: () => ['Tool2', { description: 'Tool 2', inputSchema: {} }, jest.fn()] |
| 128 | + }, |
| 129 | + isValid: false |
86 | 130 | } |
87 | | - ])('should normalize module exports, $description', ({ moduleExports, expectedLength }) => { |
| 131 | + ])('should normalize module exports with specific formats, $description', ({ moduleExports, isValid }) => { |
88 | 132 | const result = resolveExternalCreators(moduleExports); |
89 | 133 |
|
90 | 134 | expect(Array.isArray(result)).toBe(true); |
91 | | - expect(result.length).toBe(expectedLength); |
92 | | - if (expectedLength > 0) { |
93 | | - expect(typeof result[0]).toBe('function'); |
94 | | - } |
95 | | - }); |
96 | | - |
97 | | - it('should handle multiple candidates (default and named)', () => { |
98 | | - const defaultTuple = ['Tool1', { description: 'Tool 1', inputSchema: {} }, jest.fn()] as const; |
99 | | - const moduleExports = { |
100 | | - default: () => defaultTuple, |
101 | | - named: 'not a creator' |
102 | | - } as any; |
103 | | - |
104 | | - const result = resolveExternalCreators(moduleExports); |
105 | | - |
106 | | - // Should use default export and wrap cached tuple |
107 | | - expect(result.length).toBe(1); |
108 | | - expect(typeof result[0]).toBe('function'); |
109 | | - expect((result[0] as any).toolName).toBe('Tool1'); |
110 | | - expect(result[0]!()).toBe(defaultTuple); |
111 | | - }); |
112 | | - |
113 | | - it('should prefer default export over named export', () => { |
114 | | - const defaultTuple = ['DefaultTool', { description: 'Default', inputSchema: {} }, jest.fn()] as const; |
115 | | - const namedCreator: McpToolCreator = () => ['NamedTool', { description: 'Named', inputSchema: {} }, jest.fn()]; |
116 | | - |
117 | | - const moduleExports = { |
118 | | - default: () => defaultTuple, |
119 | | - named: namedCreator |
120 | | - } as any; |
121 | | - |
122 | | - const result = resolveExternalCreators(moduleExports); |
123 | | - |
124 | | - expect(result.length).toBe(1); |
125 | | - expect(typeof result[0]).toBe('function'); |
126 | | - expect(result[0]!()).toBe(defaultTuple); |
| 135 | + expect(result.length > 0).toBe(isValid); |
127 | 136 | }); |
128 | 137 |
|
129 | | - it('should handle function export that throws during invocation', () => { |
130 | | - const moduleExports = () => { |
131 | | - throw new Error('Factory error'); |
| 138 | + it('should return a normalized module output with expected properties', () => { |
| 139 | + const moduleExport = { |
| 140 | + default: () => ['Tool1', { description: 'Tool 1', inputSchema: z.any() }, jest.fn()] |
132 | 141 | }; |
133 | 142 |
|
134 | | - const result = resolveExternalCreators(moduleExports); |
| 143 | + const [result] = resolveExternalCreators(moduleExport); |
| 144 | + const [name, schema = {}, handler]: any[] = result?.() || []; |
135 | 145 |
|
136 | | - expect(result.length).toBe(0); |
| 146 | + expect([ |
| 147 | + name, |
| 148 | + { |
| 149 | + description: schema.description, |
| 150 | + inputSchema: `${schema} isZod = ${isZodSchema(schema.inputSchema)}` |
| 151 | + }, |
| 152 | + handler |
| 153 | + ]).toMatchSnapshot('normalized'); |
137 | 154 | }); |
138 | 155 | }); |
0 commit comments