Skip to content

Commit ece3485

Browse files
authored
Merge branch 'master' into Zerryth/QnAMakerServiceHostname
2 parents e33750b + 3dcc4fc commit ece3485

File tree

8 files changed

+63
-49
lines changed

8 files changed

+63
-49
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ root = true
77

88
# Tab indentation
99
[*]
10-
indent_style = tab
10+
indent_style = space
1111
indent_size = 4
1212
trim_trailing_whitespace = true
1313

libraries/botbuilder-testing/README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Use the DialogTestClient to drive unit tests of your dialogs.
1818

1919
To create a test client:
2020
```javascript
21-
let client = new DialogTestClient(dialog_to_test, dialog_options, OptionalMiddlewares);
21+
const client = new DialogTestClient('test', dialog_to_test, dialog_options, OptionalMiddlewares);
2222
```
2323

2424
To "send messages" through the dialog:
@@ -28,7 +28,7 @@ let reply = await client.sendActivity('test');
2828

2929
To check for additional messages:
3030
```javascript
31-
reply = await client.getNextReply();
31+
reply = client.getNextReply();
3232
```
3333

3434
Here is a sample unit test using assert:
@@ -37,21 +37,21 @@ Here is a sample unit test using assert:
3737
const { DialogTestClient, DialogTestLogger } = require('botbuilder-testing');
3838
const assert = require('assert');
3939

40-
let my_dialog = new SomeDialog();
41-
let options = { ... dialog options ... };
40+
const my_dialog = new SomeDialog();
41+
const options = { ... dialog options ... };
4242

4343
// set up a test client with a logger middleware that logs messages in and out
44-
let client = new DialogTestClient(my_dialog, options, [new DialogTestLogger()]);
44+
const client = new DialogTestClient('test', my_dialog, options, [new DialogTestLogger()]);
4545

4646
// send a test message, catch the reply
4747
let reply = await client.sendActivity('hello');
48-
assert(reply.text == 'hello yourself', 'first message was wrong');
48+
assert.strictEqual(reply.text, 'hello yourself', 'first message was wrong');
4949
// expecting 2 messages in a row?
50-
reply = await client.getNextReply();
51-
assert(reply.text == 'second message', 'second message as wrong');
50+
reply = client.getNextReply();
51+
assert.strictEqual(reply.text, 'second message', 'second message as wrong');
5252

5353
// test end state
54-
assert(client.dialogTurnResult.status == 'empty', 'dialog is not empty');
54+
assert.strictEqual(client.dialogTurnResult.status, 'empty', 'dialog is not empty');
5555
```
5656

5757
[Additional examples are available here](tests/)
@@ -63,6 +63,5 @@ By default, the transcript will be logged with the `mocha-logger` package. You m
6363
your own logger:
6464

6565
```javascript
66-
let testlogger = new DialogTestLogger(console);
66+
const testlogger = new DialogTestLogger(console);
6767
```
68-

libraries/botbuilder-testing/src/dialogTestClient.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,51 @@ export class DialogTestClient {
1818
private readonly _callback: (turnContext: TurnContext) => Promise<void>;
1919
private readonly _testAdapter: TestAdapter;
2020
public dialogTurnResult: DialogTurnResult;
21+
public conversationState: ConversationState;
2122

2223
/**
2324
* Create a DialogTestClient to test a dialog without having to create a full-fledged adapter.
24-
*
25+
*
2526
* ```javascript
26-
* let client = new DialogTestClient(MY_DIALOG, MY_OPTIONS);
27-
* let reply = client.sendActivity('first message');
28-
* assert(reply.text == 'first reply','reply failed');
27+
* let client = new DialogTestClient('test', MY_DIALOG, MY_OPTIONS);
28+
* let reply = await client.sendActivity('first message');
29+
* assert.strictEqual(reply.text, 'first reply', 'reply failed');
2930
* ```
30-
*
31+
*
32+
* @param channelId The channelId to be used for the test.
33+
* Use 'emulator' or 'test' if you are uncertain of the channel you are targeting.
34+
* Otherwise, it is recommended that you use the id for the channel(s) your bot will be using and write a test case for each channel.
35+
* @param testAdapter A list of middlewares to be added to the test adapter.
3136
* @param targetDialog The dialog to be tested. This will be the root dialog for the test client.
3237
* @param initialDialogOptions (Optional) additional argument(s) to pass to the dialog being started.
3338
* @param middlewares (Optional) The test adapter to use. If this parameter is not provided, the test client will use a default TestAdapter
34-
* @param testAdapter (Optional) A list of middlewares to be added to the test adapter.
35-
* @param callback (Optional) The bot turn processing logic for the test. If this value is not provided, the test client will create a default BotCallbackHandler
36-
* @param adapterOptions (Optional) Options passed to TestAdapter that allow customizing behavior such as the channelId. eg {channelId: 'custom'}
39+
* @param conversationState (Optional) A ConversationState instance to use in the test client
3740
*/
38-
public constructor(targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], testAdapter?: TestAdapter, callback?: (turnContext: TurnContext) => Promise<void>, adapterOptions?: Partial<Activity>) {
39-
let convoState = new ConversationState(new MemoryStorage());
41+
public constructor(channelId: string, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState);
42+
public constructor(testAdapter: TestAdapter, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState)
43+
constructor(channelOrAdapter: string|TestAdapter, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState) {
44+
this.conversationState = conversationState || new ConversationState(new MemoryStorage());
4045

41-
let dialogState = convoState.createProperty('DialogState');
46+
let dialogState = this.conversationState.createProperty('DialogState');
4247

43-
this._callback = callback || this.getDefaultCallback(targetDialog, initialDialogOptions || null, dialogState);
48+
this._callback = this.getDefaultCallback(targetDialog, initialDialogOptions || null, dialogState);
4449

45-
this._testAdapter = testAdapter || new TestAdapter(this._callback,adapterOptions).use(new AutoSaveStateMiddleware(convoState));
50+
if (typeof channelOrAdapter == 'string') {
51+
const channelIdToUse: string = channelOrAdapter;
52+
this._testAdapter = new TestAdapter(this._callback, {channelId: channelIdToUse}).use(new AutoSaveStateMiddleware(this.conversationState));
53+
} else {
54+
const testAdapterToUse: TestAdapter = channelOrAdapter;
55+
this._testAdapter = testAdapterToUse;
56+
}
4657

4758
this.addUserMiddlewares(middlewares);
48-
4959
}
5060

5161
/**
5262
* Send an activity into the dialog.
5363
* @returns a TestFlow that can be used to assert replies etc
5464
* @param activity an activity potentially with text
55-
*
65+
*
5666
* ```javascript
5767
* DialogTest.send('hello').assertReply('hello yourself').then(done);
5868
* ```
@@ -65,7 +75,7 @@ export class DialogTestClient {
6575
/**
6676
* Get the next reply waiting to be delivered (if one exists)
6777
*/
68-
public async getNextReply(): Promise<any> {
78+
public getNextReply() {
6979
return this._testAdapter.activityBuffer.shift();
7080
}
7181

@@ -91,5 +101,5 @@ export class DialogTestClient {
91101
});
92102
}
93103
}
94-
104+
95105
}

libraries/botbuilder-testing/tests/dialogTestClient.test.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ const assert = require('assert');
1111
describe('DialogTestClient', function() {
1212

1313
it('should create a DialogTestClient', async function() {
14-
let client = new DialogTestClient();
14+
let client = new DialogTestClient('test', null);
1515
assert(client instanceof DialogTestClient, 'Created an invalid object');
1616
});
1717

1818
it('should create a DialogTestClient with a custom channelId', async function() {
19-
let client = new DialogTestClient(null, null, null, null, null, {channelId: 'custom'});
19+
let client = new DialogTestClient('custom', null);
2020
assert(client._testAdapter.template.channelId == 'custom', 'Created with wrong channel id');
2121
});
2222

23-
2423
it('should process a single turn waterfall dialog', async function() {
2524

2625
let dialog = new WaterfallDialog('waterfall', [
@@ -30,16 +29,13 @@ describe('DialogTestClient', function() {
3029
}
3130
]);
3231

33-
let client = new DialogTestClient(dialog, null, null, null, null, {channelId: 'custom'});
32+
let client = new DialogTestClient('test', dialog);
3433
let reply = await client.sendActivity('hello');
3534
assert(reply.text == 'hello', 'dialog responded with incorrect message');
36-
assert(reply.channelId == 'custom', 'custom channel id didnt get set');
35+
assert(reply.channelId == 'test', 'test channel id didnt get set');
3736
assert(client.dialogTurnResult.status == DialogTurnStatus.complete, 'dialog did not end properly');
38-
3937
});
4038

41-
42-
4339
it('should process a 2 turn waterfall dialog', async function() {
4440

4541
let dialog = new WaterfallDialog('waterfall', [
@@ -54,18 +50,17 @@ describe('DialogTestClient', function() {
5450
},
5551
]);
5652

57-
let client = new DialogTestClient(dialog, null, [new DialogTestLogger()], null, null, {channelId: 'custom'});
53+
let client = new DialogTestClient('test', dialog, null, [new DialogTestLogger()], null, null);
5854
let reply = await client.sendActivity('hello');
5955
assert(reply.text == 'hello', 'dialog responded with incorrect message');
6056
// get typing
61-
reply = await client.getNextReply();
57+
reply = client.getNextReply();
6258
assert(reply.type == 'typing', 'dialog responded with incorrect message');
63-
reply = await client.getNextReply();
59+
reply = client.getNextReply();
6460
assert(reply.text == 'hello 2', 'dialog responded with incorrect 2nd message');
6561
assert(client.dialogTurnResult.status == DialogTurnStatus.complete, 'dialog did not end properly');
6662
});
6763

68-
6964
it('should process a component dialog', async function() {
7065

7166
class MainDialog extends ComponentDialog {
@@ -81,15 +76,15 @@ describe('DialogTestClient', function() {
8176
return step.next();
8277
},
8378
]);
84-
79+
8580
this.addDialog(dialog);
8681
this.addDialog(new TextPrompt('textPrompt'));
8782
}
8883

8984
async run(turnContext, accessor) {
9085
const dialogSet = new DialogSet(accessor);
9186
dialogSet.add(this);
92-
87+
9388
const dialogContext = await dialogSet.createContext(turnContext);
9489
const results = await dialogContext.continueDialog();
9590
if (results.status === DialogTurnStatus.empty) {
@@ -98,15 +93,12 @@ describe('DialogTestClient', function() {
9893
}
9994
}
10095

101-
10296
let component = new MainDialog('component');
103-
104-
let client = new DialogTestClient(component,null, [new DialogTestLogger()]);
97+
let client = new DialogTestClient('test', component, null, [new DialogTestLogger()]);
10598
let reply = await client.sendActivity('hello');
10699
assert(reply.text == 'Tell me something','dialog responded with incorrect message');
107100
reply = await client.sendActivity('foo');
108101
assert(reply.text == 'you said: foo', 'dialog responded with incorrect 2nd message');
109102
assert(client.dialogTurnResult.status == DialogTurnStatus.complete, 'dialog did not end properly');
110103
});
111-
112104
});

libraries/testbot/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MicrosoftAppId=
2+
MicrosoftAppPassword=

libraries/testbot/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
// Licensed under the MIT License.
33

44
const restify = require('restify');
5+
const path = require('path');
56

67
const { BotFrameworkAdapter, MemoryStorage, UserState, ConversationState, InspectionState, InspectionMiddleware } = require('botbuilder');
8+
const { MicrosoftAppCredentials } = require('botframework-connector');
79
const { MyBot } = require('./bots/myBot')
810

11+
const ENV_FILE = path.join(__dirname, '.env');
12+
require('dotenv').config({ path: ENV_FILE });
13+
914
const adapter = new BotFrameworkAdapter({
1015
appId: process.env.MicrosoftAppId,
1116
appPassword: process.env.MicrosoftAppPassword
@@ -17,7 +22,7 @@ var inspectionState = new InspectionState(memoryStorage);
1722
var userState = new UserState(memoryStorage);
1823
var conversationState = new ConversationState(memoryStorage);
1924

20-
adapter.use(new InspectionMiddleware(inspectionState, userState, conversationState));
25+
adapter.use(new InspectionMiddleware(inspectionState, userState, conversationState, new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword)));
2126

2227
adapter.onTurnError = async (context, error) => {
2328
console.error(`\n [onTurnError]: ${ error }`);

libraries/testbot/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libraries/testbot/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "MIT",
1111
"dependencies": {
1212
"botbuilder": "^4.1.6",
13-
"restify": "^8.3.0"
13+
"restify": "^8.3.0",
14+
"dotenv": "^6.1.0"
1415
}
1516
}

0 commit comments

Comments
 (0)