import { describe, expect, it } from 'vitest';

import { getApiListRows, mapApiListRows, selectApiListRows } from '@/shared/business/apiListRows';

describe('apiListRows', () => {
  it('returns rows from an API list response', async () => {
    await expect(getApiListRows(Promise.resolve({
      rows: ['alpha', 'beta'],
      count: 2,
    }))).resolves.toEqual(['alpha', 'beta']);
  });

  it('maps rows from an API list response', async () => {
    await expect(mapApiListRows(Promise.resolve({
      rows: [{ value: 1 }, { value: 2 }],
      count: 2,
    }), (item) => item.value * 10)).resolves.toEqual([10, 20]);
  });

  it('selects a value from full API list rows', async () => {
    await expect(selectApiListRows(Promise.resolve({
      rows: [{ value: 1 }, { value: 2 }],
      count: 2,
    }), (rows) => rows.reduce((sum, item) => sum + item.value, 0))).resolves.toBe(3);
  });
});
