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

import { ApiError, AuthExpiredError } from '@/shared/api/httpClient';
import {
  AUTH_EXPIRED_ERROR_MESSAGE,
  DEFAULT_ERROR_MESSAGE,
  getErrorMessage,
  getOptionalErrorMessage,
} from '@/shared/errors/errorMessages';

describe('errorMessages', () => {
  it('uses backend ApiError message', () => {
    const error = new ApiError(400, { message: 'Backend validation failed', code: 'VALIDATION_ERROR' });

    expect(getErrorMessage(error)).toBe('Backend validation failed');
  });

  it('uses auth expired message for expired sessions', () => {
    expect(getErrorMessage(new AuthExpiredError())).toBe(AUTH_EXPIRED_ERROR_MESSAGE);
  });

  it('uses fallback for unknown errors', () => {
    expect(getErrorMessage('not an error', 'Custom fallback')).toBe('Custom fallback');
  });

  it('returns null for empty optional errors', () => {
    expect(getOptionalErrorMessage(null)).toBeNull();
    expect(getOptionalErrorMessage(undefined)).toBeNull();
  });

  it('uses default fallback when no fallback is provided', () => {
    expect(getErrorMessage({})).toBe(DEFAULT_ERROR_MESSAGE);
  });
});
