/**
 * formatAuthError tests (auth/utils/errors.ts).
 *
 * Turns whatever the auth layer throws (string, Error, DRF `{detail}`, axios-ish
 * nested `response.data.detail`) into a user-facing string, with a stable
 * fallback. The precedence order matters: message > detail > nested detail, and
 * it must never throw on null/odd shapes.
 */

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

import { formatAuthError } from '../utils/errors';

describe('formatAuthError', () => {
  it('returns a plain string as-is', () => {
    expect(formatAuthError('Something broke')).toBe('Something broke');
  });

  it('prefers error.message (Error instances)', () => {
    expect(formatAuthError(new Error('boom'))).toBe('boom');
  });

  it('reads DRF-style { detail }', () => {
    expect(formatAuthError({ detail: 'Token is blacklisted' })).toBe('Token is blacklisted');
  });

  it('reads nested response.data.detail', () => {
    expect(formatAuthError({ response: { data: { detail: 'nested detail' } } })).toBe('nested detail');
  });

  it('message wins over detail when both present', () => {
    expect(formatAuthError({ message: 'msg', detail: 'det' })).toBe('msg');
  });

  it('detail wins over nested detail', () => {
    expect(
      formatAuthError({ detail: 'top', response: { data: { detail: 'deep' } } }),
    ).toBe('top');
  });

  it('falls back for null / undefined / empty object', () => {
    const FALLBACK = 'An unexpected error occurred';
    expect(formatAuthError(null)).toBe(FALLBACK);
    expect(formatAuthError(undefined)).toBe(FALLBACK);
    expect(formatAuthError({})).toBe(FALLBACK);
    expect(formatAuthError(42)).toBe(FALLBACK);
  });
});
