import { describe, it, expect } from 'vitest'; import { detectSecrets, redactSecrets, SECRET_PATTERN_NAMES } from '../src/secret-patterns.js'; describe('detectSecrets', () => { it('returns null for clean text', () => { expect(detectSecrets('Hello world, no secrets here.')).toBeNull(); expect(detectSecrets('pk_test_looks_partial')).toBeNull(); // too short }); it('detects AWS access keys', () => { expect(detectSecrets('AKIAIOSFODNN7EXAMPLE used for upload')).toBe('aws-access-key'); }); it('detects GitHub PATs', () => { expect(detectSecrets('token=ghp_AbCdEfGhIjKlMnOpQrStUvWxYz0123456789')).toBe('github-pat'); }); it('detects OpenAI keys', () => { expect(detectSecrets('sk-proj-aaaaaaaaaaaaaaaaaaaaaaaa')).toBe('openai-key'); }); it('detects Anthropic keys', () => { expect(detectSecrets('key = sk-ant-api03-aaaaaaaaaaaaaaaaaaa')).toBe('anthropic-key'); }); it('detects Google API keys', () => { // Google API keys = literal "AIza" + exactly 35 chars expect(detectSecrets('AIzaSyA0B1c2d3e4F5G6H7I8J9K0L1M2N3O4P5Q')).toBe('google-api-key'); }); it('detects Stripe keys', () => { expect(detectSecrets('sk_live_51AbCdEfGhIjKlMnOpQrStUvWxYz')).toBe('stripe-secret'); }); it('detects private key blocks', () => { expect(detectSecrets('-----BEGIN RSA PRIVATE KEY-----\nMII...')).toBe('private-key-block'); }); it('detects JWT tokens', () => { const jwt = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; expect(detectSecrets(jwt)).toBe('jwt'); }); it('detects basic-auth URLs', () => { expect(detectSecrets('Use https://user:hunter2@example.com/api')) .toBe('basic-auth-url'); }); it('detects env-var password assignments', () => { expect(detectSecrets('PASSWORD=supersecret123')) .toBe('env-password'); }); it('detects postgres connection strings with creds', () => { expect(detectSecrets('postgres://admin:pw1234@db.local:5432/prod')) .toBe('pgsql-url'); }); it('SECRET_PATTERN_NAMES is non-empty and unique', () => { expect(SECRET_PATTERN_NAMES.length).toBeGreaterThan(10); expect(new Set(SECRET_PATTERN_NAMES).size).toBe(SECRET_PATTERN_NAMES.length); }); }); describe('redactSecrets', () => { it('replaces every match and reports the patterns that fired', () => { const { text, found } = redactSecrets('key AKIAIOSFODNN7EXAMPLE and PASSWORD=supersecret123'); expect(text).toContain('[REDACTED:aws-access-key]'); expect(text).toContain('[REDACTED:env-password]'); expect(text).not.toContain('AKIAIOSFODNN7EXAMPLE'); expect(found.sort()).toEqual(['aws-access-key', 'env-password']); }); it('leaves clean text untouched', () => { expect(redactSecrets('nothing to hide')).toEqual({ text: 'nothing to hide', found: [] }); }); });