| 1 | import { IdeInfo } from "../../index.js"; |
| 2 | import { |
| 3 | SentryLogger, |
| 4 | captureException, |
| 5 | captureLog, |
| 6 | createSpan, |
| 7 | initializeSentry, |
| 8 | } from "./SentryLogger"; |
| 9 | import { anonymizeSentryEvent } from "./anonymization"; |
| 10 | |
| 11 | describe("SentryLogger Integration Tests", () => { |
| 12 | const mockIdeInfo: IdeInfo = { |
| 13 | extensionVersion: "1.0.0", |
| 14 | name: "vscode", |
| 15 | ideType: "vscode", |
| 16 | version: "1.0.0", |
| 17 | remoteName: "local", |
| 18 | isPrerelease: false, |
| 19 | }; |
| 20 | |
| 21 | beforeEach(() => { |
| 22 | // Reset SentryLogger state before each test |
| 23 | SentryLogger.client = undefined; |
| 24 | SentryLogger.scope = undefined; |
| 25 | SentryLogger.allowTelemetry = false; |
| 26 | SentryLogger.uniqueId = "NOT_UNIQUE"; |
| 27 | SentryLogger.ideInfo = undefined; |
| 28 | }); |
| 29 | |
| 30 | afterEach(() => { |
| 31 | // Clean up after each test |
| 32 | SentryLogger.shutdownSentryClient(); |
| 33 | }); |
| 34 | |
| 35 | describe("Telemetry Toggle Behavior", () => { |
| 36 | it("should not initialize when telemetry is disabled", async () => { |
| 37 | await SentryLogger.setup( |
| 38 | false, |
| 39 | "test-id", |
| 40 | mockIdeInfo, |
| 41 | "test@continue.dev", |
| 42 | ); |
| 43 | |
| 44 | expect(SentryLogger.allowTelemetry).toBe(false); |
| 45 | expect(SentryLogger.client).toBeUndefined(); |
| 46 | expect(SentryLogger.scope).toBeUndefined(); |
| 47 | expect(SentryLogger.uniqueId).toBe("test-id"); |
| 48 | expect(SentryLogger.ideInfo).toBe(mockIdeInfo); |
| 49 | }); |
| 50 | |
| 51 | it("should not initialize in test environment even when telemetry is enabled", async () => { |
| 52 | const originalNodeEnv = process.env.NODE_ENV; |
| 53 | process.env.NODE_ENV = "test"; |
| 54 | |
| 55 | await SentryLogger.setup( |
| 56 | true, |
| 57 | "test-id", |
| 58 | mockIdeInfo, |
| 59 | "test@continue.dev", |
| 60 | ); |
| 61 | |
| 62 | // In the updated implementation, the Continue team member check is used instead of NODE_ENV check |
| 63 | // so we're not setting allowTelemetry to false just because we're in test environment anymore |
| 64 | expect(SentryLogger.allowTelemetry).toBe(true); |
| 65 | // But client and scope should still be initialized |
| 66 | expect(SentryLogger.client).toBeDefined(); |
| 67 | expect(SentryLogger.scope).toBeDefined(); |
| 68 | |
| 69 | process.env.NODE_ENV = originalNodeEnv; |
| 70 | }); |
| 71 | |
| 72 | it("should initialize when telemetry is enabled in non-test environment", async () => { |
| 73 | const originalNodeEnv = process.env.NODE_ENV; |
| 74 | process.env.NODE_ENV = "development"; |
| 75 | |
| 76 | await SentryLogger.setup( |
| 77 | true, |
| 78 | "test-id", |
| 79 | mockIdeInfo, |
| 80 | "test@continue.dev", |
| 81 | ); |
| 82 | |
| 83 | expect(SentryLogger.allowTelemetry).toBe(true); |
| 84 | expect(SentryLogger.uniqueId).toBe("test-id"); |
| 85 | expect(SentryLogger.ideInfo).toBe(mockIdeInfo); |
| 86 | expect(SentryLogger.client).toBeDefined(); |
| 87 | expect(SentryLogger.scope).toBeDefined(); |
| 88 | |
| 89 | process.env.NODE_ENV = originalNodeEnv; |
| 90 | }); |
| 91 | |
| 92 | it("should handle telemetry being disabled after being enabled", async () => { |
| 93 | const originalNodeEnv = process.env.NODE_ENV; |
| 94 | process.env.NODE_ENV = "development"; |
| 95 | |
| 96 | // Enable telemetry first |
| 97 | await SentryLogger.setup( |
| 98 | true, |
| 99 | "test-id", |
| 100 | mockIdeInfo, |
| 101 | "test@continue.dev", |
| 102 | ); |
| 103 | expect(SentryLogger.allowTelemetry).toBe(true); |
| 104 | expect(SentryLogger.client).toBeDefined(); |
| 105 | |
| 106 | // Then disable it |
| 107 | await SentryLogger.setup(false, "test-id-2", mockIdeInfo); |
| 108 | expect(SentryLogger.allowTelemetry).toBe(false); |
| 109 | expect(SentryLogger.client).toBeUndefined(); |
| 110 | expect(SentryLogger.scope).toBeUndefined(); |
| 111 | |
| 112 | process.env.NODE_ENV = originalNodeEnv; |
| 113 | }); |
| 114 | |
| 115 | it("should not reinitialize if client already exists", async () => { |
| 116 | const originalNodeEnv = process.env.NODE_ENV; |
| 117 | process.env.NODE_ENV = "development"; |
| 118 | |
| 119 | // First setup |
| 120 | await SentryLogger.setup( |
| 121 | true, |
| 122 | "test-id", |
| 123 | mockIdeInfo, |
| 124 | "test@continue.dev", |
| 125 | ); |
| 126 | const firstClient = SentryLogger.client; |
| 127 | const firstScope = SentryLogger.scope; |
| 128 | |
| 129 | // Second setup should not change the client |
| 130 | await SentryLogger.setup( |
| 131 | true, |
| 132 | "test-id-2", |
| 133 | mockIdeInfo, |
| 134 | "test@continue.dev", |
| 135 | ); |
| 136 | expect(SentryLogger.client).toBe(firstClient); |
| 137 | expect(SentryLogger.scope).toBe(firstScope); |
| 138 | expect(SentryLogger.uniqueId).toBe("test-id-2"); // ID should update |
| 139 | expect(SentryLogger.ideInfo).toBe(mockIdeInfo); // Info should update |
| 140 | |
| 141 | process.env.NODE_ENV = originalNodeEnv; |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | describe("Lazy Initialization", () => { |
| 146 | it("should initialize when accessing lazyClient if conditions are met", () => { |
| 147 | const originalNodeEnv = process.env.NODE_ENV; |
| 148 | process.env.NODE_ENV = "development"; |
| 149 | |
| 150 | // Set up conditions for lazy initialization |
| 151 | SentryLogger.allowTelemetry = true; |
| 152 | SentryLogger.ideInfo = mockIdeInfo; |
| 153 | |
| 154 | // Access lazy client should trigger initialization |
| 155 | const client = SentryLogger.lazyClient; |
| 156 | |
| 157 | expect(client).toBeDefined(); |
| 158 | expect(SentryLogger.client).toBe(client); |
| 159 | expect(SentryLogger.scope).toBeDefined(); |
| 160 | |
| 161 | process.env.NODE_ENV = originalNodeEnv; |
| 162 | }); |
| 163 | |
| 164 | it("should not initialize if telemetry is disabled", () => { |
| 165 | SentryLogger.allowTelemetry = false; |
| 166 | SentryLogger.ideInfo = mockIdeInfo; |
| 167 | |
| 168 | const client = SentryLogger.lazyClient; |
| 169 | |
| 170 | expect(client).toBeUndefined(); |
| 171 | expect(SentryLogger.client).toBeUndefined(); |
| 172 | expect(SentryLogger.scope).toBeUndefined(); |
| 173 | }); |
| 174 | |
| 175 | it("should not initialize if ideInfo is missing", () => { |
| 176 | const originalNodeEnv = process.env.NODE_ENV; |
| 177 | process.env.NODE_ENV = "development"; |
| 178 | |
| 179 | SentryLogger.allowTelemetry = true; |
| 180 | SentryLogger.ideInfo = undefined; |
| 181 | |
| 182 | const client = SentryLogger.lazyClient; |
| 183 | |
| 184 | expect(client).toBeUndefined(); |
| 185 | expect(SentryLogger.client).toBeUndefined(); |
| 186 | |
| 187 | process.env.NODE_ENV = originalNodeEnv; |
| 188 | }); |
| 189 | }); |
| 190 | |
| 191 | describe("Shutdown Behavior", () => { |
| 192 | it("should properly shutdown when client exists", async () => { |
| 193 | const originalNodeEnv = process.env.NODE_ENV; |
| 194 | process.env.NODE_ENV = "development"; |
| 195 | |
| 196 | // Setup Sentry first |
| 197 | await SentryLogger.setup( |
| 198 | true, |
| 199 | "test-id", |
| 200 | mockIdeInfo, |
| 201 | "test@continue.dev", |
| 202 | ); |
| 203 | expect(SentryLogger.client).toBeDefined(); |
| 204 | |
| 205 | // Shutdown should clean up |
| 206 | SentryLogger.shutdownSentryClient(); |
| 207 | expect(SentryLogger.client).toBeUndefined(); |
| 208 | expect(SentryLogger.scope).toBeUndefined(); |
| 209 | |
| 210 | process.env.NODE_ENV = originalNodeEnv; |
| 211 | }); |
| 212 | |
| 213 | it("should handle shutdown gracefully when client is undefined", () => { |
| 214 | expect(() => SentryLogger.shutdownSentryClient()).not.toThrow(); |
| 215 | expect(SentryLogger.client).toBeUndefined(); |
| 216 | expect(SentryLogger.scope).toBeUndefined(); |
| 217 | }); |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | describe("Sentry Utility Functions", () => { |
| 222 | const mockIdeInfo: IdeInfo = { |
| 223 | extensionVersion: "1.0.0", |
| 224 | name: "vscode", |
| 225 | ideType: "vscode", |
| 226 | version: "1.0.0", |
| 227 | remoteName: "local", |
| 228 | isPrerelease: false, |
| 229 | }; |
| 230 | |
| 231 | beforeEach(() => { |
| 232 | // Reset SentryLogger state |
| 233 | SentryLogger.client = undefined; |
| 234 | SentryLogger.scope = undefined; |
| 235 | SentryLogger.allowTelemetry = false; |
| 236 | }); |
| 237 | |
| 238 | afterEach(() => { |
| 239 | SentryLogger.shutdownSentryClient(); |
| 240 | }); |
| 241 | |
| 242 | describe("initializeSentry", () => { |
| 243 | it("should return undefined when telemetry is disabled", () => { |
| 244 | const result = initializeSentry(); |
| 245 | |
| 246 | expect(result.client).toBeUndefined(); |
| 247 | expect(result.scope).toBeUndefined(); |
| 248 | }); |
| 249 | |
| 250 | it("should return client and scope when telemetry is enabled", async () => { |
| 251 | const originalNodeEnv = process.env.NODE_ENV; |
| 252 | process.env.NODE_ENV = "development"; |
| 253 | |
| 254 | await SentryLogger.setup( |
| 255 | true, |
| 256 | "test-id", |
| 257 | mockIdeInfo, |
| 258 | "test@continue.dev", |
| 259 | ); |
| 260 | const result = initializeSentry(); |
| 261 | |
| 262 | expect(result.client).toBeDefined(); |
| 263 | expect(result.scope).toBeDefined(); |
| 264 | expect(result.client).toBe(SentryLogger.client); |
| 265 | expect(result.scope).toBe(SentryLogger.scope); |
| 266 | |
| 267 | process.env.NODE_ENV = originalNodeEnv; |
| 268 | }); |
| 269 | }); |
| 270 | |
| 271 | describe("createSpan", () => { |
| 272 | it("should execute callback normally when telemetry is disabled", () => { |
| 273 | const callback = jest.fn().mockReturnValue("test-result"); |
| 274 | |
| 275 | const result = createSpan("test.operation", "test span", callback); |
| 276 | |
| 277 | expect(result).toBe("test-result"); |
| 278 | expect(callback).toHaveBeenCalledTimes(1); |
| 279 | }); |
| 280 | |
| 281 | it("should execute callback with span when telemetry is enabled", async () => { |
| 282 | const originalNodeEnv = process.env.NODE_ENV; |
| 283 | process.env.NODE_ENV = "development"; |
| 284 | |
| 285 | await SentryLogger.setup( |
| 286 | true, |
| 287 | "test-id", |
| 288 | mockIdeInfo, |
| 289 | "test@continue.dev", |
| 290 | ); |
| 291 | |
| 292 | const callback = jest.fn().mockReturnValue("test-result"); |
| 293 | const result = createSpan("test.operation", "test span", callback); |
| 294 | |
| 295 | expect(result).toBe("test-result"); |
| 296 | expect(callback).toHaveBeenCalledTimes(1); |
| 297 | |
| 298 | process.env.NODE_ENV = originalNodeEnv; |
| 299 | }); |
| 300 | |
| 301 | it("should handle async callbacks", async () => { |
| 302 | const originalNodeEnv = process.env.NODE_ENV; |
| 303 | process.env.NODE_ENV = "development"; |
| 304 | |
| 305 | await SentryLogger.setup( |
| 306 | true, |
| 307 | "test-id", |
| 308 | mockIdeInfo, |
| 309 | "test@continue.dev", |
| 310 | ); |
| 311 | |
| 312 | const asyncCallback = jest.fn().mockResolvedValue("async-result"); |
| 313 | const result = await createSpan( |
| 314 | "test.operation", |
| 315 | "test span", |
| 316 | asyncCallback, |
| 317 | ); |
| 318 | |
| 319 | expect(result).toBe("async-result"); |
| 320 | expect(asyncCallback).toHaveBeenCalledTimes(1); |
| 321 | |
| 322 | process.env.NODE_ENV = originalNodeEnv; |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | describe("captureException", () => { |
| 327 | it("should not throw when telemetry is disabled", () => { |
| 328 | const error = new Error("test error"); |
| 329 | |
| 330 | expect(() => captureException(error)).not.toThrow(); |
| 331 | expect(() => captureException(error, { context: "test" })).not.toThrow(); |
| 332 | }); |
| 333 | |
| 334 | it("should not throw when telemetry is enabled", async () => { |
| 335 | const originalNodeEnv = process.env.NODE_ENV; |
| 336 | process.env.NODE_ENV = "development"; |
| 337 | |
| 338 | await SentryLogger.setup( |
| 339 | true, |
| 340 | "test-id", |
| 341 | mockIdeInfo, |
| 342 | "test@continue.dev", |
| 343 | ); |
| 344 | |
| 345 | const error = new Error("test error"); |
| 346 | |
| 347 | expect(() => captureException(error)).not.toThrow(); |
| 348 | expect(() => captureException(error, { context: "test" })).not.toThrow(); |
| 349 | |
| 350 | process.env.NODE_ENV = originalNodeEnv; |
| 351 | }); |
| 352 | |
| 353 | it("should handle malformed context gracefully", async () => { |
| 354 | const originalNodeEnv = process.env.NODE_ENV; |
| 355 | process.env.NODE_ENV = "development"; |
| 356 | |
| 357 | await SentryLogger.setup( |
| 358 | true, |
| 359 | "test-id", |
| 360 | mockIdeInfo, |
| 361 | "test@continue.dev", |
| 362 | ); |
| 363 | |
| 364 | const error = new Error("test error"); |
| 365 | |
| 366 | expect(() => captureException(error, null as any)).not.toThrow(); |
| 367 | expect(() => captureException(error, undefined)).not.toThrow(); |
| 368 | expect(() => captureException(error, { circular: {} })).not.toThrow(); |
| 369 | |
| 370 | process.env.NODE_ENV = originalNodeEnv; |
| 371 | }); |
| 372 | }); |
| 373 | |
| 374 | describe("captureLog", () => { |
| 375 | it("should not throw when telemetry is disabled", () => { |
| 376 | expect(() => captureLog("test message")).not.toThrow(); |
| 377 | expect(() => |
| 378 | captureLog("test message", "info", { context: "test" }), |
| 379 | ).not.toThrow(); |
| 380 | }); |
| 381 | |
| 382 | it("should not throw when telemetry is enabled", async () => { |
| 383 | const originalNodeEnv = process.env.NODE_ENV; |
| 384 | process.env.NODE_ENV = "development"; |
| 385 | |
| 386 | await SentryLogger.setup( |
| 387 | true, |
| 388 | "test-id", |
| 389 | mockIdeInfo, |
| 390 | "test@continue.dev", |
| 391 | ); |
| 392 | |
| 393 | expect(() => captureLog("test message")).not.toThrow(); |
| 394 | expect(() => |
| 395 | captureLog("test message", "error", { context: "test" }), |
| 396 | ).not.toThrow(); |
| 397 | |
| 398 | process.env.NODE_ENV = originalNodeEnv; |
| 399 | }); |
| 400 | |
| 401 | it("should handle malformed context gracefully", async () => { |
| 402 | const originalNodeEnv = process.env.NODE_ENV; |
| 403 | process.env.NODE_ENV = "development"; |
| 404 | |
| 405 | await SentryLogger.setup( |
| 406 | true, |
| 407 | "test-id", |
| 408 | mockIdeInfo, |
| 409 | "test@continue.dev", |
| 410 | ); |
| 411 | |
| 412 | expect(() => |
| 413 | captureLog("test message", "info", null as any), |
| 414 | ).not.toThrow(); |
| 415 | expect(() => |
| 416 | captureLog("test message", "warning", undefined), |
| 417 | ).not.toThrow(); |
| 418 | expect(() => |
| 419 | captureLog("test message", "error", { circular: {} }), |
| 420 | ).not.toThrow(); |
| 421 | |
| 422 | process.env.NODE_ENV = originalNodeEnv; |
| 423 | }); |
| 424 | }); |
| 425 | |
| 426 | describe("Integration Tests", () => { |
| 427 | it("should maintain consistent state across utility functions", async () => { |
| 428 | const originalNodeEnv = process.env.NODE_ENV; |
| 429 | process.env.NODE_ENV = "development"; |
| 430 | |
| 431 | // Start with telemetry disabled |
| 432 | expect(initializeSentry().client).toBeUndefined(); |
| 433 | |
| 434 | const callback = jest.fn().mockReturnValue("result"); |
| 435 | createSpan("test", "test", callback); |
| 436 | expect(callback).toHaveBeenCalled(); |
| 437 | |
| 438 | // Enable telemetry |
| 439 | await SentryLogger.setup( |
| 440 | true, |
| 441 | "test-id", |
| 442 | mockIdeInfo, |
| 443 | "test@continue.dev", |
| 444 | ); |
| 445 | |
| 446 | // Now utility functions should work with Sentry |
| 447 | const sentryResult = initializeSentry(); |
| 448 | expect(sentryResult.client).toBeDefined(); |
| 449 | expect(sentryResult.scope).toBeDefined(); |
| 450 | |
| 451 | // Functions should work without throwing |
| 452 | const callback2 = jest.fn().mockReturnValue("result2"); |
| 453 | const spanResult = createSpan("test2", "test2", callback2); |
| 454 | expect(spanResult).toBe("result2"); |
| 455 | expect(callback2).toHaveBeenCalled(); |
| 456 | |
| 457 | const error = new Error("test error"); |
| 458 | expect(() => captureException(error, { test: "context" })).not.toThrow(); |
| 459 | expect(() => |
| 460 | captureLog("test log message", "info", { test: "context" }), |
| 461 | ).not.toThrow(); |
| 462 | |
| 463 | // Disable telemetry again |
| 464 | await SentryLogger.setup( |
| 465 | false, |
| 466 | "test-id", |
| 467 | mockIdeInfo, |
| 468 | "test@continue.dev", |
| 469 | ); |
| 470 | expect(initializeSentry().client).toBeUndefined(); |
| 471 | |
| 472 | process.env.NODE_ENV = originalNodeEnv; |
| 473 | }); |
| 474 | }); |
| 475 | }); |
| 476 | |
| 477 | describe("Sentry Data Anonymization Tests", () => { |
| 478 | describe("anonymizeSentryEvent", () => { |
| 479 | it("should anonymize user file paths in stack traces", () => { |
| 480 | const event: any = { |
| 481 | exception: { |
| 482 | values: [ |
| 483 | { |
| 484 | stacktrace: { |
| 485 | frames: [ |
| 486 | { |
| 487 | filename: "/Users/john/workspace/my-project/src/app.js", |
| 488 | abs_path: "/Users/john/workspace/my-project/src/app.js", |
| 489 | function: "myFunction", |
| 490 | lineno: 42, |
| 491 | vars: { secret: "password123" }, |
| 492 | pre_context: ["const secret = 'password123'"], |
| 493 | post_context: ["console.log(secret)"], |
| 494 | context_line: "throw new Error('test')", |
| 495 | }, |
| 496 | ], |
| 497 | }, |
| 498 | }, |
| 499 | ], |
| 500 | }, |
| 501 | }; |
| 502 | |
| 503 | const result = anonymizeSentryEvent(event); |
| 504 | expect(result).not.toBeNull(); |
| 505 | expect( |
| 506 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename, |
| 507 | ).toBe("<file>"); |
| 508 | expect( |
| 509 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path, |
| 510 | ).toBe(""); |
| 511 | expect( |
| 512 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.vars, |
| 513 | ).toBeUndefined(); |
| 514 | expect( |
| 515 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.pre_context, |
| 516 | ).toBeUndefined(); |
| 517 | expect( |
| 518 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.post_context, |
| 519 | ).toBeUndefined(); |
| 520 | expect( |
| 521 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.context_line, |
| 522 | ).toBe("<code>"); |
| 523 | }); |
| 524 | |
| 525 | it("should anonymize all absolute file paths equally", () => { |
| 526 | const event: any = { |
| 527 | exception: { |
| 528 | values: [ |
| 529 | { |
| 530 | stacktrace: { |
| 531 | frames: [ |
| 532 | { |
| 533 | filename: "/Users/dev/continue/core/util/sentry.js", |
| 534 | abs_path: "/Users/dev/continue/core/util/sentry.js", |
| 535 | function: "captureException", |
| 536 | lineno: 42, |
| 537 | vars: { secret: "password123" }, |
| 538 | pre_context: ["const secret = 'password123'"], |
| 539 | post_context: ["console.log(secret)"], |
| 540 | context_line: "throw new Error('test')", |
| 541 | }, |
| 542 | ], |
| 543 | }, |
| 544 | }, |
| 545 | ], |
| 546 | }, |
| 547 | }; |
| 548 | |
| 549 | const result = anonymizeSentryEvent(event); |
| 550 | expect(result).not.toBeNull(); |
| 551 | expect( |
| 552 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename, |
| 553 | ).toBe("<file>"); |
| 554 | expect( |
| 555 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path, |
| 556 | ).toBe(""); |
| 557 | expect( |
| 558 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.vars, |
| 559 | ).toBeUndefined(); |
| 560 | expect( |
| 561 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.pre_context, |
| 562 | ).toBeUndefined(); |
| 563 | expect( |
| 564 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.post_context, |
| 565 | ).toBeUndefined(); |
| 566 | expect( |
| 567 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.context_line, |
| 568 | ).toBe("<code>"); |
| 569 | }); |
| 570 | |
| 571 | it("should preserve node_modules package names for debugging", () => { |
| 572 | const event: any = { |
| 573 | exception: { |
| 574 | values: [ |
| 575 | { |
| 576 | stacktrace: { |
| 577 | frames: [ |
| 578 | { |
| 579 | filename: |
| 580 | "/Users/dev/project/node_modules/@sentry/node/index.js", |
| 581 | abs_path: |
| 582 | "/Users/dev/project/node_modules/@sentry/node/index.js", |
| 583 | function: "init", |
| 584 | lineno: 123, |
| 585 | }, |
| 586 | ], |
| 587 | }, |
| 588 | }, |
| 589 | ], |
| 590 | }, |
| 591 | }; |
| 592 | |
| 593 | const result = anonymizeSentryEvent(event); |
| 594 | expect(result).not.toBeNull(); |
| 595 | expect( |
| 596 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename, |
| 597 | ).toBe("node_modules/@sentry/<file>"); |
| 598 | expect( |
| 599 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path, |
| 600 | ).toBe(""); |
| 601 | }); |
| 602 | |
| 603 | it("should hash user IDs", () => { |
| 604 | const event: any = { |
| 605 | user: { |
| 606 | id: "real-machine-id-12345", |
| 607 | username: "john.doe", |
| 608 | email: "john@example.com", |
| 609 | ip_address: "192.168.1.100", |
| 610 | }, |
| 611 | }; |
| 612 | |
| 613 | const result = anonymizeSentryEvent(event); |
| 614 | expect(result).not.toBeNull(); |
| 615 | expect(result?.user?.id).not.toBe("real-machine-id-12345"); |
| 616 | expect(result?.user?.id).toMatch(/^[a-f0-9]{8}$/); // Should be 8-char hash |
| 617 | expect(result?.user?.username).toBeUndefined(); |
| 618 | expect(result?.user?.email).toBeUndefined(); |
| 619 | expect(result?.user?.ip_address).toBeUndefined(); |
| 620 | }); |
| 621 | |
| 622 | it("should preserve breadcrumbs unchanged", () => { |
| 623 | const event: any = { |
| 624 | breadcrumbs: [ |
| 625 | { |
| 626 | message: |
| 627 | "File opened: /Users/john/Documents/secret-project/config.js", |
| 628 | category: "file", |
| 629 | data: { |
| 630 | filename: "/Users/john/Documents/secret-project/config.js", |
| 631 | action: "open", |
| 632 | }, |
| 633 | }, |
| 634 | { |
| 635 | message: "Error in module initialization", |
| 636 | category: "error", |
| 637 | data: { |
| 638 | module: "auth", |
| 639 | error: "Connection failed", |
| 640 | }, |
| 641 | }, |
| 642 | ], |
| 643 | }; |
| 644 | |
| 645 | const result = anonymizeSentryEvent(event); |
| 646 | expect(result).not.toBeNull(); |
| 647 | // Breadcrumbs are preserved as-is in our minimalist approach |
| 648 | expect(result?.breadcrumbs?.[0]?.message).toBe( |
| 649 | "File opened: /Users/john/Documents/secret-project/config.js", |
| 650 | ); |
| 651 | expect(result?.breadcrumbs?.[0]?.data?.filename).toBe( |
| 652 | "/Users/john/Documents/secret-project/config.js", |
| 653 | ); |
| 654 | expect(result?.breadcrumbs?.[0]?.data?.action).toBe("open"); |
| 655 | expect(result?.breadcrumbs?.[1]?.message).toBe( |
| 656 | "Error in module initialization", |
| 657 | ); |
| 658 | expect(result?.breadcrumbs?.[1]?.data?.module).toBe("auth"); |
| 659 | expect(result?.breadcrumbs?.[1]?.data?.error).toBe("Connection failed"); |
| 660 | }); |
| 661 | |
| 662 | it("should preserve tags unchanged", () => { |
| 663 | const event: any = { |
| 664 | tags: { |
| 665 | username: "john.doe", |
| 666 | user_id: "12345", |
| 667 | workspace: "/Users/john/workspace", |
| 668 | project_path: "/path/to/project", |
| 669 | environment: "production", |
| 670 | version: "1.2.3", |
| 671 | }, |
| 672 | }; |
| 673 | |
| 674 | const result = anonymizeSentryEvent(event); |
| 675 | expect(result).not.toBeNull(); |
| 676 | // Tags are preserved as-is in our minimalist approach |
| 677 | expect(result?.tags?.username).toBe("john.doe"); |
| 678 | expect(result?.tags?.user_id).toBe("12345"); |
| 679 | expect(result?.tags?.workspace).toBe("/Users/john/workspace"); |
| 680 | expect(result?.tags?.project_path).toBe("/path/to/project"); |
| 681 | expect(result?.tags?.environment).toBe("production"); |
| 682 | expect(result?.tags?.version).toBe("1.2.3"); |
| 683 | }); |
| 684 | |
| 685 | it("should preserve extra data unchanged", () => { |
| 686 | const event: any = { |
| 687 | extra: { |
| 688 | path: "/Users/john/secret-project", |
| 689 | filename: "config.js", |
| 690 | directory: "/home/user/workspace", |
| 691 | workspace: "/Users/john/workspace", |
| 692 | user: "john.doe", |
| 693 | home: "/Users/john", |
| 694 | config_file: "/some/long/path/to/config.json", |
| 695 | error_code: 500, |
| 696 | timestamp: "2023-01-01T00:00:00Z", |
| 697 | }, |
| 698 | }; |
| 699 | |
| 700 | const result = anonymizeSentryEvent(event); |
| 701 | expect(result).not.toBeNull(); |
| 702 | // Extra data is preserved as-is in our minimalist approach |
| 703 | expect(result?.extra?.path).toBe("/Users/john/secret-project"); |
| 704 | expect(result?.extra?.filename).toBe("config.js"); |
| 705 | expect(result?.extra?.directory).toBe("/home/user/workspace"); |
| 706 | expect(result?.extra?.workspace).toBe("/Users/john/workspace"); |
| 707 | expect(result?.extra?.user).toBe("john.doe"); |
| 708 | expect(result?.extra?.home).toBe("/Users/john"); |
| 709 | expect(result?.extra?.config_file).toBe("/some/long/path/to/config.json"); |
| 710 | expect(result?.extra?.error_code).toBe(500); |
| 711 | expect(result?.extra?.timestamp).toBe("2023-01-01T00:00:00Z"); |
| 712 | }); |
| 713 | |
| 714 | it("should anonymize thread stack traces", () => { |
| 715 | const event: any = { |
| 716 | threads: { |
| 717 | values: [ |
| 718 | { |
| 719 | id: "main", |
| 720 | name: "MainThread", |
| 721 | stacktrace: { |
| 722 | frames: [ |
| 723 | { |
| 724 | filename: "/Users/john/workspace/app.js", |
| 725 | abs_path: "/Users/john/workspace/app.js", |
| 726 | function: "main", |
| 727 | lineno: 1, |
| 728 | vars: { apiKey: "secret-key-123" }, |
| 729 | context_line: "const apiKey = 'secret-key-123'", |
| 730 | }, |
| 731 | ], |
| 732 | }, |
| 733 | }, |
| 734 | ], |
| 735 | }, |
| 736 | }; |
| 737 | |
| 738 | const result = anonymizeSentryEvent(event); |
| 739 | expect(result).not.toBeNull(); |
| 740 | expect( |
| 741 | result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.filename, |
| 742 | ).toBe("<file>"); |
| 743 | expect( |
| 744 | result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path, |
| 745 | ).toBe(""); |
| 746 | expect( |
| 747 | result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.vars, |
| 748 | ).toBeUndefined(); |
| 749 | expect( |
| 750 | result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.context_line, |
| 751 | ).toBe("<code>"); |
| 752 | }); |
| 753 | |
| 754 | it("should remove OS environment variables", () => { |
| 755 | const event: any = { |
| 756 | contexts: { |
| 757 | os: { |
| 758 | name: "macOS", |
| 759 | version: "12.6", |
| 760 | environment: { |
| 761 | HOME: "/Users/john", |
| 762 | USER: "john", |
| 763 | PATH: "/usr/local/bin:/usr/bin", |
| 764 | }, |
| 765 | }, |
| 766 | }, |
| 767 | }; |
| 768 | |
| 769 | const result = anonymizeSentryEvent(event); |
| 770 | expect(result).not.toBeNull(); |
| 771 | expect(result?.contexts?.os?.name).toBe("macOS"); |
| 772 | expect(result?.contexts?.os?.version).toBe("12.6"); |
| 773 | expect(result?.contexts?.os?.environment).toBeUndefined(); |
| 774 | }); |
| 775 | |
| 776 | it("should handle events with no sensitive data", () => { |
| 777 | const event: any = { |
| 778 | message: "Simple error message", |
| 779 | level: "error", |
| 780 | timestamp: 1234567890, |
| 781 | tags: { |
| 782 | component: "auth", |
| 783 | level: "error", |
| 784 | }, |
| 785 | extra: { |
| 786 | error_code: 404, |
| 787 | retry_count: 3, |
| 788 | }, |
| 789 | }; |
| 790 | |
| 791 | const result = anonymizeSentryEvent(event); |
| 792 | expect(result).not.toBeNull(); |
| 793 | expect(result?.message).toBe("Simple error message"); |
| 794 | expect(result?.level).toBe("error"); |
| 795 | expect(result?.timestamp).toBe(1234567890); |
| 796 | expect(result?.tags?.component).toBe("auth"); |
| 797 | expect(result?.extra?.error_code).toBe(404); |
| 798 | }); |
| 799 | |
| 800 | it("should handle malformed events gracefully", () => { |
| 801 | const malformedEvent: any = { |
| 802 | exception: { |
| 803 | values: null, |
| 804 | }, |
| 805 | user: null, |
| 806 | breadcrumbs: "not-an-array", |
| 807 | tags: null, |
| 808 | extra: null, |
| 809 | }; |
| 810 | |
| 811 | const result = anonymizeSentryEvent(malformedEvent); |
| 812 | expect(result).not.toBeNull(); |
| 813 | }); |
| 814 | |
| 815 | it("should handle anonymization errors by returning null", () => { |
| 816 | // Create a circular reference that would cause JSON serialization issues |
| 817 | const circularEvent: any = { |
| 818 | message: "Test event", |
| 819 | }; |
| 820 | circularEvent.circular = circularEvent; |
| 821 | |
| 822 | // Mock the anonymization to throw an error |
| 823 | const originalConsoleError = console.error; |
| 824 | console.error = jest.fn(); |
| 825 | |
| 826 | const result = anonymizeSentryEvent(circularEvent); |
| 827 | |
| 828 | // Should not throw, but should return null for safety |
| 829 | expect(result).not.toBeNull(); // Actually, our function might handle this case |
| 830 | |
| 831 | console.error = originalConsoleError; |
| 832 | }); |
| 833 | |
| 834 | it("should anonymize all absolute paths regardless of format", () => { |
| 835 | const event: any = { |
| 836 | exception: { |
| 837 | values: [ |
| 838 | { |
| 839 | stacktrace: { |
| 840 | frames: [ |
| 841 | { |
| 842 | filename: |
| 843 | "C:\\Users\\dev\\continue\\extensions\\vscode\\src\\extension.js", |
| 844 | abs_path: |
| 845 | "C:\\Users\\dev\\continue\\extensions\\vscode\\src\\extension.js", |
| 846 | }, |
| 847 | { |
| 848 | filename: "/home/dev/continue/core/util/sentry.js", |
| 849 | abs_path: "/home/dev/continue/core/util/sentry.js", |
| 850 | }, |
| 851 | { |
| 852 | filename: "relative/path/file.js", |
| 853 | abs_path: "/absolute/path/file.js", |
| 854 | }, |
| 855 | ], |
| 856 | }, |
| 857 | }, |
| 858 | ], |
| 859 | }, |
| 860 | }; |
| 861 | |
| 862 | const result = anonymizeSentryEvent(event); |
| 863 | expect(result).not.toBeNull(); |
| 864 | // All absolute paths get anonymized equally |
| 865 | expect( |
| 866 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename, |
| 867 | ).toBe("<file>"); |
| 868 | expect( |
| 869 | result?.exception?.values?.[0]?.stacktrace?.frames?.[1]?.filename, |
| 870 | ).toBe("<file>"); |
| 871 | expect( |
| 872 | result?.exception?.values?.[0]?.stacktrace?.frames?.[2]?.filename, |
| 873 | ).toBe("relative/path/file.js"); // Relative path preserved |
| 874 | // All abs_path fields cleared |
| 875 | expect( |
| 876 | result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path, |
| 877 | ).toBe(""); |
| 878 | expect( |
| 879 | result?.exception?.values?.[0]?.stacktrace?.frames?.[1]?.abs_path, |
| 880 | ).toBe(""); |
| 881 | expect( |
| 882 | result?.exception?.values?.[0]?.stacktrace?.frames?.[2]?.abs_path, |
| 883 | ).toBe(""); |
| 884 | }); |
| 885 | }); |
| 886 | }); |