import { describe, it, expect, vi } from 'vitest';
import { validateChineseLetter } from '../validateChineseLetter';
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']),
  PleaseEnterChineseWords: '请输入中文字符'
};

// 模拟配置
interface ICompletionConfig {
  include_mark: number;
}

describe('validateChineseLetter 函数测试', () => {
  it('当包含标点且输入有效中文和标点时，应返回空字符串', () => {
    const config: ICompletionConfig = { include_mark: 1 };
    const validInputs = ['你好', '中文字符', '中文，标点！', '中文。标点？', '中文：标点；'];

    validInputs.forEach((input) => {
      const result = validateChineseLetter(config, input, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('当包含标点但输入无效字符时，应返回错误信息', () => {
    const config: ICompletionConfig = { include_mark: 1 };
    // 根据validateChineseLetter.ts的实现，当include_mark为1时，允许中文、英文字母和标点符号
    // 所以只有纯数字或其他非中英文字符且不在正则表达式中的字符才是无效的
    const invalidInputs = ['123456'];

    invalidInputs.forEach((input) => {
      const result = validateChineseLetter(config, input, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterChineseWords);
    });
  });

  it('当不包含标点且输入纯中文时，应返回空字符串', () => {
    const config: ICompletionConfig = { include_mark: 2 };
    const validInputs = ['你好', '中文字符', '纯中文无标点'];

    validInputs.forEach((input) => {
      const result = validateChineseLetter(config, input, mockTranslatedText);
      expect(result).toBe('');
    });
  });

  it('当不包含标点但输入包含标点或其他字符时，应返回错误信息', () => {
    const config: ICompletionConfig = { include_mark: 2 };
    const invalidInputs = [
      '中文，标点',
      '中文。标点',
      '中文：标点',
      '中文123',
      '中文abc',
      'English',
      '123456',
      '，。！？'
    ];

    invalidInputs.forEach((input) => {
      const result = validateChineseLetter(config, input, mockTranslatedText);
      expect(result).toBe(mockTranslatedText.PleaseEnterChineseWords);
    });
  });

  it('应该处理空字符串', () => {
    const config1: ICompletionConfig = { include_mark: 1 };
    const config2: ICompletionConfig = { include_mark: 2 };

    const result1 = validateChineseLetter(config1, '', mockTranslatedText);
    const result2 = validateChineseLetter(config2, '', mockTranslatedText);

    expect(result1).toBe(mockTranslatedText.PleaseEnterChineseWords);
    expect(result2).toBe(mockTranslatedText.PleaseEnterChineseWords);
  });

  it('应该使用默认的翻译文本', () => {
    // 模拟 getLanguage 函数
    vi.mock('@/views/Survey/views/Preview/js/language', () => ({
      getLanguage: vi.fn().mockReturnValue({
        PleaseEnterChineseWords: '请输入中文字符'
      })
    }));

    const config: ICompletionConfig = { include_mark: 1 };
    // 使用纯数字作为输入，这在include_mark为1时是无效的
    const result = validateChineseLetter(config, '123456');
    expect(result).toBe('请输入中文字符');
  });
});
