/**
 * Unit tests for auth module — types + validation.
 */

import { describe, it, expect } from 'vitest';
import { LoginSchema, RegisterSchema, RefreshSchema } from './types.js';

describe('LoginSchema', () => {
  it('accepts valid email + password', () => {
    const result = LoginSchema.safeParse({
      email: 'admin@lysnrai.com',
      password: 'secret123',
      productId: 'lysnrai',
    });
    expect(result.success).toBe(true);
  });

  it('rejects invalid email', () => {
    const result = LoginSchema.safeParse({
      email: 'not-an-email',
      password: 'secret123',
    });
    expect(result.success).toBe(false);
  });

  it('rejects empty password', () => {
    const result = LoginSchema.safeParse({
      email: 'admin@lysnrai.com',
      password: '',
    });
    expect(result.success).toBe(false);
  });
});

describe('RegisterSchema', () => {
  it('accepts valid registration with defaults', () => {
    const result = RegisterSchema.safeParse({
      email: 'new@lysnrai.com',
      password: 'password123',
      displayName: 'New User',
      productId: 'lysnrai',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.role).toBe('user');
    }
  });

  it('accepts admin role', () => {
    const result = RegisterSchema.safeParse({
      email: 'admin@lysnrai.com',
      password: 'password123',
      displayName: 'Admin',
      role: 'admin',
      productId: 'lysnrai',
    });
    expect(result.success).toBe(true);
  });

  it('rejects password shorter than 8 chars', () => {
    const result = RegisterSchema.safeParse({
      email: 'new@lysnrai.com',
      password: 'short',
      displayName: 'User',
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid role', () => {
    const result = RegisterSchema.safeParse({
      email: 'new@lysnrai.com',
      password: 'password123',
      displayName: 'User',
      role: 'super_admin',
    });
    expect(result.success).toBe(false);
  });
});

describe('RefreshSchema', () => {
  it('accepts valid refresh token', () => {
    const result = RefreshSchema.safeParse({ refreshToken: 'some.jwt.token' });
    expect(result.success).toBe(true);
  });

  it('rejects empty token', () => {
    const result = RefreshSchema.safeParse({ refreshToken: '' });
    expect(result.success).toBe(false);
  });
});
