import { normalizeCollaboratorPaymentsResponse } from '../utils/normalizeCollaboratorPayments';

describe('normalizeCollaboratorPaymentsResponse', () => {
  it('maps a bare array', () => {
    const out = normalizeCollaboratorPaymentsResponse([
      {
        installmentNumber: 1,
        dueDate: '2026-01-01',
        amount: 100,
        status: 'PENDING',
      },
    ]);
    expect(out).toHaveLength(1);
    expect(out[0]).toMatchObject({
      installmentNumber: 1,
      dueDate: '2026-01-01',
      amount: 100,
      status: 'PENDING',
    });
  });

  it('unwraps { data: [...] }', () => {
    const out = normalizeCollaboratorPaymentsResponse({
      data: [{ installment_number: 2, due_date: '2026-02-01', status: 'PAID' }],
    });
    expect(out).toHaveLength(1);
    expect(out[0]?.installmentNumber).toBe(2);
    expect(out[0]?.dueDate).toBe('2026-02-01');
  });

  it('unwraps { payments: [...] }', () => {
    const out = normalizeCollaboratorPaymentsResponse({
      payments: [{ paymentNumber: 3 }],
    });
    expect(out).toHaveLength(1);
    expect(out[0]?.installmentNumber).toBe(3);
  });

  it('returns empty for invalid input', () => {
    expect(normalizeCollaboratorPaymentsResponse(null)).toEqual([]);
    expect(normalizeCollaboratorPaymentsResponse({})).toEqual([]);
  });
});
