@tiledesk/tiledesk-server 2.5.3 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
package/test/kbRoute.js CHANGED
@@ -1,10 +1,11 @@
1
1
  //During the test the env variable is set to test
2
2
  process.env.NODE_ENV = 'test';
3
+ process.env.GPTKEY = "fakegptkey";
3
4
 
4
5
  var userService = require('../services/userService');
5
6
  var projectService = require('../services/projectService');
6
7
 
7
- let log = true;
8
+ let log = false;
8
9
 
9
10
  //Require the dev-dependencies
10
11
  let chai = require('chai');
@@ -37,7 +38,7 @@ describe('KbRoute', () => {
37
38
  name: "example_name5",
38
39
  type: "url",
39
40
  source: "https://www.exampleurl5.com",
40
- content: ""
41
+ content: "",
41
42
  }
42
43
 
43
44
  chai.request(server)
@@ -45,36 +46,106 @@ describe('KbRoute', () => {
45
46
  .auth(email, pwd)
46
47
  .send(kb) // can be empty
47
48
  .end((err, res) => {
49
+
50
+ if (err) { console.error("err: ", err); }
48
51
  if (log) { console.log("create kb res.body: ", res.body); }
52
+
49
53
  res.should.have.status(200);
50
- // res.body.should.be.a('object');
51
- // expect(res.body.id_project).to.equal(savedProject._id.toString());
54
+ res.body.should.be.a('object');
52
55
 
53
56
  done();
54
- // chai.request(server)
55
- // .get('/' + savedProject._id + "/kbsettings")
56
- // .auth(email, pwd)
57
- // .end((err, res) => {
58
- // if (log) { console.log("get kbsettings res.body: ", res.body); }
59
- // res.should.have.status(200);
60
- // res.body.should.be.a('object');
61
- // expect(res.body.id_project).to.equal(savedProject._id.toString())
62
- // expect(res.body.maxKbsNumber).to.equal(3);
63
- // expect(res.body.maxPagesNumber).to.equal(1000);
64
- // expect(res.body.kbs).is.an('array').that.is.empty;
57
+
58
+
59
+ })
60
+
61
+ });
62
+ });
63
+
64
+ }).timeout(10000);
65
+
66
+ // logic in standby
67
+ // it('createNewKb-namespaceNotBelongsProject', (done) => {
68
+
69
+ // var email = "test-signup-" + Date.now() + "@email.com";
70
+ // var pwd = "pwd";
71
+
72
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
73
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
74
+
75
+ // let kb = {
76
+ // name: "example_name5",
77
+ // type: "url",
78
+ // source: "https://www.exampleurl5.com",
79
+ // content: "",
80
+ // namespace: "fakenamespace"
81
+ // }
82
+
83
+ // chai.request(server)
84
+ // .post('/' + savedProject._id + '/kb')
85
+ // .auth(email, pwd)
86
+ // .send(kb) // can be empty
87
+ // .end((err, res) => {
88
+
89
+ // if (err) { console.error("err: ", err); }
90
+ // if (log) { console.log("create kb res.body: ", res.body); }
91
+
92
+ // res.should.have.status(403);
93
+ // res.body.should.be.a('object');
94
+ // expect(res.body.success).to.equal(false);
95
+ // expect(res.body.error).to.equal("Not allowed. The namespace does not belong to the current project.");
96
+
97
+ // done();
98
+
99
+
100
+ // })
101
+
102
+ // });
103
+ // });
104
+
105
+ //}).timeout(10000);
106
+
107
+ it('createNewKb-replaceNamespace', (done) => {
65
108
 
109
+ var email = "test-signup-" + Date.now() + "@email.com";
110
+ var pwd = "pwd";
111
+
112
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
113
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
114
+
115
+ let kb = {
116
+ name: "example_name5",
117
+ type: "url",
118
+ source: "https://www.exampleurl5.com",
119
+ content: "",
120
+ namespace: "fakenamespace"
121
+ }
122
+
123
+ chai.request(server)
124
+ .post('/' + savedProject._id + '/kb')
125
+ .auth(email, pwd)
126
+ .send(kb) // can be empty
127
+ .end((err, res) => {
128
+
129
+ if (err) { console.error("err: ", err); }
130
+ if (log) { console.log("create kb res.body: ", res.body); }
131
+
132
+ res.should.have.status(200);
133
+ res.body.should.be.a('object');
134
+ expect(res.body.value.namespace).not.equal("fakenamespace");
135
+ expect(res.body.value.namespace).to.equal(savedProject._id.toString());
66
136
 
67
- // })
137
+ done();
138
+
68
139
 
69
140
  })
70
141
 
71
142
  });
72
143
  });
73
144
 
74
- });
145
+ }).timeout(10000);
75
146
 
76
147
  it('getWithQueries', (done) => {
77
-
148
+
78
149
  var email = "test-signup-" + Date.now() + "@email.com";
79
150
  var pwd = "pwd";
80
151
 
@@ -136,7 +207,7 @@ describe('KbRoute', () => {
136
207
  .get('/' + savedProject._id + "/kb" + query)
137
208
  .auth(email, pwd)
138
209
  .end((err, res) => {
139
- console.log("getall res.body: ", res.body);
210
+ if (log) { console.log("getall res.body: ", res.body); }
140
211
  res.should.have.status(200);
141
212
  res.body.should.be.a('object');
142
213
  res.body.kbs.should.be.a('array');
@@ -153,7 +224,7 @@ describe('KbRoute', () => {
153
224
  done();
154
225
 
155
226
  })
156
-
227
+
157
228
  })
158
229
  }, 1000)
159
230
  })
@@ -220,6 +291,246 @@ describe('KbRoute', () => {
220
291
 
221
292
  });
222
293
 
294
+ // it('scrapeSingle-namespaceNotBelongsProject', (done) => {
295
+
296
+ // var email = "test-signup-" + Date.now() + "@email.com";
297
+ // var pwd = "pwd";
298
+
299
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
300
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
301
+
302
+ // let kb = {
303
+ // name: "example_name6",
304
+ // type: "url",
305
+ // source: "https://www.exampleurl6.com",
306
+ // content: ""
307
+ // }
308
+
309
+ // chai.request(server)
310
+ // .post('/' + savedProject._id + '/kb')
311
+ // .auth(email, pwd)
312
+ // .send(kb) // can be empty
313
+ // .end((err, res) => {
314
+ // if (log) { console.log("create kb res.body: ", res.body); }
315
+ // res.should.have.status(200);
316
+
317
+ // let kbid = res.body.value._id;
318
+ // console.log("kbid: ", kbid)
319
+ // chai.request(server)
320
+ // .post('/' + savedProject._id + "/kb/scrape/single")
321
+ // .auth(email, pwd)
322
+ // .send({ id: kbid })
323
+ // .end((err, res) => {
324
+ // if (log) { console.log("single scrape res.body: ", res.body); }
325
+ // //res.should.have.status(200);
326
+ // // res.body.should.be.a('object');
327
+ // // expect(res.body.id_project).to.equal(savedProject._id.toString())
328
+ // // expect(res.body.maxKbsNumber).to.equal(3);
329
+ // // expect(res.body.maxPagesNumber).to.equal(1000);
330
+ // // expect(res.body.kbs).is.an('array').that.is.empty;
331
+ // done();
332
+
333
+ // })
334
+
335
+
336
+ // // res.body.should.be.a('object');
337
+ // // expect(res.body.id_project).to.equal(savedProject._id.toString());
338
+
339
+
340
+
341
+
342
+ // })
343
+ // });
344
+ // });
345
+ // });
346
+
347
+ it('multiadd', (done) => {
348
+
349
+ var email = "test-signup-" + Date.now() + "@email.com";
350
+ var pwd = "pwd";
351
+
352
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
353
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
354
+
355
+ chai.request(server)
356
+ .post('/' + savedProject._id + '/kb/multi')
357
+ .auth(email, pwd)
358
+ .set('Content-Type', 'text/plain')
359
+ .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
360
+ .end((err, res) => {
361
+
362
+ // console.log("res.body: ", res.body)
363
+ res.should.have.status(200);
364
+ expect(res.body.length).to.equal(4)
365
+
366
+ done();
367
+
368
+ // setTimeout(() => {
369
+
370
+ // chai.request(server)
371
+ // .post('/' + savedProject._id + '/kb/multi')
372
+ // .auth(email, pwd)
373
+ // .set('Content-Type', 'text/plain')
374
+ // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
375
+ // .end((err, res) => {
376
+
377
+ // // console.log("res.body: ", res.body);
378
+ // res.should.have.status(200);
379
+ // expect(res.body.length).to.equal(4)
380
+
381
+ // done()
382
+ // })
383
+ // }, 2000)
384
+
385
+ })
386
+
387
+ });
388
+ });
389
+
390
+ }).timeout(10000)
391
+
392
+ it('multiadd-fail', (done) => {
393
+
394
+ var email = "test-signup-" + Date.now() + "@email.com";
395
+ var pwd = "pwd";
396
+
397
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
398
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
399
+
400
+ chai.request(server)
401
+ .post('/' + savedProject._id + '/kb/multi')
402
+ .auth(email, pwd)
403
+ .set('Content-Type', 'text/plain')
404
+ .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
405
+ .end((err, res) => {
406
+
407
+ // console.log("res.body: ", res.body)
408
+ res.should.have.status(200);
409
+ expect(res.body.length).to.equal(4)
410
+
411
+ setTimeout(() => {
412
+
413
+ chai.request(server)
414
+ .post('/' + savedProject._id + '/kb/multi')
415
+ .auth(email, pwd)
416
+ .set('Content-Type', 'text/plain')
417
+ .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './kbUrlsList.txt')), 'kbUrlsList.txt')
418
+ .end((err, res) => {
419
+
420
+ // console.log("res.body: ", res.body);
421
+ res.should.have.status(200);
422
+ expect(res.body.length).to.equal(4)
423
+
424
+ done()
425
+ })
426
+ }, 2000)
427
+
428
+ })
429
+
430
+ });
431
+ });
432
+
433
+ }).timeout(10000)
434
+
435
+ // it('tooManyUrls', (done) => {
436
+
437
+ // var email = "test-signup-" + Date.now() + "@email.com";
438
+ // var pwd = "pwd";
439
+
440
+ // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
441
+ // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
442
+
443
+ // chai.request(server)
444
+ // .post('/' + savedProject._id + '/kb/multi')
445
+ // .auth(email, pwd)
446
+ // .set('Content-Type', 'text/plain')
447
+ // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './TooManykbUrlsList.txt')), 'TooManykbUrlsList.txt')
448
+ // .end((err, res) => {
449
+
450
+ // // console.log("res.body: ", res.body)
451
+ // res.should.have.status(403);
452
+ // expect(res.body.success).to.equal(false);
453
+ // expect(res.body.error).to.equal("Too many urls. Can't index more than 300 urls at a time.");
454
+
455
+ // done()
456
+
457
+ // })
458
+
459
+ // });
460
+ // });
461
+
462
+ // })
463
+
464
+ // logic in standby
465
+ // it('askkb-namespaceNotBelongsProject', (done) => {
466
+
467
+ // var email = "test-signup-" + Date.now() + "@email.com";
468
+ // var pwd = "pwd";
469
+
470
+ // userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => {
471
+ // projectService.create("test-kb-qa", savedUser._id).then((savedProject) => {
472
+
473
+ // /**
474
+ // * README
475
+ // * Namespace should be equal to savedProject._id;
476
+ // * A generic mongodb ID (like user id) is used instead for test porpouse
477
+ // */
478
+ // chai.request(server)
479
+ // .post('/' + savedProject._id + "/kb/qa")
480
+ // .auth(email, pwd)
481
+ // .send({ model: "gpt-4", namespace: savedUser._id, question: "sample question"})
482
+ // .end((err, res) => {
483
+
484
+ // if (err) { console.log("error: ", err) };
485
+ // if (log) { console.log("res.body: ", res.body) };
486
+
487
+ // res.should.have.status(403);
488
+ // res.body.should.be.a('object');
489
+ // expect(res.body.success).to.equal(false);
490
+ // expect(res.body.error).to.equal("Not allowed. The namespace does not belong to the current project.");
491
+
492
+ // done();
493
+ // })
494
+ // })
495
+ // })
496
+ // }).timeout(10000)
497
+
498
+
499
+ it('sitemap', (done) => {
500
+
501
+ var email = "test-signup-" + Date.now() + "@email.com";
502
+ var pwd = "pwd";
503
+
504
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
505
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
506
+
507
+ chai.request(server)
508
+ .post('/' + savedProject._id + '/kb/sitemap')
509
+ .auth(email, pwd)
510
+ // .send({ sitemap: "https://www.wired.it/sitemap.xml" })
511
+ .send({ sitemap: "https://gethelp.tiledesk.com/sitemap.xml" })
512
+ .end((err, res) => {
513
+
514
+ if (err) { console.log("error: ", err) };
515
+ if (log) { console.log("res.body: ", res.body) }
516
+
517
+ console.log("error: ", err)
518
+ console.log("res.body: ", res.body)
519
+
520
+ console.log("sites length: ", res.body.sites.length)
521
+ res.should.have.status(200);
522
+ res.body.should.be.a('object');
523
+ res.body.sites.should.be.a('array');
524
+
525
+ done();
526
+
527
+ })
528
+
529
+ });
530
+ });
531
+
532
+ }).timeout(10000)
533
+
223
534
  })
224
535
  });
225
536
 
@@ -0,0 +1,4 @@
1
+ https://gethelp.tiledesk.com/articles/article1
2
+ https://gethelp.tiledesk.com/articles/article2
3
+ https://gethelp.tiledesk.com/articles/article3
4
+ https://gethelp.tiledesk.com/articles/article4
@@ -0,0 +1,146 @@
1
+ var QueueManager = require("./queueManagerClassV2");
2
+
3
+ class JobManager {
4
+ constructor(queueUrl, options) {
5
+ this.queueUrl = queueUrl;
6
+ // this.queueManager = null;
7
+ this.debug = false;
8
+ if (options && options.debug!=undefined) {
9
+ this.debug = options.debug;
10
+ }
11
+
12
+ this.info = true;
13
+ this.queueManager = new QueueManager(this.queueUrl, options);
14
+
15
+ this.sendingJobs = [];
16
+ this.queuePublisherConnected = false;
17
+ this.queueConsumerConnected = false;
18
+
19
+ }
20
+ connectAndStartPublisher(callback) {
21
+ var that = this;
22
+ if (this.info) {console.log("[JobWorker] JobManager publisher started");}
23
+
24
+ this.queueManager.connect(function() {
25
+ if (that.debug) {console.log("[JobWorker] Queue started");}
26
+ that.queuePublisherConnected = true;
27
+
28
+ that.queueManager.startPublisher(function() {
29
+ if (that.debug) {console.log("[JobWorker] Queue that.sendingJobs.length",that.sendingJobs.length);}
30
+ if (that.sendingJobs.length > 0) {
31
+ for (var i = 0; i<that.sendingJobs.length; i++) {
32
+ if (that.debug) {console.log("[JobWorker] Queue that.sendingJobs[i]",that.sendingJobs[i]);}
33
+ // that.queueManager.send(that.sendingJobs[i].toString(), "functions");
34
+ that.queueManager.sendJson(that.sendingJobs[i], "functions");
35
+ // that.sendingJobs[i]();
36
+ }
37
+ //that.sendingJob = [];
38
+ that.sendingJobs.length = 0; //empty array that.sendingJobs
39
+ if (that.debug) {console.log("[JobWorker] that.sendingJobs[i] cleared",that.sendingJobs);}
40
+
41
+ }
42
+
43
+ if (callback) {
44
+ callback();
45
+ }
46
+ });
47
+ });
48
+ }
49
+
50
+ diconnectPublisher(callback) {
51
+
52
+ if (this.info) {console.log("[JobWorker] JobManager publisher disconnected");}
53
+
54
+ this.queueManager.close((err) => {
55
+ if (err) {
56
+ console.error("JobManager: error closing connection: ", err);
57
+ }
58
+ callback(err);
59
+ })
60
+
61
+
62
+ }
63
+
64
+ connectAndStartWorker() {
65
+ var that = this;
66
+ if (this.info) {console.log("[JobWorker] JobManager worker started");}
67
+
68
+ this.queueManager.connect(function() {
69
+ if (that.debug) {console.log("[JobWorker] Queue started");}
70
+ that.queueConsumerConnected = true;
71
+
72
+ that.queueManager.startWorker(function() {
73
+ if (that.debug) {console.log("[JobWorker] Queue that.queueManager.startWorker");}
74
+ });
75
+ });
76
+ }
77
+
78
+ publish(payload, callback) {
79
+
80
+ var packet = {payload: payload}
81
+ // if (this.debug) {console.log("JobManager this.queueConnected",this.queueStarted);
82
+ if (this.queuePublisherConnected == true) {
83
+
84
+ if (this.debug) {console.log("[JobWorker] JobManager this.queuePublisherConnected == true");}
85
+ this.queueManager.sendJson(packet, "functions", (err, ok) => {
86
+ if (err) {
87
+ console.error("sendJson error: ", err);
88
+ } else {
89
+ if (this.debug) { console.log("sendJson ok"); };
90
+ }
91
+ callback(err, ok);
92
+ });
93
+
94
+ // this.queueManager.on(fn);
95
+ }
96
+ // else {
97
+ // if (this.debug) {console.log("[JobWorker] JobManager this.queuePublisherConnected == false");}
98
+
99
+ // this.connectAndStartPublisher();
100
+ // this.sendingJobs.push(packet);
101
+
102
+ // }
103
+
104
+ }
105
+
106
+
107
+ //Deprecated
108
+ schedule(fn, payload) {
109
+
110
+ var func = {function: fn.toString(), payload: payload}
111
+ // if (this.debug) {console.log("JobManager this.queueConnected",this.queueStarted);
112
+ if (this.queuePublisherConnected == true) {
113
+ if (this.debug) {console.log("[JobWorker] JobManager this.queuePublisherConnected == true");}
114
+ this.queueManager.send(func, "functions");
115
+
116
+ // this.queueManager.on(fn);
117
+ } else {
118
+ if (this.debug) {console.log("[JobWorker] JobManager this.queuePublisherConnected == false");}
119
+
120
+ this.connectAndStartPublisher();
121
+ this.sendingJobs.push(func);
122
+
123
+ }
124
+
125
+ }
126
+
127
+ run(callback) {
128
+ // if (this.queueConsumerConnected == true) {
129
+ // if (this.debug) {console.log("JobManager this.queueConsumerConnected == true");
130
+ // this.queueManager.send(fn.toString(), "functions");
131
+
132
+ // } else {
133
+ if (this.debug) {console.log("[JobWorker] JobManager connectAndStartWorker");}
134
+ this.connectAndStartWorker();
135
+
136
+ if (callback) {
137
+ if (this.debug) {console.log("[JobWorker] JobManager callback", callback);}
138
+ this.queueManager.on(callback);
139
+ }
140
+ // }
141
+ }
142
+ }
143
+
144
+
145
+
146
+ module.exports = JobManager;