surrsoft
10/1/2019 - 1:00 PM

agent2-2.spec.js

const mongoose = require('mongoose');
const { describe, it } = require('mocha');
const { assert, expect } = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');

const fakeNewAgentAiDriver = {
  createSubscription() {

  },
};
const stub6 = sinon.stub().returns(fakeNewAgentAiDriver);
const AgentSchema = proxyquire('../../src/model/mongoose/Agent', {
  '../../lib/cognitive-driver-sdk': {
    getAIDriver: stub6,
  },
});

//del const AgentSchema = require('../../src/model/mongoose/Agent');
//del const cognitiveDriverSdk = require('../../src/lib/cognitive-driver-sdk');

describe('test', () => {
  const agentModel = mongoose.model('Agent', AgentSchema);

  it('test-it', async () => {
    // -
    const fakeSource = {
      source: {
        getAzureApi() {
          console.log('-> getAzureApi()'); //del
        },
      },
    };
    const fakeAzureApi = {
      getCognitiveServiceSubscriptionData() {
        console.log('-> getCognitiveServiceSubscriptionData()'); //del
      },
    };

    const stub1 = sinon.stub(agentModel, 'findByIdOrThrow').returns(Promise.resolve({ model: { id: 1 } }));
    const stub2 = sinon.stub(agentModel, 'getLuisResource').returns(Promise.resolve(fakeSource));
    const stub3 = sinon.stub(fakeSource.source, 'getAzureApi').returns(
      Promise.resolve(fakeAzureApi)
    );
    const stub4 = sinon.stub(fakeAzureApi, 'getCognitiveServiceSubscriptionData').returns(
      Promise.resolve({ infoSubscriptionData: {} })
    );
    const stub5 = sinon.stub(agentModel, 'getAiApiKey').returns(
      Promise.resolve({ infoApiKey: {} })
    );
    const stub7 = sinon.stub(fakeNewAgentAiDriver, 'createSubscription').returns(Promise.resolve({}));
    //const stub8 = sinon.stub(agentModel, 'getAiQueryKeyByResource').returns(Promise.resolve({ xx: 1 }));

    // --- test

    //del const { queryKey, endpoint } =
    await agentModel.assignSubscription(1, {});

    // ---

    expect(stub1.calledOnce);
    expect(stub2.calledOnce);
    expect(stub3.calledOnce);
    expect(stub4.calledOnce);
    expect(stub5.calledOnce);
    expect(stub6.calledOnce);
    expect(stub7.calledOnce);

    // ---

    stub1.restore();
    stub2.restore();
    stub3.restore();
    stub4.restore();
    stub5.restore();
    stub7.restore();
  });

});