import { describe, expect, it } from 'vitest'
import {
  applyTokenDisplayOverrides,
  buildCanonicalAddressSet,
  isCanonicalTokenAddress,
  sortIndexedTokensCanonicalFirst,
  WETH9_CANONICAL_ADDRESS,
} from './canonicalTokens'

describe('canonicalTokens', () => {
  it('builds a lowercase canonical address set', () => {
    const set = buildCanonicalAddressSet([
      { chainId: 138, address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22', symbol: 'cUSDT' },
    ])

    expect(isCanonicalTokenAddress('0x93e66202a11b1772e55407b32b44e5cd8eda7f22', set)).toBe(true)
    expect(isCanonicalTokenAddress('0x0000000000000000000000000000000000000001', set)).toBe(false)
  })

  it('sorts canonical tokens before non-canonical entries', () => {
    const canonicalSet = buildCanonicalAddressSet([
      { chainId: 138, address: WETH9_CANONICAL_ADDRESS, symbol: 'WETH9' },
    ])

    const sorted = sortIndexedTokensCanonicalFirst(
      [
        { address: '0x0000000000000000000000000000000000000001', symbol: 'AAA' },
        { address: WETH9_CANONICAL_ADDRESS, symbol: 'WETH9' },
      ],
      canonicalSet,
    )

    expect(sorted[0]?.address).toBe(WETH9_CANONICAL_ADDRESS)
  })

  it('applies WETH9 metadata override', () => {
    const token = applyTokenDisplayOverrides({
      address: WETH9_CANONICAL_ADDRESS,
      name: null as unknown as string,
      symbol: 'WETH',
      decimals: 18,
    })

    expect(token.symbol).toBe('WETH9')
    expect(token.name).toBe('Wrapped Ether (WETH9)')
  })
})
