import { describe, it, expect, vi } from 'vitest';
import { validateEmail } from '../validateEmail';
import { getLanguage } from '@/views/Survey/views/Preview/js/language';
import type { ITranslatedText } from '@/views/Survey/views/Preview/components/questions/types/translatedText';

// 模拟翻译函数
const mockTranslatedText: ITranslatedText = {
  ...getLanguage(['zh']),
  PleaseEnterACorrectEmail: '请输入正确的邮箱地址'
};

describe('validateEmail 函数测试', () => {
  it('应该验证有效的邮箱地址', () => {
    const validEmails = [
      'test@example.com',
      'test.name@example.com',
      'test+name@example.com',
      'test@example.co.uk',
      'test@example-domain.com',
      'test123@example.com'
    ];

    validEmails.forEach((email) => {
      const result = validateEmail(email, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('应该拒绝无效的邮箱地址', () => {
    const invalidEmails = [
      'test',
      'test@',
      '@example.com',
      'test@example',
      'test@.com',
      'test@example..com',
      'test@example.com.',
      'test..name@example.com',
      'test@example_domain.com',
      'test@example.com@example.com'
    ];

    invalidEmails.forEach((email) => {
      const result = validateEmail(email, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterACorrectEmail);
    });
  });

  it('应该处理空字符串', () => {
    const result = validateEmail('', mockTranslatedText);
    expect(result).toBe(mockTranslatedText.PleaseEnterACorrectEmail);
  });

  it('应该使用默认的翻译文本', () => {
    // 模拟 getLanguage 函数
    vi.mock('@/views/Survey/views/Preview/js/language', () => ({
      getLanguage: vi.fn().mockReturnValue({
        PleaseEnterACorrectEmail: '请输入正确的邮箱地址'
      })
    }));

    const result = validateEmail('invalid');
    expect(result).toBe('请输入正确的邮箱地址');
  });
});
