import { PhoneNumber } from './index';
import { DomainError } from '@/shared/exceptions/domain.exception';

describe('PhoneNumber ValueObject', () => {
  describe('create', () => {
    it('应该接受有效的中国手机号', () => {
      const validPhones = [
        '13800138000',
        '13912345678',
        '15800001111',
        '18600002222',
        '19900003333',
      ];

      validPhones.forEach((phone) => {
        const phoneNumber = PhoneNumber.create(phone);
        expect(phoneNumber.value).toBe(phone);
      });
    });

    it('应该拒绝无效的手机号格式', () => {
      const invalidPhones = [
        '12800138000', // 不是1开头
        '1380013800', // 少于11位
        '138001380000', // 多于11位
        '10800138000', // 第二位不是3-9
        'abcdefghijk', // 非数字
        '', // 空字符串
      ];

      invalidPhones.forEach((phone) => {
        expect(() => {
          PhoneNumber.create(phone);
        }).toThrow(DomainError);
      });
    });

    it('应该拒绝包含特殊字符的手机号', () => {
      const invalidPhones = [
        '138-0013-8000',
        '138 0013 8000',
        '+8613800138000',
      ];

      invalidPhones.forEach((phone) => {
        expect(() => {
          PhoneNumber.create(phone);
        }).toThrow(DomainError);
      });
    });
  });

  describe('masked', () => {
    it('应该正确掩码手机号', () => {
      const phoneNumber = PhoneNumber.create('13800138000');
      const masked = phoneNumber.masked();

      expect(masked).toBe('138****8000');
    });

    it('掩码后应该隐藏中间4位', () => {
      const testCases = [
        { input: '13912345678', expected: '139****5678' },
        { input: '15800001111', expected: '158****1111' },
        { input: '18600002222', expected: '186****2222' },
      ];

      testCases.forEach(({ input, expected }) => {
        const phoneNumber = PhoneNumber.create(input);
        expect(phoneNumber.masked()).toBe(expected);
      });
    });
  });

  describe('equals', () => {
    it('相同的手机号应该相等', () => {
      const phone1 = PhoneNumber.create('13800138000');
      const phone2 = PhoneNumber.create('13800138000');

      expect(phone1.equals(phone2)).toBe(true);
    });

    it('不同的手机号应该不相等', () => {
      const phone1 = PhoneNumber.create('13800138000');
      const phone2 = PhoneNumber.create('13912345678');

      expect(phone1.equals(phone2)).toBe(false);
    });
  });
});
