 # lib/launcher.js module
 #==============================================================================
 describe 'launcher', ->
+  di = require 'di'
   events = require '../../lib/events'
   logger = require '../../lib/logger'
-  loadFile = require('mocks').loadFile
+  launcher = require '../../lib/launcher'
 
-  mockSpawn = sinon.spy (cmd, args) ->
-    process = new events.EventEmitter
-    process.stderr = new events.EventEmitter
-    process.kill = sinon.spy()
-    process.exitCode = null
-    mockSpawn._processes.push process
-    process
+  # mock out id generator
+  lastGeneratedId = null
+  launcher.Launcher.generateId = ->
+    ++lastGeneratedId
 
-  mockRimraf = sinon.spy (p, fn) ->
-    mockRimraf._callbacks.push fn
+  class FakeBrowser
+    constructor: (@id, @name, baseBrowserDecorator) ->
+      baseBrowserDecorator @
+      FakeBrowser._instances.push @
+      sinon.stub @, 'start'
+      sinon.stub @, 'kill'
 
-  mocks =
-    child_process:
-      spawn: mockSpawn
-    './logger': logger
-    '../logger': logger
-    rimraf: mockRimraf
+  class ScriptBrowser
+    constructor: (@id, @name, baseBrowserDecorator) ->
+      baseBrowserDecorator @
+      ScriptBrowser._instances.push @
+      sinon.stub @, 'start'
+      sinon.stub @, 'kill'
 
-  timeouts = [];
-
-  globals =
-    global: global
-    process:
-      nextTick: process.nextTick
-      platform: 'linux'
-      env: TMPDIR: '/temp'
-    setTimeout: timer.setTimeout
-
-  m = loadFile __dirname + '/../../lib/launcher.js', mocks, globals, true
-  e = m.exports
 
   beforeEach ->
-    mockSpawn.reset()
-    mockSpawn._processes = []
-    mockRimraf.reset()
-    mockRimraf._callbacks = []
-    timeouts = []
-
-    # mock out id generator
     lastGeneratedId = 0
-    e.Launcher.generateId = ->
-      ++lastGeneratedId
+    FakeBrowser._instances = []
+    ScriptBrowser._instances = []
 
     # disable logger
     logger.setup 'OFF'


    beforeEach ->
      emitter = new events.EventEmitter()
      l = new e.Launcher emitter

    describe 'launch', ->

      it 'should start all browsers', ->
        l.launch ['Chrome', 'ChromeCanary'], 'localhost', 1234

        expect(mockSpawn).to.have.been.calledTwice
        expect(mockSpawn.getCall(0).args[0]).to.equal 'google-chrome'
        expect(mockSpawn.getCall(1).args[0]).to.equal 'google-chrome-canary'


      it 'should allow launching a script', ->
        l.launch ['/usr/local/bin/special-browser'], 'localhost', 1234, '/'
        expect(mockSpawn).to.have.been.calledWith '/usr/local/bin/special-browser', ['http://localhost:1234/?id=1']


      it 'should use the non default host', ->
        l.launch ['/usr/local/bin/special-browser'], '127.0.0.1', 1234, '/'
        expect(mockSpawn).to.have.been.calledWith '/usr/local/bin/special-browser', ['http://127.0.0.1:1234/?id=1']


    describe 'kill', ->

    describe 'onExit', ->

      it 'should kill all browsers', (done) ->
        l.launch ['Fake', 'Fake'], 'localhost', 1234, '/', 0, 1

        emitter.emitAsync('exit').then done

        browser = FakeBrowser._instances.pop()
        browser.kill.invokeCallback()

        browser = FakeBrowser._instances.pop()
        browser.kill.invokeCallback()
