text: /*
   *
   *
   *       FILL IN EACH FUNCTIONAL TEST BELOW COMPLETELY
   *       -----[Keep the tests in the same order!]-----
   *
   */
  
  var chaiHttp = require('chai-http')
  var chai = require('chai')
  var assert = chai.assert
  var server = require('../server')
  let globalId
  chai.use(chaiHttp)
  
  suite('Functional Tests', function() {
    /*
     * ----[EXAMPLE TEST]----
     * Each test should completely test the response of the API end-point including response status code!
     */
    test('#example Test GET /api/books', function(done) {
      chai
        .request(server)
        .get('/api/books')
        .end(function(err, res) {
          assert.equal(res.status, 200)
          assert.isArray(res.body, 'response should be an array')
          assert.property(
            res.body[0],
            'commentcount',
            'Books in array should contain commentcount'
          )
          assert.property(
            res.body[0],
            'title',
            'Books in array should contain title'
          )
          assert.property(res.body[0], '_id', 'Books in array should contain _id')
          done()
        })
    })
    /*
     * ----[END of EXAMPLE TEST]----
     */
  
    suite('Routing tests', function() {
      suite(
        'POST /api/books with title => create book object/expect book object',
        function() {
          test('Test POST /api/books with title', function(done) {
            chai
              .request(server)
              .post('/api/books')
              .send({
                title: 'A new book title'
              })
              .end((err, res) => {
                assert.equal(res.status, 200)
                assert.equal(res.body.title, 'A new book title')
                assert.isDefined(res.body._id)
                globalId = res.body._id
                console.log(globalId)
                done()
              })
          })
  
          test('Test POST /api/books with no title given', function(done) {
            chai
              .request(server)
              .post('/api/books')
              .end((err, res) => {
                assert.equal(res.status, 400)
                assert.equal(res.body, 'missing title')
                done()
              })
          })
        }
      )
  
      suite('GET /api/books => array of books', function() {
        test('Test GET /api/books', function(done) {
          chai
            .request(server)
            .get('/api/books')
            .end((err, res) => {
              assert.equal(res.status, 200)
              assert.isArray(res.body)
              assert.hasAllKeys(res.body[0], ['title', '_id', 'commentcount'])
              done()
            })
        })
      })
  
      suite('GET /api/books/[id] => book object with [id]', function() {
        test('Test GET /api/books/[id] with id not in db', function(done) {
          chai
            .request(server)
            .get('/api/books/5e508e179ca59f0f94033bda')
            .end((err, res) => {
              assert.equal(res.status, 404)
              assert.equal(res.body, 'no book exists')
              done()
            })
        })
  
        test('Test GET /api/books/[id] with valid id in db', function(done) {
          chai
            .request(server)
            .get('/api/books/' + globalId)
            .end((err, res) => {
              assert.equal(res.status, 200)
              assert.equal(res.body.title, 'A new book title')
              assert.deepEqual(res.body.comments, [])
              assert.equal(res.body._id, globalId)
              done()
            })
        })
      })
  
      suite(
        'POST /api/books/[id] => add comment/expect book object with id',
        function() {
          test('Test POST /api/books/[id] with comment', function(done) {
            chai
              .request(server)
              .post('/api/books/' + globalId)
              .send({
                comment: 'The first comment of the book'
              })
              .end((err, res) => {
                assert.equal(res.status, 200)
                assert.equal(res.body.title, 'A new book title')
                assert.equal(res.body._id, globalId)
                assert.deepEqual(res.body.comments, ['The first comment of the book'])
                done()
              })
          })
  
          test('Test POST /api/books/[id] with comment but invalid id', done => {
            chai
              .request(server)
              .post('/api/books/5e508e179ca59f0f94033bda')
              .send({ comment: 'This should not work' })
              .end((err, res) => {
                assert.equal(res.status, 404)
                assert.equal(res.body, 'no book exists')
                done()
              })
          })
        }
      )
  
      suite('DELETE /api/books/[id] => Delete a book', () => {
        test('Test DELETE /api/books/[id] with invalid id', done => {
          chai
            .request(server)
            .delete('/api/books/5e508e179ca59f0f94033bda')
            .end((err, res) => {
              assert.equal(res.status, 404)
              assert.equal(res.body, 'no book exists')
              done()
            })
        })
  
        test('Test DELETE /api/books/[id]', done => {
          chai
            .request(server)
            .delete('/api/books/' + globalId)
            .end((err, res) => {
              assert.equal(res.status, 200)
              assert.equal(res.body, 'delete successful')
              chai
                .request(server)
                .get('/api/books/' + globalId)
                .end((err, res) => {
                  assert.equal(res.body, 'no book exists')
                })
              done()
            })
        })
      })
  
      suite('DELETE /api/books => Delete all books', () => {
        before(async function() {
          this.timeout(5000)
          // create new books to test all delete
          let requests = []
          for (let x = 0; x < 5; x++) {
            let fn = chai
              .request(server)
              .post('/api/books')
              .send({ title: 'New test book' + x })
            requests.push(fn)
          }
          await Promise.all(requests)
        })
  
        test('Test DELETE all books', done => {
          chai
            .request(server)
            .delete('/api/books')
            .end((err, res) => {
              assert.equal(res.status, 200)
              assert.equal(res.body, 'complete delete successful')
  
              chai
                .request(server)
                .get('/api/books')
                .end((err, res) => {
                  assert.equal(res.status, 200)
                  assert.deepEqual(res.body, [])
                  done()
                })
            })
        })
      })
    })
  })
  