Skip to content

Commit 4324e9f

Browse files
committed
test: add unit and integration tests for assistant v2
1 parent 8a99676 commit 4324e9f

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
const watson = require('../../index');
4+
const assert = require('assert');
5+
const authHelper = require('./auth_helper.js');
6+
7+
const auth = authHelper.auth.assistant;
8+
auth.version = '2018-09-19';
9+
10+
describe('assistant v2 integration', function() {
11+
const assistant = new watson.AssistantV2(auth);
12+
const assistant_id = auth.assistant_id;
13+
let session_id;
14+
15+
it('should createSession', function(done) {
16+
const params = {
17+
assistant_id,
18+
};
19+
assistant.createSession(params, function(err, res) {
20+
assert.ifError(err);
21+
assert(res.session_id);
22+
session_id = res.session_id;
23+
done();
24+
});
25+
});
26+
27+
it('should message - generic', function(done) {
28+
const params = {
29+
assistant_id,
30+
session_id,
31+
};
32+
assistant.message(params, function(err, res) {
33+
assert.ifError(err);
34+
assert(res.output);
35+
assert(Array.isArray(res.output.generic));
36+
assert.equal(res.output.generic[0].response_type, 'text');
37+
assert(res.output.generic[0].text);
38+
assert(Array.isArray(res.output.intents));
39+
assert(Array.isArray(res.output.entities));
40+
assert.equal(res.output.intents.length, 0);
41+
assert.equal(res.output.entities.length, 0);
42+
done();
43+
});
44+
});
45+
46+
it('should message - non-generic', function(done) {
47+
const input = {
48+
text: 'please tell me a joke',
49+
};
50+
const params = {
51+
assistant_id,
52+
session_id,
53+
input,
54+
};
55+
assistant.message(params, function(err, res) {
56+
assert.ifError(err);
57+
assert(res.output);
58+
assert(Array.isArray(res.output.generic));
59+
assert.equal(res.output.generic[0].response_type, 'text');
60+
assert(res.output.generic[0].text);
61+
assert(Array.isArray(res.output.intents));
62+
assert(Array.isArray(res.output.entities));
63+
assert.equal(res.output.intents[0].intent, 'Joke');
64+
assert.equal(res.output.entities.length, 0);
65+
done();
66+
});
67+
});
68+
69+
it('should deleteSession', function(done) {
70+
const params = {
71+
assistant_id: auth.assistant_id,
72+
session_id,
73+
};
74+
assistant.deleteSession(params, function(err, res) {
75+
assert.ifError(err);
76+
done();
77+
});
78+
});
79+
});

test/unit/test.assistant.v2.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const watson = require('../../index');
5+
const nock = require('nock');
6+
7+
// Test params
8+
const service = {
9+
username: 'batman',
10+
password: 'bruce-wayne',
11+
url: 'http://ibm.com:80',
12+
version: '2018-09-19',
13+
};
14+
15+
const noop = function() {};
16+
17+
const missingParameter = function(err) {
18+
assert.ok(err instanceof Error && /required parameters/.test(err));
19+
};
20+
21+
const assistant = new watson.AssistantV2(service);
22+
23+
describe('assistant v2 unit tests', function() {
24+
before(function() {
25+
nock.disableNetConnect();
26+
nock(service.url)
27+
.persist()
28+
.post('*')
29+
.reply(200, {});
30+
});
31+
32+
after(function() {
33+
nock.cleanAll();
34+
});
35+
36+
describe('createSession tests', function() {
37+
const params = { assistant_id: 'robin123' };
38+
it('should generate a valid payload', function() {
39+
const req = assistant.createSession(params, noop);
40+
assert.equal(
41+
req.uri.href,
42+
service.url + '/v2/assistants/robin123/sessions?version=2018-09-19'
43+
);
44+
assert.equal(req.method, 'POST');
45+
assert.equal(req.headers.Accept, 'application/json');
46+
assert.equal(req.headers['Content-Type'], 'application/json');
47+
});
48+
49+
it('should check no parameters provided (negative test)', function() {
50+
assistant.createSession({}, missingParameter);
51+
assistant.createSession(null, missingParameter);
52+
assistant.createSession(undefined, missingParameter);
53+
});
54+
});
55+
56+
describe('message tests', function() {
57+
const params = { assistant_id: 'robin123', session_id: 'abc123' };
58+
it('should generate a valid payload', function() {
59+
const req = assistant.message(params, noop);
60+
assert.equal(
61+
req.uri.href,
62+
service.url + '/v2/assistants/robin123/sessions/abc123/message?version=2018-09-19'
63+
);
64+
assert.equal(req.method, 'POST');
65+
assert.equal(req.headers.Accept, 'application/json');
66+
assert.equal(req.headers['Content-Type'], 'application/json');
67+
});
68+
69+
it('should check no parameters provided (negative test)', function() {
70+
assistant.message({}, missingParameter);
71+
assistant.message(null, missingParameter);
72+
assistant.message(undefined, missingParameter);
73+
assistant.message({ assistant_id: 'a' }, missingParameter);
74+
assistant.message({ session_id: 'a' }, missingParameter);
75+
});
76+
});
77+
78+
describe('deleteSession tests', function() {
79+
const params = { assistant_id: 'robin123', session_id: 'abc123' };
80+
it('should generate a valid payload', function() {
81+
const req = assistant.deleteSession(params, noop);
82+
assert.equal(
83+
req.uri.href,
84+
service.url + '/v2/assistants/robin123/sessions/abc123?version=2018-09-19'
85+
);
86+
assert.equal(req.method, 'DELETE');
87+
assert.equal(req.headers.Accept, 'application/json');
88+
});
89+
90+
it('should check no parameters provided (negative test)', function() {
91+
assistant.deleteSession({}, missingParameter);
92+
assistant.deleteSession(null, missingParameter);
93+
assistant.deleteSession(undefined, missingParameter);
94+
assistant.deleteSession({ assistant_id: 'a' }, missingParameter);
95+
assistant.deleteSession({ session_id: 'a' }, missingParameter);
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)