import test from 'ava'
import { Server } from 'mongodb-topology-manager'
import fs from 'fs'
import path from 'path'
import HAMongoClient from '../src/ha_mongo_client'

const DB_PATH = path.join(__dirname, 'db')

test.before(() => {

    const serverManager = new Server()
    return serverManager.discover().catch(err => { throw Error('Unable to find mongod binary') })
})

test('Correct intervals', (t) => new Promise((resolve, reject) => {

    const attemptsLimit = 5

    let interval = 0.1
    let multiplier = 2
    let maxInterval = 1
    let attemptsCounter = 0

    const hAMongoClient = new HAMongoClient('mongodb://unknown:27017', {}, {
        initInterval: interval,
        multiplier:   multiplier,
        maxInterval:  maxInterval,
    })

    t.plan(attemptsLimit * 2)

    hAMongoClient.on('retry', retry => {

        attemptsCounter += 1
        interval = Math.min(interval * multiplier, maxInterval)

        t.is(retry.attempt, attemptsCounter)
        t.is(retry.interval, interval)

        if (attemptsCounter === attemptsLimit) {

            hAMongoClient.abort()
            resolve()

        }
    })

    hAMongoClient.connect()
}))

test('Connecting to delayed mongod', (t) => {

    t.plan(1)

    const serverManager = new Server('mongod', { dbpath: DB_PATH })

    const hAMongoClient = new HAMongoClient('mongodb://localhost:27017', {})

    return serverManager.discover()
        .then(() => {
            // Starting mongod with delay
            setTimeout(
                () => serverManager.purge()
                    .then(() => serverManager.start())
                    .catch(err => { throw err }),
                1000
            )
        })
        .then(() => hAMongoClient.connect())
        .then(db => db.close())
        .then(() => serverManager.stop())
        .then(() => serverManager.purge())
        .then(() => new Promise((resolve, reject) => {
            fs.rmdir(DB_PATH, (err) => {
                if (err) return reject(err)
                resolve()
            })
        }))
        .then(() => t.pass())
        .catch(err => { throw err })
})

@@ -262,24 +262,25 @@
 ew Server()%0A
+%0A
     return s
@@ -283,32 +283,41 @@
 rn serverManager
+%0A        
 .discover().catc
@@ -311,32 +311,54 @@
 scover()
+%0A        
 .catch(
-err
+()
  =%3E %7B 
+%0A%0A            
 throw Er
@@ -392,20 +392,30 @@
 binary')
+%0A%0A       
  %7D)%0A
+%0A
 %7D)%0A%0Atest
@@ -459,32 +459,24 @@
 ise((resolve
-, reject
 ) =%3E %7B%0A%0A    
@@ -503,54 +503,58 @@
 = 5%0A
-%0A    let interval = 0.1%0A    let multiplier = 2
+    const multiplier = 2%0A    const maxInterval = 1
 %0A   
@@ -558,20 +558,17 @@
     let 
-maxI
+i
 nterval 
@@ -561,32 +561,34 @@
  let interval = 
+0.
 1%0A    let attemp
@@ -733,44 +733,16 @@
 lier
-:   multiplier,%0A        maxInterval:
+,%0A      
   ma
@@ -1129,16 +1129,17 @@
       %7D%0A
+%0A
     %7D)%0A%0A
@@ -1166,16 +1166,17 @@
 nnect()%0A
+%0A
 %7D))%0A%0Ates
@@ -1223,23 +1223,8 @@
  %7B%0A%0A
-    t.plan(1)%0A%0A
     
@@ -1283,25 +1283,24 @@
  DB_PATH %7D)%0A
-%0A
     const hA
@@ -1363,16 +1363,35 @@
 17', %7B%7D)
+%0A    %0A    t.plan(1)
 %0A%0A    re
@@ -1438,24 +1438,25 @@
 hen(() =%3E %7B%0A
+%0A
             
@@ -1643,26 +1643,92 @@
 err =%3E %7B
- throw err
+%0A%0A                        throw err%0A                    %0A                   
  %7D),%0A   
@@ -1759,16 +1759,17 @@
       )%0A
+%0A
         
@@ -1984,24 +1984,25 @@
 eject) =%3E %7B%0A
+%0A
             
@@ -2030,16 +2030,17 @@
 r) =%3E %7B%0A
+%0A
         
@@ -2075,16 +2075,17 @@
 ct(err)%0A
+%0A
         
@@ -2084,32 +2084,39 @@
                 
+return 
 resolve()%0A      
@@ -2105,24 +2105,25 @@
 n resolve()%0A
+%0A
             
@@ -2117,32 +2117,33 @@
 %0A            %7D)%0A
+%0A
         %7D))%0A    
@@ -2191,16 +2191,29 @@
 err =%3E %7B
+%0A%0A           
  throw e
@@ -2214,15 +2214,25 @@
 hrow err
+%0A%0A       
  %7D)%0A
+%0A
 %7D)%0A

Update tests code style due to ESLint rules