import { AxiosError } from 'axios';

import { getRetryTimeout, isNetworkError } from './retry';

describe('getRetryTimeout', () => {
  it('returns 0ms on attempt 0', () => {
    expect(getRetryTimeout(0)).toBe(0);
  });

  it('returns 5000ms on attempt 1', () => {
    expect(getRetryTimeout(1)).toBe(5_000);
  });

  it('returns 15000ms on attempt 2', () => {
    expect(getRetryTimeout(2)).toBe(15_000);
  });

  it('returns 30000ms on attempt 3', () => {
    expect(getRetryTimeout(3)).toBe(30_000);
  });

  it('clamps to 30000ms for attempts beyond the table', () => {
    expect(getRetryTimeout(4)).toBe(30_000);
    expect(getRetryTimeout(100)).toBe(30_000);
  });
});

describe('isNetworkError', () => {
  it('returns true for AxiosError with no response', () => {
    const error = new AxiosError('Network Error');
    error.response = undefined;
    expect(isNetworkError(error)).toBe(true);
  });

  it('returns false for AxiosError with a response (e.g. 401)', () => {
    const error = new AxiosError('Unauthorized');
    (error as unknown as { response: unknown }).response = {
      status: 401,
      data: {},
    };
    expect(isNetworkError(error)).toBe(false);
  });

  it('returns false for a plain Error', () => {
    expect(isNetworkError(new Error('something'))).toBe(false);
  });

  it('returns false for non-error values', () => {
    expect(isNetworkError(null)).toBe(false);
    expect(isNetworkError('string error')).toBe(false);
    expect(isNetworkError(undefined)).toBe(false);
  });
});
