import { mergeRelayLaneMessages } from './relay-lane-indexer';
import type { MonadRelayLaneConfig } from '../config/monad-relay-lane';

const TEST_CONFIG: MonadRelayLaneConfig = {
  version: 'test',
  laneId: '138-monad-relay',
  description: 'test lane',
  chain138: {
    chainId: 138,
    selector: '16015286601757825753',
    explorer: 'https://explorer.d-bis.org',
    sendBridgeWeth9: '0xcacfd227A040002e49e2e01626363071324f820a',
    relayBridgeInbound: '0xEB87B3bE09Ab383Ea391caf5bCE70B44Dbd80A9B',
  },
  monad: {
    chainId: 143,
    selector: '8481857512324358265',
    explorer: 'https://monadscan.com',
    sendBridgeWeth9: '0xF9DD3349C7f11812d727Db5335690b1e6AD16a52',
    relayBridgeOutbound: '0xC158b6cD3A3088C52F797D41f5Aa02825361629e',
  },
};

describe('mergeRelayLaneMessages', () => {
  it('merges initiated and completed by messageId', () => {
    const messageId = '0xdebc46c8ce3b4698331768dcddb9c6ee69c7d891fbe48e930f383a27330bf8e8';
    const messages = mergeRelayLaneMessages(
      TEST_CONFIG,
      [
        {
          messageId,
          direction: '138-to-monad',
          sourceChainId: 138,
          destChainId: 143,
          destSelector: '8481857512324358265',
          sourceTxHash: '0xd2200edf828a7310aebc28e704f668732a5eb4fcba648b7339aa132a1496dd39',
          sourceBlockNumber: 100,
          sender: '0x4A666F96fC8764181194447A7dFdb7d471b301C8',
          recipient: '0x4A666F96fC8764181194447A7dFdb7d471b301C8',
          amountWei: '1000000000000000',
          tokenSymbol: 'WETH',
        },
      ],
      [
        {
          messageId,
          direction: '138-to-monad',
          destChainId: 143,
          destTxHash: '0xbd79554a89e2abb3024462009725851f57a54eb8ddfd3d095068607c8a5e2402',
          destBlockNumber: 200,
          recipient: '0x4A666F96fC8764181194447A7dFdb7d471b301C8',
          amountWei: '1000000000000000',
        },
      ],
      []
    );

    expect(messages).toHaveLength(1);
    expect(messages[0].status).toBe('delivered');
    expect(messages[0].sourceTxHash).toMatch(/^0x/);
    expect(messages[0].relayTxHash).toMatch(/^0x/);
    expect(messages[0].explorerLinks.sourceTx).toContain('explorer.d-bis.org');
    expect(messages[0].explorerLinks.relayTx).toContain('monadscan.com');
  });

  it('includes seed messages when RPC data is absent', () => {
    const messages = mergeRelayLaneMessages(TEST_CONFIG, [], [], [
      {
        messageId: '0x82dcbc28d1e7b50a0b9caa1a9415078148dd78b5aeba9a23aa5714ef6cf8b970',
        direction: 'monad-to-138',
        status: 'delivered',
        sourceTxHash: '0x27d4ccf43c8bc17bab56b0a18c830dce21dd7ae175fd24e2261cb3a414f994ae',
        relayTxHash: '0x4435ecacc79919f64e6de6cd6b2d454a92580bb72a4c7e320d06cff2354e9567',
        amountWei: '1000000000000000',
        tokenSymbol: 'WMON',
      },
    ]);

    expect(messages).toHaveLength(1);
    expect(messages[0].direction).toBe('monad-to-138');
    expect(messages[0].explorerLinks.sourceTx).toContain('monadscan.com');
    expect(messages[0].explorerLinks.relayTx).toContain('explorer.d-bis.org');
  });
});
