import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';

import { RecordCheckpointDto } from './record-checkpoint.dto';

const validateBody = (body: Record<string, unknown>) =>
  validate(plainToInstance(RecordCheckpointDto, body));

describe('RecordCheckpointDto', () => {
  // The final checkpoint arrives the schedule, so a backdated one rewrites the
  // journey after the fact. No UI sends occurredAt; the endpoint still accepts it.
  it('rejects a backdated occurredAt', async () => {
    const errors = await validateBody({
      sequenceNo: 3,
      occurredAt: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(),
    });

    expect(errors).toHaveLength(1);
    expect(errors[0].property).toBe('occurredAt');
    expect(errors[0].constraints).toHaveProperty('IsNotBackdated');
  });

  it('accepts occurredAt of now', async () => {
    const errors = await validateBody({
      sequenceNo: 3,
      occurredAt: new Date().toISOString(),
    });

    expect(errors).toHaveLength(0);
  });

  it('accepts a body that omits occurredAt, leaving the service to stamp it', async () => {
    const errors = await validateBody({ sequenceNo: 0 });

    expect(errors).toHaveLength(0);
  });
});
