/**
 * ENS utilities — forward resolve for impersonation / admin flows.
 */

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

const DEPLOYER = '0x4A666F96fC8764181194447A7dFdb7d471b301C8'

vi.mock('@wagmi/core', () => ({
  getPublicClient: vi.fn(() => ({
    getEnsAddress: vi.fn(async ({ name }: { name: string }) =>
      name.toLowerCase() === 'defi-oracle.eth' ? DEPLOYER : null,
    ),
    getEnsName: vi.fn(async () => null),
  })),
}))

vi.mock('../../config/wagmi', () => ({ config: {} }))

describe('ENS utilities', () => {
  beforeEach(async () => {
    const { clearENSCache } = await import('../../utils/ens')
    clearENSCache()
  })

  it('resolveAddressOrHex resolves defi-oracle.eth to deployer', async () => {
    const { resolveAddressOrHex } = await import('../../utils/ens')
    const result = await resolveAddressOrHex('defi-oracle.eth')
    expect(result.valid).toBe(true)
    expect(result.checksummed?.toLowerCase()).toBe(DEPLOYER.toLowerCase())
  })

  it('resolveAddressOrHex rejects unknown ENS names', async () => {
    const { resolveAddressOrHex } = await import('../../utils/ens')
    const result = await resolveAddressOrHex('unknown-name.eth')
    expect(result.valid).toBe(false)
    expect(result.error).toMatch(/Could not resolve ENS/)
  })
})
