import { ProvinceCitySelection } from './province-city-selection.vo';

describe('ProvinceCitySelection', () => {
  describe('create', () => {
    it('应该成功创建省市选择', () => {
      const selection = ProvinceCitySelection.create(
        '440000',
        '广东省',
        '440100',
        '广州市',
      );

      expect(selection.provinceCode).toBe('440000');
      expect(selection.provinceName).toBe('广东省');
      expect(selection.cityCode).toBe('440100');
      expect(selection.cityName).toBe('广州市');
      expect(selection.isConfirmed).toBe(false);
    });

    it('应该拒绝空的省份代码', () => {
      expect(() =>
        ProvinceCitySelection.create('', '广东省', '440100', '广州市'),
      ).toThrow('省市代码不能为空');
    });

    it('应该拒绝空的城市代码', () => {
      expect(() =>
        ProvinceCitySelection.create('440000', '广东省', '', '广州市'),
      ).toThrow('省市代码不能为空');
    });
  });

  describe('confirm', () => {
    it('应该成功确认省市选择', () => {
      const selection = ProvinceCitySelection.create(
        '440000',
        '广东省',
        '440100',
        '广州市',
      );

      // 模拟等待5秒
      jest.useFakeTimers();
      jest.advanceTimersByTime(5000);

      const confirmed = selection.confirm();
      expect(confirmed.isConfirmed).toBe(true);

      jest.useRealTimers();
    });

    it('应该拒绝重复确认', () => {
      const selection = ProvinceCitySelection.create(
        '440000',
        '广东省',
        '440100',
        '广州市',
      );

      jest.useFakeTimers();
      jest.advanceTimersByTime(5000);

      const confirmed = selection.confirm();

      expect(() => confirmed.confirm()).toThrow('省市已确认，不可重复确认');

      jest.useRealTimers();
    });
  });

  describe('canConfirm', () => {
    it('应该在5秒前返回false', () => {
      const selection = ProvinceCitySelection.create(
        '440000',
        '广东省',
        '440100',
        '广州市',
      );

      expect(selection.canConfirm()).toBe(false);
    });

    it('应该在5秒后返回true', () => {
      jest.useFakeTimers();

      const selection = ProvinceCitySelection.create(
        '440000',
        '广东省',
        '440100',
        '广州市',
      );

      jest.advanceTimersByTime(5000);

      expect(selection.canConfirm()).toBe(true);

      jest.useRealTimers();
    });
  });
});
