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

import { getFirstQueryError, isAnyLoading } from '@/shared/business/queryState';

describe('queryState', () => {
  it('detects loading state across multiple sources', () => {
    expect(isAnyLoading(
      { isLoading: false, error: null },
      { isLoading: true, error: null },
    )).toBe(true);
  });

  it('returns false when no sources are loading', () => {
    expect(isAnyLoading(
      { isLoading: false, error: null },
      { isLoading: false, error: null },
    )).toBe(false);
  });

  it('returns the first query error', () => {
    const firstError = new Error('First failed');
    const secondError = new Error('Second failed');

    expect(getFirstQueryError(
      { isLoading: false, error: null },
      { isLoading: false, error: firstError },
      { isLoading: false, error: secondError },
    )).toBe(firstError);
  });

  it('returns null when no query has errors', () => {
    expect(getFirstQueryError(
      { isLoading: false, error: null },
      { isLoading: false, error: null },
    )).toBeNull();
  });
});
