import { IssuedAt } from './IssuedAt';
import { RacingDomainValidationError } from '../errors/RacingDomainError';

describe('IssuedAt', () => {
  describe('create', () => {
    it('should create an IssuedAt with valid date', () => {
      const date = new Date('2023-01-01T00:00:00Z');
      const issuedAt = IssuedAt.create(date);
      expect(issuedAt.toDate().getTime()).toBe(date.getTime());
    });

    it('should create a copy of the date', () => {
      const date = new Date();
      const issuedAt = IssuedAt.create(date);
      date.setFullYear(2000); // modify original
      expect(issuedAt.toDate().getFullYear()).not.toBe(2000);
    });

    it('should throw error for invalid date', () => {
      expect(() => IssuedAt.create(new Date('invalid'))).toThrow(RacingDomainValidationError);
    });

    it('should throw error for non-Date', () => {
      expect(() => IssuedAt.create('2023-01-01' as unknown as Date)).toThrow(RacingDomainValidationError);
    });
  });

  describe('equals', () => {
    it('should return true for equal dates', () => {
      const date1 = new Date('2023-01-01T00:00:00Z');
      const date2 = new Date('2023-01-01T00:00:00Z');
      const issuedAt1 = IssuedAt.create(date1);
      const issuedAt2 = IssuedAt.create(date2);
      expect(issuedAt1.equals(issuedAt2)).toBe(true);
    });

    it('should return false for different dates', () => {
      const issuedAt1 = IssuedAt.create(new Date('2023-01-01'));
      const issuedAt2 = IssuedAt.create(new Date('2023-01-02'));
      expect(issuedAt1.equals(issuedAt2)).toBe(false);
    });
  });
});