import { describe, it, expect } from 'vitest';
import { SecretRedactor } from '@zyphon/logger';

describe('SecretRedactor', () => {
  it('redacts sensitive object keys', () => {
    const redactor = new SecretRedactor();
    const payload = {
      username: 'john_doe',
      password: 'super_secret_password',
      apiKey: 'xyz987654321',
      nested: {
        token: 'auth_token_value',
        normalField: 42,
      },
    };

    const redacted = redactor.redact(payload);
    expect(redacted.password).toBe('[REDACTED]');
    expect(redacted.apiKey).toBe('[REDACTED]');
    expect(redacted.username).toBe('john_doe');
    expect(redacted.nested.token).toBe('[REDACTED]');
    expect(redacted.nested.normalField).toBe(42);
  });

  it('redacts Bearer tokens and regex secret patterns in strings', () => {
    const redactor = new SecretRedactor();
    const text = 'Authorization: Bearer mySecretToken123 with key AKIAIOSFODNN7EXAMPLE';
    const redacted = redactor.redactString(text);

    expect(redacted).not.toContain('mySecretToken123');
    expect(redacted).not.toContain('AKIAIOSFODNN7EXAMPLE');
    expect(redacted).toContain('[REDACTED]');
  });

  it('redacts registered dynamic secrets', () => {
    const redactor = new SecretRedactor();
    redactor.registerSecret('custom-db-credential-9988');

    const message = 'Connected using custom-db-credential-9988 successfully.';
    const redacted = redactor.redactString(message);

    expect(redacted).toBe('Connected using [REDACTED] successfully.');
  });
});

