import { describe, it, expect, vi } from 'vitest';
import { validatePhone } from '../validatePhone';
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']),
  PleaseEnterACorrectPhone: '请输入正确的手机号码'
};

describe('validatePhone 函数测试', () => {
  it('应该验证有效的中国手机号码', () => {
    const validPhones = [
      '13800138000',
      '15900000000',
      '17612345678',
      '19912345678',
      '+8613800138000',
      '008613800138000'
    ];

    validPhones.forEach((phone) => {
      const result = validatePhone(phone, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('应该拒绝无效的手机号码', () => {
    const invalidPhones = [
      '1380013800', // 少一位
      '138001380000', // 多一位
      '12345678901', // 不是1开头的号码
      '12800138000', // 不是13-19开头
      '2380013800', // 不是1开头
      'abcdefghijk', // 非数字
      '+1234567890', // 非中国号码
      '00112345678901' // 非中国号码
    ];

    invalidPhones.forEach((phone) => {
      const result = validatePhone(phone, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterACorrectPhone);
    });
  });

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

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

    const result = validatePhone('invalid');
    expect(result).toBe('请输入正确的手机号码');
  });
});
