import type { LiquidityPool, PoolRepository } from '../database/repositories/pool-repo';
import { RouteGraphBuilder } from './route-graph-builder';

jest.mock('./dodo-v3-pilot', () => ({
  __esModule: true,
  getChain138DodoV3PilotEdges: jest.fn().mockResolvedValue([]),
  isChain138DodoV3ExecutionLive: jest.fn(() => false),
}));

jest.mock('./chain138-pilot-venues', () => ({
  __esModule: true,
  getChain138PilotVenueEdges: jest.fn().mockResolvedValue([]),
}));

jest.mock('./live-dodo-fallback', () => ({
  __esModule: true,
  getLiveDodoPools: jest.fn().mockResolvedValue([]),
}));

function pool(overrides: Partial<LiquidityPool>): LiquidityPool {
  return {
    chainId: 138,
    poolAddress: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
    token0Address: '0x93e66202a11b1772e55407b32b44e5cd8eda7f22',
    token1Address: '0xf22258f57794cc8e06237084b353ab30fffa640b',
    dexType: 'dodo',
    reserve0: '1000000',
    reserve1: '1000000',
    reserve0Usd: 0,
    reserve1Usd: 0,
    totalLiquidityUsd: 0,
    volume24h: 0,
    lastUpdated: new Date('2026-04-05T00:00:00Z'),
    ...overrides,
  };
}

class MockPoolRepo {
  constructor(private readonly pools: LiquidityPool[]) {}

  async getPoolsByChain(): Promise<LiquidityPool[]> {
    return this.pools;
  }
}

describe('RouteGraphBuilder', () => {
  it('filters dust Chain 138 DODO pools from planner visibility', async () => {
    const builder = new RouteGraphBuilder(
      new MockPoolRepo([
        pool({
          poolAddress: '0x1111111111111111111111111111111111111111',
          token0Address: '0xf22258f57794cc8e06237084b353ab30fffa640b',
          token1Address: '0x71d6687f38b93ccad569fa6352c876eea967201b',
          reserve0: '999999997998',
          reserve1: '999999997998',
        }),
        pool({
          poolAddress: '0x2222222222222222222222222222222222222222',
          reserve0: '1001',
          reserve1: '1001',
        }),
      ]) as unknown as PoolRepository
    );

    const edges = await builder.buildSwapEdges(138);

    expect(edges).toHaveLength(2);
    expect(edges.every((edge) => edge.poolAddress === '0x1111111111111111111111111111111111111111')).toBe(true);
  });

  it('keeps non-DODO venues visible even when they are small', async () => {
    const builder = new RouteGraphBuilder(
      new MockPoolRepo([
        pool({
          poolAddress: '0x3333333333333333333333333333333333333333',
          dexType: 'uniswap_v3',
          token0Address: '0xc02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
          token1Address: '0x71D6687F38b93CCad569Fa6352c876eea967201b',
          reserve0: '10000000000000000',
          reserve1: '10000',
          totalLiquidityUsd: 1,
        }),
      ]) as unknown as PoolRepository
    );

    const edges = await builder.buildSwapEdges(138);

    expect(edges).toHaveLength(2);
    expect(edges[0].provider).toBe('uniswap_v3');
    expect(edges[1].provider).toBe('uniswap_v3');
  });
});
