{"size": 3243, "ext": "js", "max_stars_repo_path": "test/channel.js", "max_stars_repo_name": "kassi-tech/thread-bus", "max_stars_repo_stars_event_min_datetime": "2016-04-23T22:53:33.000Z", "max_stars_repo_stars_event_max_datetime": "2016-04-23T22:53:33.000Z", "max_issues_repo_path": "test/channel.js", "max_issues_repo_name": "kassi-tech/thread-ms", "max_issues_repo_issues_event_min_datetime": "2016-07-11T21:44:42.000Z", "max_issues_repo_issues_event_max_datetime": "2016-07-11T22:05:12.000Z", "max_forks_repo_path": "test/channel.js", "max_forks_repo_name": "kassi-tech/thread-ms", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 29.4818181818, "max_line_length": 113, "alphanum_fraction": 0.5559666975}
'use strict';

var Thread = require('../index.js');
var expect = require('chai').expect;
var simple = require('simple-mock');

describe('Thread', () => {
  describe('channels', () => {
    let channelOptions = {};

    beforeEach((done) => {
      channelOptions = {};
      done();
    });

    afterEach((done) => {
      Thread.disconnect();
      simple.restore();
      done();
    });

    it('should create exchange point', (done) => {
      channelOptions = {
        channel: 'test'
      };

      Thread.connect().then(() => {
        return Thread.open(channelOptions);
      }).then((result) => {
        expect(result.status).to.equal('open');
        expect(result.channel).to.equal(channelOptions.channel);
        expect(Thread.channels[channelOptions.channel]).to.exist;
        expect(Thread.channels[channelOptions.channel]).to.be.object;
        expect(Thread.channels[channelOptions.channel].state).to.equal('open');
        expect(Thread.channels[channelOptions.channel].name).to.equal(channelOptions.channel);
        done();
      }).catch(done);
    });

    it('should destroy exchange point', (done) => {
      channelOptions = {
        channel: 'test'
      };

      Thread.connect().then(() => {
        return Thread.open(channelOptions);
      }).then(() => {
        return Thread.close(channelOptions.channel);
      }).then((result) => {
        expect(result.status).to.equal('destroyed');
        expect(result.channel).to.equal(channelOptions.channel);
        expect(Thread.channels[channelOptions.channel]).to.equal(null);
        done();
      }).catch(done);
    });

    it('should not destroy exchange point', (done) => {
      let errorMessage = new Error('Specified channel doesn\'t exist');

      Thread.connect().then(() => {
        return Thread.close('test');
      }).then(done).catch((error) => {
        expect(error).to.deep.equal(errorMessage);
        done();
      });
    });

    it('should listen channel', (done) => {
      let channelOptions = {
        channel: 'test',
        listener: function () { }
      };

      Thread.connect().then(() => {
        return Thread.listen(channelOptions);
      }).then((ans) => {
        expect(ans.status).to.equal('subscribed');
        expect(ans.channel).to.equal(channelOptions.channel);
        done();
      }).catch(done);
    });

    it('should reject listener with error', (done) => {
      let errorMessage = 'Queue doesn\'t exist';
      let channelOptions = {
        channel: 'test',
        listener: function () { }
      };

      Thread.connect().then(() => {
        simple.mock(Thread.connection, 'queue').callbackWith(null);
        return Thread.listen(channelOptions);
      }).then(done).catch((error) => {
        expect(error.message).to.equal(errorMessage);
        done();
      });
    });

    it('test', (done) => {
      Thread.connect().then(() => {
        return Thread.listen({ channel: 'test-queue', listener: function (a, b) { console.log('LOL', a, b); } });
      }).then(() => {
        Thread.publish({ channel: 'test-queue', body: 'lol', options: { replyTo: '' } });
        Thread.publish({ channel: 'test-queue', body: 'lol2' });
        setTimeout(done, 1000);
      });
    });
  });
});
