|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { sanitizeStackTrace } from './sanitization'; |
| 4 | + |
| 5 | +describe('sanitizeStackTrace', () => { |
| 6 | + const cases: { name: string; input: string | undefined; expected: string }[] = [ |
| 7 | + { name: 'undefined input', input: undefined, expected: '' }, |
| 8 | + { name: 'empty string input', input: '', expected: '' }, |
| 9 | + { |
| 10 | + name: 'redacts double-quoted content', |
| 11 | + input: 'Error: "secret value" found', |
| 12 | + expected: 'Error: <redacted> found', |
| 13 | + }, |
| 14 | + { |
| 15 | + name: 'redacts backtick content', |
| 16 | + input: 'Error: `token abc` found', |
| 17 | + expected: 'Error: <redacted> found', |
| 18 | + }, |
| 19 | + { |
| 20 | + name: 'preserves single-quoted content (apostrophes)', |
| 21 | + input: "Error: can't find module 'foo'", |
| 22 | + expected: "Error: can't find module 'foo'", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'redacts email addresses', |
| 26 | + input: 'Error: contact user.name+tag@example.co.uk for help', |
| 27 | + expected: 'Error: contact <email> for help', |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'redacts home path preserving filename and :line:col', |
| 31 | + input: 'at fn ~/project/src/file.js:10:5', |
| 32 | + expected: 'at fn <path>/file.js:10:5', |
| 33 | + }, |
| 34 | + { |
| 35 | + name: 'redacts home path preserving filename without line:col', |
| 36 | + input: 'at fn ~/project/src/file.js', |
| 37 | + expected: 'at fn <path>/file.js', |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'redacts ~user home path preserving filename', |
| 41 | + input: 'at fn ~user/project/file.js:3:1', |
| 42 | + expected: 'at fn <path>/file.js:3:1', |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'redacts unix absolute path preserving filename and :line:col', |
| 46 | + input: 'at fn /Users/user/project/file.js:42:7', |
| 47 | + expected: 'at fn <path>/file.js:42:7', |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'redacts unix absolute path preserving filename without line:col', |
| 51 | + input: 'at fn /Users/user/project/file.js', |
| 52 | + expected: 'at fn <path>/file.js', |
| 53 | + }, |
| 54 | + { |
| 55 | + name: 'redacts windows path preserving filename and :line:col', |
| 56 | + input: String.raw`at fn C:\Users\user\project\file.js:12:4`, |
| 57 | + expected: 'at fn <path>/file.js:12:4', |
| 58 | + }, |
| 59 | + { |
| 60 | + name: 'redacts windows path preserving filename without line:col', |
| 61 | + input: String.raw`at fn D:\work\file.js`, |
| 62 | + expected: 'at fn <path>/file.js', |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'preserves filename for single-segment absolute path', |
| 66 | + input: 'at fn /foo', |
| 67 | + expected: 'at fn <path>/foo', // not worth the regex complexity to handle this very unlikely weird case |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'does not eat closing paren around path', |
| 71 | + input: 'at fn (/Users/alice/file.js:1:1)', |
| 72 | + expected: 'at fn (<path>/file.js:1:1)', |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'handles multi-line stack trace without eating across lines', |
| 76 | + input: 'at x (/a/first.js:1:1)\n at y (/b/second.js:2:2)', |
| 77 | + expected: 'at x (<path>/first.js:1:1)\n at y (<path>/second.js:2:2)', |
| 78 | + }, |
| 79 | + { |
| 80 | + name: 'caps input at 8000 chars before sanitization', |
| 81 | + input: 'a'.repeat(10_000), |
| 82 | + expected: 'a'.repeat(8000), |
| 83 | + }, |
| 84 | + ]; |
| 85 | + |
| 86 | + it.each(cases)('$name', ({ input, expected }) => { |
| 87 | + expect(sanitizeStackTrace(input)).toBe(expected); |
| 88 | + }); |
| 89 | +}); |
0 commit comments