import type { DeploymentStatusFile } from './deployment-status';
import {
  buildGruV2PoolRegistryFromDeploymentData,
  preferGruV2OfficialDexPairs,
  resolveDeploymentTokenAddress,
} from './gru-v2-deployment-pools';

describe('gru-v2-deployment-pools', () => {
  it('resolveDeploymentTokenAddress checks cwTokens, gasMirrors, anchors, gasQuoteAddresses', () => {
    const chain = {
      cwTokens: { cWUSDT: '0xcc' },
      anchorAddresses: { USDC: '0xaa' },
      gasQuoteAddresses: { WETH: '0xee' },
    };
    expect(resolveDeploymentTokenAddress(chain, 'cWUSDT')).toBe('0xcc');
    expect(resolveDeploymentTokenAddress(chain, 'USDC')).toBe('0xaa');
    expect(resolveDeploymentTokenAddress(chain, 'WETH')).toBe('0xee');
    expect(resolveDeploymentTokenAddress(chain, 'MISSING')).toBeNull();
  });

  it('buildGruV2PoolRegistryFromDeploymentData merges pmmPools, pmmPoolsVolatile, and gasPmmPools', () => {
    const data = {
      chains: {
        '1': {
          name: 'Ethereum Mainnet',
          cwTokens: { cWUSDT: '0xaf5017d0163ecb99d9b5d94e3b4d7b09af44d8ae' },
          anchorAddresses: {
            USDC: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
            TRUU: '0xdae0fafd65385e7775cf75b1398735155ef6acd2',
          },
          pmmPools: [
            {
              base: 'cWUSDT',
              quote: 'USDC',
              poolAddress: '0x1111111111111111111111111111111111111111',
              feeBps: 3,
              role: 'public_routing',
              publicRoutingEnabled: true,
            },
          ],
          pmmPoolsVolatile: [
            {
              base: 'cWUSDT',
              quote: 'TRUU',
              poolAddress: '0x2222222222222222222222222222222222222222',
              feeBps: 30,
              role: 'truu_routing',
            },
          ],
          gasMirrors: { cWETH: '0xf6dc5587e18f27adff60e303fdd98f35b50fa8a5' },
          gasQuoteAddresses: {
            WETH: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
            USDC: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
          },
          gasPmmPools: [
            {
              familyKey: 'eth_mainnet',
              base: 'cWETH',
              quote: 'USDC',
              poolAddress: '0x3333333333333333333333333333333333333333',
              feeBps: 30,
              role: 'public_routing',
              venue: 'dodo_pmm',
            },
          ],
        },
      },
    } as unknown as DeploymentStatusFile;

    const { rows, byChainTokenPools } = buildGruV2PoolRegistryFromDeploymentData(data);
    expect(rows).toHaveLength(3);
    expect(rows.map((r) => r.section).sort()).toEqual(['gasPmmPools', 'pmmPools', 'pmmPoolsVolatile']);

    const usdt = '0xaf5017d0163ecb99d9b5d94e3b4d7b09af44d8ae';
    expect(byChainTokenPools.get(`1:${usdt}`)?.has('0x1111111111111111111111111111111111111111')).toBe(true);
    expect(byChainTokenPools.get(`1:${usdt}`)?.has('0x2222222222222222222222222222222222222222')).toBe(true);
  });

  it('preferGruV2OfficialDexPairs leaves pairs unchanged when no deployment pools index the token', () => {
    const pairs = [{ pairAddress: '0xbb', priceUsd: '1' }];
    expect(preferGruV2OfficialDexPairs(99999, '0x0000000000000000000000000000000000000001', pairs)).toEqual(pairs);
  });
});
