|
| 1 | +import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js'; |
| 2 | +import { StyleComparisonTool } from './StyleComparisonTool.js'; |
| 3 | + |
| 4 | +describe('StyleComparisonTool', () => { |
| 5 | + let tool: StyleComparisonTool; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + tool = new StyleComparisonTool(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + jest.restoreAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('run', () => { |
| 16 | + it('should generate comparison URL with provided access token', async () => { |
| 17 | + const input = { |
| 18 | + before: 'mapbox/streets-v11', |
| 19 | + after: 'mapbox/outdoors-v12', |
| 20 | + accessToken: 'pk.test.token' |
| 21 | + }; |
| 22 | + |
| 23 | + const result = await tool.run(input); |
| 24 | + |
| 25 | + expect(result.isError).toBe(false); |
| 26 | + expect(result.content[0].type).toBe('text'); |
| 27 | + const url = (result.content[0] as { type: 'text'; text: string }).text; |
| 28 | + expect(url).toContain('https://agent.mapbox.com/tools/style-compare'); |
| 29 | + expect(url).toContain('access_token=pk.test.token'); |
| 30 | + expect(url).toContain('before=mapbox%2Fstreets-v11'); |
| 31 | + expect(url).toContain('after=mapbox%2Foutdoors-v12'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should require access token', async () => { |
| 35 | + const input = { |
| 36 | + before: 'mapbox/streets-v11', |
| 37 | + after: 'mapbox/satellite-v9' |
| 38 | + // Missing accessToken |
| 39 | + }; |
| 40 | + |
| 41 | + const result = await tool.run(input as any); |
| 42 | + |
| 43 | + expect(result.isError).toBe(true); |
| 44 | + expect( |
| 45 | + (result.content[0] as { type: 'text'; text: string }).text |
| 46 | + ).toContain('Required'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle full style URLs', async () => { |
| 50 | + const input = { |
| 51 | + before: 'mapbox://styles/mapbox/streets-v11', |
| 52 | + after: 'mapbox://styles/mapbox/outdoors-v12', |
| 53 | + accessToken: 'pk.test.token' |
| 54 | + }; |
| 55 | + |
| 56 | + const result = await tool.run(input); |
| 57 | + |
| 58 | + expect(result.isError).toBe(false); |
| 59 | + const url = (result.content[0] as { type: 'text'; text: string }).text; |
| 60 | + expect(url).toContain('before=mapbox%2Fstreets-v11'); |
| 61 | + expect(url).toContain('after=mapbox%2Foutdoors-v12'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle just style IDs with valid public token', async () => { |
| 65 | + // Mock MapboxApiBasedTool.getUserNameFromToken to return a username |
| 66 | + jest |
| 67 | + .spyOn(MapboxApiBasedTool, 'getUserNameFromToken') |
| 68 | + .mockReturnValue('testuser'); |
| 69 | + |
| 70 | + const input = { |
| 71 | + before: 'style-id-1', |
| 72 | + after: 'style-id-2', |
| 73 | + accessToken: 'pk.test.token' |
| 74 | + }; |
| 75 | + |
| 76 | + const result = await tool.run(input); |
| 77 | + |
| 78 | + expect(result.isError).toBe(false); |
| 79 | + const url = (result.content[0] as { type: 'text'; text: string }).text; |
| 80 | + expect(url).toContain('before=testuser%2Fstyle-id-1'); |
| 81 | + expect(url).toContain('after=testuser%2Fstyle-id-2'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should reject secret tokens', async () => { |
| 85 | + const input = { |
| 86 | + before: 'mapbox/streets-v11', |
| 87 | + after: 'mapbox/outdoors-v12', |
| 88 | + accessToken: 'sk.secret.token' |
| 89 | + }; |
| 90 | + |
| 91 | + const result = await tool.run(input); |
| 92 | + |
| 93 | + expect(result.isError).toBe(true); |
| 94 | + expect( |
| 95 | + (result.content[0] as { type: 'text'; text: string }).text |
| 96 | + ).toContain('Invalid token type'); |
| 97 | + expect( |
| 98 | + (result.content[0] as { type: 'text'; text: string }).text |
| 99 | + ).toContain('Secret tokens (sk.*) cannot be exposed'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should reject invalid token formats', async () => { |
| 103 | + const input = { |
| 104 | + before: 'mapbox/streets-v11', |
| 105 | + after: 'mapbox/outdoors-v12', |
| 106 | + accessToken: 'invalid.token' |
| 107 | + }; |
| 108 | + |
| 109 | + const result = await tool.run(input); |
| 110 | + |
| 111 | + expect(result.isError).toBe(true); |
| 112 | + expect( |
| 113 | + (result.content[0] as { type: 'text'; text: string }).text |
| 114 | + ).toContain('Invalid token type'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return error for style ID without valid username in token', async () => { |
| 118 | + // Mock getUserNameFromToken to throw an error |
| 119 | + jest |
| 120 | + .spyOn(MapboxApiBasedTool, 'getUserNameFromToken') |
| 121 | + .mockImplementation(() => { |
| 122 | + throw new Error( |
| 123 | + 'MAPBOX_ACCESS_TOKEN does not contain username in payload' |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + const input = { |
| 128 | + before: 'style-id-only', |
| 129 | + after: 'mapbox/outdoors-v12', |
| 130 | + accessToken: 'pk.test.token' |
| 131 | + }; |
| 132 | + |
| 133 | + const result = await tool.run(input); |
| 134 | + |
| 135 | + expect(result.isError).toBe(true); |
| 136 | + expect( |
| 137 | + (result.content[0] as { type: 'text'; text: string }).text |
| 138 | + ).toContain('Could not determine username'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should properly encode URL parameters', async () => { |
| 142 | + const input = { |
| 143 | + before: 'user-name/style-id-1', |
| 144 | + after: 'user-name/style-id-2', |
| 145 | + accessToken: 'pk.test.token' |
| 146 | + }; |
| 147 | + |
| 148 | + const result = await tool.run(input); |
| 149 | + |
| 150 | + expect(result.isError).toBe(false); |
| 151 | + const url = (result.content[0] as { type: 'text'; text: string }).text; |
| 152 | + // Check that forward slashes are URL encoded |
| 153 | + expect(url).toContain('before=user-name%2Fstyle-id-1'); |
| 154 | + expect(url).toContain('after=user-name%2Fstyle-id-2'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should include hash fragment with map position when coordinates are provided', async () => { |
| 158 | + const input = { |
| 159 | + before: 'mapbox/streets-v11', |
| 160 | + after: 'mapbox/outdoors-v12', |
| 161 | + accessToken: 'pk.test.token', |
| 162 | + zoom: 5.72, |
| 163 | + latitude: 9.503, |
| 164 | + longitude: -67.473 |
| 165 | + }; |
| 166 | + |
| 167 | + const result = await tool.run(input); |
| 168 | + |
| 169 | + expect(result.isError).toBe(false); |
| 170 | + const url = (result.content[0] as { type: 'text'; text: string }).text; |
| 171 | + expect(url).toContain('#5.72/9.503/-67.473'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should not include hash fragment when coordinates are incomplete', async () => { |
| 175 | + // Only zoom provided |
| 176 | + const input1 = { |
| 177 | + before: 'mapbox/streets-v11', |
| 178 | + after: 'mapbox/outdoors-v12', |
| 179 | + accessToken: 'pk.test.token', |
| 180 | + zoom: 10 |
| 181 | + }; |
| 182 | + |
| 183 | + const result1 = await tool.run(input1); |
| 184 | + expect(result1.isError).toBe(false); |
| 185 | + const url1 = (result1.content[0] as { type: 'text'; text: string }).text; |
| 186 | + expect(url1).not.toContain('#'); |
| 187 | + |
| 188 | + // Only latitude and longitude, no zoom |
| 189 | + const input2 = { |
| 190 | + before: 'mapbox/streets-v11', |
| 191 | + after: 'mapbox/outdoors-v12', |
| 192 | + accessToken: 'pk.test.token', |
| 193 | + latitude: 40.7128, |
| 194 | + longitude: -74.006 |
| 195 | + }; |
| 196 | + |
| 197 | + const result2 = await tool.run(input2); |
| 198 | + expect(result2.isError).toBe(false); |
| 199 | + const url2 = (result2.content[0] as { type: 'text'; text: string }).text; |
| 200 | + expect(url2).not.toContain('#'); |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + describe('metadata', () => { |
| 205 | + it('should have correct name and description', () => { |
| 206 | + expect(tool.name).toBe('style_comparison_tool'); |
| 207 | + expect(tool.description).toBe( |
| 208 | + 'Generate a comparison URL for comparing two Mapbox styles side-by-side' |
| 209 | + ); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments