import { describe, it, expect } from 'vitest';
import { validateMinLength, validateMaxLength } from '../validateStringLength';
import type { ITranslatedText } from '@/views/Survey/views/Preview/components/questions/types/translatedText';

// 模拟翻译函数
const mockTranslatedText: ITranslatedText = {
  PleaseEnterMoreCharacters: (min) => `请输入至少${min}个字符`,
  PleaseEnterLessCharacters: (max) => `请输入不超过${max}个字符`
} as ITranslatedText;

// 模拟配置
interface ICompletionConfig {
  min: number | string;
  max: number | string;
  text_type: number;
}

describe('validateStringLength 函数测试', () => {
  describe('validateMinLength 函数', () => {
    it('当字符串长度大于最小值时，应返回 undefined', () => {
      const answer = '测试字符串';
      const config: ICompletionConfig = { min: 3, max: 20, text_type: 0 };

      const result = validateMinLength(answer, false, config, mockTranslatedText);
      expect(result).toBeUndefined();
    });

    it('当字符串长度小于最小值时，应返回错误信息', () => {
      const answer = '测试';
      const config: ICompletionConfig = { min: 5, max: 20, text_type: 0 };

      const result = validateMinLength(answer, false, config, mockTranslatedText);
      expect(result).toEqual({
        isError: true,
        errorMessage: mockTranslatedText.PleaseEnterMoreCharacters(config.min)
      });
    });

    it('当 min 为空字符串时，应返回 undefined', () => {
      const answer = '';
      const config: ICompletionConfig = { min: '', max: 20, text_type: 0 };

      const result = validateMinLength(answer, false, config, mockTranslatedText);
      expect(result).toBeUndefined();
    });

    it('当已有错误时，应返回 undefined', () => {
      const answer = '测试';
      const config: ICompletionConfig = { min: 5, max: 20, text_type: 0 };

      const result = validateMinLength(answer, true, config, mockTranslatedText);
      expect(result).toBeUndefined();
    });

    it('当 text_type 为 1 或 2 时，应返回 undefined', () => {
      const answer = '123';
      const config1: ICompletionConfig = { min: 5, max: 20, text_type: 1 };
      const config2: ICompletionConfig = { min: 5, max: 20, text_type: 2 };

      const result1 = validateMinLength(answer, false, config1, mockTranslatedText);
      const result2 = validateMinLength(answer, false, config2, mockTranslatedText);

      expect(result1).toBeUndefined();
      expect(result2).toBeUndefined();
    });
  });

  describe('validateMaxLength 函数', () => {
    it('当字符串长度小于最大值时，应返回 undefined', () => {
      const answer = '测试字符串';
      const config: ICompletionConfig = { min: 3, max: 20, text_type: 0 };

      const result = validateMaxLength(answer, false, config, mockTranslatedText);
      expect(result).toBeUndefined();
    });

    it('当字符串长度大于等于最大值时，应返回错误信息', () => {
      const answer = '这是一个非常长的测试字符串，超过了最大长度限制';
      const config: ICompletionConfig = { min: 5, max: 20, text_type: 0 };

      const result = validateMaxLength(answer, false, config, mockTranslatedText);
      expect(result).toEqual({
        isError: true,
        errorMessage: mockTranslatedText.PleaseEnterLessCharacters(config.max)
      });
    });

    it('当 max 为空字符串时，应返回 undefined', () => {
      const answer = '测试字符串';
      const config: ICompletionConfig = { min: 3, max: '', text_type: 0 };

      const result = validateMaxLength(answer, false, config, mockTranslatedText);
      expect(result).toBeUndefined();
    });

    it('当已有错误时，应返回 undefined', () => {
      const answer = '这是一个非常长的测试字符串，超过了最大长度限制';
      const config: ICompletionConfig = { min: 5, max: 20, text_type: 0 };

      const result = validateMaxLength(answer, true, config, mockTranslatedText);
      expect(result).toBeUndefined();
    });

    it('当 text_type 为 1 或 2 时，应返回 undefined', () => {
      const answer = '123456789012345678901234567890';
      const config1: ICompletionConfig = { min: 5, max: 20, text_type: 1 };
      const config2: ICompletionConfig = { min: 5, max: 20, text_type: 2 };

      const result1 = validateMaxLength(answer, false, config1, mockTranslatedText);
      const result2 = validateMaxLength(answer, false, config2, mockTranslatedText);

      expect(result1).toBeUndefined();
      expect(result2).toBeUndefined();
    });
  });
});
