chat-platform 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +17 -0
- package/__tests__/chat-platform.js +223 -0
- package/__tests__/context-provider-memory.js +222 -0
- package/__tests__/context-provider-plain-file.js +174 -0
- package/__tests__/context-provider-sqlite.js +239 -0
- package/__tests__/dummy/audio.mp3 +0 -0
- package/__tests__/dummy/file.bin +0 -0
- package/__tests__/dummy/file.mp4 +0 -0
- package/__tests__/dummy/file.pdf +0 -0
- package/__tests__/dummy/image.png +0 -0
- package/__tests__/dummy/mission-control.backup +0 -0
- package/__tests__/dummy/video.mov +0 -0
- package/__tests__/universal-platform.js +190 -0
- package/blank/empty.sqlite +0 -0
- package/chat-context-factory.js +92 -0
- package/chat-log.js +115 -0
- package/chat-platform.js +1276 -0
- package/helpers/lcd.js +120 -0
- package/helpers/promises-queue.js +41 -0
- package/helpers/utils.js +25 -0
- package/index.js +6 -0
- package/jest.config.js +5 -0
- package/lib/lcd.js +151 -0
- package/lib/red-stub.js +212 -0
- package/lib/utils.js +29 -0
- package/model.nlp +688 -0
- package/package.json +32 -0
- package/providers/memory.js +170 -0
- package/providers/plain-file.js +345 -0
- package/providers/sqlite.js +267 -0
- package/universal.js +67 -0
package/.eslintrc
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"globals": {
|
|
3
|
+
"Promise": true
|
|
4
|
+
},
|
|
5
|
+
"env": {
|
|
6
|
+
"node": true,
|
|
7
|
+
"es6": true
|
|
8
|
+
},
|
|
9
|
+
"extends": "eslint:recommended",
|
|
10
|
+
"rules": {
|
|
11
|
+
"quotes": ["error", "single"],
|
|
12
|
+
"no-useless-escape": 0
|
|
13
|
+
},
|
|
14
|
+
"parserOptions": {
|
|
15
|
+
"ecmaVersion": 8
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
const moment = require('moment');
|
|
2
|
+
const assert = require('chai').assert;
|
|
3
|
+
const RED = require('../lib/red-stub')();
|
|
4
|
+
const ChatExpress = require('../chat-platform');
|
|
5
|
+
const ContextProviders = require('../chat-context-factory');
|
|
6
|
+
const EventEmitter = require('events').EventEmitter;
|
|
7
|
+
const inherits = require('util').inherits;
|
|
8
|
+
const contextProviders = ContextProviders(RED);
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const Connector = function() {
|
|
12
|
+
this.send = message => this.emit('message', message);
|
|
13
|
+
return this;
|
|
14
|
+
};
|
|
15
|
+
inherits(Connector, EventEmitter);
|
|
16
|
+
const connector = new Connector();
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const promisify = (chatServer, payload) => new Promise(
|
|
20
|
+
resolve => {
|
|
21
|
+
chatServer.on('start', () => connector.send(payload));
|
|
22
|
+
chatServer.on('message', message => resolve(message));
|
|
23
|
+
chatServer.start();
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
describe('Chat platform framework', () => {
|
|
28
|
+
|
|
29
|
+
it('should create a simple platform and retain options', async () => {
|
|
30
|
+
|
|
31
|
+
const GenericPlatform = new ChatExpress({
|
|
32
|
+
optionKey1: 'value1',
|
|
33
|
+
chatIdKey: 'myChatIdKey',
|
|
34
|
+
userIdKey: 'myUserIdKey',
|
|
35
|
+
transport: 'generic',
|
|
36
|
+
inboundMessageEvent: 'message',
|
|
37
|
+
connector: connector,
|
|
38
|
+
debug: false
|
|
39
|
+
});
|
|
40
|
+
const chatServer = GenericPlatform.createServer({
|
|
41
|
+
optionKey2: 'value2',
|
|
42
|
+
contextProvider: contextProviders.getProvider('memory')
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
assert.equal(chatServer.getOptions().optionKey1, 'value1');
|
|
46
|
+
assert.equal(chatServer.getOptions().optionKey2, 'value2');
|
|
47
|
+
|
|
48
|
+
const message = await promisify(chatServer, {
|
|
49
|
+
myChatIdKey: '52',
|
|
50
|
+
myUserIdKey: '62',
|
|
51
|
+
type: 'a_kind_of_magic'
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
assert.equal(message.originalMessage.myChatIdKey, '52');
|
|
55
|
+
assert.equal(message.originalMessage.chatId, '52');
|
|
56
|
+
assert.equal(message.originalMessage.myUserIdKey, '62');
|
|
57
|
+
assert.equal(message.originalMessage.userId, '62');
|
|
58
|
+
assert.equal(message.originalMessage.transport, 'generic');
|
|
59
|
+
assert.equal(message.originalMessage.type, 'a_kind_of_magic');
|
|
60
|
+
assert.equal(message.payload.type, 'a_kind_of_magic');
|
|
61
|
+
assert.equal(message.payload.chatId, '52');
|
|
62
|
+
assert.equal(message.payload.userId, '62');
|
|
63
|
+
assert.equal(message.payload.inbound, true);
|
|
64
|
+
assert.equal(message.payload.transport, 'generic');
|
|
65
|
+
assert.isFunction(message.chat);
|
|
66
|
+
const variables = message.chat().all();
|
|
67
|
+
assert.equal(variables.pending, false);
|
|
68
|
+
assert.equal(variables.authorized, false);
|
|
69
|
+
assert.equal(variables.authorized, false);
|
|
70
|
+
const chatContext = message.chat();
|
|
71
|
+
assert.equal(chatContext.get('transport'), 'generic');
|
|
72
|
+
assert.equal(chatContext.get('chatId'), '52');
|
|
73
|
+
assert.equal(chatContext.get('userId'), '62');
|
|
74
|
+
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should create a simple platform with callbacks', async () => {
|
|
78
|
+
const GenericPlatform = new ChatExpress({
|
|
79
|
+
chatIdKey: payload => payload.myChatIdKey,
|
|
80
|
+
userIdKey: payload => payload.myUserIdKey,
|
|
81
|
+
type: payload => payload.type,
|
|
82
|
+
tsKey: () => moment(),
|
|
83
|
+
transport: 'generic',
|
|
84
|
+
inboundMessageEvent: 'message',
|
|
85
|
+
connector: connector,
|
|
86
|
+
debug: false
|
|
87
|
+
});
|
|
88
|
+
const chatServer = GenericPlatform.createServer({
|
|
89
|
+
contextProvider: contextProviders.getProvider('memory')
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const message = await promisify(chatServer, {
|
|
93
|
+
myChatIdKey: '52',
|
|
94
|
+
myUserIdKey: '62',
|
|
95
|
+
type: 'a_kind_of_magic'
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
assert.equal(message.originalMessage.myChatIdKey, '52');
|
|
99
|
+
assert.equal(message.originalMessage.myUserIdKey, '62');
|
|
100
|
+
assert.equal(message.originalMessage.type, 'a_kind_of_magic');
|
|
101
|
+
assert.equal(message.payload.type, 'a_kind_of_magic');
|
|
102
|
+
assert.equal(message.payload.chatId, '52');
|
|
103
|
+
assert.equal(message.payload.userId, '62');
|
|
104
|
+
assert.equal(message.payload.inbound, true);
|
|
105
|
+
assert.equal(message.payload.transport, 'generic');
|
|
106
|
+
assert.equal(message.payload.ts.isValid(), true);
|
|
107
|
+
assert.isFunction(message.chat);
|
|
108
|
+
const variables = message.chat().all();
|
|
109
|
+
assert.equal(variables.pending, false);
|
|
110
|
+
assert.equal(variables.authorized, false);
|
|
111
|
+
assert.equal(variables.authorized, false);
|
|
112
|
+
const chatContext = message.chat();
|
|
113
|
+
assert.equal(chatContext.get('transport'), 'generic');
|
|
114
|
+
assert.equal(chatContext.get('chatId'), '52');
|
|
115
|
+
assert.equal(chatContext.get('userId'), '62');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should create a platform with middlewares', async () => {
|
|
119
|
+
const GenericPlatform = new ChatExpress({
|
|
120
|
+
chatIdKey: 'myChatIdKey',
|
|
121
|
+
userIdKey: 'myUserIdKey',
|
|
122
|
+
transport: 'generic',
|
|
123
|
+
inboundMessageEvent: 'message',
|
|
124
|
+
connector: connector,
|
|
125
|
+
debug: false
|
|
126
|
+
});
|
|
127
|
+
GenericPlatform.use(message => {
|
|
128
|
+
message.payload.customKey = 'value';
|
|
129
|
+
return message;
|
|
130
|
+
});
|
|
131
|
+
GenericPlatform.in('a_kind_of_magic', message => {
|
|
132
|
+
message.payload.customKey2 = 'value2';
|
|
133
|
+
return message;
|
|
134
|
+
});
|
|
135
|
+
GenericPlatform.in('another_type', message => {
|
|
136
|
+
message.payload.customKey2 = 'value3';
|
|
137
|
+
return message;
|
|
138
|
+
});
|
|
139
|
+
GenericPlatform.in(message => {
|
|
140
|
+
message.payload.customKey3 = 'value3';
|
|
141
|
+
message.chat().set('customVar', 'ahaha');
|
|
142
|
+
return message;
|
|
143
|
+
});
|
|
144
|
+
GenericPlatform.registerMessageType('a_kind_of_magic', 'A Kind of Magic');
|
|
145
|
+
|
|
146
|
+
assert.isTrue(ChatExpress.isSupported('generic'));
|
|
147
|
+
assert.isTrue(ChatExpress.isSupported('generic', 'a_kind_of_magic'));
|
|
148
|
+
assert.isFalse(ChatExpress.isSupported('i_dont_exists'));
|
|
149
|
+
assert.isFalse(ChatExpress.isSupported('generic', 'strange_type'));
|
|
150
|
+
|
|
151
|
+
const chatServer = GenericPlatform.createServer({
|
|
152
|
+
contextProvider: contextProviders.getProvider('memory')
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const message = await promisify(chatServer, {
|
|
156
|
+
myChatIdKey: '52',
|
|
157
|
+
myUserIdKey: '62',
|
|
158
|
+
type: 'a_kind_of_magic'
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
assert.equal(message.originalMessage.myChatIdKey, '52');
|
|
162
|
+
assert.equal(message.originalMessage.myUserIdKey, '62');
|
|
163
|
+
assert.equal(message.originalMessage.type, 'a_kind_of_magic');
|
|
164
|
+
assert.equal(message.payload.type, 'a_kind_of_magic');
|
|
165
|
+
assert.equal(message.payload.chatId, '52');
|
|
166
|
+
assert.equal(message.payload.userId, '62');
|
|
167
|
+
assert.equal(message.payload.inbound, true);
|
|
168
|
+
assert.equal(message.payload.transport, 'generic');
|
|
169
|
+
assert.equal(message.payload.customKey, 'value');
|
|
170
|
+
assert.equal(message.payload.customKey2, 'value2');
|
|
171
|
+
assert.equal(message.payload.customKey3, 'value3');
|
|
172
|
+
assert.isFunction(message.chat);
|
|
173
|
+
const variables = message.chat().all();
|
|
174
|
+
assert.equal(variables.pending, false);
|
|
175
|
+
assert.equal(variables.authorized, false);
|
|
176
|
+
assert.equal(variables.authorized, false);
|
|
177
|
+
assert.equal(variables.customVar, 'ahaha');
|
|
178
|
+
const chatContext = message.chat();
|
|
179
|
+
assert.equal(chatContext.get('transport'), 'generic');
|
|
180
|
+
assert.equal(chatContext.get('chatId'), '52');
|
|
181
|
+
assert.equal(chatContext.get('userId'), '62');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should default userId on chatId', async () => {
|
|
185
|
+
const GenericPlatform = new ChatExpress({
|
|
186
|
+
chatIdKey: 'myChatIdKey',
|
|
187
|
+
userIdKey: 'myUserIdKey',
|
|
188
|
+
transport: 'generic',
|
|
189
|
+
inboundMessageEvent: 'message',
|
|
190
|
+
connector: connector,
|
|
191
|
+
debug: false
|
|
192
|
+
});
|
|
193
|
+
const chatServer = GenericPlatform.createServer({
|
|
194
|
+
contextProvider: contextProviders.getProvider('memory')
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const message = await promisify(chatServer, {
|
|
198
|
+
myChatIdKey: '52',
|
|
199
|
+
type: 'a_kind_of_magic'
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
assert.equal(message.originalMessage.myChatIdKey, '52');
|
|
203
|
+
assert.equal(message.originalMessage.chatId, '52');
|
|
204
|
+
assert.equal(message.originalMessage.userId, '52');
|
|
205
|
+
assert.equal(message.originalMessage.transport, 'generic');
|
|
206
|
+
assert.equal(message.originalMessage.type, 'a_kind_of_magic');
|
|
207
|
+
assert.equal(message.payload.type, 'a_kind_of_magic');
|
|
208
|
+
assert.equal(message.payload.chatId, '52');
|
|
209
|
+
assert.equal(message.payload.userId, '52');
|
|
210
|
+
assert.equal(message.payload.inbound, true);
|
|
211
|
+
assert.equal(message.payload.transport, 'generic');
|
|
212
|
+
assert.isFunction(message.chat);
|
|
213
|
+
const variables = message.chat().all();
|
|
214
|
+
assert.equal(variables.pending, false);
|
|
215
|
+
assert.equal(variables.authorized, false);
|
|
216
|
+
assert.equal(variables.authorized, false);
|
|
217
|
+
const chatContext = message.chat();
|
|
218
|
+
assert.equal(chatContext.get('transport'), 'generic');
|
|
219
|
+
assert.equal(chatContext.get('chatId'), '52');
|
|
220
|
+
assert.equal(chatContext.get('userId'), '52');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
});
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
const _ = require('underscore');
|
|
2
|
+
const assert = require('chai').assert;
|
|
3
|
+
const RED = require('../lib/red-stub')();
|
|
4
|
+
const ContextProviders = require('../chat-context-factory');
|
|
5
|
+
const { when } = require('../lib/utils');
|
|
6
|
+
|
|
7
|
+
describe('Chat context provider memory', () => {
|
|
8
|
+
|
|
9
|
+
const contextProviders = ContextProviders(RED);
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
const provider = contextProviders.getProvider('memory');
|
|
13
|
+
provider.reset();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should create a context provider with some default params with chatId', () => {
|
|
17
|
+
|
|
18
|
+
assert.isTrue(contextProviders.hasProvider('memory'));
|
|
19
|
+
const provider = contextProviders.getProvider('memory');
|
|
20
|
+
assert.isFunction(provider.getOrCreate);
|
|
21
|
+
assert.isFunction(provider.get);
|
|
22
|
+
|
|
23
|
+
return when(provider.getOrCreate(43, null, { myVariable: 'initial value'}))
|
|
24
|
+
.then(chatContext => {
|
|
25
|
+
assert.isFunction(chatContext.get);
|
|
26
|
+
assert.isFunction(chatContext.set);
|
|
27
|
+
assert.isFunction(chatContext.all);
|
|
28
|
+
assert.isFunction(chatContext.remove);
|
|
29
|
+
assert.isFunction(chatContext.clear);
|
|
30
|
+
return chatContext.get('myVariable');
|
|
31
|
+
}).then(myVariable => {
|
|
32
|
+
assert.equal(myVariable, 'initial value');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should create a context provider with some default params with userId', () => {
|
|
37
|
+
|
|
38
|
+
assert.isTrue(contextProviders.hasProvider('memory'));
|
|
39
|
+
const provider = contextProviders.getProvider('memory');
|
|
40
|
+
assert.isFunction(provider.getOrCreate);
|
|
41
|
+
assert.isFunction(provider.get);
|
|
42
|
+
|
|
43
|
+
return when(provider.getOrCreate(null, 44, { myVariable: 'initial value'}))
|
|
44
|
+
.then(chatContext => {
|
|
45
|
+
assert.isFunction(chatContext.get);
|
|
46
|
+
assert.isFunction(chatContext.set);
|
|
47
|
+
assert.isFunction(chatContext.all);
|
|
48
|
+
assert.isFunction(chatContext.remove);
|
|
49
|
+
assert.isFunction(chatContext.clear);
|
|
50
|
+
return chatContext.get('myVariable');
|
|
51
|
+
}).then(myVariable => {
|
|
52
|
+
assert.equal(myVariable, 'initial value');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
it('should set some value and then get and remove it with chatId', () => {
|
|
58
|
+
|
|
59
|
+
const provider = contextProviders.getProvider('memory');
|
|
60
|
+
|
|
61
|
+
return when(provider.getOrCreate(42, null, {}))
|
|
62
|
+
.then(chatContext => chatContext.set('firstName', 'Guidone'))
|
|
63
|
+
.then(() => when(provider.get(42).get('firstName')))
|
|
64
|
+
.then(firstName => assert.equal(firstName, 'Guidone'))
|
|
65
|
+
.then(() => when(provider.get(42).remove('firstName')))
|
|
66
|
+
.then(() => when(provider.get(42).get('firstName')))
|
|
67
|
+
.then(
|
|
68
|
+
() => {},
|
|
69
|
+
firstName => {
|
|
70
|
+
assert.isUndefined(firstName);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should set some value and then get and remove it with userId', () => {
|
|
75
|
+
|
|
76
|
+
const provider = contextProviders.getProvider('memory');
|
|
77
|
+
|
|
78
|
+
return when(provider.getOrCreate(null, 43, {}))
|
|
79
|
+
.then(chatContext => chatContext.set('firstName', 'Guidone'))
|
|
80
|
+
.then(() => when(provider.get(null, 43).get('firstName')))
|
|
81
|
+
.then(firstName => assert.equal(firstName, 'Guidone'))
|
|
82
|
+
.then(() => when(provider.get(null, 43).remove('firstName')))
|
|
83
|
+
.then(() => when(provider.get(null, 43).get('firstName')))
|
|
84
|
+
.then(
|
|
85
|
+
() => {},
|
|
86
|
+
firstName => {
|
|
87
|
+
assert.isUndefined(firstName);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should set some values and then get and remove it with chatId', () => {
|
|
92
|
+
|
|
93
|
+
const provider = contextProviders.getProvider('memory');
|
|
94
|
+
|
|
95
|
+
return when(provider.getOrCreate(42, null, {}))
|
|
96
|
+
.then(chatContext => chatContext.set({firstName: 'Guido', lastName: 'Bellomo'}))
|
|
97
|
+
.then(() => when(provider.get(42).get('firstName')))
|
|
98
|
+
.then(firstName => assert.equal(firstName, 'Guido'))
|
|
99
|
+
.then(() =>when(provider.get(42).get('lastName')))
|
|
100
|
+
.then(lastName => assert.equal(lastName, 'Bellomo'))
|
|
101
|
+
.then(() => when(provider.get(42).get('firstName', 'lastName')))
|
|
102
|
+
.then(json => {
|
|
103
|
+
assert.isObject(json);
|
|
104
|
+
assert.equal(json.firstName, 'Guido');
|
|
105
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should set some values and then get and remove it with userId', () => {
|
|
110
|
+
|
|
111
|
+
const provider = contextProviders.getProvider('memory');
|
|
112
|
+
|
|
113
|
+
return when(provider.getOrCreate(null, 43, {}))
|
|
114
|
+
.then(chatContext => chatContext.set({firstName: 'Guido', lastName: 'Bellomo'}))
|
|
115
|
+
.then(() => when(provider.get(null, 43).get('firstName')))
|
|
116
|
+
.then(firstName => assert.equal(firstName, 'Guido'))
|
|
117
|
+
.then(() =>when(provider.get(null, 43).get('lastName')))
|
|
118
|
+
.then(lastName => assert.equal(lastName, 'Bellomo'))
|
|
119
|
+
.then(() => when(provider.get(null, 43).get('firstName', 'lastName')))
|
|
120
|
+
.then(json => {
|
|
121
|
+
assert.isObject(json);
|
|
122
|
+
assert.equal(json.firstName, 'Guido');
|
|
123
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should set some values and get the dump with chatId', () => {
|
|
128
|
+
|
|
129
|
+
const provider = contextProviders.getProvider('memory');
|
|
130
|
+
|
|
131
|
+
return when(provider.getOrCreate(42, null, {}))
|
|
132
|
+
.then(chatContext => chatContext.set({firstName: 'Guido', lastName: 'Bellomo', email: 'spam@gmail.com'}))
|
|
133
|
+
.then(() => when(provider.get(42).all()))
|
|
134
|
+
.then(json => {
|
|
135
|
+
assert.isObject(json);
|
|
136
|
+
assert.equal(json.firstName, 'Guido');
|
|
137
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
138
|
+
assert.equal(json.email, 'spam@gmail.com');
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should set some values and get the dump with userId', () => {
|
|
143
|
+
|
|
144
|
+
const provider = contextProviders.getProvider('memory');
|
|
145
|
+
|
|
146
|
+
return when(provider.getOrCreate(null, 43, {}))
|
|
147
|
+
.then(chatContext => chatContext.set({firstName: 'Guido', lastName: 'Bellomo', email: 'spam@gmail.com'}))
|
|
148
|
+
.then(() => when(provider.get(null, 43).all()))
|
|
149
|
+
.then(json => {
|
|
150
|
+
assert.isObject(json);
|
|
151
|
+
assert.equal(json.firstName, 'Guido');
|
|
152
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
153
|
+
assert.equal(json.email, 'spam@gmail.com');
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should set some values and remove all with chatId', () => {
|
|
158
|
+
|
|
159
|
+
const provider = contextProviders.getProvider('memory');
|
|
160
|
+
|
|
161
|
+
return when(provider.getOrCreate(42, null, {}))
|
|
162
|
+
.then(chatContext => chatContext.set({firstName: 'Guido', lastName: 'Bellomo'}))
|
|
163
|
+
.then(() => when(provider.get(42).clear()))
|
|
164
|
+
.then(() => when(provider.get(42).all()))
|
|
165
|
+
.then(json => {
|
|
166
|
+
assert.isObject(json);
|
|
167
|
+
assert.isTrue(_.isEmpty(json));
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should set some values and remove all with userId', () => {
|
|
172
|
+
|
|
173
|
+
const provider = contextProviders.getProvider('memory');
|
|
174
|
+
|
|
175
|
+
return when(provider.getOrCreate(null, 43, {}))
|
|
176
|
+
.then(chatContext => chatContext.set({firstName: 'Guido', lastName: 'Bellomo'}))
|
|
177
|
+
.then(() => when(provider.get(null, 43).clear()))
|
|
178
|
+
.then(() => when(provider.get(null, 43).all()))
|
|
179
|
+
.then(json => {
|
|
180
|
+
assert.isObject(json);
|
|
181
|
+
assert.isTrue(_.isEmpty(json));
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should set some value the remove with multiple arguments', () => {
|
|
186
|
+
|
|
187
|
+
const provider = contextProviders.getProvider('memory');
|
|
188
|
+
|
|
189
|
+
return when(provider.getOrCreate(42, null, {}))
|
|
190
|
+
.then(chatContext =>chatContext.set({firstName: 'Guidone', lastName: 'Bellomo', email: 'some@email'}))
|
|
191
|
+
.then(() => when(provider.get(42).get('firstName')))
|
|
192
|
+
.then(firstName => assert.equal(firstName, 'Guidone'))
|
|
193
|
+
.then(() => when(provider.get(42).remove('firstName', 'lastName', 'email')))
|
|
194
|
+
.then(() => when(provider.get(42).all()))
|
|
195
|
+
.then(json => {
|
|
196
|
+
assert.isUndefined(json.firstName);
|
|
197
|
+
assert.isUndefined(json.lastName);
|
|
198
|
+
assert.isUndefined(json.email);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should set some value with chatId and userId', () => {
|
|
203
|
+
const provider = contextProviders.getProvider('memory');
|
|
204
|
+
return when(provider.getOrCreate(42, 44, { myVar: 123 }))
|
|
205
|
+
.then(chatContext => chatContext.set('firstName', 'Guidone'))
|
|
206
|
+
.then(() => when(provider.get(null, 44).get('firstName')))
|
|
207
|
+
.then(firstName => assert.equal(firstName, 'Guidone'))
|
|
208
|
+
.then(() => when(provider.get(null, 44, { myVar: 123 })))
|
|
209
|
+
.then(chatContext => {
|
|
210
|
+
assert.equal(chatContext.get('firstName'), 'Guidone');
|
|
211
|
+
assert.equal(chatContext.get('myVar'), 123);
|
|
212
|
+
})
|
|
213
|
+
.then(() => when(provider.get(null, 44).remove('firstName')))
|
|
214
|
+
.then(() => when(provider.get(null, 44).get('firstName')))
|
|
215
|
+
/*.then(
|
|
216
|
+
() => {},
|
|
217
|
+
firstName => {
|
|
218
|
+
assert.isUndefined(firstName);
|
|
219
|
+
});*/
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
const _ = require('underscore');
|
|
2
|
+
const assert = require('chai').assert;
|
|
3
|
+
const RED = require('../lib/red-stub')();
|
|
4
|
+
const ContextProviders = require('../chat-context-factory');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const { when } = require('../lib/utils');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
describe('Chat context provider file', () => {
|
|
11
|
+
|
|
12
|
+
const contextProviders = ContextProviders(RED);
|
|
13
|
+
//const path = os.tmpdir();
|
|
14
|
+
const path = __dirname + '/test-files';
|
|
15
|
+
const provider = contextProviders.getProvider('plain-file', { path });
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
return provider.reset({ path });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterAll(() => {
|
|
22
|
+
return provider.reset({ path });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should create a context provider with some default params', () => {
|
|
26
|
+
|
|
27
|
+
assert.isTrue(contextProviders.hasProvider('plain-file'));
|
|
28
|
+
const provider = contextProviders.getProvider('plain-file', { path });
|
|
29
|
+
assert.isFunction(provider.getOrCreate);
|
|
30
|
+
assert.isFunction(provider.get);
|
|
31
|
+
|
|
32
|
+
return when(provider.getOrCreate(42, null, { myVariable: 'initial value'}))
|
|
33
|
+
.then(chatContext => {
|
|
34
|
+
assert.isFunction(chatContext.get);
|
|
35
|
+
assert.isFunction(chatContext.set);
|
|
36
|
+
assert.isFunction(chatContext.all);
|
|
37
|
+
assert.isFunction(chatContext.remove);
|
|
38
|
+
assert.isFunction(chatContext.clear);
|
|
39
|
+
return chatContext.get('myVariable');
|
|
40
|
+
}).then(myVariable => {
|
|
41
|
+
assert.equal(myVariable, 'initial value');
|
|
42
|
+
// assert.isTrue(fs.existsSync(`${path}/store-7369f3c86bf3c0a354615432832d9e8f.json`));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should set some value and then get and remove it', async () => {
|
|
47
|
+
const chatContext = await provider.getOrCreate(42, null, {});
|
|
48
|
+
await chatContext.set('firstName', 'Guidone');
|
|
49
|
+
let firstName =await chatContext.get('firstName');
|
|
50
|
+
assert.equal(firstName, 'Guidone');
|
|
51
|
+
await chatContext.remove('firstName');
|
|
52
|
+
firstName = await chatContext.get('firstName');
|
|
53
|
+
assert.isUndefined(firstName);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
it('should set some values and then get and remove it', async () => {
|
|
58
|
+
const chatContext = await provider.getOrCreate(42, null, {});
|
|
59
|
+
await chatContext.set({firstName: 'Guido', lastName: 'Bellomo'});
|
|
60
|
+
let firstName = await chatContext.get('firstName');
|
|
61
|
+
assert.equal(firstName, 'Guido');
|
|
62
|
+
let lastName = await chatContext.get('lastName');
|
|
63
|
+
assert.equal(lastName, 'Bellomo');
|
|
64
|
+
let json = await chatContext.get('firstName', 'lastName');
|
|
65
|
+
assert.isObject(json);
|
|
66
|
+
assert.equal(json.firstName, 'Guido');
|
|
67
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should set some values and then get and remove it with userId', async () => {
|
|
71
|
+
const chatContext = await provider.getOrCreate(null, 43, {});
|
|
72
|
+
await chatContext.set({ firstName: 'Guido', lastName: 'Bellomo' });
|
|
73
|
+
let firstName = await chatContext.get('firstName');
|
|
74
|
+
assert.equal(firstName, 'Guido');
|
|
75
|
+
let lastName = await chatContext.get('lastName');
|
|
76
|
+
assert.equal(lastName, 'Bellomo');
|
|
77
|
+
let json = await chatContext.get('firstName', 'lastName');
|
|
78
|
+
assert.isObject(json);
|
|
79
|
+
assert.equal(json.firstName, 'Guido');
|
|
80
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should set some values and get the dump with chatId', async () => {
|
|
84
|
+
const chatContext = await provider.getOrCreate(42, null, {});
|
|
85
|
+
await chatContext.set({firstName: 'Guido', lastName: 'Bellomo', email: 'spam@gmail.com'});
|
|
86
|
+
const json = await chatContext.all();
|
|
87
|
+
assert.isObject(json);
|
|
88
|
+
assert.equal(json.firstName, 'Guido');
|
|
89
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
90
|
+
assert.equal(json.email, 'spam@gmail.com');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should set some values and get the dump with userId', async () => {
|
|
94
|
+
const chatContext = await provider.getOrCreate(null, 43, {});
|
|
95
|
+
await chatContext.set({firstName: 'Guido', lastName: 'Bellomo', email: 'spam@gmail.com'});
|
|
96
|
+
const json = await chatContext.all();
|
|
97
|
+
assert.isObject(json);
|
|
98
|
+
assert.equal(json.firstName, 'Guido');
|
|
99
|
+
assert.equal(json.lastName, 'Bellomo');
|
|
100
|
+
assert.equal(json.email, 'spam@gmail.com');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should set some values and remove all for chatId', async () => {
|
|
104
|
+
const chatContext = await provider.getOrCreate(42, {});
|
|
105
|
+
await chatContext.set({firstName: 'Guido', lastName: 'Bellomo'});
|
|
106
|
+
await chatContext.clear();
|
|
107
|
+
let firstName = await chatContext.get('firstName');
|
|
108
|
+
assert.isUndefined(firstName);
|
|
109
|
+
let lastName = await chatContext.get('lastName');
|
|
110
|
+
assert.isUndefined(lastName);
|
|
111
|
+
const json = await chatContext.all();
|
|
112
|
+
assert.isObject(json);
|
|
113
|
+
assert.isTrue(_.isEmpty(json));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should set some values and remove all for userId', async () => {
|
|
117
|
+
const chatContext = await provider.getOrCreate(null, 43, {});
|
|
118
|
+
await chatContext.set({firstName: 'Guido', lastName: 'Bellomo'});
|
|
119
|
+
await chatContext.clear();
|
|
120
|
+
const firstName = await chatContext.get('firstName');
|
|
121
|
+
assert.isUndefined(firstName)
|
|
122
|
+
const lastName = await chatContext.get('lastName');
|
|
123
|
+
assert.isUndefined(lastName)
|
|
124
|
+
const json = await chatContext.all();
|
|
125
|
+
assert.isObject(json);
|
|
126
|
+
assert.isTrue(_.isEmpty(json));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should set some value the remove with multiple arguments for chatId', async () => {
|
|
130
|
+
const chatContext = await provider.getOrCreate(42, {});
|
|
131
|
+
await chatContext.set({firstName: 'Guidone', lastName: 'Bellomo', email: 'some@email'})
|
|
132
|
+
const firstName = await chatContext.get('firstName');
|
|
133
|
+
assert.equal(firstName, 'Guidone');
|
|
134
|
+
await chatContext.remove('firstName', 'lastName', 'email');
|
|
135
|
+
const json = await chatContext.all();
|
|
136
|
+
assert.isUndefined(json.firstName);
|
|
137
|
+
assert.isUndefined(json.lastName);
|
|
138
|
+
assert.isUndefined(json.email);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should set some value the remove with multiple arguments for userId', async () => {
|
|
142
|
+
const chatContext = await provider.getOrCreate(null, 43, {});
|
|
143
|
+
await chatContext.set({firstName: 'Guidone', lastName: 'Bellomo', email: 'some@email'});
|
|
144
|
+
const firstName = await chatContext.get('firstName');
|
|
145
|
+
assert.equal(firstName, 'Guidone');
|
|
146
|
+
await chatContext.remove('firstName', 'lastName', 'email');
|
|
147
|
+
const json = await chatContext.all();
|
|
148
|
+
assert.isUndefined(json.firstName);
|
|
149
|
+
assert.isUndefined(json.lastName);
|
|
150
|
+
assert.isUndefined(json.email);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should set some value with chatId and userId', async () => {
|
|
154
|
+
const chatContext = await provider.getOrCreate(42, 44, { userId: 44, chatId: 42 });
|
|
155
|
+
await chatContext.set('firstName', 'Guidone');
|
|
156
|
+
let firstName = await chatContext.get('firstName');
|
|
157
|
+
assert.equal(firstName, 'Guidone');
|
|
158
|
+
let userId = await chatContext.get('userId');
|
|
159
|
+
assert.equal(userId, 44);
|
|
160
|
+
let chatId = await chatContext.get('chatId');
|
|
161
|
+
assert.equal(chatId, 42);
|
|
162
|
+
const chatContext2 = await provider.getOrCreate(null, 44, { userId: 44, chatId: 42 });
|
|
163
|
+
firstName = await chatContext2.get('firstName');
|
|
164
|
+
assert.equal(firstName, 'Guidone');
|
|
165
|
+
userId = await chatContext2.get('userId');
|
|
166
|
+
assert.equal(userId, 44);
|
|
167
|
+
chatId = await chatContext2.get('chatId');
|
|
168
|
+
assert.equal(chatId, 42);
|
|
169
|
+
await chatContext2.remove('firstName');
|
|
170
|
+
firstName = await chatContext2.get('firstName');
|
|
171
|
+
assert.isUndefined(firstName);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
});
|