/*!
 * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
 */
/* eslint-disable no-underscore-dangle */
import {assert} from '@webex/test-helper-chai';
import MockWebex from '@webex/test-helper-mock-webex';
import sinon from 'sinon';
import Conversation from '@webex/internal-plugin-conversation';

describe('plugin-conversation', () => {
  describe('Conversation', () => {
    let webex;

    const convoUrl = 'https://conv-test.wbx2.com/conversation';

    beforeEach(() => {
      webex = new MockWebex({
        children: {
          conversation: Conversation
        }
      });

      webex.request = sinon.stub();
      webex.internal.services = {};
      webex.internal.services.get = sinon.stub().returns(Promise.resolve(convoUrl));
      webex.internal.services.getServiceFromClusterId = sinon.stub().returns({url: convoUrl});
    });

    describe('#_inferConversationUrl', () => {
      const testConvo = {test: 'convo'};

      it('Returns given convo if no id', () => webex.internal.conversation._inferConversationUrl(testConvo)
        .then((convo) => {
          assert.notCalled(webex.internal.feature.getFeature);
          assert.notCalled(webex.internal.services.get);
          assert.equal(convo.test, 'convo');
        }));

      describe('HA is disabled', () => {
        beforeEach(() => {
          webex.internal.feature.getFeature = sinon.stub().returns(Promise.resolve(false));
          testConvo.id = 'id1';
        });
        it('returns unmodified convo if URL is defined', () => {
          testConvo.url = 'http://example.com';

          return webex.internal.conversation._inferConversationUrl(testConvo)
            .then((convo) => {
              assert.called(webex.internal.feature.getFeature);
              assert.notCalled(webex.internal.services.get);
              assert.equal(convo.url, 'http://example.com');
            });
        });
        it('builds URL if not defined', () => {
          delete testConvo.url;

          return webex.internal.conversation._inferConversationUrl(testConvo)
            .then((convo) => {
              assert.called(webex.internal.feature.getFeature);
              assert.called(webex.internal.services.get);
              assert.equal(convo.url, `${convoUrl}/conversations/id1`);
            });
        });
      });
      describe('HA is enabled', () => {
        beforeEach(() => {
          webex.internal.feature.getFeature = sinon.stub().returns(Promise.resolve(true));
          testConvo.id = 'id1';
        });
        it('builds URL if already defined', () => {
          testConvo.url = 'https://example.com';

          return webex.internal.conversation._inferConversationUrl(testConvo)
            .then((convo) => {
              assert.called(webex.internal.feature.getFeature);
              assert.called(webex.internal.services.get);
              assert.equal(convo.url, `${convoUrl}/conversations/id1`);
            });
        });
        it('builds URL if not defined', () => {
          delete testConvo.url;

          return webex.internal.conversation._inferConversationUrl(testConvo)
            .then((convo) => {
              assert.called(webex.internal.feature.getFeature);
              assert.called(webex.internal.services.get);
              assert.equal(convo.url, `${convoUrl}/conversations/id1`);
            });
        });
      });
    });

    describe('getConvoUrl', () => {
      it('should not return a promise', () => {
        try {
          webex.internal.conversation.getConvoUrl({url: 'convoUrl'}).then();
        }
        catch (error) {
          assert.equal(error.message, 'webex.internal.conversation.getConvoUrl(...).then is not a function');
        }
      });

      it('should return the url if a url is provided', () => {
        const url = webex.internal.conversation.getConvoUrl({url: convoUrl});

        assert.equal(url, convoUrl);
      });
    });

    describe('#getUrlFromClusterId', () => {
      it('should convert a "us" cluster to WEBEX_CONVERSATION_DEFAULT_CLUSTER cluster', async () => {
        await webex.internal.conversation.getUrlFromClusterId({cluster: 'us'});

        sinon.assert.calledWith(webex.internal.services.getServiceFromClusterId, {clusterId: process.env.WEBEX_CONVERSATION_DEFAULT_CLUSTER});
      });

      it('should add the cluster service when missing', async () => {
        await webex.internal.conversation.getUrlFromClusterId({cluster: 'urn:TEAM:us-west-2_r'});

        sinon.assert.calledWith(webex.internal.services.getServiceFromClusterId, {clusterId: 'urn:TEAM:us-west-2_r:identityLookup'});
      });
    });

    describe('paginate', () => {
      it('should throw an error if a page is passed with no links', () => {
        try {
          webex.internal.conversation.paginate({page: {}});
        }
        catch (error) {
          assert.equal(error.message, 'No link to follow for the provided page');
        }
      });
    });

    describe('#getReactionSummaryByParentId()', () => {
      it('should call request', () => {
        webex.internal.conversation.getReactionSummaryByParentId(convoUrl, 'test-id')
          .then(() => {
            assert.called(webex.request);
          });
      });
    });
  });
});

@@ -594,44 +594,8 @@
 );%0A%0A
-      webex.request = sinon.stub();%0A
     
@@ -4983,191 +4983,856 @@
     
-it('should call request', () =%3E %7B%0A        webex.internal.conversation.getReactionSummaryByParentId(convoUrl, 'test-id')%0A          .then(() =%3E %7B%0A            assert.called(webex.request
+beforeEach(() =%3E %7B%0A        webex.request = sinon.stub().returns(Promise.resolve(%7B%0A          body: %7B%0A            children: %5B%0A              %7Btype: 'reactionSelfSummary'%7D,%0A              %7Btype: 'reactionSelfSummary'%7D,%0A              %7Btype: 'reactionSummary'%7D,%0A              %7Btype: 'notAReaction'%7D%0A            %5D%0A          %7D%0A        %7D));%0A      %7D);%0A%0A      it('should call request', () =%3E %7B%0A        webex.internal.conversation.getReactionSummaryByParentId(convoUrl, 'test-id')%0A          .then(() =%3E %7B%0A            assert.called(webex.request);%0A          %7D);%0A      %7D);%0A%0A      it('should not retrieve non reaction summary objects', () =%3E %7B%0A        webex.internal.conversation.getReactionSummaryByParentId(convoUrl, 'test-id')%0A          .then((result) =%3E %7B%0A            assert.equal(result.length, 3);%0A            assert.notInclude(result, %7Btype: 'notAReaction'%7D
 );%0A 

add additional reactionSummary unit tests