wa-chat-server-microsoft
Version:
wa-chat-server adapter for the Microsoft Bot Framework
294 lines (293 loc) • 12.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WatsonHandlerTest = void 0;
const botbuilder_1 = require("botbuilder");
const WatsonHandler_1 = require("../../class/WatsonHandler");
const WAChatServerMock_1 = require("../mock/WAChatServerMock");
const TurnContextMock_1 = require("../mock/TurnContextMock");
class WatsonHandlerTest {
getWatsonHandler(waChatServer) {
return new WatsonHandler_1.WatsonHandler('SESSION_ID', waChatServer);
}
getTextActivity(text) {
return botbuilder_1.ActivityFactory.fromObject(text);
}
getOptionsActivity(...options) {
const retVal = botbuilder_1.MessageFactory.suggestedActions(options.map((option) => {
return {
type: 'imBack',
title: option.label,
value: option.input,
};
}));
return retVal;
}
conversationStartTest() {
return __awaiter(this, void 0, void 0, function* () {
it('conversation-start', () => __awaiter(this, void 0, void 0, function* () {
const outputText = 'T';
const chatServer = new WAChatServerMock_1.WAChatServerMock({
watsonRequest: {
input: {
text: '',
},
},
response: {
session: null,
watsonResponse: {
result: {
output: {
generic: [
{
response_type: 'text',
text: outputText,
},
],
},
},
},
},
});
const turnContext = new TurnContextMock_1.TurnContextMock({
inputActivity: null,
outputActivities: [outputText],
});
const handler = this.getWatsonHandler(chatServer);
yield handler.startSession(); // because of branch coverage
yield handler.onConversationUpdateHandler(turnContext, () => Promise.resolve(null));
turnContext.end();
}));
});
}
htmlTranslationTest() {
return __awaiter(this, void 0, void 0, function* () {
it('html-translation', () => __awaiter(this, void 0, void 0, function* () {
const inputText = 'T';
const outputMarkUp = '<a href="http://i.cz"><strong>T</strong></a>';
const outputMarkDown = '[**T**](http://i.cz)';
const chatServer = new WAChatServerMock_1.WAChatServerMock({
watsonRequest: {
input: {
text: inputText,
},
},
response: {
session: null,
watsonResponse: {
result: {
output: {
generic: [
{
response_type: 'text',
text: outputMarkUp,
},
],
},
},
},
},
});
const turnContext = new TurnContextMock_1.TurnContextMock({
inputActivity: this.getTextActivity(inputText),
outputActivities: [outputMarkDown],
});
const handler = this.getWatsonHandler(chatServer);
yield handler.onMessageHandler(turnContext, () => Promise.resolve(null));
turnContext.end();
}));
});
}
optionsTest() {
return __awaiter(this, void 0, void 0, function* () {
it('options', () => __awaiter(this, void 0, void 0, function* () {
const inputText = 'T';
const chatServer = new WAChatServerMock_1.WAChatServerMock({
watsonRequest: {
input: {
text: inputText,
},
},
response: {
session: null,
watsonResponse: {
result: {
output: {
generic: [
{
response_type: 'option',
options: [
{
label: 'L1',
value: { input: { text: 'I1' } },
},
{
label: 'L2',
value: { input: { text: 'I2' } },
},
],
},
],
},
},
},
},
});
const turnContext = new TurnContextMock_1.TurnContextMock({
inputActivity: this.getTextActivity(inputText),
outputActivities: [
this.getOptionsActivity({ label: 'L1', input: 'I1' }, { label: 'L2', input: 'I2' }),
],
});
const handler = this.getWatsonHandler(chatServer);
yield handler.onMessageHandler(turnContext, () => Promise.resolve(null));
turnContext.end();
}));
});
}
disambiguationTest() {
return __awaiter(this, void 0, void 0, function* () {
it('disambiguation', () => __awaiter(this, void 0, void 0, function* () {
const inputText = 'T';
const chatServer = new WAChatServerMock_1.WAChatServerMock({
watsonRequest: {
input: {
text: inputText,
},
},
response: {
session: null,
watsonResponse: {
result: {
output: {
generic: [
{
response_type: 'suggestion',
suggestions: [
{ label: 'S1', value: { input: 'I1' } },
{ label: 'S2', value: { input: 'I2' } },
],
},
],
},
},
},
},
});
const turnContext = new TurnContextMock_1.TurnContextMock({
inputActivity: this.getTextActivity(inputText),
outputActivities: [
this.getOptionsActivity({ label: 'S1', input: 'S1' }, { label: 'S2', input: 'S2' }),
],
});
const handler = this.getWatsonHandler(chatServer);
yield handler.onMessageHandler(turnContext, () => Promise.resolve(null));
turnContext.end();
}));
});
}
disambiguationSuggestionUsedTest() {
return __awaiter(this, void 0, void 0, function* () {
it('disambiguation-suggestion-used', () => __awaiter(this, void 0, void 0, function* () {
const inputText = 'S1';
const outputText = 'T';
const chatServer = new WAChatServerMock_1.WAChatServerMock({
watsonRequest: {
input: 'I1',
},
response: {
session: null,
watsonResponse: {
result: {
output: {
generic: [
{
response_type: 'text',
text: outputText,
},
],
},
},
},
},
});
chatServer.session.data = {
microsoft: {
suggestions: [
{ label: 'S1', input: 'I1' },
{ label: 'S2', input: 'I2' },
],
},
};
const turnContext = new TurnContextMock_1.TurnContextMock({
inputActivity: this.getTextActivity(inputText),
outputActivities: [outputText],
});
const handler = this.getWatsonHandler(chatServer);
yield handler.onMessageHandler(turnContext, () => Promise.resolve(null));
turnContext.end();
}));
});
}
disambiguationSuggestionNotUsedTest() {
return __awaiter(this, void 0, void 0, function* () {
it('disambiguation-suggestion-not-used', () => __awaiter(this, void 0, void 0, function* () {
const inputText = 'S3';
const outputText = 'T';
const chatServer = new WAChatServerMock_1.WAChatServerMock({
watsonRequest: {
input: { text: inputText },
},
response: {
session: null,
watsonResponse: {
result: {
output: {
generic: [
{
response_type: 'text',
text: outputText,
},
],
},
},
},
},
});
chatServer.session.data = {
microsoft: {
suggestions: [
{ label: 'S1', input: 'I1' },
{ label: 'S2', input: 'I2' },
],
},
};
const turnContext = new TurnContextMock_1.TurnContextMock({
inputActivity: this.getTextActivity(inputText),
outputActivities: [outputText],
});
const handler = this.getWatsonHandler(chatServer);
yield handler.onMessageHandler(turnContext, () => Promise.resolve(null));
turnContext.end();
}));
});
}
test() {
this.conversationStartTest();
this.htmlTranslationTest();
this.optionsTest();
this.disambiguationTest();
this.disambiguationSuggestionUsedTest();
this.disambiguationSuggestionNotUsedTest();
}
}
exports.WatsonHandlerTest = WatsonHandlerTest;
new WatsonHandlerTest().test();