import { describe, expect, it, vi } from 'vitest';
import { retrySettlementBatch } from './BillingSettlementsPanel';

describe('billing settlement batch processing', () => {
  it('continues after individual failures and reports failed settlement IDs', async () => {
    const active = new Set<string>();
    let peakConcurrency = 0;
    const retryOne = vi.fn(async (item: { id: string }) => {
      active.add(item.id);
      peakConcurrency = Math.max(peakConcurrency, active.size);
      await Promise.resolve();
      active.delete(item.id);
      if (item.id === 'failed') throw new Error('retry failed');
    });

    const result = await retrySettlementBatch(
      [{ id: 'first' }, { id: 'failed' }, { id: 'last' }],
      retryOne,
      2,
    );

    expect(result).toEqual({ failedIds: ['failed'], succeeded: 2 });
    expect(retryOne).toHaveBeenCalledTimes(3);
    expect(peakConcurrency).toBeLessThanOrEqual(2);
  });
});
