@tiledesk/tiledesk-server 3.0.1 → 3.1.0

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.
Files changed (136) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +34 -0
  3. package/Dockerfile +3 -2
  4. package/Dockerfile-en +3 -2
  5. package/Dockerfile-jobs +3 -2
  6. package/Dockerfile-profiler +3 -2
  7. package/README.md +15 -0
  8. package/app.js +12 -1
  9. package/bin/quota-reset +68 -0
  10. package/bin/quota-restore +86 -0
  11. package/channels/chat21/chat21Handler.js +50 -34
  12. package/channels/chat21/chat21JoinTracker.js +43 -0
  13. package/config/database.js +5 -5
  14. package/config/native_mcp/native_mcp_servers.json +1 -0
  15. package/controllers/chatbot.controller.js +80 -27
  16. package/controllers/intents.controller.js +42 -0
  17. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
  18. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
  19. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
  20. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
  21. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
  22. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
  23. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
  24. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
  25. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
  26. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
  27. package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
  28. package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
  29. package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
  30. package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
  31. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
  32. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
  33. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
  34. package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
  35. package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
  36. package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
  37. package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
  38. package/event/botEvent.js +12 -1
  39. package/event/emailEvent.js +3 -0
  40. package/event/messageEvent.js +44 -0
  41. package/jobs.js +10 -0
  42. package/jobsManager.js +19 -0
  43. package/middleware/errorHandler.js +6 -2
  44. package/models/analyticResult.js +1 -1
  45. package/models/channelConstants.js +3 -2
  46. package/models/quotaUsage.js +71 -0
  47. package/models/request.js +6 -0
  48. package/models/requestSnapshot.js +12 -0
  49. package/models/routerLogger.js +1 -1
  50. package/models/webhook.js +44 -0
  51. package/package.json +7 -5
  52. package/pubmodules/emailNotification/requestNotification.js +14 -22
  53. package/pubmodules/events/test/eventRoute.js +7 -6
  54. package/pubmodules/rules/conciergeBot.js +9 -4
  55. package/routes/auth.js +1 -12
  56. package/routes/campaigns.js +0 -13
  57. package/routes/dataTable.js +0 -3
  58. package/routes/faq_kb.js +12 -1
  59. package/routes/messagesRoot.js +0 -1
  60. package/routes/quotes.js +38 -1
  61. package/routes/request.js +81 -5
  62. package/routes/users.js +0 -1
  63. package/routes/webhook.js +160 -8
  64. package/routes/webhooks.js +276 -6
  65. package/routes/widgetLoader.js +6 -3
  66. package/services/QuoteManager.js +77 -16
  67. package/services/aiService.js +1 -1
  68. package/services/chatbotService.js +55 -1
  69. package/services/departmentService.js +60 -43
  70. package/services/dolphinSchedulerClient.js +116 -0
  71. package/services/emailService.js +6 -1
  72. package/services/hitlParticipantListener.js +260 -0
  73. package/services/participantInviteService.js +302 -0
  74. package/services/participantRemoveService.js +79 -0
  75. package/services/quotaSnapshotService.js +1167 -0
  76. package/services/requestService.js +102 -144
  77. package/services/scheduledStart/dolphinBackend.js +246 -0
  78. package/services/scheduledStart/schedulerBackend.js +101 -0
  79. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  80. package/services/scheduledStartPublishListener.js +186 -0
  81. package/services/scheduledStartRules.js +173 -0
  82. package/services/scheduledStartService.js +130 -0
  83. package/services/startPointCleanupListener.js +30 -0
  84. package/services/startPointService.js +158 -0
  85. package/services/testWsService.js +1 -4
  86. package/services/updateRequestSnapshotQueued.js +8 -9
  87. package/services/webhookConversationService.js +185 -0
  88. package/services/webhookSettingsValidator.js +61 -0
  89. package/template/email/checkpointReachedEmail.html +5 -0
  90. package/test/agentPromptsRoute.test.js +3 -2
  91. package/test/agentTokensQuota.js +237 -0
  92. package/test/botEventHitlFallback.js +19 -0
  93. package/test/chat21HandlerJoinWiring.js +59 -0
  94. package/test/dataTablesRoute.test.js +10 -2
  95. package/test/dateUtils.test.js +0 -3
  96. package/test/departmentService.js +8 -12
  97. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  98. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  99. package/test/faqRoute.js +6 -1
  100. package/test/faqkbRoute.js +14 -10
  101. package/test/faqkbV3Route.js +1 -1
  102. package/test/filepRoute.js +4 -50
  103. package/test/hitlParticipantListener.js +446 -0
  104. package/test/hitlQueueWiring.js +191 -0
  105. package/test/hitlWaitingReset.js +124 -0
  106. package/test/inviteParticipantsRoute.js +492 -0
  107. package/test/kbRoute.js +1 -1
  108. package/test/messageRoute.js +33 -21
  109. package/test/nativemcp.test.js +7 -1
  110. package/test/openaiRoute.js +2 -8
  111. package/test/publishFailLoud.test.js +414 -0
  112. package/test/quotaSnapshots.js +1044 -0
  113. package/test/removeParticipantsRoute.js +186 -0
  114. package/test/requestService.js +24 -16
  115. package/test/requestServiceRemoveParticipant.js +144 -0
  116. package/test/requestVisibility.test.js +186 -0
  117. package/test/scheduledStartFacade.js +184 -0
  118. package/test/scheduledStartHandover.js +95 -0
  119. package/test/scheduledStartPublish.js +320 -0
  120. package/test/scheduledStartRoute.js +163 -0
  121. package/test/scheduledStartRules.js +177 -0
  122. package/test/scheduledStartRun.js +299 -0
  123. package/test/scheduledStartSchedulerBackend.js +116 -0
  124. package/test/scheduledStartService.js +290 -0
  125. package/test/startPointsCleanup.js +69 -0
  126. package/test/startPointsRegression.js +169 -0
  127. package/test/startPointsRoute.js +158 -0
  128. package/test/webhookConversationRoute.js +347 -0
  129. package/test/webhookConversationService.js +261 -0
  130. package/test/webhookConversationTestUtils.js +55 -0
  131. package/utils/aiUtils.js +95 -1
  132. package/utils/intentFieldNames.js +192 -0
  133. package/utils/quotaCliUtil.js +43 -0
  134. package/utils/requestVisibility.js +355 -0
  135. package/websocket/webSocketServer.js +36 -31
  136. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
@@ -8,6 +8,7 @@ var requestService = require('../services/requestService');
8
8
  var userService = require('../services/userService');
9
9
  var leadService = require('../services/leadService');
10
10
  var messageService = require('../services/messageService');
11
+ var geoService = require('../services/geoService');
11
12
  var Project_user = require("../models/project_user");
12
13
  var RoleConstants = require('../models/roleConstants');
13
14
  const uuidv4 = require('uuid/v4');
@@ -412,6 +413,9 @@ describe('MessageRoute', () => {
412
413
  // mocha test/messageRoute.js --grep 'createWithLocationAsAttributes'
413
414
  it('createWithLocationAsAttributes', function (done) {
414
415
 
416
+ // With JOB_WORKER_ENABLED the API process does not start the geo listener.
417
+ geoService.listen();
418
+
415
419
  var email = "test-message-create-" + Date.now() + "@email.com";
416
420
  var pwd = "pwd";
417
421
 
@@ -446,31 +450,39 @@ describe('MessageRoute', () => {
446
450
  expect(res.body.request.first_text).to.equal("text");
447
451
  expect(res.body.request.id_project).to.equal(savedProject._id.toString());
448
452
 
449
- chai.request(server)
450
- .get('/' + savedProject._id + '/requests/' + request_id)
451
- .auth(email, pwd)
452
- .set('content-type', 'application/json')
453
- .send()
454
- .end(function (err, res) {
455
-
456
- if (err) { console.error("err", err); }
457
- if (log) { console.log("res.body", res.body); }
458
-
459
- res.should.have.status(200);
460
- res.body.should.be.a('object');
461
- expect(res.body.request_id).to.equal(request_id);
462
- expect(res.body.location.country).to.equal("IT");
463
- expect(res.body.location.ipAddress).to.equal("95.255.73.34");
464
- expect(res.body.location.geometry.type).to.equal("Point");
465
- expect(res.body.location.geometry.coordinates[0]).to.equal(42.6716);
466
- expect(res.body.location.geometry.coordinates[1]).to.equal(14.0148);
453
+ function waitForLocation(attempt) {
454
+ chai.request(server)
455
+ .get('/' + savedProject._id + '/requests/' + request_id)
456
+ .auth(email, pwd)
457
+ .set('content-type', 'application/json')
458
+ .send()
459
+ .end(function (err, res) {
467
460
 
468
- done();
469
- });
461
+ if (err) { console.error("err", err); }
462
+ if (log) { console.log("res.body", res.body); }
463
+
464
+ if ((!res.body || !res.body.location || !res.body.location.country) && attempt < 10) {
465
+ return setTimeout(function () { waitForLocation(attempt + 1); }, 200);
466
+ }
467
+
468
+ res.should.have.status(200);
469
+ res.body.should.be.a('object');
470
+ expect(res.body.request_id).to.equal(request_id);
471
+ expect(res.body.location.country).to.equal("IT");
472
+ expect(res.body.location.ipAddress).to.equal("95.255.73.34");
473
+ expect(res.body.location.geometry.type).to.equal("Point");
474
+ expect(res.body.location.geometry.coordinates[0]).to.equal(42.6716);
475
+ expect(res.body.location.geometry.coordinates[1]).to.equal(14.0148);
476
+
477
+ done();
478
+ });
479
+ }
480
+
481
+ waitForLocation(0);
470
482
  });
471
483
  });
472
484
  });
473
- });
485
+ }).timeout(10000);
474
486
 
475
487
  it('createDifferentChannel', function (done) {
476
488
 
@@ -3,6 +3,7 @@ process.env.NODE_ENV = 'test';
3
3
 
4
4
  const projectService = require('../services/projectService');
5
5
  const userService = require('../services/userService');
6
+ const mcpService = require('../services/mcpService');
6
7
 
7
8
  //Require the dev-dependencies
8
9
  let chai = require('chai');
@@ -30,6 +31,7 @@ describe('NativeMCP', () => {
30
31
 
31
32
  it('get-native-mcp-servers-list', (done) => {
32
33
  createProjectUser(done, (savedProject, email, pwd, done) => {
34
+ const loadCatalog = () => {
33
35
  chai.request(server)
34
36
  .get('/' + savedProject._id + '/mcp/native')
35
37
  .auth(email, pwd)
@@ -60,6 +62,10 @@ describe('NativeMCP', () => {
60
62
 
61
63
  done();
62
64
  });
65
+ };
66
+
67
+ if (!mcpService.tdCache) return loadCatalog();
68
+ mcpService.tdCache.del('native_mcp:servers').then(loadCatalog).catch(loadCatalog);
63
69
  });
64
70
  });
65
71
 
@@ -100,7 +106,7 @@ describe('NativeMCP', () => {
100
106
  connectRes.body.tools.should.be.an('array');
101
107
  connectRes.body.tools.length.should.be.greaterThan(0);
102
108
  connectRes.body.tools[0].should.have.property('name');
103
- connectRes.body.tools.map((tool) => tool.name).should.include('reply_to_user');
109
+ connectRes.body.tools.map((tool) => tool.name).should.include('REPLY_TO_USER');
104
110
 
105
111
  done();
106
112
  });
@@ -109,8 +109,6 @@ describe('OpenaiRoute', () => {
109
109
 
110
110
  it('newCompletionsMissingGptkey', (done) => {
111
111
 
112
- let original_value = process.env.GPTKEY;
113
- process.env.GPTKEY = "";
114
112
  var email = "test-signup-" + Date.now() + "@email.com";
115
113
  var pwd = "pwd";
116
114
 
@@ -126,12 +124,8 @@ describe('OpenaiRoute', () => {
126
124
  if (err) { console.error("err: ", err); }
127
125
  if (log) { console.log("res.body: ", res.body); }
128
126
 
129
- res.should.have.status(400);
130
- res.body.should.be.a('object');
131
- expect(res.body.success).to.equal(false);
132
- expect(res.body.message).to.equal("Missing gptkey parameter");
133
-
134
- process.env.GPTKEY = original_value;
127
+ // POST /openai completions is disabled. Only /openai/quotes remains.
128
+ res.should.have.status(404);
135
129
 
136
130
  done();
137
131
  })
@@ -0,0 +1,414 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+ process.env.LOG_LEVEL = 'error';
4
+
5
+ const mongoose = require('mongoose');
6
+ const Faq = require('../models/faq');
7
+ const Faq_kb = require('../models/faq_kb');
8
+ const projectService = require('../services/projectService');
9
+ const userService = require('../services/userService');
10
+ const intentsController = require('../controllers/intents.controller');
11
+ const { ChatbotService } = require('../services/chatbotService');
12
+
13
+ let chai = require('chai');
14
+ let chaiHttp = require('chai-http');
15
+ let server = require('../app');
16
+ chai.should();
17
+ const expect = chai.expect;
18
+
19
+ chai.use(chaiHttp);
20
+
21
+ /**
22
+ * Publish must not leave a half-published flow (26/10/2026).
23
+ * A block holding a field name MongoDB cannot insert ('.' inside, or '$' at the start) used to be
24
+ * dropped from the published copy while publish answered 200. Now:
25
+ * - writes of a block refuse such names with a 400 that names the block and the field;
26
+ * - publish refuses a flow holding such a block with a 422 (message + errors) and changes nothing;
27
+ * - a copy that fails half-way is removed, and the draft keeps pointing at the previous copy.
28
+ */
29
+ describe('Publish fails loud', () => {
30
+
31
+ let listener;
32
+ let previousApiUrl;
33
+ let previousService;
34
+
35
+ // publish forks through the HTTP API (API_URL): serve the app on a free port for these tests.
36
+ // Other suites replace the app's chatbot_service with stubs and leave them there: use the real one.
37
+ before((done) => {
38
+ previousService = server.get('chatbot_service');
39
+ server.set('chatbot_service', new ChatbotService());
40
+ previousApiUrl = process.env.API_URL;
41
+ listener = server.listen(0, '127.0.0.1', () => {
42
+ process.env.API_URL = 'http://127.0.0.1:' + listener.address().port;
43
+ done();
44
+ });
45
+ });
46
+
47
+ after((done) => {
48
+ server.set('chatbot_service', previousService);
49
+ if (previousApiUrl === undefined) { delete process.env.API_URL; } else { process.env.API_URL = previousApiUrl; }
50
+ listener.close(() => done());
51
+ });
52
+
53
+ async function signupAndProject(prefix) {
54
+ const email = "test-signup-" + prefix + "-" + Date.now() + "@email.com";
55
+ const pwd = "pwd";
56
+ const savedUser = await userService.signup(email, pwd, "Test Firstname", "Test lastname");
57
+ const savedProject = await projectService.create("test-publish-fail-loud", savedUser._id);
58
+ return { email, pwd, project: savedProject };
59
+ }
60
+
61
+ function api(ctx, method, path) {
62
+ return chai.request(server)[method]('/' + ctx.project._id + path).auth(ctx.email, ctx.pwd);
63
+ }
64
+
65
+ async function createBot(ctx, body) {
66
+ const res = await api(ctx, 'post', '/faq_kb').send(body);
67
+ res.should.have.status(200);
68
+ return res.body;
69
+ }
70
+
71
+ async function addBlock(ctx, bot_id, block) {
72
+ const res = await api(ctx, 'post', '/faq').send(Object.assign({ id_faq_kb: bot_id }, block));
73
+ res.should.have.status(200);
74
+ return res.body;
75
+ }
76
+
77
+ async function publish(ctx, bot_id) {
78
+ return api(ctx, 'put', '/faq_kb/' + bot_id + '/publish').send({ release_note: "test" });
79
+ }
80
+
81
+ async function publishedCopies(ctx, bot_id) {
82
+ const res = await api(ctx, 'get', '/faq_kb/' + bot_id + '/published');
83
+ res.should.have.status(200);
84
+ return res.body;
85
+ }
86
+
87
+ async function blocksOf(bot_id) {
88
+ return Faq.find({ id_faq_kb: String(bot_id) }).lean().exec();
89
+ }
90
+
91
+ /** A blank bot plus two blocks of ours, with all the template blocks saved. */
92
+ async function botWithBlocks(ctx, body) {
93
+ const bot = await createBot(ctx, Object.assign({ type: "tilebot", template: "blank", language: "en" }, body));
94
+ for (let attempt = 0; attempt < 20; attempt++) {
95
+ if ((await blocksOf(bot._id)).length >= 2) { break; }
96
+ await new Promise((resolve) => setTimeout(resolve, 100));
97
+ }
98
+ await addBlock(ctx, bot._id, {
99
+ intent_id: "b-ok-" + Date.now(), intent_display_name: "Say hello",
100
+ actions: [{ _tdActionType: "reply", text: "hello" }]
101
+ });
102
+ await addBlock(ctx, bot._id, {
103
+ intent_id: "b-hitl-" + Date.now(), intent_display_name: "Invite On Call",
104
+ actions: [{ _tdActionType: "setattribute-v2", destination: "x", operation: { operands: [] } }]
105
+ });
106
+ return bot;
107
+ }
108
+
109
+ /** A dotted key written straight into the draft, the way a stored broken block looks. */
110
+ async function breakBlock(bot_id, name) {
111
+ const result = await Faq.collection.updateOne(
112
+ { id_faq_kb: String(bot_id), intent_display_name: name },
113
+ { $set: { actions: [{ _tdActionType: "setattribute-v2", "attributes.commands": [{ type: "wait" }] }] } }
114
+ );
115
+ expect(result.modifiedCount).to.equal(1);
116
+ }
117
+
118
+ describe('Saves of a block behave as before', () => {
119
+
120
+ it('legacy-block-with-a-dotted-key-stays-editable', async () => {
121
+ const ctx = await signupAndProject("legacy-edit");
122
+ const bot = await botWithBlocks(ctx, { name: "legacy-edit" });
123
+ await breakBlock(bot._id, "Invite On Call");
124
+ const block = (await blocksOf(bot._id)).find((b) => b.intent_display_name === "Invite On Call");
125
+ expect(block.actions[0]).to.have.property("attributes.commands");
126
+
127
+ // DS sends the whole block: the stored dotted key travels with an unrelated change
128
+ const actions = block.actions.concat([{ _tdActionType: "reply", text: "added" }]);
129
+ const put = await api(ctx, 'put', '/faq/intentId' + block.intent_id).send({
130
+ id_faq_kb: bot._id, intent_display_name: "Invite On Call", actions: actions
131
+ });
132
+ put.should.have.status(200);
133
+ let stored = await Faq.findById(block._id).lean().exec();
134
+ expect(stored.actions).to.have.length(2);
135
+ expect(stored.actions[1].text).to.equal("added");
136
+
137
+ // a drag of the block
138
+ const patched = await api(ctx, 'patch', '/faq/' + block._id + '/attributes').send({ position: { x: 10, y: 20 } });
139
+ patched.should.have.status(200);
140
+ stored = await Faq.findById(block._id).lean().exec();
141
+ expect(stored.attributes.position).to.deep.equal({ x: 10, y: 20 });
142
+ }).timeout(20000);
143
+
144
+ it('put-of-a-dotted-key-is-stored-as-before', async () => {
145
+ const ctx = await signupAndProject("put-dot");
146
+ const bot = await botWithBlocks(ctx, { name: "put-dot" });
147
+ const block = (await blocksOf(bot._id)).find((b) => b.intent_display_name === "Say hello");
148
+ const res = await api(ctx, 'put', '/faq/intentId' + block.intent_id).send({
149
+ id_faq_kb: bot._id,
150
+ actions: [{ _tdActionType: "reply", text: "ok", "attributes.commands": [] }]
151
+ });
152
+ res.should.have.status(200);
153
+ const stored = await Faq.findById(block._id).lean().exec();
154
+ expect(stored.actions[0]).to.have.property("attributes.commands");
155
+ }).timeout(20000);
156
+
157
+ it('valid-put-is-unchanged', async () => {
158
+ const ctx = await signupAndProject("put-ok");
159
+ const bot = await botWithBlocks(ctx, { name: "put-ok" });
160
+ const block = (await blocksOf(bot._id)).find((b) => b.intent_display_name === "Say hello");
161
+ const res = await api(ctx, 'put', '/faq/intentId' + block.intent_id).send({
162
+ id_faq_kb: bot._id,
163
+ actions: [{ _tdActionType: "reply", text: "changed", attributes: { commands: [{ type: "message" }] } }],
164
+ attributes: { position: { x: 5, y: 6 } }
165
+ });
166
+ res.should.have.status(200);
167
+ expect(res.body.actions[0].text).to.equal("changed");
168
+ }).timeout(20000);
169
+ });
170
+
171
+ describe('Import, fork and restore (they insert)', () => {
172
+
173
+ it('importjson-with-dotted-key-is-refused-before-any-write', async () => {
174
+ const ctx = await signupAndProject("import-dot");
175
+ const bot = await botWithBlocks(ctx, { name: "import-dot" });
176
+ const exported = await api(ctx, 'get', '/faq_kb/exportjson/' + bot._id);
177
+ exported.should.have.status(200);
178
+ const json = JSON.parse(exported.text);
179
+ json.intents.find((i) => i.intent_display_name === "Invite On Call").actions = [{ _tdActionType: "x", "attributes.commands": [] }];
180
+ const botsBefore = await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec();
181
+
182
+ const res = await api(ctx, 'post', '/faq_kb/importjson/null?create=true').send(json);
183
+ res.should.have.status(422);
184
+ expect(res.body.message).to.equal(
185
+ 'The flow could not be imported: 1 block could not be stored — "Invite On Call": field name "attributes.commands" ' +
186
+ "in action 1 (x) contains '.', which is not allowed. Fix the block and try again."
187
+ );
188
+ expect(res.body.errors).to.have.length(1);
189
+ expect(await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec()).to.equal(botsBefore);
190
+
191
+ // replace: the draft keeps its blocks
192
+ const blocksBefore = await blocksOf(bot._id);
193
+ const replaced = await api(ctx, 'post', '/faq_kb/importjson/' + bot._id + '?replace=true').send(json);
194
+ replaced.should.have.status(422);
195
+ expect(await blocksOf(bot._id)).to.deep.equal(blocksBefore);
196
+ }).timeout(20000);
197
+
198
+ it('importjson-with-a-dotted-key-in-the-agent-attributes-names-the-field', async () => {
199
+ const ctx = await signupAndProject("import-attr");
200
+ const bot = await botWithBlocks(ctx, { name: "import-attr" });
201
+ const exported = await api(ctx, 'get', '/faq_kb/exportjson/' + bot._id);
202
+ const json = JSON.parse(exported.text);
203
+ json.attributes = Object.assign({}, json.attributes, { "rules.list": [] });
204
+ const botsBefore = await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec();
205
+
206
+ const res = await api(ctx, 'post', '/faq_kb/importjson/null?create=true').send(json);
207
+ res.should.have.status(422);
208
+ expect(res.body.message).to.include('the agent settings of "import-attr": field name "rules.list" in the agent attributes contains \'.\'');
209
+ expect(res.body.message).to.include("Fix the agent settings and try again.");
210
+ expect(await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec()).to.equal(botsBefore);
211
+ }).timeout(20000);
212
+
213
+ it('restore-of-a-release-with-a-dotted-key-is-refused-and-the-draft-is-untouched', async () => {
214
+ const ctx = await signupAndProject("restore-dot");
215
+ const bot = await botWithBlocks(ctx, { name: "restore-dot", ds_version: "v3" });
216
+ const first = await publish(ctx, bot._id);
217
+ first.should.have.status(200);
218
+ await breakBlock(first.body.bot_id, "Invite On Call");
219
+ await Faq.updateOne({ id_faq_kb: String(bot._id), intent_display_name: "Say hello" }, { $set: { actions: [{ _tdActionType: "reply", text: "draft edit" }] } }).exec();
220
+ const draftBlocks = await blocksOf(bot._id);
221
+ const draftBefore = await Faq_kb.findById(bot._id).lean().exec();
222
+
223
+ const res = await api(ctx, 'put', '/faq_kb/' + bot._id + '/restore/' + first.body.bot_id).send({});
224
+ res.should.have.status(422);
225
+ expect(res.body.message).to.include('The flow could not be imported: 1 block could not be stored — "Invite On Call"');
226
+ expect(await blocksOf(bot._id)).to.deep.equal(draftBlocks);
227
+ const draftAfter = await Faq_kb.findById(bot._id).lean().exec();
228
+ expect(draftAfter.attributes).to.deep.equal(draftBefore.attributes);
229
+
230
+ // the same release through publish restore_from
231
+ const viaPublish = await api(ctx, 'put', '/faq_kb/' + bot._id + '/publish').send({ restore_from: first.body.bot_id });
232
+ viaPublish.should.have.status(422);
233
+ expect(viaPublish.body.message).to.include('1 block could not be published — "Invite On Call"');
234
+ expect(await publishedCopies(ctx, bot._id)).to.have.length(1);
235
+ }).timeout(30000);
236
+ });
237
+
238
+ describe('Publish', () => {
239
+
240
+ it('publish-of-a-valid-v3-flow-is-unchanged', async () => {
241
+ const ctx = await signupAndProject("pub-ok");
242
+ const bot = await botWithBlocks(ctx, { name: "pub-ok", ds_version: "v3" });
243
+ const draftBlocks = await blocksOf(bot._id);
244
+
245
+ const res = await publish(ctx, bot._id);
246
+ res.should.have.status(200);
247
+ expect(res.body.message).to.equal("Chatbot published successfully");
248
+ expect(await blocksOf(res.body.bot_id)).to.have.length(draftBlocks.length);
249
+ const draft = await Faq_kb.findById(bot._id).lean().exec();
250
+ expect(draft.url).to.match(new RegExp(res.body.bot_id + '$'));
251
+ }).timeout(30000);
252
+
253
+ it('publish-of-a-v2-bot-is-unchanged', async () => {
254
+ const ctx = await signupAndProject("pub-v2");
255
+ const bot = await botWithBlocks(ctx, { name: "pub-v2" });
256
+ expect(bot.ds_version).to.equal(undefined);
257
+ const draftBlocks = await blocksOf(bot._id);
258
+
259
+ const res = await publish(ctx, bot._id);
260
+ res.should.have.status(200);
261
+ expect(res.body.message).to.equal("Chatbot published successfully");
262
+ expect(await blocksOf(res.body.bot_id)).to.have.length(draftBlocks.length);
263
+ expect(await publishedCopies(ctx, bot._id)).to.have.length(1);
264
+ }).timeout(30000);
265
+
266
+ it('publish-with-an-unsavable-block-answers-422-and-changes-nothing', async () => {
267
+ const ctx = await signupAndProject("pub-bad");
268
+ const bot = await botWithBlocks(ctx, { name: "pub-bad", ds_version: "v3" });
269
+ const first = await publish(ctx, bot._id);
270
+ first.should.have.status(200);
271
+ const draftBefore = await Faq_kb.findById(bot._id).lean().exec();
272
+
273
+ await breakBlock(bot._id, "Invite On Call");
274
+ await Faq_kb.findByIdAndUpdate(bot._id, { modified: true }).exec();
275
+ const botsBefore = await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec();
276
+ const blocksBefore = await Faq.countDocuments({ id_project: String(ctx.project._id) }).exec();
277
+
278
+ const res = await publish(ctx, bot._id);
279
+ res.should.have.status(422);
280
+ expect(res.body.success).to.equal(false);
281
+ expect(res.body.message).to.equal(
282
+ 'The flow was not published: 1 block could not be published — "Invite On Call": field name "attributes.commands" ' +
283
+ "in action 1 (setattribute-v2) contains '.', which is not allowed. Fix the block and publish again."
284
+ );
285
+ expect(res.body.errors).to.have.length(1);
286
+ expect(res.body.errors[0]).to.include({ intent_display_name: "Invite On Call", field: "attributes.commands" });
287
+ expect(res.body.errors[0].intent_id).to.be.a('string');
288
+ expect(res.body.errors[0].reason).to.include("contains '.'");
289
+
290
+ const draftAfter = await Faq_kb.findById(bot._id).lean().exec();
291
+ expect(draftAfter.url).to.equal(draftBefore.url);
292
+ expect(draftAfter.modified).to.equal(true);
293
+ const copies = await publishedCopies(ctx, bot._id);
294
+ expect(copies).to.have.length(1);
295
+ expect(String(copies[0]._id)).to.equal(first.body.bot_id);
296
+ expect(await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec()).to.equal(botsBefore);
297
+ expect(await Faq.countDocuments({ id_project: String(ctx.project._id) }).exec()).to.equal(blocksBefore);
298
+ }).timeout(30000);
299
+
300
+ it('publish-multi-reports-the-readable-message-per-agent', async () => {
301
+ const ctx = await signupAndProject("pub-multi");
302
+ const bot = await botWithBlocks(ctx, { name: "pub-multi", ds_version: "v3" });
303
+ await breakBlock(bot._id, "Invite On Call");
304
+
305
+ const res = await api(ctx, 'put', '/faq_kb/' + bot._id + '/publish/multi').send({ chatbots: [{ id: bot._id }] });
306
+ res.should.have.status(500);
307
+ expect(res.body.results[0].success).to.equal(false);
308
+ expect(res.body.results[0].error).to.include('The flow was not published: 1 block could not be published — "Invite On Call"');
309
+ expect(res.body.results[0].errors).to.have.length(1);
310
+ expect(await publishedCopies(ctx, bot._id)).to.have.length(0);
311
+ }).timeout(30000);
312
+
313
+ it('publish-with-a-dotted-key-in-the-agent-attributes-names-the-field', async () => {
314
+ const ctx = await signupAndProject("pub-attr");
315
+ const bot = await botWithBlocks(ctx, { name: "pub-attr", ds_version: "v3" });
316
+ const raw = await Faq_kb.collection.updateOne({ _id: new mongoose.Types.ObjectId(bot._id) }, { $set: { "attributes.rules": [{ "when.x": 1 }] } });
317
+ expect(raw.modifiedCount).to.equal(1);
318
+ const res = await publish(ctx, bot._id);
319
+ res.should.have.status(422);
320
+ expect(res.body.message).to.equal(
321
+ 'The flow was not published: the agent settings could not be published — the agent settings of "pub-attr": ' +
322
+ "field name \"when.x\" in the agent attributes contains '.', which is not allowed. Fix the agent settings and publish again."
323
+ );
324
+ expect(await publishedCopies(ctx, bot._id)).to.have.length(0);
325
+ }).timeout(30000);
326
+
327
+ it('restore-from-another-project-reads-nothing-of-it', async () => {
328
+ const other = await signupAndProject("pub-other");
329
+ const foreign = await botWithBlocks(other, { name: "foreign-secret-bot", ds_version: "v3" });
330
+ await breakBlock(foreign._id, "Invite On Call");
331
+ await Faq.collection.updateOne({ id_faq_kb: String(foreign._id), intent_display_name: "Invite On Call" }, { $set: { intent_display_name: "Foreign Secret Block" } });
332
+
333
+ const ctx = await signupAndProject("pub-mine");
334
+ const bot = await botWithBlocks(ctx, { name: "mine", ds_version: "v3" });
335
+ const draftBefore = await Faq_kb.findById(bot._id).lean().exec();
336
+ const res = await api(ctx, 'put', '/faq_kb/' + bot._id + '/publish').send({ restore_from: String(foreign._id) });
337
+ expect(res.status).to.not.equal(200);
338
+ expect(JSON.stringify(res.body)).to.not.include("Foreign Secret Block");
339
+ expect(JSON.stringify(res.body)).to.not.include("attributes.commands");
340
+ expect(res.body.errors).to.equal(undefined);
341
+ const draftAfter = await Faq_kb.findById(bot._id).lean().exec();
342
+ expect(draftAfter.url).to.equal(draftBefore.url);
343
+ expect(await publishedCopies(ctx, bot._id)).to.have.length(0);
344
+ }).timeout(30000);
345
+
346
+ it('a-copy-that-fails-half-way-is-removed-and-the-draft-keeps-its-published-copy', async () => {
347
+ const ctx = await signupAndProject("pub-midway");
348
+ const bot = await botWithBlocks(ctx, { name: "pub-midway", ds_version: "v3" });
349
+ const first = await publish(ctx, bot._id);
350
+ first.should.have.status(200);
351
+ const draftBefore = await Faq_kb.findById(bot._id).lean().exec();
352
+ const botsBefore = await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec();
353
+ const blocksBefore = await Faq.countDocuments({ id_project: String(ctx.project._id) }).exec();
354
+
355
+ // the store refuses the third block of the copy, after the first two were saved
356
+ const originalCreate = intentsController.create;
357
+ let calls = 0;
358
+ intentsController.create = async function (...args) {
359
+ calls++;
360
+ if (calls === 3) { throw new Error("simulated store failure"); }
361
+ return originalCreate.apply(this, args);
362
+ };
363
+ let res;
364
+ try {
365
+ res = await publish(ctx, bot._id);
366
+ } finally {
367
+ intentsController.create = originalCreate;
368
+ }
369
+ expect(res.status).to.equal(500);
370
+ expect(res.body.message).to.include("The flow was not published");
371
+ expect(res.body.message).to.include("The published version was not changed");
372
+
373
+ const draftAfter = await Faq_kb.findById(bot._id).lean().exec();
374
+ expect(draftAfter.url).to.equal(draftBefore.url);
375
+ expect(await publishedCopies(ctx, bot._id)).to.have.length(1);
376
+ expect(await Faq_kb.countDocuments({ id_project: String(ctx.project._id) }).exec()).to.equal(botsBefore);
377
+ expect(await Faq.countDocuments({ id_project: String(ctx.project._id) }).exec()).to.equal(blocksBefore);
378
+ }).timeout(30000);
379
+ });
380
+
381
+ describe('IntentsController createMany / upsertMany', () => {
382
+
383
+ async function failing(method, inner) {
384
+ const original = intentsController[inner];
385
+ intentsController[inner] = async function (chatbot_id, id_project, user_id, intent) {
386
+ if (intent.intent_display_name === "b") { throw new Error("boom"); }
387
+ return { intent_display_name: intent.intent_display_name };
388
+ };
389
+ try {
390
+ await intentsController[method]("bot", "project", "user", [{ intent_id: "ia", intent_display_name: "a" }, { intent_id: "ib", intent_display_name: "b" }]);
391
+ } catch (err) {
392
+ return err;
393
+ } finally {
394
+ intentsController[inner] = original;
395
+ }
396
+ return undefined;
397
+ }
398
+
399
+ it('createMany rejects when a block cannot be created instead of dropping it', async () => {
400
+ const err = await failing("createMany", "create");
401
+ expect(err).to.not.equal(undefined);
402
+ expect(err.status).to.equal(500);
403
+ expect(err.message).to.equal('1 block could not be saved — "b": boom');
404
+ expect(err.errors).to.deep.equal([{ intent_id: "ib", intent_display_name: "b", reason: "boom" }]);
405
+ });
406
+
407
+ it('upsertMany rejects when a block cannot be saved instead of dropping it', async () => {
408
+ const err = await failing("upsertMany", "upsert");
409
+ expect(err).to.not.equal(undefined);
410
+ expect(err.message).to.equal('1 block could not be saved — "b": boom');
411
+ expect(err.errors).to.have.length(1);
412
+ });
413
+ });
414
+ });