/**
 * Namespace Utility Tests
 *
 * Tests the namespace sanitization utility including:
 * - Basic transformation
 * - Special character handling
 * - Edge cases
 */
import { describe, it, expect } from 'vitest';
import { sanitizeNamespace } from './namespace';

describe('sanitizeNamespace', () => {
  // ==========================================================================
  // Basic Transformation Tests
  // ==========================================================================

  describe('basic transformation', () => {
    it('converts spaces to underscores', () => {
      expect(sanitizeNamespace('Create Post')).toBe('create_post');
    });

    it('converts to lowercase', () => {
      expect(sanitizeNamespace('CreatePost')).toBe('createpost');
    });

    it('handles mixed case with spaces', () => {
      expect(sanitizeNamespace('Send Email')).toBe('send_email');
    });

    it('handles already lowercase with underscores', () => {
      expect(sanitizeNamespace('create_post')).toBe('create_post');
    });
  });

  // ==========================================================================
  // Special Character Handling Tests
  // ==========================================================================

  describe('special character handling', () => {
    it('removes special characters', () => {
      expect(sanitizeNamespace('My Action #1')).toBe('my_action_1');
    });

    it('replaces multiple special chars with single underscore', () => {
      expect(sanitizeNamespace('Test!@#Action')).toBe('test_action');
    });

    it('handles parentheses', () => {
      expect(sanitizeNamespace('Action (New)')).toBe('action_new');
    });

    it('handles dashes', () => {
      expect(sanitizeNamespace('send-email')).toBe('send_email');
    });

    it('handles dots', () => {
      expect(sanitizeNamespace('action.name')).toBe('action_name');
    });

    it('handles ampersand', () => {
      expect(sanitizeNamespace('Copy & Paste')).toBe('copy_paste');
    });

    it('preserves numbers', () => {
      expect(sanitizeNamespace('Step 1 of 5')).toBe('step_1_of_5');
    });

    it('handles numbers at start', () => {
      expect(sanitizeNamespace('2nd Action')).toBe('2nd_action');
    });
  });

  // ==========================================================================
  // Edge Cases Tests
  // ==========================================================================

  describe('edge cases', () => {
    it('handles empty string', () => {
      expect(sanitizeNamespace('')).toBe('');
    });

    it('removes leading underscores', () => {
      expect(sanitizeNamespace('_private_action')).toBe('private_action');
    });

    it('removes trailing underscores', () => {
      expect(sanitizeNamespace('action_')).toBe('action');
    });

    it('removes leading and trailing underscores', () => {
      expect(sanitizeNamespace('__action__')).toBe('action');
    });

    it('handles only special characters', () => {
      expect(sanitizeNamespace('!@#$%')).toBe('');
    });

    it('handles only spaces', () => {
      expect(sanitizeNamespace('   ')).toBe('');
    });

    it('collapses multiple underscores', () => {
      expect(sanitizeNamespace('test___action')).toBe('test_action');
    });

    it('handles unicode characters', () => {
      expect(sanitizeNamespace('Café Action')).toBe('caf_action');
    });

    it('handles single character', () => {
      expect(sanitizeNamespace('A')).toBe('a');
    });

    it('handles single word', () => {
      expect(sanitizeNamespace('Action')).toBe('action');
    });
  });

  // ==========================================================================
  // Real-world Examples Tests
  // ==========================================================================

  describe('real-world examples', () => {
    it('handles typical action names', () => {
      expect(sanitizeNamespace('User Registration')).toBe('user_registration');
      expect(sanitizeNamespace('Send Email Notification')).toBe('send_email_notification');
      expect(sanitizeNamespace('Add to Mailchimp List')).toBe('add_to_mailchimp_list');
    });

    it('handles integration names', () => {
      expect(sanitizeNamespace('Kit (ConvertKit)')).toBe('kit_convertkit');
      expect(sanitizeNamespace('Campaign Monitor')).toBe('campaign_monitor');
    });

    it('handles versioned names', () => {
      expect(sanitizeNamespace('Action v2')).toBe('action_v2');
      expect(sanitizeNamespace('API 3.0 Call')).toBe('api_3_0_call');
    });
  });
});
