var _ = require('underscore');
var assert = require('chai').assert;
var sinon = require('sinon');
var EventEmitter = require('events');
var ProxyConnection = require('../../lib/proxy-connection');
var PluginStreaming = require('../../lib/plugin/streaming');


var Logger = require('../../lib/logger');
var serviceLocator = require('../../lib/service-locator');
serviceLocator.register('logger', function() {
  return new Logger();
});
serviceLocator.register('streams', function() {
  return new EventEmitter;
});

describe('ProxyConnection', function() {

  it('create/destroy session', function() {
    ////////////////// create ////////////////////////
    var proxy = new ProxyConnection();

    var createRequest = {
      janus: 'create',
      token: 'token',
      transaction: proxy._generateTransactionId()
    };
    var createResponse = {
      janus: 'success',
      data: {id: 'id'},
      transaction: createRequest.transaction
    };
    proxy.processMessage(createRequest);
    proxy.processMessage(createResponse);

    assert.equal(proxy.sessionId, createResponse.data.id);
    assert.equal(proxy.sessionData, createRequest.token);

    ////////////////// destroy ////////////////////////
    sinon.stub(proxy, 'close');
    var destroyRequest = {
      janus: 'destroy',
      transaction: proxy._generateTransactionId()
    };
    var destroyResponse = {
      transaction: destroyRequest.transaction
    };

    assert(!proxy.close.calledOnce);
    proxy.processMessage(destroyRequest);
    proxy.processMessage(destroyResponse);
    assert(proxy.close.calledOnce);
  });

  it('Attach plugin', function() {
    var proxy = new ProxyConnection();
    var pluginName = _.invert(ProxyConnection.pluginTypes)[PluginStreaming];
    var attachRequest = {
      janus: 'attach',
      plugin: pluginName,
      transaction: proxy._generateTransactionId()
    };
    var attachResponse = {
      janus: 'success',
      data: {id: 'id'},
      transaction: attachRequest.transaction
    };

    proxy.processMessage(attachRequest);
    proxy.processMessage(attachResponse);

    assert(proxy.getPlugin(attachResponse.data.id) instanceof PluginStreaming);
  });

  it('stop stream', function(done) {
    var pluginStub = {id: 'id', stream: {id: 'streamId'}};
    var janusConnection = {
      send: function(message) {
        assert.equal(message['janus'], 'message');
        assert.equal(message['body']['request'], 'stop');
        assert.equal(message['handle_id'], pluginStub.id);
        done();
      }
    };
    var proxy = new ProxyConnection(null, janusConnection);

    proxy.plugins[pluginStub.id] = pluginStub;
    proxy.stopStream('streamId');
  });
});

@@ -550,16 +550,1391 @@
 on() %7B%0A%0A
+  it('message processing. onCreate.', function() %7B%0A    var proxy = new ProxyConnection();%0A    var onCreateStub = sinon.stub(proxy, 'onCreate', function() %7B%0A      return Promise.resolve();%0A    %7D);%0A    var createRequest = %7B%0A      janus: 'create',%0A      token: 'token',%0A      transaction: proxy._generateTransactionId()%0A    %7D;%0A    proxy.processMessage(createRequest);%0A%0A    assert(onCreateStub.calledOnce);%0A    assert(onCreateStub.calledWith(createRequest));%0A  %7D);%0A%0A  it('message processing. onDestroy.', function() %7B%0A    var proxy = new ProxyConnection();%0A    var onDestroyStub = sinon.stub(proxy, 'onDestroy', function() %7B%0A      return Promise.resolve();%0A    %7D);%0A    var destroyRequest = %7B%0A      janus: 'destroy',%0A      transaction: proxy._generateTransactionId()%0A    %7D;%0A    proxy.processMessage(destroyRequest);%0A%0A    assert(onDestroyStub.calledOnce);%0A    assert(onDestroyStub.calledWith(destroyRequest));%0A  %7D);%0A%0A  it('message processing. onAttach.', function() %7B%0A    var proxy = new ProxyConnection();%0A%0A    var onAttachStub = sinon.stub(proxy, 'onAttach', function() %7B%0A      return Promise.resolve();%0A    %7D);%0A    var attachRequest = %7B%0A      janus: 'attach',%0A      plugin: 'plugin',%0A      transaction: proxy._generateTransactionId()%0A    %7D;%0A    proxy.processMessage(attachRequest);%0A    assert(onAttachStub.calledOnce);%0A    assert(onAttachStub.calledWith(attachRequest));%0A  %7D);%0A%0A
   it('cr

Add basic tests for processMessage