import { describe, it, expect, vi, beforeEach } from 'vitest'

const mockGet = vi.fn()
vi.mock('axios', () => ({
  default: {
    create: () => ({
      get: mockGet,
      interceptors: {
        request: { use: vi.fn(), eject: vi.fn() },
        response: { use: vi.fn(), eject: vi.fn() },
      },
    }),
  },
}))

describe('createApiClient getSafe', () => {
  beforeEach(() => {
    mockGet.mockReset()
  })

  it('returns { ok: false, data: null } when response status is 404', async () => {
    const { createApiClient } = await import('./client')
    mockGet.mockResolvedValue({ status: 404, data: { error: 'Not found' } })

    const client = createApiClient('http://test')
    const result = await client.getSafe<unknown>('/api/v1/transactions/138/0xabc')

    expect(result).toEqual({ ok: false, data: null })
  })

  it('returns { ok: true, data } when response status is 200 and body has data', async () => {
    const { createApiClient } = await import('./client')
    mockGet.mockResolvedValue({ status: 200, data: { data: { hash: '0x123' } } })

    const client = createApiClient('http://test')
    const result = await client.getSafe<{ hash: string }>('/api/v1/transactions/138/0x123')

    expect(result.ok).toBe(true)
    expect(result.data).toEqual({ hash: '0x123' })
  })
})
