Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Default search type for Teams #4172

Merged
merged 12 commits into from
Mar 24, 2022
19 changes: 12 additions & 7 deletions libraries/botbuilder-core/src/activityHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Activity,
AdaptiveCardInvokeResponse,
AdaptiveCardInvokeValue,
Channels,
SearchInvokeResponse,
SearchInvokeValue,
MessageReaction,
Expand Down Expand Up @@ -727,20 +728,24 @@ export class ActivityHandler extends ActivityHandlerBase {
}

if (!value.kind) {
const response = this.createAdaptiveCardInvokeErrorResponse(
StatusCodes.BAD_REQUEST,
'BadRequest',
`Missing kind property for search.`
);
if (activity.channelId === Channels.Msteams) {
value.kind = 'search';
} else {
const response = this.createAdaptiveCardInvokeErrorResponse(
StatusCodes.BAD_REQUEST,
'BadRequest',
'Missing kind property for search.'
);

throw new InvokeException(StatusCodes.BAD_REQUEST, response);
throw new InvokeException(StatusCodes.BAD_REQUEST, response);
}
}

if (!value.queryText) {
const response = this.createAdaptiveCardInvokeErrorResponse(
StatusCodes.BAD_REQUEST,
'BadRequest',
`Missing queryText for search.`
'Missing queryText for search.'
);

throw new InvokeException(StatusCodes.BAD_REQUEST, response);
Expand Down
68 changes: 44 additions & 24 deletions libraries/botbuilder-core/tests/ActivityHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const { ActivityHandler, ActivityTypes, TestAdapter, tokenResponseEventName, TurnContext } = require('../lib');
const { ActivityHandler, ActivityTypes, Channels, TestAdapter, tokenResponseEventName, TurnContext } = require('../lib');

describe('ActivityHandler', function () {
const adapter = new TestAdapter();
Expand Down Expand Up @@ -340,16 +340,6 @@ describe('ActivityHandler', function () {

describe('ActivityHandler.onSearchInvoke', function () {
it(`should fire onSearchInvoke`, async function () {
const bot = new ActivityHandler();

let onSearchInvokeCalled = false;
let value = null;
bot.onSearchInvoke = async (context, invokeValue) => {
onSearchInvokeCalled = true;
value = invokeValue;
return { statusCode: 200, value: 'called' };
};

const activity = {
type: ActivityTypes.Invoke,
name: 'application/search',
Expand All @@ -364,17 +354,7 @@ describe('ActivityHandler', function () {
},
};

await processActivity(
activity,
bot
);

assert(onSearchInvokeCalled);
assert.equal(activity.value.queryText, value.queryText, 'missing query text');
assert.equal(activity.value.kind, value.kind, 'missing kind');
assert.equal(activity.value.queryOptions.skip, value.queryOptions.skip, 'missing skip');
assert.equal(activity.value.queryOptions.top, value.queryOptions.top, 'missing top');
assert.equal(activity.value.context, value.context, 'missing context');
await assertonSearchInvoke(activity, activity.value.kind);
});

it(`should throw on onSearchInvoke activity missing value`, async function () {
Expand All @@ -392,24 +372,64 @@ describe('ActivityHandler', function () {
name: 'application/search',
value: {
queryText: 'test bot'
},
}
};

await assertSearchResultError(activity, 'Missing kind property for search.');
});

it(`should not throw on onSearchInvoke activity missing kind when channel is msTeams`, async function () {
const activity = {
type: ActivityTypes.Invoke,
name: 'application/search',
channelId: Channels.Msteams,
value: {
queryText: 'test bot',
queryOptions: {
skip: 5,
top: 10
}
}
};

await assertonSearchInvoke(activity, 'search');
});

it(`should throw on onSearchInvoke activity missing queryText`, async function () {
const activity = {
type: ActivityTypes.Invoke,
name: 'application/search',
value: {
kind: 'search'
},
}
};

await assertSearchResultError(activity, 'Missing queryText for search.');
});

async function assertonSearchInvoke(activity, expectedKind) {
const bot = new ActivityHandler();
const testAdapter = new TestAdapter();

let onSearchInvokeCalled = false;
let value = null;
bot.onSearchInvoke = async (context, invokeValue) => {
onSearchInvokeCalled = true;
value = invokeValue;
return { statusCode: 200, value: 'called' };
};

const context = new TurnContext(testAdapter, activity);
await bot.run(context);

assert(onSearchInvokeCalled);
assert.equal(activity.value.queryText, value.queryText, 'missing query text');
assert.equal(expectedKind, value.kind, 'missing kind');
assert.equal(activity.value.queryOptions.skip, value.queryOptions.skip, 'missing skip');
assert.equal(activity.value.queryOptions.top, value.queryOptions.top, 'missing top');
assert.equal(activity.value.context, value.context, 'missing context');
}

async function assertSearchResultError(activity, errorMessage) {
const bot = new ActivityHandler();
const testAdapter = new TestAdapter();
Expand Down