-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStyleComparisonTool.test.ts
More file actions
212 lines (175 loc) · 6.58 KB
/
StyleComparisonTool.test.ts
File metadata and controls
212 lines (175 loc) · 6.58 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
import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js';
import { StyleComparisonTool } from './StyleComparisonTool.js';
describe('StyleComparisonTool', () => {
let tool: StyleComparisonTool;
beforeEach(() => {
tool = new StyleComparisonTool();
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('run', () => {
it('should generate comparison URL with provided access token', async () => {
const input = {
before: 'mapbox/streets-v11',
after: 'mapbox/outdoors-v12',
accessToken: 'pk.test.token'
};
const result = await tool.run(input);
expect(result.isError).toBe(false);
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-v11');
expect(url).toContain('after=mapbox%2Foutdoors-v12');
});
it('should require access token', async () => {
const input = {
before: 'mapbox/streets-v11',
after: 'mapbox/satellite-v9'
// Missing accessToken
};
const result = await tool.run(input as any);
expect(result.isError).toBe(true);
expect(
(result.content[0] as { type: 'text'; text: string }).text
).toContain('Required');
});
it('should handle full style URLs', async () => {
const input = {
before: 'mapbox://styles/mapbox/streets-v11',
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-v11');
expect(url).toContain('after=mapbox%2Foutdoors-v12');
});
it('should handle just style IDs with valid public token', async () => {
// Mock MapboxApiBasedTool.getUserNameFromToken to return a username
jest
.spyOn(MapboxApiBasedTool, '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-v11',
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: 'mapbox/streets-v11',
after: 'mapbox/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
jest
.spyOn(MapboxApiBasedTool, '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');
});
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-v11',
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-v11',
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-v11',
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('#');
});
});
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'
);
});
});
});