// Inspired from https://github.com/zgreen/lighthouse-circleci-example/blob/master/lighthouse.test.js
const lighthouse = require('lighthouse')
const chromeLauncher = require('chrome-launcher')

const TIME_FOR_LIGHTHOUSE_FULL_TEST = 60000
jest.setTimeout(TIME_FOR_LIGHTHOUSE_FULL_TEST)

const launchChromeAndRunLighthouse = async url => {
  const chrome = await chromeLauncher.launch({ chromeFlags: [] })
  const opts = { chromeFlags: [], port: chrome.port }
  const configJSON = null
  const results = await lighthouse(url, opts, configJSON)
  await chrome.kill()
  return results
}

const buildLighthouseReport = results => {
  const { categories } = results.lhr
  let rows = ''
  const scores = Object.values(categories).map(({ score }) => score * 100)
  const lighthouseScore = scores.reduce((a, b) => a + b, 0) / scores.length

  Object.values(categories).forEach(cat => {
    rows += `| ${cat.title} | ${cat.score * 100} |\n`
  })
  console.log('Total Lighthouse score is : ', lighthouseScore)
  console.log(rows)

  return { lighthouseScore, categories }
}

test('Article 85 : Meaningful first paint score', () =>
  launchChromeAndRunLighthouse('https://french-test.recontact.me/articles/85')
    .then(buildLighthouseReport)
    .then(report => {
      const { categories, lighthouseScore } = report

      expect(lighthouseScore).toBeGreaterThanOrEqual(75) // 78
      expect(categories.performance.score * 100).toBeGreaterThanOrEqual(60) // 34, 64, 65
      expect(categories.pwa.score * 100).toBeGreaterThanOrEqual(56) // 56
      expect(categories.accessibility.score * 100).toBeGreaterThanOrEqual(84) // 84
      expect(categories['best-practices'].score * 100).toBeGreaterThanOrEqual(86) // 77, 86
      expect(categories.seo.score * 100).toBeGreaterThanOrEqual(100) // 100
    }))

test('HomePage : Meaningful first paint score', () =>
  launchChromeAndRunLighthouse('https://french-test.recontact.me')
    .then(buildLighthouseReport)
    .then(report => {
      const { categories, lighthouseScore } = report

      expect(lighthouseScore).toBeGreaterThanOrEqual(83) // 86
      expect(categories.performance.score * 100).toBeGreaterThanOrEqual(85) // 93, 95, 90
      expect(categories.pwa.score * 100).toBeGreaterThanOrEqual(59) // 59
      expect(categories.accessibility.score * 100).toBeGreaterThanOrEqual(96) // 96
      expect(categories['best-practices'].score * 100).toBeGreaterThanOrEqual(86) // 92, 86
      expect(categories.seo.score * 100).toBeGreaterThanOrEqual(100) // 100
    }))

test('Static Page : Meaningful first paint score', () =>
  launchChromeAndRunLighthouse('https://french-test.recontact.me/browserconfig.xml')
    .then(buildLighthouseReport)
    .then(report => {
      const { categories, lighthouseScore } = report

      expect(lighthouseScore).toBeGreaterThanOrEqual(47) // 47
      expect(categories.performance.score * 100).toBeGreaterThanOrEqual(100) // 100
      expect(categories.pwa.score * 100).toBeGreaterThanOrEqual(44) // 44
      expect(categories.accessibility.score * 100).toBeGreaterThanOrEqual(0) // 0
      expect(categories['best-practices'].score * 100).toBeGreaterThanOrEqual(92) // 92, 93
      expect(categories.seo.score * 100).toBeGreaterThanOrEqual(0) // 0
    }))

@@ -1457,82 +1457,30 @@
 , 65
-%0A      expect(categories.pwa.score * 100).toBeGreaterThanOrEqual(56) // 56
+, 64 (-4 just in case)
 %0A   
@@ -1642,21 +1642,17 @@
 ual(
-86
+93
 ) // 
-77, 86
+93
 %0A   
@@ -1732,722 +1732,78 @@
     
-%7D))%0A%0Atest('HomePage : Meaningful first paint score', () =%3E%0A  launchChromeAndRunLighthouse('https://french-test.recontact.me')%0A    .then(buildLighthouseReport)%0A    .then(report =%3E %7B%0A      const %7B categories, lighthouseScore %7D = report%0A%0A      expect(lighthouseScore).toBeGreaterThanOrEqual(83) // 86%0A      expect(categories.performance.score * 100).toBeGreaterThanOrEqual(85) // 93, 95, 90%0A      expect(categories.pwa.score * 100).toBeGreaterThanOrEqual(59) // 59%0A      expect(categories.accessibility.score * 100).toBeGreaterThanOrEqual(9
+  expect(categories.pwa.score * 100).toBeGreaterThanOrEqual(5
 6) // 
-9
+5
 6%0A
-      expect(categories%5B'best-practices'%5D.score * 100).toBeGreaterThanOrEqual(86) // 92, 86%0A      expect(categories.seo.score * 100).toBeGreaterThanOrEqual(100) // 100%0A
     
@@ -1817,15 +1817,12 @@
 st('
-Static 
+Home
 Page
@@ -1893,37 +1893,38 @@
 thouse('https://
-frenc
+englis
 h-test.recontact
@@ -1930,26 +1930,8 @@
 t.me
-/browserconfig.xml
 ')%0A 
@@ -2095,17 +2095,17 @@
 ual(
-47
+83
 ) // 
-47
+86
 %0A   
@@ -2169,35 +2169,64 @@
 ThanOrEqual(
-100
+85
 ) // 
-100
+94, 93, 95, 90 (-10 just in case)
 %0A      expec
@@ -2230,35 +2230,45 @@
 pect(categories.
-pwa
+accessibility
 .score * 100).to
@@ -2292,17 +2292,17 @@
 ual(
-44
+96
 ) // 
-44
+96
 %0A   
@@ -2317,38 +2317,42 @@
 t(categories
-.accessibility
+%5B'best-practices'%5D
 .score * 100
@@ -2372,31 +2372,33 @@
 ThanOrEqual(
-0
+93
 ) // 
-0
+93
 %0A      expec
@@ -2409,34 +2409,20 @@
 tegories
-%5B'best-practices'%5D
+.seo
 .score *
@@ -2454,21 +2454,19 @@
 ual(
-92
+100
 ) // 
-92, 93
+100
 %0A   
@@ -2478,35 +2478,35 @@
 pect(categories.
-seo
+pwa
 .score * 100).to
@@ -2530,15 +2530,17 @@
 ual(
-0
+59
 ) // 
-0
+59
 %0A   
@@ -2536,16 +2536,17 @@
 ) // 59%0A    %7D))%0A
+%0A

improve lighthouse efficiency