var $require = require('proxyquire');
var expect = require('chai').expect;
var sinon = require('sinon');
var factory = require('../../app/session/store');
var MemoryStore = require('express-session').MemoryStore;


function MockSessionStore(){};

describe('session/store', function() {
  var NODE_ENV;
  
  var _container = {
    components: function(){},
    create: function(){}
  };
  var _logger = {
    emergency: function(){},
    alert: function(){},
    critical: function(){},
    error: function(){},
    warning: function(){},
    notice: function(){},
    info: function(){},
    debug: function(){}
  };
  
  beforeEach(function() {
    NODE_ENV = process.env.NODE_ENV;
  });
  
  afterEach(function() {
    process.env.NODE_ENV = NODE_ENV;
  });
  
  
  it('should export factory function', function() {
    expect(factory).to.be.a('function');
  });
  
  it('should be annotated', function() {
    expect(factory['@singleton']).to.equal(true);
  });
  
  it('should resolve with session store found via service discovery', function(done) {
    sinon.stub(_container, 'components').returns([ { a: { '@name': 'sessions-mock' } } ]);
    var _connect = sinon.stub().yieldsAsync(null, new MockSessionStore());
    
    var promise = factory(_container, _connect, _logger);
    promise.then(function(store) {
      expect(_container.components).to.have.been.calledWith('http://i.bixbyjs.org/http/ISessionStore');
      expect(_connect).to.have.been.calledWith([ 'sessions-mock' ]);
      expect(store).to.be.an.instanceof(MockSessionStore);
      done();
    }).catch(done);
  }); // should resolve with session store found via service discovery
  
  it('should resolve with memory store as last resort in development environment', function(done) {
    sinon.stub(_container, 'components').returns([ { a: { '@name': 'sess-redis' } } ]);
    var error = new Error('querySrv ENOTFOUND sess-redis');
    error.code = 'ENOTFOUND';
    var _connect = sinon.stub().yieldsAsync(error);
    sinon.stub(_container, 'create').returns(new MemoryStore());
    
    process.env.NODE_ENV = 'development';
    var promise = factory(_container, _connect, _logger);
    promise.then(function(store) {
      expect(_container.components).to.have.been.calledWith('http://i.bixbyjs.org/http/ISessionStore');
      expect(_connect).to.have.been.calledWith([ 'sess-redis' ]);
      expect(_container.create).to.have.been.calledOnceWith('./store/memory');
      expect(store).to.be.an.instanceof(MemoryStore);
      done();
    }).catch(done);
  }); // should resolve with memory store as last resort in development environment
  
}); // session/store

@@ -992,48 +992,31 @@
 th s
-ession store found via service discov
+tore from IoC contain
 er
-y
 ', f
@@ -1039,167 +1039,118 @@
     
-sinon.stub(_container, 'components').returns(%5B %7B a: %7B '@name': 'sessions-mock' %7D %7D %5D);%0A    var _connect = sinon.stub().yieldsAsync(null, new MockSessionS
+var store = new Object();%0A    var container = new Object();%0A    container.create = sinon.stub().resolves(s
 tore
-()
 );%0A 
@@ -1152,38 +1152,24 @@
 e);%0A    %0A   
- var promise =
  factory(_co
@@ -1157,33 +1157,32 @@
    %0A    factory(
-_
 container, _conn
@@ -1180,39 +1180,25 @@
 er, 
-_connect, _logger);%0A    promise
+undefined)%0A      
 .the
@@ -1200,37 +1200,35 @@
  .then(function(
-store
+obj
 ) %7B%0A      expect
@@ -1217,32 +1217,33 @@
 bj) %7B%0A      
+  
 expect(
-_
 container.co
@@ -1237,37 +1237,30 @@
 (container.c
-omponents).to
+reate)
 .have.been.c
@@ -1288,33 +1288,32 @@
 ixbyjs.org/http/
-I
 SessionStore');%0A
@@ -1322,130 +1322,41 @@
     
-expect(_connect).to.have.been.calledWith(%5B 'sessions-mock' %5D);%0A      expect(store).to.be.an.instanceof(MockSessionS
+  expect(obj).to.equal(s
 tore);%0A
+  
     
@@ -1357,32 +1357,34 @@
         done();%0A
+  
     %7D).catch(don
@@ -1421,48 +1421,31 @@
 th s
-ession store found via service discov
+tore from IoC contain
 er
-y
 %0A  %0A

Fix tests.