import OPENDB from '..'
// OPENDB instance for API call
let BP_ENDPOINT, opendb
beforeEach(async () => {
  BP_ENDPOINT = 'ws://13.200.184.78:11110'
  // BP_ENDPOINT = 'wss://sql-ws.guardianlink.io/ws'
  opendb = new OPENDB(BP_ENDPOINT)
  await opendb.connect()
})
// Positive case
describe('Open DB Client test', () => {
  it('Connection Test', () => {
    expect(opendb.client.endpoint).toBe(BP_ENDPOINT)
    expect(opendb.client.connect()).not.toBeNull()
    expect(opendb.client.isConnected).toBeTruthy()
  })
  it('RPC test', () => {
    expect(opendb.client.rpc).toBeTruthy()
    expect(opendb.client.rpc.connect).toBeTruthy()
    expect(opendb.client.rpc.onClose).toBeTruthy()
  })
})
describe('Open DB BP test Block', () => {
  it('getBlockList Test with valid params', async () => {
    const getBlockListData = await opendb.bp.getBlockList(1, 2)
    expect(getBlockListData).toBeTruthy()
    expect(getBlockListData).toEqual({
      blocks: [
        {
          height: 55188700,
          hash: '5c7dd5cb60fea526fd2178670bbe016867950fc7b70224c2d4b76adc660a12f0',
          timestamp: 1712661847000182000,
          timestamp_human: '2024-04-09T11:24:07.000182111Z',
          version: 16777216,
          producer:
            '8d7604acfdb391891a4c795f0939425b6d58bd50a81e579d15f06ecd381ad549',
          merkle_root:
            'e618e37920219dd949a6bc81bf9910936c9ec191d1d92b828ba1ff90dd5a3755',
          parent:
            'be5990f750cf2b09ec8eb2894668cd3affdd6838191b63378181b7835dd0d5a8',
          tx_count: 3,
        },
        {
          height: 55188699,
          hash: 'be5990f750cf2b09ec8eb2894668cd3affdd6838191b63378181b7835dd0d5a8',
          timestamp: 1712661844000170000,
          timestamp_human: '2024-04-09T11:24:04.00017005Z',
          version: 16777216,
          producer:
            '8d7604acfdb391891a4c795f0939425b6d58bd50a81e579d15f06ecd381ad549',
          merkle_root:
            '7e86227f99300da914901c391f7793095bd2d1df6ffb7624603ba7dd0c2553c8',
          parent:
            'f70c535bbef0a74d3b102592f33ee33ad99696caaf371df9418c7859786b4aa1',
          tx_count: 3,
        },
      ],
      pagination: { page: 1, size: 2, total: 318843, pages: 159422 },
    })
  })
  it('getBlockByHash Test with valid Hash', async () => {
    await expect(
      opendb.bp.getBlockByHash(
        '1ec2d8ea08f85fa911323ef41ae2f133992ffae75a9bd86f64d679132f821651'
      )
    ).toBeTruthy()
  })
  it('getBlockByHeight Test with valid Height', async () => {
    await expect(opendb.bp.getBlockByHeight(55188697)).toBeTruthy()
  })
})
describe('Open DB BP test TransactionList', () => {
  it('getTransactionList Test with valid params', async () => {
    const getTransactionListData = await opendb.bp.getTransactionList(1, 2)
    expect(getTransactionListData).toBeTruthy()
    expect(getTransactionListData).toEqual({
      transactions: [
        {
          block_height: 55188700,
          index: 2,
          hash: 'db47d3ae0010ef44e56536f6a0f84fd1dcfc94ce425f09176151400ab505fbb9',
          block_hash:
            '5c7dd5cb60fea526fd2178670bbe016867950fc7b70224c2d4b76adc660a12f0',
          timestamp: 1712661845716714500,
          timestamp_human: '2024-04-09T11:24:05.716714379Z',
          type: 9,
          address:
            '1a7b0959bbd0d0ec529278a61c0056c277bffe75b2646e1699b46b10a90210be',
          raw: '{"Space":33172803584,"Memory":14789107712,"LoadAvgPerCPU":0.0275,"TargetUser":null,"GasPrice":1,"TokenType":0,"NodeID":"000005f4f22c06f76c43c4f48d5a7ec1309cc94030cbf9ebae814172884ac8b5","Nonce":318843,"TxType":9,"Timestamp":"2024-04-09T11:24:05.716714379Z","DataHash":"db47d3ae0010ef44e56536f6a0f84fd1dcfc94ce425f09176151400ab505fbb9","Signee":{"Curve":{"P":115792089237316195423570985008687907853269984665640564039457584007908834671663,"N":115792089237316195423570985008687907852837564279074904382605163141518161494337,"B":7,"Gx":55066263022277343669578718895168534326250603453777594175500187360389116729240,"Gy":32670510020758816978083085130507043184471273380659243275938904335757337482424,"BitSize":256,"Name":"","H":1},"X":65719270943604338427905752337893210135845472056018326024127522629670524209686,"Y":35497802239556337884222127738547385904561999414342714456895417367493562245332},"Signature":{"R":38675844899950829639690380171382593038923764384816603554883127048781541395881,"S":34237655500141589236100238282878058242372634904943393953512937461423001370083}}',
          tx: {
            DataHash:
              'db47d3ae0010ef44e56536f6a0f84fd1dcfc94ce425f09176151400ab505fbb9',
            GasPrice: 1,
            LoadAvgPerCPU: 0.0275,
            Memory: 14789107712,
            NodeID:
              '000005f4f22c06f76c43c4f48d5a7ec1309cc94030cbf9ebae814172884ac8b5',
            Nonce: 318843,
            Signature: { R: 3.867584489995083e76, S: 3.4237655500141586e76 },
            Signee: {
              Curve: {
                B: 7,
                BitSize: 256,
                Gx: 5.5066263022277344e76,
                Gy: 3.2670510020758816e76,
                H: 1,
                N: 1.157920892373162e77,
                Name: '',
                P: 1.157920892373162e77,
              },
              X: 6.571927094360434e76,
              Y: 3.549780223955634e76,
            },
            Space: 33172803584,
            TargetUser: null,
            Timestamp: '2024-04-09T11:24:05.716714379Z',
            TokenType: 0,
            TxType: 9,
          },
        },
        {
          block_height: 55188700,
          index: 1,
          hash: '942f47cb2a331ee5c37f5b23b7b3b4261033ccd39cc12fc66adcaa48664f1d6a',
          block_hash:
            '5c7dd5cb60fea526fd2178670bbe016867950fc7b70224c2d4b76adc660a12f0',
          timestamp: 1712661845770187800,
          timestamp_human: '2024-04-09T11:24:05.770187851Z',
          type: 9,
          address:
            'ba0ba731c7a76ccef2c1170f42038f7e228dfb474ef0190dfe35d9a37911ed37',
          raw: '{"Space":33172803584,"Memory":14789107712,"LoadAvgPerCPU":0.0275,"TargetUser":null,"GasPrice":1,"TokenType":0,"NodeID":"000005aa62048f85da4ae9698ed59c14ec0d48a88a07c15a32265634e7e64ade","Nonce":318843,"TxType":9,"Timestamp":"2024-04-09T11:24:05.770187851Z","DataHash":"942f47cb2a331ee5c37f5b23b7b3b4261033ccd39cc12fc66adcaa48664f1d6a","Signee":{"Curve":{"P":115792089237316195423570985008687907853269984665640564039457584007908834671663,"N":115792089237316195423570985008687907852837564279074904382605163141518161494337,"B":7,"Gx":55066263022277343669578718895168534326250603453777594175500187360389116729240,"Gy":32670510020758816978083085130507043184471273380659243275938904335757337482424,"BitSize":256,"Name":"","H":1},"X":46889149913685178140156963369364502857171763704599626739506996032145349700896,"Y":107351296487730405960229873787712063587054507408879896649250634583933168665635},"Signature":{"R":48816205973163661129696558208109977111499883738533422344063974348392988798502,"S":32520652476819776705229003905131857051523200598119728185685160196802626427851}}',
          tx: {
            DataHash:
              '942f47cb2a331ee5c37f5b23b7b3b4261033ccd39cc12fc66adcaa48664f1d6a',
            GasPrice: 1,
            LoadAvgPerCPU: 0.0275,
            Memory: 14789107712,
            NodeID:
              '000005aa62048f85da4ae9698ed59c14ec0d48a88a07c15a32265634e7e64ade',
            Nonce: 318843,
            Signature: { R: 4.8816205973163663e76, S: 3.2520652476819775e76 },
            Signee: {
              Curve: {
                B: 7,
                BitSize: 256,
                Gx: 5.5066263022277344e76,
                Gy: 3.2670510020758816e76,
                H: 1,
                N: 1.157920892373162e77,
                Name: '',
                P: 1.157920892373162e77,
              },
              X: 4.6889149913685175e76,
              Y: 1.073512964877304e77,
            },
            Space: 33172803584,
            TargetUser: null,
            Timestamp: '2024-04-09T11:24:05.770187851Z',
            TokenType: 0,
            TxType: 9,
          },
        },
      ],
      pagination: { page: 1, size: 2, total: 956535, pages: 478268 },
    })
  })
  it('getTransactionListOfBlock Test with valid params', async () => {
    await expect(opendb.bp.getTransactionListOfBlock(42170, 1, 4)).toBeTruthy()
  })
  it('getTransactionByHash Test with valid Hash', async () => {
    await expect(
      opendb.bp.getTransactionByHash(
        '942f47cb2a331ee5c37f5b23b7b3b4261033ccd39cc12fc66adcaa48664f1d6a'
      )
    ).toBeTruthy()
  })
})
// Negative case
describe('Open DB Client test', () => {
  it('should throw error for empty hostname', async () => {
    opendb = new OPENDB('')
    await expect(opendb.connect()).rejects.toThrow()
  })
  it('should throw error for invalid hostname', async () => {
    opendb = new OPENDB('unknown:3330')
    await expect(opendb.connect()).rejects.toThrow()
  })
})
describe('Open DB BP test Block', () => {
  //Testcase for Negative case getBlockListData -without params,invalid page number,invalid size
  it('getBlockListData Test invalid page number', async () => {
    const getBlockListData = await opendb.bp.getBlockList(67159428, 1)
    expect(getBlockListData.blocks).toEqual([])
  })
  it('getBlockListData Test without params', async () => {
    const getBlockListData = await opendb.bp.getBlockList()
    expect(getBlockListData.blocks).toBeTruthy()
    // -------TODO: Without parameter its get data from page 1 and first 10 record
  })
  it('getBlockListData Test 0 size', async () => {
    const getBlockListData = await opendb.bp.getBlockList(6, 0)
    expect(getBlockListData.blocks).toBeTruthy()
    // -------TODO: Valid page number 6 and size tobe 0 it means function retun data 6th page 10 records
  })
  //Testcase for Negative case getBlockListData -without params,invalid Hash
  it('getBlockByHashData Test  without params', async () => {
    // Failed case
    // '1ec2d8ea08f85fa911323ef41ae2f133992ffae75a9bd86f64d679132f821651'
    // -------TODO: Cannot check this case because of timeout
    await expect(opendb.bp.getBlockByHash()).rejects.toThrow()
  })
  it('getBlockByHashData Test with wrong Hash', async () => {
    // Failed case
    // -------TODO: Cannot check this case because of timeout
    await expect(
      opendb.bp.getBlockByHash(
        '1ec2d8ea08f85fa911323ef41ae2f133992ffae75a9bd86f64d679132f821655'
      )
    ).toBeTruthy()
  })
  //Testcase for Negative case getBlockListData -without params,invalid Height
  it('getBlockByHeightData Test without param(height)', async () => {
    // Failed case
    // -------TODO: Cannot check this case because of timeout
    await expect(opendb.bp.getBlockByHeight()).toBeTruthy()
  })
  it('getBlockByHeightData Test Wrong param(height)', async () => {
    // Failed case
    // -------TODO: Cannot check this case because of timeout
    await expect(opendb.bp.getBlockByHeight(13)).toBeTruthy()
    // 4324235453453453434- replace 13 to this number i change for tslint
  })
})
describe('Open DB BP test TransactionList', () => {
  //=========> getTransactionListData
  it('getTransactionListData Test without param', async () => {
    // -------TODO: Without parameter its get data from page 1 and first 10 record
    await expect(opendb.bp.getTransactionList()).toBeTruthy()
  })
  it('getTransactionListData Test invalid pagenumber', async () => {
    const getTransactionListData = await opendb.bp.getTransactionList(
      333956535,
      1
    )
    // -------TODO:  invalid pagenumber its get data from page 333956535 and first 1 record
    expect(getTransactionListData).toBeTruthy()
    expect(getTransactionListData.transactions).toBeNull()
  })
  it('getTransactionListData Test 0 size', async () => {
    // -------TODO: Without parameter its get data from page 1 and first 10 record
    await expect(opendb.bp.getTransactionList(1, 0)).toBeTruthy()
  })
  //==========> getTransactionListOfBlockData
  it('getTransactionListOfBlockData Test without param', async () => {
    // TODO: Received function did not throw
    await expect(opendb.bp.getTransactionListOfBlock()).rejects.toThrow()
  })
  it('getTransactionListOfBlockData Test invalid Height', async () => {
    const getTransactionListOfBlockData =
      await opendb.bp.getTransactionListOfBlock(99955188695, 1, 58)
    // TODO: invalid height to get empty result
    expect(getTransactionListOfBlockData.transactions).toEqual([])
  })
  it('getTransactionListOfBlockData Test invalid Height without from, to', async () => {
    const getTransactionListOfBlockData =
      await opendb.bp.getTransactionListOfBlock(99955188695)
    // TODO: invalid height to get empty result
    expect(getTransactionListOfBlockData.transactions).toEqual([])
  })
  it('getTransactionListOfBlockData Test valid Height without from, to', async () => {
    const getTransactionListOfBlockData =
      await opendb.bp.getTransactionListOfBlock(55188695)
    // TODO: Default 10 record load fro the height 55188695
    expect(getTransactionListOfBlockData.transactions).not.toBeNull()
  })
  //==================> getTransactionByHashData
  it('getTransactionByHashData Test without Hash', async () => {
    // -------TODO: Cannot check this case because of timeout
    await expect(opendb.bp.getTransactionByHash('')).toBeTruthy()
  })
  it('getTransactionByHashData Test with wromg Hash', async () => {
    //Failed case
    // 942f47cb2a331ee5c37f5b23b7b3b4261033ccd39cc12fc66adcaa48664f1d6a
    // -------TODO: Cannot check this case because of timeout
    await expect(
      opendb.bp.getTransactionByHash(
        '942f47cb2a331ee5c37f5b23b7b3b4261033ccd39cc12fc66adcaa48664f1d98'
      )
    ).toBeTruthy()
  })
})

describe('Open DB BP getBlockListData test Block', () => {
  it('getBlockListData Test invalid size', async () => {
    await expect(
      opendb.bp.getBlockList(6, 12) //763387168172361827  - replace 12 to this number i change for tslint
    ).rejects.toThrow()
    // -------TODO: Received function did not throw
  })
})
