surrsoft
10/2/2019 - 1:19 PM

Agent.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('./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, endpoint } = res);
  });

  after(() => {
    [agentModel.findByIdOrThrow,
      agentModel.getLuisResource,
      agentModel.getAiApiKey,
      agentModel.getAiQueryKeyByResource,
    ].forEach((stub) => {
      stub.reset();
      stub.restore();
    });
  });

  // --- it's
  it('should call findByIdOrThrow with specified param', () => {
    expect(agentModel.findByIdOrThrow).calledWithExactly(agentId);
  });
  it('should call getLuisResource with specified param', () => {
    expect(agentModel.getLuisResource).calledWithExactly(clientId);
  });
  it('should call source.getAzureApi without params', () => {
    expect(fakeReturnSource.source.getAzureApi).calledWithExactly();
  });
  it('should call getCognitiveServiceSubscriptionData with specified param', () => {
    expect(fakeReturnAzureApi.getCognitiveServiceSubscriptionData)
      .calledWithExactly(predictionResource);
  });
  it('should call getAiApiKey with specified param', () => {
    expect(agentModel.getAiApiKey).calledWithExactly(clientId);
  });
  it('should call getAIDriver with specified params', () => {
    expect(stubGetAIDriver).calledWithExactly(agentPlatform, apiKey);
  });
  it('should call createSubscription with specified params', () => {
    expect(fakeNewAgentAiDriver.createSubscription)
      .calledWithExactly(fakeReturnSubscriptionData, agentModelId);
  });
  it('should call getAiQueryKeyByResource 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);
  });

});