-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStyleComparisonTool.test.ts
More file actions
243 lines (201 loc) · 7.75 KB
/
Copy pathStyleComparisonTool.test.ts
File metadata and controls
243 lines (201 loc) · 7.75 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
235
236
237
238
239
240
241
242
243
// Copyright (c) Mapbox, Inc.
// Licensed under the MIT License.
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { StyleComparisonTool } from '../../../src/tools/style-comparison-tool/StyleComparisonTool.js';
import * as jwtUtils from '../../../src/utils/jwtUtils.js';
describe('StyleComparisonTool', () => {
let tool: StyleComparisonTool;
beforeEach(() => {
tool = new StyleComparisonTool();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('run', () => {
it('should generate comparison URL and MCP-UI resource with provided access token (default)', async () => {
const input = {
before: 'mapbox/streets-v12',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
expect(result.content).toHaveLength(2);
expect(result.content[0].type).toBe('text');
const url = (result.content[0] as { type: 'text'; text: string }).text;
expect(url).toContain('https://agent.mapbox.com/tools/style-compare');
expect(url).toContain('access_token=pk.test.token');
expect(url).toContain('before=mapbox%2Fstreets-v12');
expect(url).toContain('after=mapbox%2Foutdoors-v12');
// Verify MCP-UI resource is included by default
expect(result.content[1]).toMatchObject({
type: 'resource',
resource: {
uri: expect.stringMatching(
/^ui:\/\/mapbox\/style-comparison\/mapbox\/streets-v12\/mapbox\/outdoors-v12$/
),
mimeType: 'text/html;profile=mcp-app',
text: expect.stringContaining(
'https://agent.mapbox.com/tools/style-compare'
)
}
});
});
it('should require access token', async () => {
const input = {
before: 'mapbox/streets-v12',
after: 'mapbox/satellite-v9'
// Missing accessToken
} as any;
const result = await tool.run(input);
expect(result.isError).toBe(true);
expect(
(result.content[0] as { type: 'text'; text: string }).text
).toContain('invalid_type');
});
it('should handle full style URLs', async () => {
const input = {
before: 'mapbox://styles/mapbox/streets-v12',
after: 'mapbox://styles/mapbox/outdoors-v12',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
const url = (result.content[0] as { type: 'text'; text: string }).text;
expect(url).toContain('before=mapbox%2Fstreets-v12');
expect(url).toContain('after=mapbox%2Foutdoors-v12');
});
it('should handle just style IDs with valid public token', async () => {
vi.spyOn(jwtUtils, 'getUserNameFromToken').mockReturnValue('testuser');
const input = {
before: 'style-id-1',
after: 'style-id-2',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
const url = (result.content[0] as { type: 'text'; text: string }).text;
expect(url).toContain('before=testuser%2Fstyle-id-1');
expect(url).toContain('after=testuser%2Fstyle-id-2');
});
it('should reject secret tokens', async () => {
const input = {
before: 'mapbox/streets-v12',
after: 'mapbox/outdoors-v12',
accessToken: 'sk.secret.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(true);
expect(
(result.content[0] as { type: 'text'; text: string }).text
).toContain('Invalid token type');
expect(
(result.content[0] as { type: 'text'; text: string }).text
).toContain('Secret tokens (sk.*) cannot be exposed');
});
it('should reject invalid token formats', async () => {
const input = {
before: 'streets-v12',
after: 'outdoors-v12',
accessToken: 'invalid.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(true);
expect(
(result.content[0] as { type: 'text'; text: string }).text
).toContain('Invalid token type');
});
it('should return error for style ID without valid username in token', async () => {
// Mock getUserNameFromToken to throw an error
vi.spyOn(jwtUtils, 'getUserNameFromToken').mockImplementation(() => {
throw new Error(
'MAPBOX_ACCESS_TOKEN does not contain username in payload'
);
});
const input = {
before: 'style-id-only',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(true);
expect(
(result.content[0] as { type: 'text'; text: string }).text
).toContain('Could not determine username for style ID');
});
it('should properly encode URL parameters', async () => {
const input = {
before: 'user-name/style-id-1',
after: 'user-name/style-id-2',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
const url = (result.content[0] as { type: 'text'; text: string }).text;
// Check that forward slashes are URL encoded
expect(url).toContain('before=user-name%2Fstyle-id-1');
expect(url).toContain('after=user-name%2Fstyle-id-2');
});
it('should include hash fragment with map position when coordinates are provided', async () => {
const input = {
before: 'mapbox/streets-v12',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token',
zoom: 5.72,
latitude: 9.503,
longitude: -67.473
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
const url = (result.content[0] as { type: 'text'; text: string }).text;
expect(url).toContain('#5.72/9.503/-67.473');
});
it('should not include hash fragment when coordinates are incomplete', async () => {
// Only zoom provided
const input1 = {
before: 'mapbox/streets-v12',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token',
zoom: 10
};
const result1 = await tool.run(input1);
expect(result1.isError).toBe(false);
const url1 = (result1.content[0] as { type: 'text'; text: string }).text;
expect(url1).not.toContain('#');
// Only latitude and longitude, no zoom
const input2 = {
before: 'mapbox/streets-v12',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token',
latitude: 40.7128,
longitude: -74.006
};
const result2 = await tool.run(input2);
expect(result2.isError).toBe(false);
const url2 = (result2.content[0] as { type: 'text'; text: string }).text;
expect(url2).not.toContain('#');
});
it('should return URL and MCP-UI resource for backward compatibility', async () => {
const input = {
before: 'mapbox/streets-v12',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
// Now returns both URL (for text) and MCP-UI resource (for backward compat)
expect(result.content).toHaveLength(2);
expect(result.content[0].type).toBe('text');
// Second item is MCP-UI resource
expect(result.content[1].type).toBe('resource');
});
});
describe('metadata', () => {
it('should have correct name and description', () => {
expect(tool.name).toBe('style_comparison_tool');
expect(tool.description).toBe(
'Generate a comparison URL for comparing two Mapbox styles side-by-side'
);
});
});
});