-
Notifications
You must be signed in to change notification settings - Fork 63
/
builtInContext.js
156 lines (148 loc) · 6.5 KB
/
builtInContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
© Copyright IBM Corp. 2017
*/
'use strict';
const should = require('should');
const {handler} = require('skill-sdk-nodejs');
describe('Test builtInContext set/get/delete/validate', function () {
this.timeout(10000);
let dummyValidContext;
let dummyInvalidContext;
let validPath;
let invalidPath;
let dummyValue;
let dummyInvalidValue;
beforeEach(function (done){
dummyValidContext = {
"builtin": {
"currentConversation": {
"@type": "http://www.ibm.com/watson-assistant-solutions/ontology/Conversation",
"lastReferencedLocation": {
"@type": "http://www.ibm.com/watson-assistant-solutions/ontology/PlaceReference",
"object": {
"@type": "http://schema.org/Place",
"name": "John Hancock Tower Boston",
"address": {
"@type": "http://schema.org/PostalAddress",
"streetAddress": "120 St James Ave",
"addressLocality": "Boston",
"postalCode": "MA 02116",
"addressCountry": "USA"
},
"geo": {
"@type": "http://schema.org/GeoCoordinates",
"longitude": "0.1278 W",
"latitude": "51.5074 N"
}
}
}
}
}
};
dummyInvalidContext = { "builtin": {"Dsfds": "Sdfs"}};
validPath = "currentConversation.lastReferencedLocation";
invalidPath = "currentConversation.hello.world";
dummyValue = {
"@type": "http://www.ibm.com/watson-assistant-solutions/ontology/PlaceReference",
"object": {
"@type": "http://schema.org/Place",
"name": "John Hancock Tower Boston",
"address": {
"@type": "http://schema.org/PostalAddress",
"streetAddress": "120 St James Ave",
"addressLocality": "Boston",
"postalCode": "MA 02116",
"addressCountry": "USA"
},
"geo": {
"@type": "http://schema.org/GeoCoordinates",
"longitude": "no",
"latitude": "where"
}
}
};
dummyInvalidValue = {
"object": {
"@type": "http://schema.org/Place",
"name": "John Hancock Tower Boston",
"address": {
"@type": "http://schema.org/PostalAddress",
"streetAddress": "120 St James Ave",
"addressLocality": "Boston",
"postalCode": "MA 02116",
"addressCountry": "USA"
},
"geo": {
"@type": "http://schema.org/GeoCoordinates",
"longitude": "no",
"latitude": "where"
}
}
};
done();
});
describe('Test set built-in context', function() {
it('Try setting invalid built-in context property path', function (done) {
const result = handler.setBuiltinContextProperty(dummyValidContext, invalidPath, dummyValue);
result.valid.should.equal(false);
result.status.should.equal(404);
done()
});
it('Try setting valid built-in context property path', function(done) {
const result = handler.setBuiltinContextProperty(dummyValidContext, validPath, dummyValue);
result.valid.should.equal(true);
JSON.stringify(result.obj.currentConversation.lastReferencedLocation).should.equal(JSON.stringify(dummyValue));
done();
});
it('Try setting invalid built-in context property value', function(done) {
const result = handler.setBuiltinContextProperty(dummyValidContext, validPath, dummyInvalidValue);
result.valid.should.equal(false);
JSON.stringify(result.obj).should.equal(JSON.stringify(dummyValidContext.builtin));
done();
})
});
describe('Test get built-in context', function() {
it('Try getting built-in context with invalid property path', function(done) {
const result = handler.getBuiltinContextProperty(dummyValidContext, invalidPath);
result.valid.should.equal(false);
result.status.should.equal(400);
result.should.not.have.property('obj');
done();
});
it('Try getting built-in context with valid property path', function(done) {
const result = handler.getBuiltinContextProperty(dummyValidContext, validPath);
result.valid.should.equal(true);
JSON.stringify(result.value).should.equal(JSON.stringify(dummyValidContext.builtin.currentConversation.lastReferencedLocation));
done();
})
});
describe('Test delete built in context', function(){
it('Try deleting built in context with invalid path', function(done) {
const result = handler.deleteBuiltinContextProperty(dummyValidContext, invalidPath);
result.valid.should.equal(false);
result.status.should.equal(404);
result.should.not.have.property('obj');
done();
});
it('Try deleting built in context with valid path', function(done) {
const result = handler.deleteBuiltinContextProperty(dummyValidContext, validPath);
result.valid.should.equal(true);
result.status.should.equal(200);
delete dummyValidContext.builtin.currentConversation.lastReferencedLocation;
result.obj.should.deepEqual(dummyValidContext.builtin);
done();
});
});
describe('Test validate built in context', function(){
it('Check validation of invalid built in context', function(done) {
const result = handler.validateBuiltinContext(dummyInvalidContext);
result.valid.should.equal(false);
done();
});
it('Check validation of valid built in context', function(done) {
const result = handler.validateBuiltinContext(dummyValidContext);
result.valid.should.equal(true);
done();
})
})
});