<commit_before>import {} from 'dotenv/config'
import req2Server from 'supertest'
import request from 'superagent'
import test from 'ava'
import server from './../lib/server'
import config from './../superagent-mock.config'
import mock from 'superagent-mock'

const saMock = mock(request, config)

function testHeaderAndNoError (reqUrl) {
  test.cb('There are no errors', t => {
    req2Server(server).get(reqUrl)
      .end((err, res) => {
        t.ifError(err, "Errors aren't allowed")
        t.end()
      })
  })

  test.cb('Expect json response', t => {
    req2Server(server).get(reqUrl)
      .expect(200)
      .expect('Content-Type', 'application/json')
      .end((_, res) => {
        t.end()
      })
  })
}

;(function testEmptyRequest () {
  const reqUrl = '/'

  testHeaderAndNoError(reqUrl)

  test.cb('Expect usage info when emtpy request', t => {
    req2Server(server).get(reqUrl)
      .end((_, res) => {
        t.truthy(res.body.warning, 'Wrong request!')
        t.truthy(res.body.use)
        t.truthy(res.body.options)
        t.truthy(res.body.example1)
        t.end()
      })
  })
}())

;(function testSearchRequest () {
  const reqUrl = '/api?q=search%20query'

  testHeaderAndNoError(reqUrl)

  test.cb('Expect array as response', t => {
    req2Server(server).get(reqUrl)
      .end((_, res) => {
        t.true(Array.isArray(res.body))
        t.end()
      })
  })

  test.cb('Expect respect for the num parameter', t => {
    req2Server(server).get(reqUrl + '&num=1')
      .end((_, res) => {
        t.true(res.body.length === 1)
        t.end()
      })
  })

  test.cb('Expect respect for the offset parameter', t => {
    function notDeepEqualWithOffsetN (data, offset) {
      req2Server(server).get(reqUrl + `&num=1&offset=${offset}`)
        .end((_, res) => {
          t.notDeepEqual(data, res.body)
          t.end()
        })
    }

    req2Server(server).get(reqUrl + '&num=1&offset=1')
      .end((_, res) => {
        return notDeepEqualWithOffsetN(res.body, 2)
      })
  })
}())

;(function testLatestRequest () {
  const reqUrl = '/latest'

  testHeaderAndNoError(reqUrl)

  test.cb('Expect Array as result', t => {
    req2Server(server).get(reqUrl)
      .end((_, res) => {
        t.true(Array.isArray(res.body))
        t.end()
      })
  })
}())

test.cb.after(t => {
  saMock.unset()
  t.end()
})<commit_msg>Fix tests for server.js<commit_after>import {} from 'dotenv/config'
import req2Server from 'supertest'
import request from 'superagent'
import test from 'ava'
import server from './../lib/server'
import config from './../superagent-mock.config'
import mock from 'superagent-mock'
import Query from './../model/query'

const saMock = mock(request, config)

test.cb.after(t => {
  saMock.unset()
  t.end()
})

test.after.always(t => {
  return Query.collection.drop()
})

function testHeaderAndNoError (reqUrl) {
  test.cb('There are no errors', t => {
    req2Server(server).get(reqUrl)
      .end((err, res) => {
        t.ifError(err, "Errors aren't allowed")
        t.end()
      })
  })

  test.cb('Expect json response', t => {
    req2Server(server).get(reqUrl)
      .expect(200)
      .expect('Content-Type', 'application/json')
      .end((_, res) => {
        t.end()
      })
  })
}

;(function testEmptyRequest () {
  const reqUrl = '/'

  testHeaderAndNoError(reqUrl)

  test.cb('Expect usage info when emtpy request', t => {
    req2Server(server).get(reqUrl)
      .end((_, res) => {
        t.truthy(res.body.warning, 'Wrong request!')
        t.truthy(res.body.use)
        t.truthy(res.body.options)
        t.truthy(res.body.example1)
        t.end()
      })
  })
}())

;(function testSearchRequest () {
  const reqUrl = '/api?q=search+query'

  testHeaderAndNoError(reqUrl)

  test.cb('Expect array as response', t => {
    req2Server(server).get(reqUrl)
      .end((_, res) => {
        t.true(Array.isArray(res.body))
        t.end()
      })
  })

  test.cb('Expect respect for the num parameter', t => {
    req2Server(server).get(reqUrl + '&num=1')
      .end((_, res) => {
        t.true(res.body.length === 1)
        t.end()
      })
  })

  test.cb('Expect respect for the offset parameter', t => {
    function notDeepEqualWithOffsetN (data, offset) {
      req2Server(server).get(reqUrl + `&num=1&offset=${offset}`)
        .end((_, res) => {
          t.notDeepEqual(data, res.body)
          t.end()
        })
    }

    req2Server(server).get(reqUrl + '&num=1&offset=1')
      .end((_, res) => {
        return notDeepEqualWithOffsetN(res.body, 2)
      })
  })
}())

;(function testLatestRequest () {
  const reqUrl = '/latest'

  testHeaderAndNoError(reqUrl + '1')

  test.cb('Expect Array as result', t => {
    req2Server(server).get(reqUrl)
      .end((_, res) => {
        t.true(Array.isArray(res.body))
        t.end()
      })
  })
}())
{'fraction_non_alphanumeric': 0.1769436997319035, 'fraction_numerical': 0.008455351618890493, 'mean_word_length': 2.5979228486646884, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5789744', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1699, 'n_words': 319}