import { buildSortOrder } from './build-sort-order';

describe('buildSortOrder', () => {
  it('returns the fallback when sorting is empty', () => {
    expect(buildSortOrder([])).toEqual([['rowId', 'DESC']]);
  });

  it('honors the fallback column and direction overrides', () => {
    expect(buildSortOrder([], 'createdAt', 'ASC')).toEqual([['createdAt', 'ASC']]);
  });

  it('maps a single sort entry and appends the fallback', () => {
    expect(buildSortOrder([{ id: 'name', desc: false }])).toEqual([
      ['name', 'ASC'],
      ['rowId', 'ASC'],
    ]);
  });

  it('does not duplicate the fallback when it is already in the sort', () => {
    expect(buildSortOrder([{ id: 'rowId', desc: true }])).toEqual([['rowId', 'DESC']]);
  });

  it('splits dotted ids into the array form for relation columns', () => {
    expect(buildSortOrder([{ id: 'boundedContext.name', desc: false }])).toEqual([
      ['boundedContext', 'name', 'ASC'],
      ['rowId', 'ASC'],
    ]);
  });

  it('mixes flat and relation entries while keeping the primary direction for the fallback', () => {
    expect(
      buildSortOrder([
        { id: 'boundedContext.name', desc: true },
        { id: 'name', desc: false },
      ]),
    ).toEqual([
      ['boundedContext', 'name', 'DESC'],
      ['name', 'ASC'],
      ['rowId', 'DESC'],
    ]);
  });
});
