error-sanitizer.test.ts52 lines · main
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | |
| 3 | import { sanitizeErrorMessage } from './error-sanitizer.js'; |
| 4 | |
| 5 | describe('error-sanitizer', () => { |
| 6 | test('strips /tmp/briven-isolate-* paths', () => { |
| 7 | const input = "TypeError at /tmp/briven-isolate-abc123/briven/functions/poolStats.ts:42:18"; |
| 8 | expect(sanitizeErrorMessage(input, [])).toBe( |
| 9 | 'TypeError at <bundle>/briven/functions/poolStats.ts:42:18', |
| 10 | ); |
| 11 | }); |
| 12 | |
| 13 | test('strips IPv4 addresses', () => { |
| 14 | expect(sanitizeErrorMessage('connect ECONNREFUSED 10.0.0.1:5432', [])).toBe( |
| 15 | 'connect ECONNREFUSED <ip>:5432', |
| 16 | ); |
| 17 | }); |
| 18 | |
| 19 | test('strips IPv6 addresses', () => { |
| 20 | expect(sanitizeErrorMessage('connect to fe80::1', [])).toBe('connect to <ip>'); |
| 21 | }); |
| 22 | |
| 23 | test('strips env-var values', () => { |
| 24 | const input = 'auth failed: token=sk_live_abc123'; |
| 25 | expect(sanitizeErrorMessage(input, ['sk_live_abc123', 'other'])).toBe( |
| 26 | 'auth failed: token=<redacted>', |
| 27 | ); |
| 28 | }); |
| 29 | |
| 30 | test('truncates to 2 KB', () => { |
| 31 | const long = 'x'.repeat(3000); |
| 32 | const out = sanitizeErrorMessage(long, []); |
| 33 | expect(out.length).toBeLessThanOrEqual(2048); |
| 34 | expect(out.endsWith('…')).toBe(true); |
| 35 | }); |
| 36 | |
| 37 | test('passes short safe messages unchanged', () => { |
| 38 | expect(sanitizeErrorMessage('something went wrong', [])).toBe('something went wrong'); |
| 39 | }); |
| 40 | |
| 41 | test('strips IPv6 leading-:: addresses including ::1', () => { |
| 42 | expect(sanitizeErrorMessage('connect to ::1', [])).toBe('connect to <ip>'); |
| 43 | expect(sanitizeErrorMessage('connect to ::', [])).toBe('connect to <ip>'); |
| 44 | }); |
| 45 | |
| 46 | test('handles paths, IPs, and env-values together', () => { |
| 47 | expect(sanitizeErrorMessage( |
| 48 | 'fetch /tmp/briven-isolate-x7k2/h.ts -> 10.0.0.1: token=sk_live_abc123', |
| 49 | ['sk_live_abc123'], |
| 50 | )).toBe('fetch <bundle>/h.ts -> <ip>: token=<redacted>'); |
| 51 | }); |
| 52 | }); |