UNPKG

4.29 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12/**
13 * @module botkit
14 */
15/**
16 * Copyright (c) Microsoft Corporation. All rights reserved.
17 * Licensed under the MIT License.
18 */
19const botbuilder_core_1 = require("botbuilder-core");
20const botbuilder_dialogs_1 = require("botbuilder-dialogs");
21/**
22 * A client for testing dialogs in isolation.
23 */
24class BotkitTestClient {
25 constructor(channelOrAdapter, bot, dialogToTest, initialDialogOptions, middlewares, conversationState) {
26 this.conversationState = conversationState || new botbuilder_core_1.ConversationState(new botbuilder_core_1.MemoryStorage());
27 const dialogState = this.conversationState.createProperty('DialogState');
28 let targetDialogs = [];
29 if (Array.isArray(dialogToTest)) {
30 dialogToTest.forEach((dialogName) => {
31 targetDialogs.push(bot.dialogSet.find(dialogName));
32 targetDialogs.push(bot.dialogSet.find(dialogName + '_default_prompt'));
33 targetDialogs.push(bot.dialogSet.find(dialogName + ':botkit-wrapper'));
34 });
35 dialogToTest = dialogToTest[0];
36 }
37 else {
38 targetDialogs = [
39 bot.dialogSet.find(dialogToTest),
40 bot.dialogSet.find(dialogToTest + '_default_prompt'),
41 bot.dialogSet.find(dialogToTest + ':botkit-wrapper')
42 ];
43 }
44 this._callback = this.getDefaultCallback(targetDialogs, initialDialogOptions || null, dialogState);
45 if (typeof channelOrAdapter === 'string') {
46 this._testAdapter = new botbuilder_core_1.TestAdapter(this._callback, { channelId: channelOrAdapter }).use(new botbuilder_core_1.AutoSaveStateMiddleware(this.conversationState));
47 }
48 else {
49 this._testAdapter = channelOrAdapter;
50 }
51 this.addUserMiddlewares(middlewares);
52 }
53 /**
54 * Send an activity into the dialog.
55 * @returns a TestFlow that can be used to assert replies etc
56 * @param activity an activity potentially with text
57 *
58 * ```javascript
59 * DialogTest.send('hello').assertReply('hello yourself').then(done);
60 * ```
61 */
62 sendActivity(activity) {
63 return __awaiter(this, void 0, void 0, function* () {
64 yield this._testAdapter.receiveActivity(activity);
65 return this._testAdapter.activityBuffer.shift();
66 });
67 }
68 /**
69 * Get the next reply waiting to be delivered (if one exists)
70 */
71 getNextReply() {
72 return this._testAdapter.activityBuffer.shift();
73 }
74 getDefaultCallback(targetDialogs, initialDialogOptions, dialogState) {
75 return (turnContext) => __awaiter(this, void 0, void 0, function* () {
76 const dialogSet = new botbuilder_dialogs_1.DialogSet(dialogState);
77 targetDialogs.forEach(targetDialog => dialogSet.add(targetDialog));
78 const dialogContext = yield dialogSet.createContext(turnContext);
79 this.dialogTurnResult = yield dialogContext.continueDialog();
80 if (this.dialogTurnResult.status === botbuilder_dialogs_1.DialogTurnStatus.empty) {
81 this.dialogTurnResult = yield dialogContext.beginDialog(targetDialogs[0].id, initialDialogOptions);
82 }
83 });
84 }
85 addUserMiddlewares(middlewares) {
86 if (middlewares != null) {
87 middlewares.forEach((middleware) => {
88 this._testAdapter.use(middleware);
89 });
90 }
91 }
92}
93exports.BotkitTestClient = BotkitTestClient;
94//# sourceMappingURL=testClient.js.map
\No newline at end of file