surrsoft
10/2/2019 - 10:18 AM

agent2-intermediate.spec.js

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

// ---
const fakeNewAgentAiDriver = {
  createSubscription() {

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

const agentModel = mongoose.model('Agent', AgentSchema);

// --- data
const fakeReturnSource = {
  source: {
    getAzureApi() {
    },
  },
};
const fakeReturnAzureApi = {
  getCognitiveServiceSubscriptionData() {
  },
};
const fakeReturnSubscriptionData = {
  subscriptionId: '9b5b46980bde4371893d7fd7b4b82d68',
  region: 'westus2',
  keyPath:
    '/subscriptions/9911f06d-b361-49a8-ae5e-a72e0816db00/resourceGroups/CognitiveOpsDevGroup/providers/Microsoft.CognitiveServices/accounts/CognitiveOpsDevLuis',
  endpoint: 'https://westus2.api.cognitive.microsoft.com/luis/v2.0',
};
const predictionResource = {
  subscriptionId: '9911f06d-b361-49a8-ae5e-a72e0816db00',
  resourceGroupName: 'CognitiveOpsDevGroup',
  name: 'CognitiveOpsDevLuis',
  type: 'Microsoft.CognitiveServices/accounts',
  id:
    '/subscriptions/9911f06d-b361-49a8-ae5e-a72e0816db00/resourceGroups/CognitiveOpsDevGroup/providers/Microsoft.CognitiveServices/accounts/CognitiveOpsDevLuis'
};
const agentId = '5d8b7d6a94a8e4003b844fec';
const queryKeyIn = '0e1df8210db74eed92c80a82f34c3a8d';
const endpointIn = 'https://westus2.api.cognitive.microsoft.com/luis/v2.0';
const clientId = undefined;
const agentPlatform = 'luis';
const apiKey = '30933a3603df43ae8da2370d2b6dea9b';
const agentModelId = 'a5892dcc-84f4-468c-92ec-7b67deffb0bf';

describe('assignSubscription', () => {

  // --- stubs
  const stubFindByIdOrThrow = sinon.stub(agentModel, 'findByIdOrThrow')
    .returns(Promise.resolve({
      agent: { client: clientId },
      model: { id: agentModelId },
      platform: agentPlatform,
    }));
  const stubGetLuisResource = sinon.stub(agentModel, 'getLuisResource').returns(Promise.resolve(fakeReturnSource));
  const stubAzureApi = sinon.stub(fakeReturnSource.source, 'getAzureApi').returns(
    Promise.resolve(fakeReturnAzureApi),
  );
  const stubGetCognitiveServiceSubscriptionData = sinon.stub(fakeReturnAzureApi, 'getCognitiveServiceSubscriptionData').returns(
    Promise.resolve(fakeReturnSubscriptionData), // endpoint value
  );
  const stubGetAiApiKey = sinon.stub(agentModel, 'getAiApiKey').returns(
    Promise.resolve(apiKey),
  );
  const stubCreateSubscription = sinon.stub(fakeNewAgentAiDriver, 'createSubscription').returns(Promise.resolve({}));
  const stubGetAiQueryKeyByResource = sinon.stub(agentModel, 'getAiQueryKeyByResource').returns(
    Promise.resolve(queryKeyIn),
  );

  // ---
  let queryKey;
  let endpoint;
  beforeEach(async () => {
    const res = await agentModel.assignSubscription(agentId, predictionResource);
    queryKey = res.queryKey;
    endpoint = res.endpoint;
  });

  after(() => {
    // [stubFindByIdOrThrow,
    //   stubGetLuisResource,
    //   stubAzureApi,
    //   stubGetCognitiveServiceSubscriptionData,
    //   stubGetAiApiKey,
    //   stubGetAIDriver,
    //   stubCreateSubscription,
    //   stubGetAiQueryKeyByResource
    // ].forEach((stub) => {
    //   console.log('!!-!!-!! stub {10/2/19-1:13 PM}\n', stub); //del
    //   stub.reset();
    //   stub.restore();
    // });

    // stubFindByIdOrThrow.reset();
    stubFindByIdOrThrow.restore();
    // stubGetLuisResource.reset();
    stubGetLuisResource.restore();
    // stubAzureApi.reset();
    stubAzureApi.restore();
    // stubGetCognitiveServiceSubscriptionData.reset();
    stubGetCognitiveServiceSubscriptionData.restore();
    // stubGetAiApiKey.reset();
    stubGetAiApiKey.restore();
    // stubGetAIDriver.reset();
    stubGetAIDriver.restore();
    // stubCreateSubscription.reset();
    stubCreateSubscription.restore();
    // stubGetAiQueryKeyByResource.reset();
    stubGetAiQueryKeyByResource.restore();

  });

  // --- it's

  it(`should called with param agentId = ${agentId}`, () => {
    expect(stubFindByIdOrThrow).calledWithExactly(agentId);
  });
  it('it-2', () => {
    expect(stubGetLuisResource).calledWithExactly(clientId);
  });
  it('it-3', () => {
    expect(stubAzureApi).calledWithExactly();
  });
  it('it-4', () => {
    expect(stubGetCognitiveServiceSubscriptionData).calledWithExactly(predictionResource);
  });
  it('it-5', () => {
    expect(stubGetAiApiKey).calledWithExactly(clientId);
  });
  it('it-6', () => {
    expect(stubGetAIDriver).calledWithExactly(agentPlatform, apiKey);
  });
  it('it-7', () => {
    expect(stubCreateSubscription).calledWithExactly(fakeReturnSubscriptionData, agentModelId);
  });
  it('it-8', () => {
    expect(stubGetAiQueryKeyByResource).calledWithExactly(clientId, predictionResource);
  });
  it('it-9', () => {
    expect(queryKey).to.be.equal(queryKeyIn);
    expect(endpoint).to.be.equal(endpointIn);
  });

});