const { expect } = require('chai')
const { emptyData, wrongDataType, errorData, correctData } = require('./mocks')
const weighSum = require('../index')

describe('Weighted Sum: Error Management', () => {
  it('Should return an empty array if data is empty', () => {
    const ps = weighSum(emptyData)
    expect(ps).to.be.instanceof(Array)
    expect(ps).to.be.empty
  })

  it('Should throw error if data is wrong type', () => {
    const ps = weighSum.bind(null, wrongDataType)
    expect(ps).to.throw(Error, 'Weighted sum: wrong data type. Array is expected')
  })

  it('Should throw an error if a data value is not a number', () => {
    const { source, sortOptions } = errorData
    const ps = weighSum.bind(null, source, sortOptions)
    expect(ps).to.throw(Error, 'Weighted sum: wrong data type for distance. Number is expected')
  })
})

describe('Weighted Sum: Sorting', () => {
  it('Should return an array', () => {
    const { source, sortOptions } = correctData
    const ps = weighSum(source, sortOptions)

    expect(ps).to.be.instanceof(Array)
  })

  it('Should return a sorted array', () => {
    const { source, sortOptions, expected } = correctData
    const ps = weighSum(source, sortOptions)

    expect(ps.length).to.equal(expected.length)
    expect(ps).to.deep.equal(expected)
  })

  it('Should return a sorted array including score if specified in options', () => {
    const { source, sortOptions, expected } = correctData
    sortOptions.includeScore = true

    const ps = weighSum(source, sortOptions)
    const ids = ps.map((res) => res.id)
    const scores = ps.map((res) => res.score)

    expect(ps.length).to.equal(expected.length)
    expect(ids).to.deep.equal(expected)

    expect(scores[0]).to.be.above(scores[1])
    expect(scores[1]).to.be.above(scores[2])
    expect(scores[4]).to.be.below(scores[0])
  })
})

@@ -119,16 +119,17 @@
 st weigh
+t
 Sum = re
@@ -262,33 +262,33 @@
  =%3E %7B%0A    const 
-p
+w
 s = weighSum(emp
@@ -276,24 +276,25 @@
 t ws = weigh
+t
 Sum(emptyDat
@@ -299,33 +299,33 @@
 ata)%0A    expect(
-p
+w
 s).to.be.instanc
@@ -338,33 +338,33 @@
 ray)%0A    expect(
-p
+w
 s).to.be.empty%0A 
@@ -427,33 +427,33 @@
  =%3E %7B%0A    const 
-p
+w
 s = weighSum.bin
@@ -437,32 +437,33 @@
 const ws = weigh
+t
 Sum.bind(null, w
@@ -479,33 +479,33 @@
 ype)%0A    expect(
-p
+w
 s).to.throw(Erro
@@ -683,33 +683,33 @@
 rData%0A    const 
-p
+w
 s = weighSum.bin
@@ -701,16 +701,17 @@
  = weigh
+t
 Sum.bind
@@ -745,25 +745,25 @@
 %0A    expect(
-p
+w
 s).to.throw(
@@ -975,33 +975,33 @@
 tData%0A    const 
-p
+w
 s = weighSum(sou
@@ -985,32 +985,33 @@
 const ws = weigh
+t
 Sum(source, sort
@@ -1027,25 +1027,25 @@
 %0A    expect(
-p
+w
 s).to.be.ins
@@ -1170,33 +1170,33 @@
 tData%0A    const 
-p
+w
 s = weighSum(sou
@@ -1180,32 +1180,33 @@
 const ws = weigh
+t
 Sum(source, sort
@@ -1218,33 +1218,33 @@
 ns)%0A%0A    expect(
-p
+w
 s.length).to.equ
@@ -1274,17 +1274,17 @@
  expect(
-p
+w
 s).to.de
@@ -1498,17 +1498,17 @@
   const 
-p
+w
 s = weig
@@ -1508,16 +1508,17 @@
  = weigh
+t
 Sum(sour
@@ -1546,25 +1546,25 @@
 const ids = 
-p
+w
 s.map((res) 
@@ -1593,17 +1593,17 @@
 cores = 
-p
+w
 s.map((r
@@ -1632,17 +1632,17 @@
  expect(
-p
+w
 s.length

Fix Typos