import { BadRequestException } from '@nestjs/common';

import { assertDoCollectionDates } from './contract-clearance.util';

describe('assertDoCollectionDates', () => {
  it('requires both dates', () => {
    expect(() => assertDoCollectionDates(undefined)).toThrow(BadRequestException);
    expect(() =>
      assertDoCollectionDates({ vesselArrivalDate: '2026-07-01' }),
    ).toThrow(/DO collected date is required/);
    expect(() =>
      assertDoCollectionDates({ doCollectedDate: '2026-07-01' }),
    ).toThrow(/Vessel arrival date is required/);
    // Whitespace is not a date.
    expect(() =>
      assertDoCollectionDates({ vesselArrivalDate: '  ', doCollectedDate: '  ' }),
    ).toThrow(BadRequestException);
  });

  it('rejects a DO collected before the vessel arrived', () => {
    expect(() =>
      assertDoCollectionDates({
        vesselArrivalDate: '2026-07-10',
        doCollectedDate: '2026-07-09',
      }),
    ).toThrow(/cannot be earlier than the vessel arrival date/);
  });

  it('normalizes an ISO datetime down to its date part', () => {
    expect(
      assertDoCollectionDates({
        vesselArrivalDate: '2026-07-10T21:00:00.000Z',
        doCollectedDate: '2026-07-10T05:00:00.000Z',
      }),
    ).toEqual({ vesselArrivalDate: '2026-07-10', doCollectedDate: '2026-07-10' });
  });

  it('rejects a malformed date', () => {
    expect(() =>
      assertDoCollectionDates({
        vesselArrivalDate: '10/07/2026',
        doCollectedDate: '2026-07-10',
      }),
    ).toThrow(/not a valid date/);
  });
});
