import {
  buildUniswapTokenList,
  checksumTokenAddress,
  normalizeTokenListVersion,
  toUniswapTokenListToken,
} from './uniswap-token-list';

describe('uniswap-token-list utils', () => {
  it('normalizes string versions into major/minor/patch objects', () => {
    expect(normalizeTokenListVersion('1.2.3')).toEqual({ major: 1, minor: 2, patch: 3 });
    expect(normalizeTokenListVersion({ major: 4, minor: 5, patch: 6 })).toEqual({
      major: 4,
      minor: 5,
      patch: 6,
    });
  });

  it('moves non-schema token fields into extensions and checksums addresses', () => {
    const token = toUniswapTokenListToken(
      {
        chainId: 1,
        address: '0x2de5f116bfce3d0f922d9c8351e0c5fc24b9284a',
        symbol: 'cWUSDC',
        name: 'USD Coin (Compliant Wrapped ISO-4217 M1)',
        decimals: 6,
        logoURI: 'https://d-bis.org/tokens/cwusdc.svg',
        type: 'w',
        registryFamily: 'iso4217',
        originalLogoURI: 'https://example.com/logo.svg',
      },
      { displayNames: { cWUSDC: 'Wrapped cUSDC' } }
    );

    expect(token).toMatchObject({
      chainId: 1,
      address: '0x2de5F116bFcE3d0f922d9C8351e0c5Fc24b9284a',
      symbol: 'cWUSDC',
      name: 'Wrapped cUSDC',
      decimals: 6,
      logoURI: 'https://d-bis.org/tokens/cwusdc.svg',
      extensions: {
        type: 'w',
        registryFamily: 'iso4217',
        originalLogoURI: 'https://example.com/logo.svg',
      },
    });
    expect(token).not.toHaveProperty('type');
    expect(token).not.toHaveProperty('registryFamily');
    expect(checksumTokenAddress('0x2de5f116bfce3d0f922d9c8351e0c5fc24b9284a')).toBe(
      '0x2de5F116bFcE3d0f922d9C8351e0c5Fc24b9284a'
    );
  });

  it('builds a schema-compliant list envelope', () => {
    const list = buildUniswapTokenList({
      name: 'DBIS Ethereum Mainnet GRU',
      version: '1.1.0',
      timestamp: '2026-06-06T00:00:00.000Z',
      logoURI: 'https://d-bis.org/tokens/cwusdc.svg',
      tokens: [
        {
          chainId: 1,
          address: '0x2de5f116bfce3d0f922d9c8351e0c5fc24b9284a',
          symbol: 'cWUSDC',
          name: 'Wrapped cUSDC',
          decimals: 6,
          logoURI: 'https://d-bis.org/tokens/cwusdc.svg',
        },
      ],
    });

    expect(list.version).toEqual({ major: 1, minor: 1, patch: 0 });
    expect(Array.isArray(list.tokens)).toBe(true);
    expect((list.tokens as unknown[]).length).toBe(1);
  });
});
