/**
* Tests for PII sanitization
*/
import { describe, it, expect } from 'vitest';
import {
sanitizePii,
sanitizeObject,
containsPii,
detectPiiTypes,
isSensitiveField,
maskSensitiveValue,
} from './pii-sanitizer.js';
describe('sanitizePii', () => {
it('should sanitize email addresses', () => {
const input = 'Contact: user@example.com for help';
const result = sanitizePii(input);
expect(result).toBe('Contact: [EMAIL] for help');
});
it('should sanitize phone numbers', () => {
const input = 'Call me at 555-123-4567';
const result = sanitizePii(input);
expect(result).toBe('Call me at [PHONE]');
});
it('should sanitize credit card numbers', () => {
const input = 'Card: 4111-1111-1111-1111';
const result = sanitizePii(input);
expect(result).toBe('Card: [CREDIT_CARD]');
});
it('should sanitize API keys', () => {
const input = 'Key: sk-abcdefghijklmnopqrstuvwxyz';
const result = sanitizePii(input);
expect(result).toBe('Key: [API_KEY]');
});
it('should sanitize bearer tokens', () => {
const input = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N';
const result = sanitizePii(input);
expect(result).toBe('Bearer [TOKEN]');
});
it('should sanitize password fields in JSON', () => {
const input = '{"password": "secret123"}';
const result = sanitizePii(input);
expect(result).toContain('[REDACTED]');
});
it('should handle empty input', () => {
expect(sanitizePii('')).toBe('');
});
it('should return input without PII unchanged', () => {
const input = 'This is a normal message';
expect(sanitizePii(input)).toBe(input);
});
});
describe('sanitizeObject', () => {
it('should sanitize nested objects', () => {
const input = {
user: {
email: 'test@example.com',
name: 'John',
},
};
const result = sanitizeObject(input);
expect(result.user.email).toBe('[EMAIL]');
expect(result.user.name).toBe('John');
});
it('should sanitize arrays', () => {
const input = {
emails: ['a@example.com', 'b@example.com'],
};
const result = sanitizeObject(input);
expect(result.emails).toEqual(['[EMAIL]', '[EMAIL]']);
});
});
describe('containsPii', () => {
it('should detect PII', () => {
expect(containsPii('user@example.com')).toBe(true);
expect(containsPii('normal text')).toBe(false);
});
});
describe('detectPiiTypes', () => {
it('should return detected PII types', () => {
const input = 'Email: user@example.com, Phone: 555-123-4567';
const types = detectPiiTypes(input);
expect(types).toContain('email');
expect(types).toContain('phone');
});
});
describe('isSensitiveField', () => {
it('should identify sensitive field names', () => {
expect(isSensitiveField('password')).toBe(true);
expect(isSensitiveField('apiKey')).toBe(true);
expect(isSensitiveField('token')).toBe(true);
expect(isSensitiveField('username')).toBe(false);
});
});
describe('maskSensitiveValue', () => {
it('should mask values showing first/last chars', () => {
const result = maskSensitiveValue('supersecretpassword');
expect(result).toBe('supe********word');
});
it('should fully mask short values', () => {
const result = maskSensitiveValue('short');
expect(result).toBe('*****');
});
});