surrsoft
10/2/2019 - 12:35 PM

agent2.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: sinon.stub().returns(Promise.resolve({})),
};
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 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 fakeReturnAzureApi = {
  getCognitiveServiceSubscriptionData: sinon.stub().returns(fakeReturnSubscriptionData),
};
const fakeReturnSource = {
  source: {
    getAzureApi: sinon.stub().returns(fakeReturnAzureApi),
  },
};
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';
const fakeReturnFindByIdOrThrow = {
  agent: { client: clientId },
  model: { id: agentModelId },
  platform: agentPlatform,
};

// ---
describe('assignSubscription', () => {

  // --- stubs agentModel methods
  sinon.stub(agentModel, 'findByIdOrThrow').returns(Promise.resolve(fakeReturnFindByIdOrThrow));
  sinon.stub(agentModel, 'getLuisResource').returns(Promise.resolve(fakeReturnSource));
  sinon.stub(agentModel, 'getAiApiKey').returns(Promise.resolve(apiKey));
  sinon.stub(agentModel, 'getAiQueryKeyByResource').returns(Promise.resolve(queryKeyIn));

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

  after(() => {
    [agentModel.findByIdOrThrow,
      agentModel.getLuisResource,
      //fakeReturnSource.source.getAzureApi, //wht
      //fakeReturnAzureApi.getCognitiveServiceSubscriptionData, //wht
      agentModel.getAiApiKey,
      //fakeNewAgentAiDriver.createSubscription, //wht
      agentModel.getAiQueryKeyByResource,
    ].forEach((stub) => {
      stub.reset();
      stub.restore();
    });
  });

  // --- it's
  it('should called with specified param', () => {
    expect(agentModel.findByIdOrThrow).calledWithExactly(agentId);
  });
  it('should called with specified param', () => {
    expect(agentModel.getLuisResource).calledWithExactly(clientId);
  });
  it('should called without params', () => {
    expect(fakeReturnSource.source.getAzureApi).calledWithExactly();
  });
  it('should called with specified param', () => {
    expect(fakeReturnAzureApi.getCognitiveServiceSubscriptionData)
      .calledWithExactly(predictionResource);
  });
  it('should called with specified param', () => {
    expect(agentModel.getAiApiKey).calledWithExactly(clientId);
  });
  it('should called with specified params', () => {
    expect(stubGetAIDriver).calledWithExactly(agentPlatform, apiKey);
  });
  it('should called with specified params', () => {
    expect(fakeNewAgentAiDriver.createSubscription)
      .calledWithExactly(fakeReturnSubscriptionData, agentModelId);
  });
  it('should called with specified params', () => {
    expect(agentModel.getAiQueryKeyByResource).calledWithExactly(clientId, predictionResource);
  });
  it('should return specified "queryKey" and "endpoint"', () => {
    expect(queryKey).to.be.equal(queryKeyIn);
    expect(endpoint).to.be.equal(endpointIn);
  });

});