import {
  aggregateDexScreenerPairsToMarketData,
  normalizeDexScreenerTokenPairsPayload,
  type DexScreenerPair,
} from './dexscreener-adapter';

describe('normalizeDexScreenerTokenPairsPayload', () => {
  it('accepts a raw JSON array (current API)', () => {
    const pairs: DexScreenerPair[] = [
      {
        chainId: 'ethereum',
        dexId: 'x',
        url: '',
        pairAddress: '0x',
        baseToken: { address: '0xa', name: '', symbol: 'A' },
        quoteToken: { address: '0xb', name: '', symbol: 'B' },
        priceUsd: '1',
      },
    ];
    expect(normalizeDexScreenerTokenPairsPayload(pairs)).toEqual(pairs);
  });

  it('accepts legacy { pairs: [...] } shape', () => {
    const inner: DexScreenerPair[] = [
      {
        chainId: 'ethereum',
        dexId: 'x',
        url: '',
        pairAddress: '0x',
        baseToken: { address: '0xa', name: '', symbol: 'A' },
        quoteToken: { address: '0xb', name: '', symbol: 'B' },
        priceUsd: '2',
      },
    ];
    expect(normalizeDexScreenerTokenPairsPayload({ pairs: inner })).toEqual(inner);
  });
});

describe('aggregateDexScreenerPairsToMarketData', () => {
  it('returns null for empty input', () => {
    expect(aggregateDexScreenerPairsToMarketData(null)).toBeNull();
    expect(aggregateDexScreenerPairsToMarketData([])).toBeNull();
  });

  it('picks price from the pair with highest USD liquidity', () => {
    const pairs: DexScreenerPair[] = [
      {
        chainId: 'ethereum',
        dexId: 'uniswap',
        url: '',
        pairAddress: '0x1',
        baseToken: { address: '0xt', name: '', symbol: 'T' },
        quoteToken: { address: '0xq', name: '', symbol: 'Q' },
        priceUsd: '100',
        liquidity: { usd: 1000 },
      },
      {
        chainId: 'ethereum',
        dexId: 'sushi',
        url: '',
        pairAddress: '0x2',
        baseToken: { address: '0xt', name: '', symbol: 'T' },
        quoteToken: { address: '0xq', name: '', symbol: 'Q' },
        priceUsd: '1',
        liquidity: { usd: 1_000_000 },
      },
    ];
    const m = aggregateDexScreenerPairsToMarketData(pairs);
    expect(m?.priceUsd).toBe(1);
    expect(m?.liquidityUsd).toBe(1_001_000);
  });

  it('when liquidity is missing, prefers higher 24h volume', () => {
    const pairs: DexScreenerPair[] = [
      {
        chainId: 'ethereum',
        dexId: 'a',
        url: '',
        pairAddress: '0x1',
        baseToken: { address: '0xt', name: '', symbol: 'T' },
        quoteToken: { address: '0xq', name: '', symbol: 'Q' },
        priceUsd: '50',
        volume: { h24: 100 },
      },
      {
        chainId: 'ethereum',
        dexId: 'b',
        url: '',
        pairAddress: '0x2',
        baseToken: { address: '0xt', name: '', symbol: 'T' },
        quoteToken: { address: '0xq', name: '', symbol: 'Q' },
        priceUsd: '51',
        volume: { h24: 1_000_000 },
      },
    ];
    const m = aggregateDexScreenerPairsToMarketData(pairs);
    expect(m?.priceUsd).toBe(51);
  });
});
