@tiledesk/tiledesk-server 2.4.100 → 2.4.101

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ const requestMock = {
2
+ "_id": "652e46f283a36264e8e8b331",
3
+ "status": 200,
4
+ "preflight": false,
5
+ "hasBot": false,
6
+ "participants": [
7
+ "652e46f183a36264e8e8b256"
8
+ ],
9
+ "priority": "medium",
10
+ "followers": [],
11
+ "participantsAgents": [
12
+ "652e46f183a36264e8e8b256"
13
+ ],
14
+ "participantsBots": [],
15
+ "request_id": "support-group-652e46f183a36264e8e8b27b-16c334bd0d214579a4c98f2f0e7794ad",
16
+ "requester": "652e46f183a36264e8e8b299",
17
+ "lead": "652e46f283a36264e8e8b323",
18
+ "first_text": "first_text",
19
+ "department": "652e46f183a36264e8e8b2a7",
20
+ "assigned_at": new Date('2023-10-17T08:45:54.058Z'),
21
+ "id_project": "652e46f183a36264e8e8b27b",
22
+ "createdBy": "652e46f183a36264e8e8b256",
23
+ "channel": {
24
+ "name": "chat21"
25
+ },
26
+ "createdAt": new Date('2023-10-19T08:45:54.058Z')
27
+ }
28
+
29
+ // const requestMock = {
30
+ // "_id": "652e46f283a36264e8e8b331",
31
+ // "status": 200,
32
+ // "preflight": false,
33
+ // "hasBot": false,
34
+ // "participants": [
35
+ // "652e46f183a36264e8e8b256"
36
+ // ],
37
+ // "priority": "medium",
38
+ // "followers": [],
39
+ // "participantsAgents": [
40
+ // "652e46f183a36264e8e8b256"
41
+ // ],
42
+ // "participantsBots": [],
43
+ // "request_id": "support-group-652e46f183a36264e8e8b27b-16c334bd0d214579a4c98f2f0e7794ad",
44
+ // "requester": "652e46f183a36264e8e8b299",
45
+ // "lead": "652e46f283a36264e8e8b323",
46
+ // "first_text": "first_text",
47
+ // "department": "652e46f183a36264e8e8b2a7",
48
+ // "assigned_at": new Date('2023-10-17T08:45:54.058Z'),
49
+ // "id_project": "652e46f183a36264e8e8b27b",
50
+ // "createdBy": "652e46f183a36264e8e8b256",
51
+ // "channel": {
52
+ // "name": "chat21"
53
+ // },
54
+ // "snapshot": {
55
+ // "department": {
56
+ // "routing": "assigned",
57
+ // "default": true,
58
+ // "status": 1,
59
+ // "_id": "652e46f183a36264e8e8b2a7",
60
+ // "name": "Default Department",
61
+ // "id_project": "652e46f183a36264e8e8b27b",
62
+ // "createdBy": "652e46f183a36264e8e8b256",
63
+ // "tags": [],
64
+ // "createdAt": new Date('2023-10-17T08:41:48.140Z'),
65
+ // "updatedAt": new Date('2023-10-17T08:41:48.140Z'),
66
+ // "__v": 0
67
+ // },
68
+ // "agents": [
69
+ // {
70
+ // "user_available": true,
71
+ // "number_assigned_requests": 0,
72
+ // "last_login_at": new Date('2023-10-17T08:33:51.266Z'),
73
+ // "status": "active",
74
+ // "_id": "652e46f183a36264e8e8b299",
75
+ // "id_project": "652e46f183a36264e8e8b27b",
76
+ // "id_user": "652e46f183a36264e8e8b256",
77
+ // "role": "owner",
78
+ // "createdBy": "652e46f183a36264e8e8b256",
79
+ // "tags": [],
80
+ // "createdAt": new Date('2023-10-17T08:33:53.484Z'),
81
+ // "updatedAt": new Date('2023-10-17T08:33:53.484Z'),
82
+ // "__v": 0
83
+ // }
84
+ // ],
85
+ // "availableAgentsCount": 1,
86
+ // "requester": {
87
+ // "user_available": true,
88
+ // "number_assigned_requests": 0,
89
+ // "last_login_at": new Date('2023-10-17T08:33:51.266Z'),
90
+ // "status": "active",
91
+ // "_id": "652e46f183a36264e8e8b299",
92
+ // "id_project": "652e46f183a36264e8e8b27b",
93
+ // "id_user": "652e46f183a36264e8e8b256",
94
+ // "role": "owner",
95
+ // "createdBy": "652e46f183a36264e8e8b256",
96
+ // "tags": [],
97
+ // "createdAt": new Date('2023-10-17T08:33:53.484Z'),
98
+ // "updatedAt": new Date('2023-10-17T08:33:53.484Z'),
99
+ // "__v": 0
100
+ // },
101
+ // "lead": {
102
+ // "tags": [],
103
+ // "status": 100,
104
+ // "_id": "652e46f283a36264e8e8b323",
105
+ // "lead_id": "652e46f183a36264e8e8b256",
106
+ // "fullname": "Test Firstname Test lastname",
107
+ // "email": "test-request-create-1697531633210@email.com",
108
+ // "id_project": "652e46f183a36264e8e8b27b",
109
+ // "createdBy": "system",
110
+ // "createdAt": new Date('2023-10-17T08:33:54.035Z'),
111
+ // "updatedAt": new Date('2023-10-17T08:33:54.035Z'),
112
+ // "__v": 0
113
+ // }
114
+ // },
115
+ // "tags": [],
116
+ // "notes": [],
117
+ // "channelOutbound": {
118
+ // "name": "chat21"
119
+ // },
120
+ // "smartAssignment": true,
121
+ // "createdAt": new Date('2023-10-19T08:45:54.058Z'),
122
+ // "updatedAt": new Date('2023-10-19T08:45:54.058Z'),
123
+ // "ticket_id": 1,
124
+ // "__v": 0
125
+ // }
126
+
127
+ module.exports = { requestMock }
@@ -0,0 +1,282 @@
1
+ const { QuoteManager } = require('../services/QuoteManager');
2
+ const pubModulesManager = require('../pubmodules/pubModulesManager'); // on constructor init is undefined beacusae pub module is loaded after
3
+ var projectService = require('../services/projectService');
4
+ var userService = require('../services/userService');
5
+
6
+
7
+ let chai = require('chai');
8
+ let chaiHttp = require('chai-http');
9
+ let server = require('../app');
10
+ let should = chai.should();
11
+
12
+ let expect = require('chai').expect;
13
+ let assert = require('chai').assert;
14
+
15
+ chai.use(chaiHttp);
16
+
17
+ // MOCK
18
+ const projectMock = require('./mock/projectMock');
19
+ const requestMock = require('./mock/requestMock');
20
+ const messageMock = require('./mock/messageMock');
21
+ const emailMock = require('./mock/emailMock');
22
+ const { MockTdCache } = require('./mock/MockTdCache');
23
+ const mockTdCache = new MockTdCache();
24
+
25
+ let log = true;
26
+
27
+ // CONNECT REDIS - CHECK IT
28
+ const { TdCache } = require('../utils/TdCache');
29
+ tdCache = new TdCache({
30
+ host: '127.0.0.1',
31
+ port: '6379'
32
+ });
33
+
34
+ tdCache.connect();
35
+ // var redis = require('redis')
36
+ // var redis_client;
37
+
38
+ // const dateList = [
39
+ // "2023-10-17T08:45:54.058Z",
40
+ // "2023-10-20T08:45:54.058Z",
41
+ // "2023-10-28T08:45:54.058Z",
42
+ // "2023-10-31T08:45:54.058Z",
43
+ // "2023-11-19T08:45:54.058Z",
44
+ // "2023-11-22T08:45:54.058Z",
45
+ // "2023-11-27T08:45:54.058Z",
46
+ // "2023-12-01T08:45:54.058Z",
47
+ // "2023-12-20T08:45:54.058Z",
48
+ // "2023-12-21T08:45:54.058Z"
49
+ // ]
50
+
51
+ const dateList = [
52
+ "2023-10-24T08:45:54.058Z",
53
+ "2023-10-25T08:45:54.058Z",
54
+ "2023-10-26T08:45:54.058Z",
55
+ "2023-10-27T08:45:54.058Z",
56
+ "2023-10-28T08:45:54.058Z",
57
+ "2023-10-29T08:45:54.058Z"
58
+ ]
59
+
60
+ // connectRedis();
61
+
62
+ // function connectRedis() {
63
+ // console.log(">>> connectRedis")
64
+ // redis_client = redis.createClient({
65
+ // host: "127.0.0.1",
66
+ // port: 6379,
67
+ // });
68
+
69
+ // redis_client.on('error', err => {
70
+ // console.log('(quoteManager TEST) Connect Redis Error ' + err);
71
+ // })
72
+
73
+ // redis_client.on('ready', () => {
74
+ // console.log("(quoteManager TEST) Redis ready!")
75
+ // })
76
+ // }
77
+
78
+
79
+
80
+ // let cacheClient = undefined;
81
+ // if (pubModulesManager.cache) {
82
+ // cacheClient = pubModulesManager.cache._cache._cache; //_cache._cache to jump directly to redis modules without cacheoose wrapper (don't support await)
83
+ // }
84
+
85
+ // let tdCache = undefined;
86
+ // if (cacheClient) {
87
+ // tdCache = cacheClient;
88
+ // console.log("using 'cacheClient' for the test")
89
+ // } else if (redis_client) {
90
+ // tdCache = redis_client
91
+ // console.log("using 'redis_client' for the test")
92
+ // } else {
93
+ // tdCache = mockTdCache;
94
+ // console.log("using 'mockTdCache' for the test")
95
+ // }
96
+
97
+ let quoteManager = new QuoteManager({ tdCache: tdCache });
98
+ quoteManager.start();
99
+
100
+
101
+ describe('QuoteManager', function () {
102
+
103
+
104
+
105
+ it('incrementRequestsCount', async function () {
106
+ let mockProject = projectMock.mockProjectSandboxPlan;
107
+ let mockRequest = requestMock.requestMock;
108
+
109
+
110
+ mockRequest.createdAt = new Date(dateList[0]);
111
+
112
+ let initial_quote = await quoteManager.getCurrentQuote(mockProject, mockRequest, 'requests');
113
+ if (log) { console.log("[Quote Test] initial_quote: ", initial_quote); }
114
+
115
+ let key_incremented = await quoteManager.incrementRequestsCount(mockProject, mockRequest);
116
+ if (log) { console.log("[Quote Test] key_incremented: ", key_incremented); }
117
+
118
+ let final_quote = await quoteManager.getCurrentQuote(mockProject, mockRequest, 'requests');
119
+ if (log) { console.log("[Quote Test] final_quote: ", final_quote); }
120
+
121
+ expect(key_incremented).to.equal("quotes:requests:64e36f5dbf72263f7c059999:20/10/2023");
122
+ expect(final_quote).to.equal(initial_quote + 1);
123
+ })
124
+
125
+ it('incrementMessagesCount', async function () {
126
+ let mockProject = projectMock.mockProjectSandboxPlan;
127
+ let mockMessage = messageMock.messageMock;
128
+
129
+ mockMessage.createdAt = new Date();
130
+
131
+ let initial_quote = await quoteManager.getCurrentQuote(mockProject, mockMessage, 'messages');
132
+ if (log) { console.log("[Quote Test] initial_quote: ", initial_quote); }
133
+
134
+ let key_incremented = await quoteManager.incrementMessagesCount(mockProject, mockMessage);
135
+ if (log) { console.log("[Quote Test] key_incremented: ", key_incremented); }
136
+
137
+ let final_quote = await quoteManager.getCurrentQuote(mockProject, mockMessage, 'messages');
138
+ if (log) { console.log("[Quote Test] current quote: ", final_quote); }
139
+
140
+ //expect(key_incremented).to.equal("quotes:messages:64e36f5dbf72263f7c059999:20/10/2023");
141
+ expect(final_quote).to.equal(initial_quote + 1);
142
+
143
+ })
144
+
145
+ it('incrementEmailCount', async function () {
146
+ let mockProject = projectMock.mockProjectSandboxPlan;
147
+ let mockEmail = emailMock.emailMock;
148
+
149
+ let result = await quoteManager.checkQuote(mockProject, mockEmail, 'email');
150
+ console.log("checkQuote result: ", result)
151
+
152
+ let initial_quote = await quoteManager.getCurrentQuote(mockProject, mockEmail, 'email');
153
+ if (log) { console.log("[Quote Test] initial_quote: ", initial_quote); }
154
+
155
+ let key_incremented = await quoteManager.incrementEmailCount(mockProject, mockEmail);
156
+ if (log) { console.log("[Quote Test] key_incremented: ", key_incremented); }
157
+
158
+ let final_quote = await quoteManager.getCurrentQuote(mockProject, mockEmail, 'email');
159
+ if (log) { console.log("[Quote Test] current quote: ", final_quote); }
160
+
161
+ expect(key_incremented).to.equal("quotes:email:64e36f5dbf72263f7c059999:20/10/2023");
162
+ expect(final_quote).to.equal(initial_quote + 1);
163
+
164
+ })
165
+
166
+ it('sendEmailDirect', (done) => {
167
+
168
+ var email = "test-signup-" + Date.now() + "@email.com";
169
+ var pwd = "pwd";
170
+
171
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
172
+ projectService.create("test-send-email-direct", savedUser._id).then(function (savedProject) {
173
+ chai.request(server)
174
+ .post('/' + savedProject._id + '/emails/internal/send')
175
+ .auth(email, pwd)
176
+ .send({ to: "giovanni.troisiub@gmail.com", text: "Hello", subject: "HelloSub", replyto: "giovanni.troisiub@gmail.com" })
177
+ .end((err, res) => {
178
+ console.log("email internal send err", err);
179
+ console.log("email internal send res.body", res.body);
180
+ done();
181
+ });
182
+ });
183
+ });
184
+
185
+ })
186
+
187
+ it('incrementTokensCount', (done) => {
188
+
189
+ var email = "test-quote-" + Date.now() + "@email.com";
190
+ var pwd = "pwd";
191
+
192
+ userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => {
193
+ projectService.create("quote-project", savedUser._id).then((savedProject) => {
194
+
195
+ let createdAt = new Date();
196
+ createdAt.setDate(createdAt.getDate() + 1)
197
+
198
+ chai.request(server)
199
+ .post('/' + savedProject._id + "/openai/quotes")
200
+ .auth(email, pwd)
201
+ .send({ createdAt: createdAt , tokens: 128 })
202
+ .end((err, res) => {
203
+ if (log) { console.log("res.body", res.body )};
204
+ res.should.have.status(200);
205
+ res.body.should.be.a('object');
206
+
207
+ let date = new Date().toLocaleDateString();
208
+
209
+ let key = "quotes:tokens:" + savedProject._id + ":" + date;
210
+ let message_resp = "value incremented for key " + key;
211
+ expect(res.body.message).to.equal(message_resp);
212
+ expect(res.body.key).to.equal(key);
213
+ expect(res.body.currentQuote).to.equal(128);
214
+
215
+
216
+ done();
217
+ })
218
+ })
219
+ })
220
+
221
+
222
+ })
223
+
224
+
225
+
226
+
227
+ // it('incrementRequestCountMulti', async function () {
228
+ // let mockProject = projectMock.mockProjectSandboxPlan;
229
+ // let mockRequest = requestMock.requestMock;
230
+
231
+ // let quoteManager = new QuoteManager({ project: mockProject, tdCache: tdCache });
232
+
233
+ // for (let date of dateList) {
234
+ // mockRequest.createdAt = new Date(date);
235
+ // let result = await quoteManager.incrementRequestsCount(mockRequest);
236
+ // console.log("result: ", result);
237
+ // console.log("\n\n");
238
+ // }
239
+
240
+ // })
241
+
242
+ // it('incrementRequestCountLimitReached', async function () {
243
+
244
+ // let mockProject = projectMock.mockProjectSandboxPlan;
245
+ // let mockRequest = requestMock.requestMock;
246
+
247
+ // // for the test the limit is fixed to 5
248
+
249
+ // let i;
250
+ // let quoteManager = new QuoteManager({ project: mockProject, tdCache: tdCache });
251
+
252
+ // for (i = 0; i < 5; i++) {
253
+ // mockRequest.createdAt = new Date(dateList[i]);
254
+ // await quoteManager.incrementRequestsCount(mockRequest);
255
+ // }
256
+
257
+ // mockRequest.createdAt = new Date(dateList[i]);
258
+ // let result = await quoteManager.incrementRequestsCount(mockRequest);
259
+ // console.log("result: ", result);
260
+
261
+
262
+ // })
263
+
264
+ // it('getCurrentCount', async function() {
265
+ // let mockProject = projectMock.mockProjectSandboxPlan;
266
+ // let mockRequest = requestMock.requestMock;
267
+
268
+ // let quoteManager = new QuoteManager({ project: mockProject, tdCache: mockTdCache } );
269
+
270
+ // for (let date of dateList) {
271
+ // mockRequest.createdAt = new Date(date);
272
+ // let result = await quoteManager.incrementRequestCount(mockRequest);
273
+ // console.log("result: ", result);
274
+ // }
275
+
276
+ // let today = new Date('2023-12-22T08:45:54.058Z');
277
+ // let quote = await quoteManager.getCurrentQuote(today, 'requests');
278
+ // console.log("request quote: ", quote)
279
+
280
+
281
+ // })
282
+ })
@@ -50,7 +50,7 @@ describe('RequestRoute', () => {
50
50
  .send({"first_text":"first_text"})
51
51
  .end(function(err, res) {
52
52
  //console.log("res", res);
53
- console.log("res.body", res.body);
53
+ //console.log("res.body", res.body);
54
54
  res.should.have.status(200);
55
55
  res.body.should.be.a('object');
56
56