-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathget-ui-building-instructions.test.ts
More file actions
234 lines (201 loc) · 5.41 KB
/
get-ui-building-instructions.test.ts
File metadata and controls
234 lines (201 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { McpServer } from 'tmcp';
import { ValibotJsonSchemaAdapter } from '@tmcp/adapter-valibot';
import {
addGetUIBuildingInstructionsTool,
GET_UI_BUILDING_INSTRUCTIONS_TOOL_NAME,
} from './get-ui-building-instructions.ts';
import type { AddonContext } from '../types.ts';
import * as telemetry from '../telemetry.ts';
import { GET_STORY_URLS_TOOL_NAME } from './get-story-urls.ts';
describe('getUIBuildingInstructionsTool', () => {
let server: McpServer<any, AddonContext>;
let collectTelemetrySpy: any;
beforeEach(async () => {
const adapter = new ValibotJsonSchemaAdapter();
server = new McpServer(
{
name: 'test-server',
version: '1.0.0',
description: 'Test server for get-ui-building-instructions tool',
},
{
adapter,
capabilities: {
tools: { listChanged: true },
},
},
).withContext<AddonContext>();
// initialize test session
await server.receive(
{
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: '2025-06-18',
capabilities: {},
clientInfo: { name: 'test', version: '1.0.0' },
},
},
{
sessionId: 'test-session',
},
);
await addGetUIBuildingInstructionsTool(server);
// Mock collectTelemetry
collectTelemetrySpy = vi.spyOn(telemetry, 'collectTelemetry');
collectTelemetrySpy.mockResolvedValue(undefined);
});
it('should return UI building instructions with framework placeholders replaced', async () => {
const mockOptions = {
presets: {
apply: vi.fn().mockResolvedValue('@storybook/react-vite'),
},
};
const testContext: AddonContext = {
origin: 'http://localhost:6006',
options: mockOptions as any,
disableTelemetry: true,
};
const request = {
jsonrpc: '2.0' as const,
id: 1,
method: 'tools/call',
params: {
name: GET_UI_BUILDING_INSTRUCTIONS_TOOL_NAME,
arguments: {},
},
};
const response = await server.receive(request, {
sessionId: 'test-session',
custom: testContext,
});
const instructions = response.result?.content[0].text as string;
// Check that placeholders were replaced
expect(instructions).toContain('@storybook/react-vite');
expect(instructions).toContain('@storybook/react');
expect(instructions).toContain(GET_STORY_URLS_TOOL_NAME);
// Check that no placeholders remain
expect(instructions).not.toContain('{{FRAMEWORK}}');
expect(instructions).not.toContain('{{RENDERER}}');
expect(instructions).not.toContain('{{GET_STORY_URLS_TOOL_NAME}}');
});
it('should handle Vue framework', async () => {
const mockOptions = {
presets: {
apply: vi.fn().mockResolvedValue('@storybook/vue3-vite'),
},
};
const testContext: AddonContext = {
origin: 'http://localhost:6006',
options: mockOptions as any,
disableTelemetry: true,
};
const request = {
jsonrpc: '2.0' as const,
id: 1,
method: 'tools/call',
params: {
name: GET_UI_BUILDING_INSTRUCTIONS_TOOL_NAME,
arguments: {},
},
};
const response = await server.receive(request, {
sessionId: 'test-session',
custom: testContext,
});
const instructions = response.result?.content[0].text as string;
expect(instructions).toContain('@storybook/vue3-vite');
expect(instructions).toContain('@storybook/vue3');
});
it('should handle framework as object with name property', async () => {
const mockOptions = {
presets: {
apply: vi.fn().mockResolvedValue({
name: '@storybook/nextjs',
options: {},
}),
},
};
const testContext: AddonContext = {
origin: 'http://localhost:6006',
options: mockOptions as any,
disableTelemetry: true,
};
const request = {
jsonrpc: '2.0' as const,
id: 1,
method: 'tools/call',
params: {
name: GET_UI_BUILDING_INSTRUCTIONS_TOOL_NAME,
arguments: {},
},
};
const response = await server.receive(request, {
sessionId: 'test-session',
custom: testContext,
});
const instructions = response.result?.content[0].text as string;
expect(instructions).toContain('@storybook/nextjs');
expect(instructions).toContain('@storybook/react');
});
it('should collect telemetry when enabled', async () => {
const mockOptions = {
presets: {
apply: vi.fn().mockResolvedValue('@storybook/react-vite'),
},
};
const testContext: AddonContext = {
origin: 'http://localhost:6006',
options: mockOptions as any,
disableTelemetry: false,
};
const request = {
jsonrpc: '2.0' as const,
id: 1,
method: 'tools/call',
params: {
name: GET_UI_BUILDING_INSTRUCTIONS_TOOL_NAME,
arguments: {},
},
};
await server.receive(request, {
sessionId: 'test-session',
custom: testContext,
});
expect(collectTelemetrySpy).toHaveBeenCalledWith({
event: 'tool:getUIBuildingInstructions',
server,
toolset: 'dev',
});
});
it('should handle missing options in context', async () => {
const testContext = {
origin: 'http://localhost:6006',
disableTelemetry: true,
} as any;
const request = {
jsonrpc: '2.0' as const,
id: 1,
method: 'tools/call',
params: {
name: GET_UI_BUILDING_INSTRUCTIONS_TOOL_NAME,
arguments: {},
},
};
const response = await server.receive(request, {
sessionId: 'test-session',
custom: testContext,
});
expect(response.result).toEqual({
content: [
{
type: 'text',
text: 'Error: Options are required in addon context',
},
],
isError: true,
});
});
});