{"size": 1480, "ext": "js", "max_stars_repo_path": "test/src.plugins.rates.spec.js", "max_stars_repo_name": "autonomoussoftware/metronome-wallet-core", "max_stars_repo_stars_event_min_datetime": "2018-10-08T19:23:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-21T02:28:01.000Z", "max_issues_repo_path": "test/src.plugins.rates.spec.js", "max_issues_repo_name": "autonomoussoftware/metronome-wallet-core", "max_issues_repo_issues_event_min_datetime": "2018-10-05T16:30:14.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-14T13:53:11.000Z", "max_forks_repo_path": "test/src.plugins.rates.spec.js", "max_forks_repo_name": "autonomoussoftware/metronome-wallet-core", "max_forks_repo_forks_event_min_datetime": "2019-09-28T13:03:20.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T08:57:43.000Z"}
{"max_stars_count": 6, "max_issues_count": 8, "max_forks_count": 2, "avg_line_length": 21.4492753623, "max_line_length": 78, "alphanum_fraction": 0.5736486486}
'use strict'

const { once } = require('lodash')
const chai = require('chai')
const EventEmitter = require('events')
const nock = require('nock')
const proxyquire = require('proxyquire')

chai.should()

describe('Rates', function () {
  before(function () {
    nock.disableNetConnect()
  })

  it('should emit coin price', function (done) {
    const symbol = 'ETH'
    const price = 200

    const createPlugin = proxyquire('../src/plugins/rates', {
      'safe-exchange-rate': {
        getExchangeRate (pair) {
          pair.should.equal(`${symbol}:USD`)
          return Promise.resolve(price)
        }
      }
    })

    const plugin = createPlugin()

    const eventBus = new EventEmitter()

    const end = once(function (err) {
      plugin.stop()
      eventBus.removeAllListeners()
      done(err)
    })

    eventBus.on('wallet-error', function ({ inner }) {
      done(inner)
    })

    const start = Date.now()
    let times = 2

    eventBus.on('coin-price-updated', function (priceData) {
      try {
        priceData.should.deep.equal({ token: symbol, currency: 'USD', price })
        times -= 1
        if (!times) {
          const elapsed = Date.now() - start
          elapsed.should.be.closeTo(100, 32)
          end()
        }
      } catch (err) {
        end(err)
      }
    })

    const config = { debug: true, ratesUpdateMs: 100, symbol }

    plugin.start({ config, eventBus })
  })

  after(function () {
    nock.enableNetConnect()
  })
})
