var Application = require('spectron').Application
var chai = require('chai')
var chaiAsPromised = require('chai-as-promised')
var path = require('path')
var glob = require("glob")


var isWin = /^win/.test(process.platform);
var appExecutablePath = 'dist/mac/OpenTrons.app/Contents/MacOS/OpenTrons'
if (isWin) {
  var detectedExes = glob.sync('releases\\*.exe');
  appExecutablePath = detectedExes[0];
  console.log('App exes on windows', detectedExes, appExecutablePath)
}
process.env.DEBUG = 'true'


chai.should()
chai.use(chaiAsPromised)

describe('application launch', function () {
  beforeEach(function () {
    this.app = new Application({
      path: appExecutablePath
    })
    return this.app.start()
  })

  beforeEach(function () {
    chaiAsPromised.transferPromiseness = this.app.transferPromiseness
  })

  afterEach(function () {
    if (this.app && this.app.isRunning()) {
      return this.app.stop()
    }
  })

  // it('opens a window', function () {
  //   return this.app.client.waitUntilWindowLoaded(5000)
  //     .getWindowCount().should.eventually.equal(1)
  //     .browserWindow.isMinimized().should.eventually.be.false
  //     .browserWindow.isDevToolsOpened().should.eventually.be.false
  //     .browserWindow.isVisible().should.eventually.be.true
  //     .browserWindow.isFocused().should.eventually.be.true
  //     .browserWindow.getBounds().should.eventually.have.property('width').and.be.above(0)
  //     .browserWindow.getBounds().should.eventually.have.property('height').and.be.above(0)
  // })

  it('runs a protocol', function () {
    var file = path.join(__dirname, '..', 'server', 'tests', 'data', '/protocol.py')
    var pauseTime = process.env.PAUSE_TIME || 0
    console.log('TEST GOT HERE...')
    return this.app.client.waitUntilWindowLoaded(5000)
      .click('//*[@id="connections"]/option[2]')
      .pause(pauseTime)
      .click('.btn-connect')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .chooseFile('//*[@id="task-pane"]/div/section/div[2]/form/div/input', file)
      .pause(1000)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('.next')
      .pause(pauseTime)
      .click('//*[@id="task-pane"]/div/section/div[2]/div[2]/button[1]')
  })
})

@@ -175,16 +175,19 @@
 lob%22)%0A%0A%0A
+// 
 var isWi
@@ -221,16 +221,19 @@
 tform);%0A
+// 
 var appE
@@ -298,16 +298,19 @@
 nTrons'%0A
+// 
 if (isWi
@@ -314,16 +314,19 @@
 sWin) %7B%0A
+// 
   var de
@@ -368,16 +368,19 @@
 .exe');%0A
+// 
   appExe
@@ -410,16 +410,19 @@
 xes%5B0%5D;%0A
+// 
   consol
@@ -483,18 +483,450 @@
 lePath)%0A
+// %7D%0A%0Alet appPath;%0Aif (process.platform === 'win32') %7B%0A  appPath = path.resolve(%0A    __dirname,%0A    '../node_modules/electron-prebuilt/dist/Electron.exe'%0A  );%0A%7D else if (process.platform === 'darwin') %7B%0A  appPath = path.resolve(%0A    __dirname,%0A    '../node_modules/electron-prebuilt/dist/Electron.app/Contents/MacOS/Electron'%0A  );%0A%7D else %7B%0A  appPath = path.resolve(%0A    __dirname,%0A    '../node_modules/electron/dist/electron'%0A  );%0A
 %7D%0A
+%0A
 process.
@@ -1102,16 +1102,29 @@
    path:
+ appPath,  //
  appExec
@@ -1134,16 +1134,38 @@
 blePath%0A
+      args: %5B'./app'%5D%0A
     %7D)%0A 

test uncompiled app