/**
 * Case Converter Test
 * 测试 snake_case ↔ camelCase 转换功能
 */

import { snakeToCamel, camelToSnake, keysToCamel, keysToSnake } from '../case-converter';

describe('Case Converter', () => {
  // ==================== 字符串转换测试 ====================
  
  describe('snakeToCamel', () => {
    it('should convert snake_case to camelCase', () => {
      expect(snakeToCamel('user_id')).toBe('userId');
      expect(snakeToCamel('created_at')).toBe('createdAt');
      expect(snakeToCamel('has_ca_data')).toBe('hasCaData');
    });

    it('should handle single word', () => {
      expect(snakeToCamel('name')).toBe('name');
    });
  });

  describe('camelToSnake', () => {
    it('should convert camelCase to snake_case', () => {
      expect(camelToSnake('userId')).toBe('user_id');
      expect(camelToSnake('createdAt')).toBe('created_at');
      expect(camelToSnake('hasCaData')).toBe('has_ca_data');
    });

    it('should handle single word', () => {
      expect(camelToSnake('name')).toBe('name');
    });
  });

  // ==================== 对象转换测试 ====================
  
  describe('keysToCamel', () => {
    it('should convert object keys from snake_case to camelCase', () => {
      const input = {
        user_id: '123',
        created_at: '2025-01-01',
        has_ca_data: true,
      };

      const expected = {
        userId: '123',
        createdAt: '2025-01-01',
        hasCaData: true,
      };

      expect(keysToCamel(input)).toEqual(expected);
    });

    it('should handle nested objects', () => {
      const input = {
        user_id: '123',
        user_profile: {
          first_name: 'John',
          last_name: 'Doe',
        },
      };

      const expected = {
        userId: '123',
        userProfile: {
          firstName: 'John',
          lastName: 'Doe',
        },
      };

      expect(keysToCamel(input)).toEqual(expected);
    });

    it('should handle arrays', () => {
      const input = [
        { user_id: '1', created_at: '2025-01-01' },
        { user_id: '2', created_at: '2025-01-02' },
      ];

      const expected = [
        { userId: '1', createdAt: '2025-01-01' },
        { userId: '2', createdAt: '2025-01-02' },
      ];

      expect(keysToCamel(input)).toEqual(expected);
    });

    it('should handle arrays of nested objects', () => {
      const input = {
        items: [
          { item_id: '1', item_name: 'Item 1' },
          { item_id: '2', item_name: 'Item 2' },
        ],
      };

      const expected = {
        items: [
          { itemId: '1', itemName: 'Item 1' },
          { itemId: '2', itemName: 'Item 2' },
        ],
      };

      expect(keysToCamel(input)).toEqual(expected);
    });

    it('should handle null and undefined', () => {
      expect(keysToCamel(null)).toBeNull();
      expect(keysToCamel(undefined)).toBeUndefined();
    });

    it('should handle primitive values', () => {
      expect(keysToCamel('string')).toBe('string');
      expect(keysToCamel(123)).toBe(123);
      expect(keysToCamel(true)).toBe(true);
    });
  });

  describe('keysToSnake', () => {
    it('should convert object keys from camelCase to snake_case', () => {
      const input = {
        userId: '123',
        createdAt: '2025-01-01',
        hasCaData: true,
      };

      const expected = {
        user_id: '123',
        created_at: '2025-01-01',
        has_ca_data: true,
      };

      expect(keysToSnake(input)).toEqual(expected);
    });

    it('should handle nested objects', () => {
      const input = {
        userId: '123',
        userProfile: {
          firstName: 'John',
          lastName: 'Doe',
        },
      };

      const expected = {
        user_id: '123',
        user_profile: {
          first_name: 'John',
          last_name: 'Doe',
        },
      };

      expect(keysToSnake(input)).toEqual(expected);
    });

    it('should handle arrays', () => {
      const input = [
        { userId: '1', createdAt: '2025-01-01' },
        { userId: '2', createdAt: '2025-01-02' },
      ];

      const expected = [
        { user_id: '1', created_at: '2025-01-01' },
        { user_id: '2', created_at: '2025-01-02' },
      ];

      expect(keysToSnake(input)).toEqual(expected);
    });
  });

  // ==================== 实际 API 场景测试 ====================
  
  describe('API Response Conversion (snake_case → camelCase)', () => {
    it('should convert Cluster API response', () => {
      const apiResponse = {
        id: 'cluster-123',
        name: 'Production',
        host: 'https://k8s.example.com',
        has_ca_data: true,
        has_cert_data: true,
        has_key_data: false,
        has_token: true,
        ca_data: '••••••••',
        cert_data: '••••••••',
        created_at: '2025-11-09T10:00:00Z',
        updated_at: '2025-11-09T10:00:00Z',
      };

      const frontendData = keysToCamel(apiResponse);

      expect(frontendData.hasCAData).toBe(true);
      expect(frontendData.hasCertData).toBe(true);
      expect(frontendData.hasKeyData).toBe(false);
      expect(frontendData.hasToken).toBe(true);
      expect(frontendData.caData).toBe('••••••••');
      expect(frontendData.certData).toBe('••••••••');
      expect(frontendData.createdAt).toBe('2025-11-09T10:00:00Z');
      expect(frontendData.updatedAt).toBe('2025-11-09T10:00:00Z');
    });

    it('should convert Instance API response', () => {
      const apiResponse = {
        id: 'instance-123',
        name: 'my-app',
        namespace: 'default',
        cluster_id: 'cluster-abc',
        registry_id: 'registry-xyz',
        created_at: '2025-11-09T10:00:00Z',
        updated_at: '2025-11-09T10:00:00Z',
      };

      const frontendData = keysToCamel(apiResponse);

      expect(frontendData.clusterId).toBe('cluster-abc');
      expect(frontendData.registryId).toBe('registry-xyz');
      expect(frontendData.createdAt).toBe('2025-11-09T10:00:00Z');
      expect(frontendData.updatedAt).toBe('2025-11-09T10:00:00Z');
    });
  });

  describe('API Request Conversion (camelCase → snake_case)', () => {
    it('should convert CreateClusterRequest', () => {
      const requestData = {
        name: 'Production',
        host: 'https://k8s.example.com',
        caData: 'base64-encoded-ca',
        certData: 'base64-encoded-cert',
        keyData: 'base64-encoded-key',
      };

      const apiRequest = keysToSnake(requestData);

      expect(apiRequest.ca_data).toBe('base64-encoded-ca');
      expect(apiRequest.cert_data).toBe('base64-encoded-cert');
      expect(apiRequest.key_data).toBe('base64-encoded-key');
    });

    it('should convert CreateInstanceRequest', () => {
      const requestData = {
        name: 'my-app',
        namespace: 'default',
        registryId: 'registry-123',
        repository: 'charts/nginx',
        tag: '1.0.0',
      };

      const apiRequest = keysToSnake(requestData);

      expect(apiRequest.registry_id).toBe('registry-123');
    });
  });
});


