import { describe, it, expect } from 'vitest';
import { validateUsername, validatePassword } from './validation.js';

describe('validateUsername', () => {
  it('accepts valid 3-char username', () => {
    expect(validateUsername('abc')).toEqual({ valid: true, value: 'abc' });
  });

  it('accepts valid username with underscore', () => {
    expect(validateUsername('test_user')).toEqual({ valid: true, value: 'test_user' });
  });

  it('trims whitespace', () => {
    expect(validateUsername('  testuser  ')).toEqual({ valid: true, value: 'testuser' });
  });

  it('rejects empty string', () => {
    expect(validateUsername('')).toEqual({ valid: false, error: 'Username is required' });
  });

  it('rejects null', () => {
    expect(validateUsername(null)).toEqual({ valid: false, error: 'Username is required' });
  });

  it('rejects undefined', () => {
    expect(validateUsername(undefined)).toEqual({ valid: false, error: 'Username is required' });
  });

  it('rejects non-string', () => {
    expect(validateUsername(123)).toEqual({ valid: false, error: 'Username is required' });
  });

  it('rejects too short (2 chars)', () => {
    expect(validateUsername('ab')).toEqual({ valid: false, error: 'Username must be at least 3 characters' });
  });

  it('rejects whitespace-only (trims to empty, fails length check)', () => {
    expect(validateUsername('   ')).toEqual({ valid: false, error: 'Username must be at least 3 characters' });
  });

  it('rejects too long (31 chars)', () => {
    const long = 'a'.repeat(31);
    expect(validateUsername(long)).toEqual({ valid: false, error: 'Username must be at most 30 characters' });
  });

  it('rejects special characters', () => {
    expect(validateUsername('user@name')).toEqual({
      valid: false,
      error: 'Username can only contain letters, numbers, and underscores',
    });
  });

  it('rejects hyphen', () => {
    expect(validateUsername('user-name')).toEqual({
      valid: false,
      error: 'Username can only contain letters, numbers, and underscores',
    });
  });

  it('rejects space in middle', () => {
    expect(validateUsername('user name')).toEqual({
      valid: false,
      error: 'Username can only contain letters, numbers, and underscores',
    });
  });

  it('accepts mixed case with numbers', () => {
    expect(validateUsername('MyUser_123')).toEqual({ valid: true, value: 'MyUser_123' });
  });
});

describe('validatePassword', () => {
  it('accepts valid 8-char password', () => {
    expect(validatePassword('12345678')).toEqual({ valid: true });
  });

  it('accepts longer password', () => {
    expect(validatePassword('a'.repeat(100))).toEqual({ valid: true });
  });

  it('rejects empty string', () => {
    expect(validatePassword('')).toEqual({ valid: false, error: 'Password is required' });
  });

  it('rejects null', () => {
    expect(validatePassword(null)).toEqual({ valid: false, error: 'Password is required' });
  });

  it('rejects undefined', () => {
    expect(validatePassword(undefined)).toEqual({ valid: false, error: 'Password is required' });
  });

  it('rejects non-string', () => {
    expect(validatePassword(123)).toEqual({ valid: false, error: 'Password is required' });
  });

  it('rejects too short (7 chars)', () => {
    expect(validatePassword('1234567')).toEqual({ valid: false, error: 'Password must be at least 8 characters' });
  });
});
