import { FormControl, FormGroup } from '@angular/forms';
import { passwordStrengthValidator, passwordMatchValidator } from './password.validators';

describe('passwordStrengthValidator', () => {
  const validate = (value: string) =>
    passwordStrengthValidator()(new FormControl(value));

  it('returns null for an empty value (required handles emptiness)', () => {
    expect(validate('')).toBeNull();
  });

  it('returns null for a strong password', () => {
    expect(validate('Str0ng!Pass')).toBeNull();
  });

  it('flags a password that is too short', () => {
    const errors = validate('Ab1!');
    expect(errors?.['passwordStrength']?.['minlength']).toBeTrue();
  });

  it('flags a missing uppercase letter', () => {
    const errors = validate('weak1234!');
    expect(errors?.['passwordStrength']?.['uppercase']).toBeTrue();
  });

  it('flags a missing special character', () => {
    const errors = validate('Weak1234');
    expect(errors?.['passwordStrength']?.['special']).toBeTrue();
  });
});

describe('passwordMatchValidator', () => {
  const buildGroup = (current: string, next: string, confirm: string) =>
    new FormGroup({
      currentPassword: new FormControl(current),
      newPassword:     new FormControl(next),
      confirmPassword: new FormControl(confirm),
    });

  it('returns null when new matches confirm and differs from current', () => {
    const group = buildGroup('Old1234!', 'New1234!', 'New1234!');
    expect(passwordMatchValidator()(group)).toBeNull();
  });

  it('flags a confirm mismatch', () => {
    const group = buildGroup('Old1234!', 'New1234!', 'Different1!');
    expect(passwordMatchValidator()(group)?.['mismatch']).toBeTrue();
  });

  it('flags a new password equal to the current password', () => {
    const group = buildGroup('Same1234!', 'Same1234!', 'Same1234!');
    expect(passwordMatchValidator()(group)?.['sameAsCurrent']).toBeTrue();
  });
});
