1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import {
|
9 | Activity,
|
10 | AutoSaveStateMiddleware,
|
11 | ConversationState,
|
12 | MemoryStorage,
|
13 | Middleware,
|
14 | TestAdapter,
|
15 | TurnContext
|
16 | } from 'botbuilder-core';
|
17 | import { Dialog, DialogSet, DialogTurnResult, DialogTurnStatus } from 'botbuilder-dialogs';
|
18 | import { Botkit } from './core';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export class BotkitTestClient {
|
24 | private readonly _callback: (turnContext: TurnContext) => Promise<void>;
|
25 | private readonly _testAdapter: TestAdapter;
|
26 | public dialogTurnResult: DialogTurnResult;
|
27 | public conversationState: ConversationState;
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | public constructor(channelId: string, bot: Botkit, dialogToTest: string | string[], initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState)
|
48 | public constructor(testAdapter: TestAdapter, bot: Botkit, dialogToTest: string | string[], initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState)
|
49 | public constructor(channelOrAdapter: string | TestAdapter, bot: Botkit, dialogToTest: string | string[], initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState) {
|
50 | this.conversationState = conversationState || new ConversationState(new MemoryStorage());
|
51 |
|
52 | const dialogState = this.conversationState.createProperty('DialogState');
|
53 |
|
54 | let targetDialogs = [];
|
55 | if (Array.isArray(dialogToTest)) {
|
56 | dialogToTest.forEach((dialogName) => {
|
57 | targetDialogs.push(
|
58 | bot.dialogSet.find(dialogName)
|
59 | );
|
60 | targetDialogs.push(
|
61 | bot.dialogSet.find(dialogName + '_default_prompt')
|
62 | );
|
63 | targetDialogs.push(
|
64 | bot.dialogSet.find(dialogName + ':botkit-wrapper')
|
65 | );
|
66 | });
|
67 | dialogToTest = dialogToTest[0];
|
68 | } else {
|
69 | targetDialogs = [
|
70 | bot.dialogSet.find(dialogToTest),
|
71 | bot.dialogSet.find(dialogToTest + '_default_prompt'),
|
72 | bot.dialogSet.find(dialogToTest + ':botkit-wrapper')
|
73 | ];
|
74 | }
|
75 |
|
76 | this._callback = this.getDefaultCallback(targetDialogs, initialDialogOptions || null, dialogState);
|
77 |
|
78 | if (typeof channelOrAdapter === 'string') {
|
79 | this._testAdapter = new TestAdapter(this._callback, { channelId: channelOrAdapter }).use(new AutoSaveStateMiddleware(this.conversationState));
|
80 | } else {
|
81 | this._testAdapter = channelOrAdapter;
|
82 | }
|
83 |
|
84 | this.addUserMiddlewares(middlewares);
|
85 | }
|
86 |
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | public async sendActivity(activity: Partial<Activity> | string): Promise<any> {
|
97 | if (!activity) { activity = { type: 'event' }}
|
98 | await this._testAdapter.receiveActivity(activity);
|
99 | return this._testAdapter.activityBuffer.shift();
|
100 | }
|
101 |
|
102 | |
103 |
|
104 |
|
105 | public getNextReply(): Partial<Activity> {
|
106 | return this._testAdapter.activityBuffer.shift();
|
107 | }
|
108 |
|
109 | private getDefaultCallback(targetDialogs: Dialog[], initialDialogOptions: any, dialogState: any): (turnContext: TurnContext) => Promise<void> {
|
110 | return async (turnContext: TurnContext): Promise<void> => {
|
111 | const dialogSet = new DialogSet(dialogState);
|
112 | targetDialogs.forEach(targetDialog => dialogSet.add(targetDialog));
|
113 | const dialogContext = await dialogSet.createContext(turnContext);
|
114 | this.dialogTurnResult = await dialogContext.continueDialog();
|
115 | if (this.dialogTurnResult.status === DialogTurnStatus.empty) {
|
116 | this.dialogTurnResult = await dialogContext.beginDialog(targetDialogs[0].id, initialDialogOptions);
|
117 | }
|
118 | };
|
119 | }
|
120 |
|
121 | private addUserMiddlewares(middlewares: Middleware[]): void {
|
122 | if (middlewares != null) {
|
123 | middlewares.forEach((middleware) => {
|
124 | this._testAdapter.use(middleware);
|
125 | });
|
126 | }
|
127 | }
|
128 | }
|