/**
 * SauceLabs Jasmine CapabilityRunner 
 * CapabilityRunner.js 0.0.0
 * (c) 2017 Omar Alshaker, Marcin Warpechowski
 * MIT license
 */

const SauceLabs = require("saucelabs").default;
const webdriver = require("selenium-webdriver");
const Promise = require("bluebird");
const retryUntil = require("bluebird-retry");

function CapabilityRunner(caps) {
  return new Promise(function(resolve, reject) {
    console.log("");
    console.log(caps.name.green);

    const username = caps.username;
    const accessKey = caps.accessKey;

    const saucelabs = new SauceLabs({
      username: username,
      password: accessKey
    });

    const By = webdriver.By;

    let driver = new webdriver.Builder()
      .withCapabilities(caps)
      .usingServer(
        "http://" + username + ":" + accessKey + "@localhost:4445/wd/hub"
      )
      .build();

    driver.get(
      "http://localhost:5000/test/index.html"
    );

    const symbols = { passed: "√", pending: "-", failed: "x" };

    function checkIfDone() {
      return new Promise(function(resolve, reject) {
        driver
          .executeScript("return window.jsApiReporter && window.jsApiReporter.finished && Object.values(window.jsApiReporter.suites());")
          .then(function(results) {
            if (results) {
              resolve(results);
            } else {
              reject();
            }
          });
      });
    }
    /* get session ID and keep checking if tests are finished */
    driver.getSession().then(sessionID => {
      /*set driver ID to end session later */
      driver.sessionID = sessionID.id_;
      retryUntil(checkIfDone, { interval: 15000 }).then(testResults => {
        console.log("Specs finished");
        analyzeResults(testResults);
      }).catch(error => {
        console.log(`${caps.name}: ${error}`);
        process.exit(1);
      });
    });

    async function analyzeResults(results) {
      const resultsSummary = { passed: 0, pending: 0, failed: 0 };
      const colorMap = { passed: "green", failed: "red", pending: "yellow" };
      var hadErrored = 0;
      results.forEach(spec => {
        resultsSummary[spec.status]++;
        console.log("");
        console.log(
          "   " + symbols[spec.status][colorMap[spec.status]] + " " + spec.fullName
        );
        if (spec.status === "failed") {
          hadErrored = 1;
          console.log(`Spec "${spec.fullName}" failed, the error was`, spec.failedExpectations);
        }
      });
      console.log("");
      console.log(
        ("Summary for (" + caps.name + ")")[hadErrored ? "red" : "green"]
      );
      console.log(resultsSummary);
      console.log("");
      console.log("Ending session: " + driver.sessionID);

      const result = {
        name: "Summary: Passed: " +
          resultsSummary.passed +
          ", pending: " +
          resultsSummary.pending +
          ", failed: " +
          resultsSummary.failed,
        passed: hadErrored === 0
      };

      driver.quit();

      await saucelabs.updateJob(username, driver.sessionID, result);
      resolve();
    }
  });
}

module.exports = CapabilityRunner;

@@ -34,17 +34,16 @@
 tyRunner
- 
 %0A * Capa
@@ -445,14 +445,8 @@
 name
-.green
 );%0A%0A
@@ -1165,22 +1165,8 @@
  && 
-Object.values(
 wind
@@ -1187,16 +1187,14 @@
 er.s
-uite
+pec
 s()
-)
 ;%22)%0A
@@ -1954,86 +1954,8 @@
  %7D;%0A
-      const colorMap = %7B passed: %22green%22, failed: %22red%22, pending: %22yellow%22 %7D;%0A
     
@@ -2051,33 +2051,8 @@
 ++;%0A
-        console.log(%22%22);%0A
     
@@ -2110,31 +2110,8 @@
 tus%5D
-%5BcolorMap%5Bspec.status%5D%5D
  + %22
@@ -2412,38 +2412,8 @@
 %22)%22)
-%5BhadErrored ? %22red%22 : %22green%22%5D
 %0A   

change way the Selenium runner receives Jasmine status