import { describe, expect, it } from 'vitest'
import {
  pickBestTokenForSymbol,
  resolveHomePriceFeedAddresses,
  selectWalletFeaturedTokens,
} from './featuredTokens'

describe('featuredTokens', () => {
  it('prefers canonical v1 cUSDC over staged v2 when both share a symbol', () => {
    const tokens = [
      {
        chainId: 138,
        symbol: 'cUSDC',
        address: '0xf22258f57794CC8E06237084b353Ab30fFfa640b',
        decimals: 6,
      },
      {
        chainId: 138,
        symbol: 'cUSDC',
        address: '0x219522c60e83dEe01FC5b0329d6fA8fD84b9D13d',
        decimals: 6,
        extensions: { deploymentVersion: 'v2', deploymentStatus: 'staged' },
      },
    ]

    expect(pickBestTokenForSymbol(tokens, 'cUSDC')?.address).toBe('0xf22258f57794CC8E06237084b353Ab30fFfa640b')
  })

  it('selects wallet featured tokens using curated address preference', () => {
    const curated = [
      {
        chainId: 138,
        symbol: 'cUSDT',
        address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
        decimals: 6,
      },
    ]
    const catalog = [
      ...curated,
      {
        chainId: 138,
        symbol: 'cUSDT',
        address: '0x9FBfab33882Efe0038DAa608185718b772EE5660',
        decimals: 6,
        extensions: { deploymentVersion: 'v2', deploymentStatus: 'staged' },
      },
      {
        chainId: 138,
        symbol: 'USDT',
        address: '0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
        decimals: 6,
      },
    ]

    const featured = selectWalletFeaturedTokens(catalog, curated)
    expect(featured[0]?.address).toBe('0x93E66202A11B1772E55407B32B44e5Cd8eda7f22')
    expect(featured.some((token) => token.symbol === 'USDT')).toBe(true)
  })

  it('falls back to hardcoded home price feed addresses when catalogs are empty', () => {
    expect(resolveHomePriceFeedAddresses([], [])).toEqual([
      '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
      '0xf22258f57794CC8E06237084b353Ab30fFfa640b',
      '0x290E52a8819A4fBd0714E517225429AA2B70EC6b',
      '0xc02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    ])
  })
})
