|
8 | 8 |
|
9 | 9 | import * as assert from 'assert'; |
10 | 10 | import * as path from 'path'; |
11 | | -import * as os from 'os'; |
12 | 11 | import * as fs from 'fs'; |
13 | 12 | import { CopilotSyncService } from '../../src/services/CopilotSyncService'; |
14 | 13 |
|
@@ -181,6 +180,131 @@ suite('CopilotSyncService', () => { |
181 | 180 | }); |
182 | 181 | }); |
183 | 182 |
|
| 183 | + suite('Windows Path Regex Handling (Backslash Escaping)', () => { |
| 184 | + // These tests verify the fix for: "Invalid regular expression: /\profiles\([^\]+)/: Unterminated character class" |
| 185 | + // The issue was that Windows path.sep (\) wasn't properly escaped in regex character classes |
| 186 | + |
| 187 | + test('should handle Windows-style path with backslashes - standard profile', async () => { |
| 188 | + // Use path.join to create platform-appropriate paths |
| 189 | + const userPath = path.join(tempDir, 'WinTest', 'Users', 'Username', 'AppData', 'Roaming', 'Code', 'User'); |
| 190 | + const globalStoragePath = path.join(userPath, 'globalStorage', 'amadeusitgroup.prompt-registry'); |
| 191 | + |
| 192 | + const winContext = { |
| 193 | + globalStorageUri: { fsPath: globalStoragePath }, |
| 194 | + storageUri: { fsPath: tempDir }, |
| 195 | + extensionPath: __dirname, |
| 196 | + subscriptions: [], |
| 197 | + } as any; |
| 198 | + |
| 199 | + const winService = new CopilotSyncService(winContext); |
| 200 | + |
| 201 | + // The key test: should not throw "Invalid regular expression" or "Unterminated character class" |
| 202 | + const status = await winService.getStatus(); |
| 203 | + |
| 204 | + // Should successfully parse without regex errors |
| 205 | + assert.ok(status.copilotDir, 'Should return a valid path'); |
| 206 | + assert.ok(status.copilotDir.includes('User'), 'Should include User directory'); |
| 207 | + assert.ok(status.copilotDir.endsWith('prompts'), 'Should end with prompts'); |
| 208 | + |
| 209 | + const expectedPath = path.join(userPath, 'prompts'); |
| 210 | + assert.strictEqual(status.copilotDir, expectedPath, 'Should resolve to User/prompts'); |
| 211 | + }); |
| 212 | + |
| 213 | + test('should handle Windows-style path with backslashes - profile-based', async () => { |
| 214 | + // Simulate the exact error case from the screenshot |
| 215 | + const userPath = path.join(tempDir, 'WinProfile', 'Users', 'Username', '.vscode', 'extensions', 'dist', 'User'); |
| 216 | + const profileId = 'security-best-practices'; |
| 217 | + const globalStoragePath = path.join(userPath, 'profiles', profileId, 'globalStorage', 'amadeusitgroup.prompt-registry'); |
| 218 | + |
| 219 | + const winProfileContext = { |
| 220 | + globalStorageUri: { fsPath: globalStoragePath }, |
| 221 | + storageUri: { fsPath: tempDir }, |
| 222 | + extensionPath: __dirname, |
| 223 | + subscriptions: [], |
| 224 | + } as any; |
| 225 | + |
| 226 | + const winProfileService = new CopilotSyncService(winProfileContext); |
| 227 | + |
| 228 | + // The key test: should not throw regex errors |
| 229 | + const status = await winProfileService.getStatus(); |
| 230 | + |
| 231 | + // Should successfully parse the profile path without regex errors |
| 232 | + assert.ok(status.copilotDir, 'Should return a valid path'); |
| 233 | + assert.ok(status.copilotDir.includes('profiles'), 'Should include profiles directory'); |
| 234 | + assert.ok(status.copilotDir.includes(profileId), 'Should include profile ID'); |
| 235 | + assert.ok(status.copilotDir.endsWith('prompts'), 'Should end with prompts'); |
| 236 | + |
| 237 | + const expectedPath = path.join(userPath, 'profiles', profileId, 'prompts'); |
| 238 | + assert.strictEqual(status.copilotDir, expectedPath, 'Should resolve to profile prompts directory'); |
| 239 | + }); |
| 240 | + |
| 241 | + test('should handle Windows-style path - custom data directory with profile', async () => { |
| 242 | + // Test custom data directory (no User folder) with profile |
| 243 | + const customDataDir = path.join(tempDir, 'CustomVSCode', 'Data'); |
| 244 | + const profileId = 'work-profile'; |
| 245 | + const globalStoragePath = path.join(customDataDir, 'profiles', profileId, 'globalStorage', 'publisher.extension'); |
| 246 | + |
| 247 | + const customContext = { |
| 248 | + globalStorageUri: { fsPath: globalStoragePath }, |
| 249 | + storageUri: { fsPath: tempDir }, |
| 250 | + extensionPath: __dirname, |
| 251 | + subscriptions: [], |
| 252 | + } as any; |
| 253 | + |
| 254 | + const customService = new CopilotSyncService(customContext); |
| 255 | + |
| 256 | + // The key test: should not throw regex errors |
| 257 | + const status = await customService.getStatus(); |
| 258 | + |
| 259 | + // Should handle custom data directory with profiles |
| 260 | + assert.ok(status.copilotDir, 'Should return a valid path'); |
| 261 | + assert.ok(status.copilotDir.includes('profiles'), 'Should include profiles directory'); |
| 262 | + assert.ok(status.copilotDir.includes(profileId), 'Should include profile ID'); |
| 263 | + |
| 264 | + const expectedPath = path.join(customDataDir, 'profiles', profileId, 'prompts'); |
| 265 | + assert.strictEqual(status.copilotDir, expectedPath, 'Should resolve custom data dir with profile'); |
| 266 | + }); |
| 267 | + |
| 268 | + test('should not throw regex errors with any path separator', async () => { |
| 269 | + // This is the core regression test for the bug fix |
| 270 | + // The bug was: new RegExp(`[^${path.sep}]`) would create [^\] on Windows |
| 271 | + // which is an unterminated character class |
| 272 | + |
| 273 | + const testPath = path.join(tempDir, 'RegexTest', 'Code', 'User', 'profiles', 'test-id', 'globalStorage', 'ext'); |
| 274 | + |
| 275 | + const testContext = { |
| 276 | + globalStorageUri: { fsPath: testPath }, |
| 277 | + storageUri: { fsPath: tempDir }, |
| 278 | + extensionPath: __dirname, |
| 279 | + subscriptions: [], |
| 280 | + } as any; |
| 281 | + |
| 282 | + const testService = new CopilotSyncService(testContext); |
| 283 | + |
| 284 | + // Should not throw regex errors regardless of platform |
| 285 | + try { |
| 286 | + const status = await testService.getStatus(); |
| 287 | + assert.ok(status, 'Should successfully get status'); |
| 288 | + assert.ok(status.copilotDir, 'Should return a path'); |
| 289 | + } catch (error: any) { |
| 290 | + // The specific errors we're testing for |
| 291 | + assert.ok( |
| 292 | + !error.message.includes('Invalid regular expression'), |
| 293 | + `Should not throw "Invalid regular expression", got: ${error.message}` |
| 294 | + ); |
| 295 | + assert.ok( |
| 296 | + !error.message.includes('Unterminated character class'), |
| 297 | + `Should not throw "Unterminated character class", got: ${error.message}` |
| 298 | + ); |
| 299 | + assert.ok( |
| 300 | + !error.message.includes('SyntaxError'), |
| 301 | + `Should not throw SyntaxError from regex, got: ${error.message}` |
| 302 | + ); |
| 303 | + // If it's a different error (like file not found), that's acceptable for this test |
| 304 | + } |
| 305 | + }); |
| 306 | + }); |
| 307 | + |
184 | 308 | suite('getStatus', () => { |
185 | 309 | test('should return status information', async () => { |
186 | 310 | const status = await service.getStatus(); |
|
0 commit comments