var assert = require('assert')

global.EventEmitter = require('events').EventEmitter

var EditorChannel = require('../../scripts/editorChannel')

describe('EditorChannel', function() {
	var ec 

	beforeEach(function() {
		global._ = {
			clone: function() {}
		}
		global.E2 = {
			app: {
				dispatcher: {
					register: function() {}
				}
			}
		}

		ec = new EditorChannel()
		ec.wsChannel = {
			send: function() {}
		}
	})

	it('can`t send if not connected', function() {
		ec.connected = false
		assert.equal(ec.canSend(), false)
	})

	it('can`t send if forking', function() {
		ec.connected = true
		ec.forking = true
		assert.equal(ec.canSend(), false)
	})

	it('queues messages', function() {
		ec.connected = false
		ec.send({ actionType: 'uiMouseMoved' })
		ec.send({ actionType: 'uiMouseClicked' })
		assert.equal(ec.queue.length, 2)
	})

	it('processes queue if it can send', function() {
		ec.send({ actionType: 'uiMouseMoved' })
		ec.send({ actionType: 'uiMouseClicked' })
		ec.connected = true
		ec.forking = false
		ec.processQueue()
		assert.equal(ec.queue.length, 0)
	})


})
@@ -23,16 +23,43 @@
 assert')
+%0Avar when = require('when')
 %0A%0Aglobal
@@ -213,16 +213,33 @@
 %09var ec 
+%0A%09var forkPromise
 %0A%0A%09befor
@@ -323,67 +323,280 @@
 %0A%09%09%09
-app: %7B%0A%09%09%09%09dispatcher: %7B%0A%09%09%09%09%09register: function() %7B%7D%0A%09%09%09%09%7D
+ui: %7B%0A%09%09%09%09updateProgressBar: function() %7B%7D%0A%09%09%09%7D,%0A%09%09%09app: %7B%0A%09%09%09%09growl: function() %7B%7D,%0A%09%09%09%09dispatcher: %7B%0A%09%09%09%09%09register: function() %7B%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09global.ForkCommand = function() %7B%0A%09%09%09this.fork = function() %7B%0A%09%09%09%09forkPromise = when.defer()%0A%09%09%09%09return forkPromise.promise
 %0A%09%09%09
@@ -1342,11 +1342,348 @@
 0)%0A%09%7D)%0A%0A
+%09it('replays queued messages after fork', function(done) %7B%0A%09%09var dispatches = 0%0A%09%09ec.fork()%0A%09%09ec.send(%7B actionType: 'uiNodeRemoved' %7D)%0A%09%09ec.send(%7B actionType: 'uiDisconnected' %7D)%0A%09%09assert.equal(ec.queue.length, 2)%0A%09%09forkPromise.resolve()%0A%09%09global.E2.app.dispatcher.dispatch = function(pl) %7B%0A%09%09%09if (++dispatches === 2)%0A%09%09%09%09done()%0A%09%09%7D%0A%09%7D)%0A
 %0A%7D)

add test for replaying after fork