
import chai from 'chai'
import {fetchLightnings} from '../src/fmi-lightnings.js'
import dateFns from 'date-fns'

const {expect} = chai
const {setMilliseconds, setMinutes, setSeconds, subMinutes} = dateFns

describe('FMI lightning data set parser', () => {
  it('Should not fail on an empty set of frame dates', async () => {
    const lightnings = await fetchLightnings([])
    expect(lightnings).to.deep.equal([])
  })

  it('Should parse large set of lightning locations from local FMI data', async () => {
    const frameDates = [new Date('2019-06-07T13:55:00Z'), new Date('2019-06-07T14:00:00Z')]
    const lightningFrames = await fetchLightnings(frameDates)
    verifyLightningStructure(lightningFrames, frameDates)
    expect(lightningFrames[0].locations).to.have.lengthOf(1711)
    expect(lightningFrames[1].locations).to.have.lengthOf(1813)
  })

  it('Should find some lightning locations (depending on weather naturally) from FMI API', async () => {
    const time2 = closestFullHour()
    const time1 = subMinutes(time2, 5)
    const frameDates = [time1, time2]
    const lightningFrames = await fetchLightnings(frameDates)
    verifyLightningStructure(lightningFrames, frameDates)
  })
})

function verifyLightningStructure(lightningFrames, frameDates) {
  expect(lightningFrames).to.have.lengthOf(frameDates.length)
  lightningFrames.forEach(lightningFrame => {
    expect(lightningFrame).to.have.property('locations')
    expect(lightningFrame.locations).to.be.an('array')
    lightningFrame.locations.forEach(location => {
      expect(location).to.be.an('array')
      expect(location).to.have.lengthOf(2)
      expect(location[0]).to.be.a('number')
      expect(location[1]).to.be.a('number')
    })
    expect(lightningFrame).to.have.property('timestamp')
    expect(lightningFrame.timestamp).to.be.a('string')
  })
}

function closestFullHour() {
  return setMinutes(setSeconds(setMilliseconds(Date.now(), 0), 0), 0)
}

@@ -647,34 +647,40 @@
 nings(frameDates
+, true
 )%0A
-
     verifyLightn
@@ -717,24 +717,25 @@
 frameDates)%0A
+%0A
     expect(l
@@ -784,12 +784,9 @@
 hOf(
-1711
+0
 )%0A  
@@ -843,18 +843,18 @@
 gthOf(18
-13
+09
 )%0A  %7D)%0A%0A

Fix lightning test (broken a long time ago)