/**
 * 兼容性测试 - 测试不同环境和数据格式的兼容性
 */
import { describe, it, expect, beforeAll } from 'vitest';
import { VotingEngine, loadVotingScripts } from '../services/voting.engine';
import { adminService } from '../services/admin.service';
import { TICKET_TYPES } from '@gala/shared/constants';
import { PRIZE_CONFIG } from '@gala/shared/types';

describe('兼容性测试', () => {
  let votingEngine: VotingEngine;

  beforeAll(async () => {
    await loadVotingScripts();
    votingEngine = new VotingEngine();
  });

  describe('数据格式兼容', () => {
    it('应该支持所有票种类型', () => {
      expect(TICKET_TYPES).toContain('creative');
      expect(TICKET_TYPES).toContain('visual');
      expect(TICKET_TYPES).toContain('atmosphere');
      expect(TICKET_TYPES.length).toBeGreaterThanOrEqual(3);
    });

    it('奖项配置应该包含必要字段', () => {
      PRIZE_CONFIG.forEach((prize) => {
        expect(prize).toHaveProperty('round');
        expect(prize).toHaveProperty('winnerCount');
        expect(prize).toHaveProperty('name');
        expect(prize).toHaveProperty('level');
      });
    });

    it('投票结果应该返回标准格式', async () => {
      const result = await votingEngine.castVote(
        'compat_test_user',
        'compat_test_program',
        'creative'
      );
      // 标准响应字段
      expect(result).toHaveProperty('success');
      expect(typeof result.success).toBe('boolean');
    });
  });

  describe('状态格式兼容', () => {
    it('系统状态应该包含必要字段', () => {
      const state = adminService.getState();
      expect(state).toHaveProperty('systemPhase');
      expect(state).toHaveProperty('voting');
      expect(state).toHaveProperty('lottery');
    });

    it('投票状态应该包含子阶段', () => {
      const state = adminService.getState();
      expect(state.voting).toHaveProperty('subPhase');
    });

    it('抽奖状态应该包含轮次信息', () => {
      const state = adminService.getState();
      expect(state.lottery).toHaveProperty('round');
      expect(state.lottery).toHaveProperty('subPhase');
    });
  });

  describe('API响应格式兼容', () => {
    it('setPhase应该返回标准响应', async () => {
      const result = await adminService.setPhase('voting');
      expect(result).toHaveProperty('success');
      expect(typeof result.success).toBe('boolean');
    });

    it('controlVoting应该返回标准响应', async () => {
      await adminService.setPhase('voting');
      const result = await adminService.controlVoting('open');
      expect(result).toHaveProperty('success');
    });

    it('controlLottery应该返回标准响应', async () => {
      await adminService.setPhase('lottery');
      await adminService.controlLottery('reset');
      const result = await adminService.controlLottery('start_galaxy');
      expect(result).toHaveProperty('success');
    });
  });

  describe('输入数据兼容', () => {
    it('应该处理字符串类型的用户ID', async () => {
      const result = await votingEngine.castVote(
        'string_user_id',
        'test_program',
        'creative'
      );
      expect(result).toBeDefined();
    });

    it('应该处理数字字符串的用户ID', async () => {
      const result = await votingEngine.castVote(
        '12345',
        'test_program',
        'visual'
      );
      expect(result).toBeDefined();
    });

    it('应该处理UUID格式的用户ID', async () => {
      const result = await votingEngine.castVote(
        'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
        'test_program',
        'atmosphere'
      );
      expect(result).toBeDefined();
    });
  });

  describe('版本兼容', () => {
    it('Node.js版本应该满足要求', () => {
      const nodeVersion = process.version;
      const majorVersion = parseInt(nodeVersion.slice(1).split('.')[0]);
      expect(majorVersion).toBeGreaterThanOrEqual(18);
    });

    it('应该支持ES模块语法', () => {
      // 如果能运行到这里，说明ES模块语法是支持的
      expect(true).toBe(true);
    });

    it('应该支持async/await语法', async () => {
      const asyncFn = async () => 'test';
      const result = await asyncFn();
      expect(result).toBe('test');
    });
  });
});
