import { describe, it, expect, vi } from 'vitest';
import { validateIDCard } from '../validateIDCard';
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']),
  PleaseEnterACorrectID: '请输入正确的身份证号码'
};

describe('validateIDCard 函数测试', () => {
  it('应该验证有效的身份证号码', () => {
    const validIDs = [
      '110101199003070953',
      '310101199001010010',
      '440101199001011234',
      '11010119900307095X',
      '11010119900307095x'
    ];

    validIDs.forEach((id) => {
      const result = validateIDCard(id, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('应该拒绝无效的身份证号码', () => {
    const invalidIDs = [
      '1101011990030709', // 少一位
      '11010119900307095A', // 非法字符
      '000101199001010010', // 非法地区码
      '110101299001010010', // 非法年份
      '110101199013010010', // 非法月份
      '110101199001320010', // 非法日期
      '110101199001010010A', // 非法校验码
      'abcdefghijklmnopqr', // 非数字
      '12345678901234567' // 格式错误
    ];

    invalidIDs.forEach((id) => {
      const result = validateIDCard(id, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterACorrectID);
    });
  });

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

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

    const result = validateIDCard('invalid');
    expect(result).toBe('请输入正确的身份证号码');
  });
});
