'use strict'

const path = require('path')
const JSUnitSaucelabs = require('jsunitsaucelabs')

// Docs: https://wiki.saucelabs.com/display/DOCS/Platform+Configurator
// Mac Opera is not currently supported by Sauce Labs
// Win Opera 15+ is not currently supported by Sauce Labs
// iOS Chrome is not currently supported by Sauce Labs

const jsUnitSaucelabs = new JSUnitSaucelabs({
  username: process.env.SAUCE_USERNAME,
  password: process.env.SAUCE_ACCESS_KEY,
  build:    process.env.TRAVIS_JOB_ID
})

const testURL      = 'http://localhost:3000/js/tests/index.html?hidepassed'
const browsersFile = require(path.resolve(__dirname, './sauce_browsers.json'))
let jobsDone       = 0
let jobsSucceeded  = 0

const waitingCallback = (error, body, id) => {
  if (error) {
    console.error(error)
    process.exit(1)
  }

  if (typeof body !== 'undefined') {
    if (!body.completed) {
      setTimeout(() => {
        jsUnitSaucelabs.getStatus(id, (error, body) => {
          waitingCallback(error, body, id)
        })
      }, 2000)
    } else {
      const test   = body['js tests'][0]
      let passed   = false
      let errorStr = false

      if (test.result !== null) {
        if (typeof test.result === 'string' && test.result === 'Test exceeded maximum duration') {
          errorStr = test.result
        } else {
          passed = test.result.total === test.result.passed
        }
      }

      console.log(`Tested ${testURL}`)
      console.log(`Platform: ${test.platform.join(',')}`)
      console.log(`Passed: ${passed.toString()}`)
      console.log(`Url ${test.url} \n`)
      if (errorStr) {
        console.error(errorStr)
      }

      if (passed) {
        jobsSucceeded++
      }
      jobsDone++

      // Exit
      if (jobsDone === browsersFile.length - 1) {
        jsUnitSaucelabs.stop()
        process.exit(jobsDone === jobsSucceeded ? 0 : 1)
      }
    }
  }
}

jsUnitSaucelabs.on('tunnelCreated', () => {
  browsersFile.forEach((tmpBrowser) => {
    const browsersPlatform = typeof tmpBrowser.platform === 'undefined' ? tmpBrowser.platformName : tmpBrowser.platform
    const browsersArray = [browsersPlatform, tmpBrowser.browserName, tmpBrowser.version]

    jsUnitSaucelabs.start([browsersArray], testURL, 'qunit', (error, success) => {
      if (typeof success !== 'undefined') {
        const taskIds = success['js tests']

        if (!taskIds || !taskIds.length) {
          throw new Error('Error starting tests through Sauce Labs API')
        }

        taskIds.forEach((id) => {
          jsUnitSaucelabs.getStatus(id, (error, body) => {
            waitingCallback(error, body, id)
          })
        })
      } else {
        console.error(error)
      }
    })
  })
})

jsUnitSaucelabs.initTunnel()

@@ -1,102 +1,205 @@
-'use strict'%0A%0Aconst path = require('path')%0Aconst JSUnitSaucelabs = require('jsunitsaucelabs')%0A%0A// 
+/*!%0A * Script to run our Sauce Labs tests.%0A * Copyright 2017 The Bootstrap Authors%0A * Copyright 2017 Twitter, Inc.%0A * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)%0A */%0A%0A/*%0A
 Docs
@@ -262,19 +262,16 @@
 gurator%0A
-// 
 Mac Oper
@@ -313,19 +313,16 @@
 ce Labs%0A
-// 
 Win Oper
@@ -368,19 +368,16 @@
 ce Labs%0A
-// 
 iOS Chro
@@ -419,16 +419,114 @@
 uce Labs
+%0A*/%0A%0A'use strict'%0A%0Aconst path = require('path')%0Aconst JSUnitSaucelabs = require('jsunitsaucelabs')
 %0A%0Aconst 
@@ -655,19 +655,16 @@
   build:
-   
  process
@@ -700,21 +700,16 @@
 testURL 
-     
 = 'http:
@@ -849,22 +849,16 @@
 obsDone 
-      
 = 0%0Alet 
@@ -871,17 +871,16 @@
 cceeded 
- 
 = 0%0A%0Acon
@@ -1233,18 +1233,16 @@
 st test 
-  
 = body%5B'
@@ -1272,18 +1272,16 @@
  passed 
-  
 = false%0A
@@ -1661,16 +1661,17 @@
 .join(',
+ 
 ')%7D%60)%0A  
@@ -1738,18 +1738,19 @@
 e.log(%60U
-rl
+RL:
  $%7Btest.
@@ -1753,17 +1753,16 @@
 est.url%7D
- 
 %5Cn%60)%0A   

Update saucelabs-unit-test.js