import { describe, it, expect, vi } from 'vitest';
import { validateAnswerNum } from '../validateAnswer';
import { getLanguage } from '@/views/Survey/views/Preview/js/language';
import type { ITranslatedText } from '@/views/Survey/views/Preview/components/questions/types/translatedText';
import type { IRateConfig } from '@/types/questions/rateConfig';
import type { IQuestionOption } from '@/types/question';

// 模拟翻译函数
const mockTranslatedText: ITranslatedText = {
  ...getLanguage(['zh']),
  PleaseSelectAValueThatIsNoLessThanOneAndNoMoreThanOne: (min, max) =>
    `请选择不小于${min}且不大于${max}的值`,
  ThisIsARequiredQuestion: '这是必答题'
};

describe('validateAnswerNum 函数测试', () => {
  // 基础测试数据
  const baseConfig: IRateConfig = {
    is_required: 1,
    max: 5,
    min: 1
  };

  const baseOptions: IQuestionOption[] = [
    { option_key: 'option1', option: '选项1' },
    { option_key: 'option2', option: '选项2' },
    { option_key: 'option3', option: '选项3' }
  ];

  it('当不是必答题时，应返回空字符串', () => {
    const config = { ...baseConfig, is_required: 0 };
    // 不完整的答案
    const answer = [2, 3];

    const result = validateAnswerNum(answer, config, baseOptions, mockTranslatedText);
    expect(result).toBe('');
  });

  it('当答案数量小于选项数量时，应返回必答题错误提示', () => {
    // 只有2个答案，但有3个选项
    const answer = [2, 3];

    const result = validateAnswerNum(answer, baseConfig, baseOptions, mockTranslatedText);
    expect(result).toBe(mockTranslatedText.ThisIsARequiredQuestion);
  });

  it('当答案值不在范围内时，应返回范围错误提示', () => {
    const answer = [0, 3, 6]; // 0小于最小值1，6大于最大值5

    const result = validateAnswerNum(answer, baseConfig, baseOptions, mockTranslatedText);
    expect(result).toBe(
      mockTranslatedText.PleaseSelectAValueThatIsNoLessThanOneAndNoMoreThanOne(
        baseConfig.min,
        baseConfig.max
      )
    );
  });

  it('当答案完整且值在范围内时，应返回空字符串', () => {
    const answer = [2, 3, 4]; // 完整且在范围内的答案

    const result = validateAnswerNum(answer, baseConfig, baseOptions, mockTranslatedText);
    expect(result).toBe('');
  });

  it('应该使用默认的翻译文本', () => {
    // 模拟 getLanguage 函数
    vi.mock('@/views/Survey/views/Preview/js/language', () => ({
      getLanguage: vi.fn().mockReturnValue({
        PleaseSelectAValueThatIsNoLessThanOneAndNoMoreThanOne: (min, max) =>
          `请选择不小于${min}且不大于${max}的值`,
        ThisIsARequiredQuestion: '这是必答题'
      })
    }));

    const answer = [0]; // 值不在范围内且不完整
    const result = validateAnswerNum(answer, baseConfig, baseOptions);

    // 由于答案不完整，应该返回必答题错误提示
    expect(result).toBe('这是必答题');
  });
});
