@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
@@ -0,0 +1,158 @@
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 chai = require('chai');
6
+ const chaiHttp = require('chai-http');
7
+ const server = require('../app');
8
+ const expect = chai.expect;
9
+ chai.use(chaiHttp);
10
+
11
+ const userService = require('../services/userService');
12
+ const { Webhook } = require('../models/webhook');
13
+ const Project_user = require('../models/project_user');
14
+ const RoleConstants = require('../models/roleConstants');
15
+ const { setupProjectWithBot, uniqueEmail, PWD } = require('./webhookConversationTestUtils');
16
+
17
+ function putStart(ctx, type, body, chatbot_id) {
18
+ return chai.request(server)
19
+ .put('/' + ctx.project._id + '/webhooks/' + (chatbot_id || ctx.chatbot_id) + '/start_points/' + type)
20
+ .auth(ctx.email, ctx.pwd)
21
+ .send(body);
22
+ }
23
+
24
+ function deleteStart(ctx, type, chatbot_id) {
25
+ return chai.request(server)
26
+ .delete('/' + ctx.project._id + '/webhooks/' + (chatbot_id || ctx.chatbot_id) + '/start_points/' + type)
27
+ .auth(ctx.email, ctx.pwd);
28
+ }
29
+
30
+ describe('StartPointsRoute', function () {
31
+
32
+ this.timeout(30000);
33
+
34
+ it('creates the conversation webhook with the webhook start point', async () => {
35
+ const ctx = await setupProjectWithBot(server);
36
+ const block_id = ctx.startIntent.intent_id;
37
+
38
+ const res = await putStart(ctx, 'webhook', { block_id: block_id, mapping: { source_name: 'Grafana' } });
39
+
40
+ expect(res).to.have.status(200);
41
+ expect(res.body.mode).to.equal('conversation');
42
+ expect(res.body.chatbot_id).to.equal(ctx.chatbot_id);
43
+ expect(res.body.start_points).to.have.length(1);
44
+ expect(res.body.start_points[0].type).to.equal('webhook');
45
+ expect(res.body.start_points[0].block_id).to.equal(block_id);
46
+ expect(res.body.start_points[0].enabled).to.equal(true);
47
+ expect(res.body.start_points[0].mapping.source_name).to.equal('Grafana');
48
+ expect(res.body.department_id).to.equal(undefined);
49
+ expect(res.body.source_name).to.equal(undefined);
50
+ });
51
+
52
+ it('updates the same entry on a second PUT', async () => {
53
+ const ctx = await setupProjectWithBot(server);
54
+ const block_id = ctx.startIntent.intent_id;
55
+ await putStart(ctx, 'webhook', { block_id: block_id, mapping: { source_name: 'Grafana' } });
56
+
57
+ const res = await putStart(ctx, 'webhook', { block_id: block_id, enabled: false });
58
+
59
+ expect(res).to.have.status(200);
60
+ expect(res.body.start_points).to.have.length(1);
61
+ expect(res.body.start_points[0].enabled).to.equal(false);
62
+ expect(res.body.start_points[0].mapping.source_name).to.equal('Grafana');
63
+ });
64
+
65
+ it('rejects an unknown start point type with 400', async () => {
66
+ const ctx = await setupProjectWithBot(server);
67
+ const res = await putStart(ctx, 'unknown', { block_id: ctx.startIntent.intent_id });
68
+ expect(res).to.have.status(400);
69
+ expect(res.body.success).to.equal(false);
70
+ });
71
+
72
+ it('rejects a chatbot with subtype webhook with 400', async () => {
73
+ const ctx = await setupProjectWithBot(server, { subtype: 'webhook', waitStart: false });
74
+ const res = await putStart(ctx, 'webhook', { block_id: 'any-block' });
75
+ expect(res).to.have.status(400);
76
+ });
77
+
78
+ it('answers 404 for a block of another chatbot and for an unknown block, saving nothing', async () => {
79
+ const ctx = await setupProjectWithBot(server);
80
+ const other = await setupProjectWithBot(server);
81
+
82
+ const resOther = await putStart(ctx, 'webhook', { block_id: other.startIntent.intent_id });
83
+ expect(resOther).to.have.status(404);
84
+
85
+ const resUnknown = await putStart(ctx, 'webhook', { block_id: 'nope' });
86
+ expect(resUnknown).to.have.status(404);
87
+
88
+ expect(await Webhook.countDocuments({ chatbot_id: ctx.chatbot_id })).to.equal(0);
89
+ });
90
+
91
+ it('answers 400 when block_id is missing', async () => {
92
+ const ctx = await setupProjectWithBot(server);
93
+ const res = await putStart(ctx, 'webhook', {});
94
+ expect(res).to.have.status(400);
95
+ });
96
+
97
+ it('asks for confirmation on an automation webhook, then converts it keeping its block', async () => {
98
+ const ctx = await setupProjectWithBot(server);
99
+ const created = await chai.request(server)
100
+ .post('/' + ctx.project._id + '/webhooks/')
101
+ .auth(ctx.email, ctx.pwd)
102
+ .send({ chatbot_id: ctx.chatbot_id, block_id: 'legacy-block' });
103
+ expect(created).to.have.status(200);
104
+
105
+ const res = await putStart(ctx, 'webhook', { block_id: ctx.startIntent.intent_id });
106
+ expect(res).to.have.status(409);
107
+ const unchanged = await Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean().exec();
108
+ expect(unchanged.mode).to.equal(undefined);
109
+ expect(unchanged.block_id).to.equal('legacy-block');
110
+ expect(unchanged.start_points || []).to.have.length(0);
111
+
112
+ const confirmed = await putStart(ctx, 'webhook', { block_id: ctx.startIntent.intent_id, confirm: true });
113
+ expect(confirmed).to.have.status(200);
114
+ expect(confirmed.body.mode).to.equal('conversation');
115
+ expect(confirmed.body.block_id).to.equal('legacy-block');
116
+ expect(confirmed.body.start_points).to.have.length(1);
117
+ });
118
+
119
+ it('removes the start point and answers 404 when it is already absent', async () => {
120
+ const ctx = await setupProjectWithBot(server);
121
+ await putStart(ctx, 'webhook', { block_id: ctx.startIntent.intent_id });
122
+
123
+ const res = await deleteStart(ctx, 'webhook');
124
+ expect(res).to.have.status(200);
125
+ expect(res.body.start_points).to.have.length(0);
126
+
127
+ const again = await deleteStart(ctx, 'webhook');
128
+ expect(again).to.have.status(404);
129
+ });
130
+
131
+ it('answers 403 to an agent', async () => {
132
+ const ctx = await setupProjectWithBot(server);
133
+ const email = uniqueEmail('test-start-points-agent');
134
+ const agent = await userService.signup(email, PWD, 'Agent', 'User');
135
+ await new Project_user({
136
+ id_project: ctx.project._id.toString(),
137
+ id_user: agent._id.toString(),
138
+ role: RoleConstants.AGENT,
139
+ roleType: RoleConstants.TYPE_AGENTS,
140
+ user_available: true,
141
+ createdBy: ctx.user._id.toString(),
142
+ updatedBy: ctx.user._id.toString()
143
+ }).save();
144
+
145
+ const res = await chai.request(server)
146
+ .put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/webhook')
147
+ .auth(email, PWD)
148
+ .send({ block_id: ctx.startIntent.intent_id });
149
+ expect(res).to.have.status(403);
150
+ });
151
+
152
+ it('answers 404 for a chatbot of another project', async () => {
153
+ const ctx = await setupProjectWithBot(server);
154
+ const other = await setupProjectWithBot(server);
155
+ const res = await putStart(ctx, 'webhook', { block_id: other.startIntent.intent_id }, other.chatbot_id);
156
+ expect(res).to.have.status(404);
157
+ });
158
+ });
@@ -0,0 +1,347 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+ process.env.LOG_LEVEL = 'error';
4
+ process.env.BUCKET_WH_CAPACITY = 2;
5
+
6
+ const chai = require('chai');
7
+ const chaiHttp = require('chai-http');
8
+ const server = require('../app');
9
+ const expect = chai.expect;
10
+ chai.use(chaiHttp);
11
+
12
+ const { setupProjectWithBot } = require('./webhookConversationTestUtils');
13
+
14
+ describe('WebhookConversationRoute - settings', function () {
15
+
16
+ this.timeout(20000);
17
+
18
+ it('creates a conversation webhook bound to the start block when block_id is omitted', async () => {
19
+ const { email, pwd, project, chatbot_id, startIntent } = await setupProjectWithBot(server);
20
+ const res = await chai.request(server)
21
+ .post('/' + project._id + '/webhooks/')
22
+ .auth(email, pwd)
23
+ .send({ chatbot_id: chatbot_id, mode: 'conversation' });
24
+
25
+ expect(res).to.have.status(200);
26
+ expect(res.body.mode).to.equal('conversation');
27
+ expect(res.body.block_id).to.equal(startIntent.intent_id);
28
+ expect(res.body.webhook_id).to.have.length(32);
29
+ });
30
+
31
+ it('rejects conversation mode for a bot that is not subtype chatbot', async () => {
32
+ const { email, pwd, project, chatbot_id } = await setupProjectWithBot(server, { subtype: 'webhook', waitStart: false });
33
+
34
+ const res = await chai.request(server)
35
+ .post('/' + project._id + '/webhooks/')
36
+ .auth(email, pwd)
37
+ .send({ chatbot_id: chatbot_id, block_id: 'any-block', mode: 'conversation' });
38
+
39
+ expect(res).to.have.status(422);
40
+ expect(res.body.success).to.equal(false);
41
+ });
42
+
43
+ it('returns 404 when the chatbot does not exist', async () => {
44
+ const { email, pwd, project } = await setupProjectWithBot(server, { waitStart: false });
45
+
46
+ const resUnknown = await chai.request(server)
47
+ .post('/' + project._id + '/webhooks/')
48
+ .auth(email, pwd)
49
+ .send({ chatbot_id: '5f0000000000000000000000', block_id: 'any-block' });
50
+ expect(resUnknown).to.have.status(404);
51
+
52
+ const resInvalid = await chai.request(server)
53
+ .post('/' + project._id + '/webhooks/')
54
+ .auth(email, pwd)
55
+ .send({ chatbot_id: 'not-an-object-id', block_id: 'any-block' });
56
+ expect(resInvalid).to.have.status(404);
57
+ });
58
+
59
+ it('switches an existing automation webhook to conversation mode with PUT', async () => {
60
+ const { email, pwd, project, chatbot_id, startIntent } = await setupProjectWithBot(server);
61
+
62
+ const created = await chai.request(server)
63
+ .post('/' + project._id + '/webhooks/')
64
+ .auth(email, pwd)
65
+ .send({ chatbot_id: chatbot_id, block_id: 'legacy-block' });
66
+ expect(created).to.have.status(200);
67
+ expect(created.body.mode).to.equal(undefined);
68
+
69
+ const res = await chai.request(server)
70
+ .put('/' + project._id + '/webhooks/' + chatbot_id)
71
+ .auth(email, pwd)
72
+ .send({ mode: 'conversation', enabled: true });
73
+
74
+ expect(res).to.have.status(200);
75
+ expect(res.body.mode).to.equal('conversation');
76
+ expect(res.body.block_id).to.equal(startIntent.intent_id);
77
+ expect(res.body.enabled).to.equal(true);
78
+ });
79
+
80
+ it('updates settings by webhook_id with PUT /update/:webhook_id', async () => {
81
+ const { email, pwd, project, chatbot_id } = await setupProjectWithBot(server);
82
+
83
+ const created = await chai.request(server)
84
+ .post('/' + project._id + '/webhooks/')
85
+ .auth(email, pwd)
86
+ .send({ chatbot_id: chatbot_id, mode: 'conversation' });
87
+ expect(created).to.have.status(200);
88
+
89
+ const res = await chai.request(server)
90
+ .put('/' + project._id + '/webhooks/update/' + created.body.webhook_id)
91
+ .auth(email, pwd)
92
+ .send({ enabled: false });
93
+
94
+ expect(res).to.have.status(200);
95
+ expect(res.body.enabled).to.equal(false);
96
+ expect(res.body.mode).to.equal('conversation');
97
+ });
98
+
99
+ it('preload returns a support-group request id for conversation webhooks', async () => {
100
+ const { email, pwd, project, chatbot_id } = await setupProjectWithBot(server);
101
+
102
+ const created = await chai.request(server)
103
+ .post('/' + project._id + '/webhooks/')
104
+ .auth(email, pwd)
105
+ .send({ chatbot_id: chatbot_id, mode: 'conversation' });
106
+
107
+ const res = await chai.request(server)
108
+ .post('/' + project._id + '/webhooks/preload/' + created.body.webhook_id)
109
+ .auth(email, pwd);
110
+
111
+ expect(res).to.have.status(200);
112
+ expect(res.body.request_id.startsWith('support-group-' + project._id + '-')).to.equal(true);
113
+ expect(res.body.request_id.split('-')).to.have.length(4);
114
+ });
115
+ });
116
+
117
+ const Request = require('../models/request');
118
+ const Message = require('../models/message');
119
+ const { waitFor } = require('./webhookConversationTestUtils');
120
+
121
+ async function createWebhook(ctx, body) {
122
+ const res = await chai.request(server)
123
+ .post('/' + ctx.project._id + '/webhooks/')
124
+ .auth(ctx.email, ctx.pwd)
125
+ .send(Object.assign({ chatbot_id: ctx.chatbot_id }, body));
126
+ expect(res).to.have.status(200);
127
+ return res.body;
128
+ }
129
+
130
+ // Conversation webhooks start at the webhook start box: create it through the start point API.
131
+ async function createConversationWebhook(ctx, startPoint) {
132
+ const res = await chai.request(server)
133
+ .put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/webhook')
134
+ .auth(ctx.email, ctx.pwd)
135
+ .send(Object.assign({ block_id: ctx.startIntent.intent_id }, startPoint));
136
+ expect(res).to.have.status(200);
137
+ return res.body;
138
+ }
139
+
140
+ function disableStartPoint(ctx) {
141
+ return chai.request(server)
142
+ .put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/webhook')
143
+ .auth(ctx.email, ctx.pwd)
144
+ .send({ block_id: ctx.startIntent.intent_id, enabled: false });
145
+ }
146
+
147
+ describe('WebhookConversationRoute - run', function () {
148
+
149
+ this.timeout(20000);
150
+
151
+ it('keeps the automation behaviour for a chatbot webhook without mode', async () => {
152
+ const ctx = await setupProjectWithBot(server);
153
+ const webhook = await createWebhook(ctx, { block_id: 'legacy-block' });
154
+
155
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ alert: 'legacy' });
156
+
157
+ expect(res).to.have.status(200);
158
+ expect(res.body.success).to.equal(true);
159
+ expect(res.body.message).to.equal('Webhook disabled in test mode');
160
+ });
161
+
162
+ it('starts a conversation for a conversation-mode webhook', async () => {
163
+ const ctx = await setupProjectWithBot(server);
164
+ const webhook = await createConversationWebhook(ctx);
165
+
166
+ const res = await chai.request(server)
167
+ .post('/webhook/' + webhook.webhook_id + '?env=prod')
168
+ .send({ alert: 'KubePodCrashLooping' });
169
+
170
+ expect(res).to.have.status(202);
171
+ expect(res.body.success).to.equal(true);
172
+ expect(res.body.request_id.startsWith('support-group-' + ctx.project._id + '-')).to.equal(true);
173
+
174
+ const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
175
+ expect(request.participants).to.deep.equal(['bot_' + ctx.chatbot_id]);
176
+ expect(request.channel.name).to.equal('webhook');
177
+ expect(request.attributes.payload.alert).to.equal('KubePodCrashLooping');
178
+ expect(request.attributes.payload.webhook_http_method).to.equal('POST');
179
+ expect(request.attributes.payload.webhook_query_params.env).to.equal('prod');
180
+
181
+ await waitFor(() => Message.findOne({ recipient: res.body.request_id, text: '/start' }).lean().exec());
182
+ });
183
+
184
+ it('starts the flow at the webhook start box with start_type webhook', async () => {
185
+ const ctx = await setupProjectWithBot(server);
186
+ const webhook = await createConversationWebhook(ctx, { mapping: { source_name: 'Grafana' } });
187
+
188
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ alert: 'box' });
189
+
190
+ expect(res).to.have.status(202);
191
+ const request = await Request.findOne({ request_id: res.body.request_id }).populate('lead').lean().exec();
192
+ expect(request.attributes.start_type).to.equal('webhook');
193
+ expect(request.lead.fullname).to.equal('Grafana');
194
+ const startMessage = await waitFor(() => Message.findOne({ recipient: res.body.request_id, text: '/start' }).lean().exec());
195
+ expect(startMessage.attributes.action).to.equal('#' + ctx.startIntent.intent_id);
196
+ });
197
+
198
+ it('names the lead after the chatbot when the mapping is empty', async () => {
199
+ const ctx = await setupProjectWithBot(server);
200
+ const webhook = await createConversationWebhook(ctx);
201
+
202
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({});
203
+
204
+ expect(res).to.have.status(202);
205
+ const request = await Request.findOne({ request_id: res.body.request_id }).populate('lead').lean().exec();
206
+ expect(request.lead.fullname).to.equal('hitl-bot');
207
+ });
208
+
209
+ it('returns 422 for a conversation webhook without start points', async () => {
210
+ const ctx = await setupProjectWithBot(server);
211
+ const webhook = await createWebhook(ctx, { mode: 'conversation' });
212
+
213
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({});
214
+ expect(res).to.have.status(422);
215
+ expect(res.body).to.deep.equal({ success: false, error: 'Webhook start is not enabled' });
216
+ });
217
+
218
+ it('returns 422 for a disabled webhook start point', async () => {
219
+ const ctx = await setupProjectWithBot(server);
220
+ const webhook = await createConversationWebhook(ctx);
221
+ const disabled = await disableStartPoint(ctx);
222
+ expect(disabled).to.have.status(200);
223
+
224
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({});
225
+ expect(res).to.have.status(422);
226
+ expect(res.body).to.deep.equal({ success: false, error: 'Webhook start is not enabled' });
227
+ });
228
+
229
+ it('returns 422 on the dev route with a preload when the start point is disabled', async () => {
230
+ const ctx = await setupProjectWithBot(server);
231
+ const webhook = await createConversationWebhook(ctx);
232
+ const preload = await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
233
+ expect(preload).to.have.status(200);
234
+ await new Promise((resolve) => setTimeout(resolve, 200)); // preload writes to Redis without awaiting
235
+ await disableStartPoint(ctx);
236
+
237
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({});
238
+ expect(res).to.have.status(422);
239
+ expect(res.body).to.deep.equal({ success: false, error: 'Webhook start is not enabled' });
240
+ });
241
+
242
+ it('sanitizes dotted payload keys (Kubernetes/Grafana labels) so the request can be created', async () => {
243
+ const ctx = await setupProjectWithBot(server);
244
+ const webhook = await createConversationWebhook(ctx);
245
+
246
+ const res = await chai.request(server)
247
+ .post('/webhook/' + webhook.webhook_id)
248
+ .send({ commonLabels: { 'app.kubernetes.io/name': 'checkout-api' } });
249
+
250
+ expect(res).to.have.status(202);
251
+
252
+ const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
253
+ expect(request.attributes.payload.commonLabels['app_kubernetes_io/name']).to.equal('checkout-api');
254
+ });
255
+
256
+ it('returns 422 for a disabled conversation webhook', async () => {
257
+ const ctx = await setupProjectWithBot(server);
258
+ const webhook = await createConversationWebhook(ctx);
259
+ await chai.request(server).put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id).auth(ctx.email, ctx.pwd).send({ enabled: false });
260
+
261
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({});
262
+ expect(res).to.have.status(422);
263
+ });
264
+
265
+ it('returns 422 on the dev route without preload', async () => {
266
+ const ctx = await setupProjectWithBot(server);
267
+ const webhook = await createConversationWebhook(ctx);
268
+
269
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({});
270
+ expect(res).to.have.status(422);
271
+ expect(res.body.code).to.equal(13001);
272
+ });
273
+
274
+ it('starts a draft conversation with the preloaded request id on the dev route', async () => {
275
+ const ctx = await setupProjectWithBot(server);
276
+ const webhook = await createConversationWebhook(ctx);
277
+ const preload = await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
278
+ expect(preload).to.have.status(200);
279
+ await new Promise((resolve) => setTimeout(resolve, 200)); // preload writes to Redis without awaiting
280
+
281
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'dev' });
282
+
283
+ expect(res).to.have.status(202);
284
+ expect(res.body.request_id).to.equal(preload.body.request_id);
285
+ const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
286
+ expect(request.draft).to.equal(true);
287
+ });
288
+
289
+ it('consumes the preload on the dev route: a second dev call without a new preload gets 422', async () => {
290
+ const ctx = await setupProjectWithBot(server);
291
+ const webhook = await createConversationWebhook(ctx);
292
+ const preload = await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
293
+ expect(preload).to.have.status(200);
294
+ await new Promise((resolve) => setTimeout(resolve, 200)); // preload writes to Redis without awaiting
295
+
296
+ const first = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'dev-1' });
297
+ expect(first).to.have.status(202);
298
+
299
+ const second = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'dev-2' });
300
+ expect(second).to.have.status(422);
301
+ expect(second.body.code).to.equal(13001);
302
+ });
303
+
304
+ it('returns 422 on the dev route for a disabled conversation webhook', async () => {
305
+ const ctx = await setupProjectWithBot(server);
306
+ const webhook = await createConversationWebhook(ctx);
307
+ await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
308
+ await chai.request(server).put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id).auth(ctx.email, ctx.pwd).send({ enabled: false });
309
+
310
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({});
311
+ expect(res).to.have.status(422);
312
+ });
313
+
314
+ it('rate limits conversation webhooks', async () => {
315
+ const ctx = await setupProjectWithBot(server);
316
+ const webhook = await createConversationWebhook(ctx);
317
+
318
+ // The bucket capacity is read once when the app is first required (2 if this file loads
319
+ // the app, 10 by default if another test file loaded it first): call until limited.
320
+ const statuses = [];
321
+ for (let i = 0; i < 15 && !statuses.includes(429); i++) {
322
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ i: i });
323
+ statuses.push(res.status);
324
+ }
325
+ expect(statuses).to.include(429);
326
+ expect(statuses.filter((s) => s !== 429 && s !== 202)).to.deep.equal([]);
327
+ });
328
+
329
+ it('rate limits the dev conversation route', async () => {
330
+ const ctx = await setupProjectWithBot(server);
331
+ const webhook = await createConversationWebhook(ctx);
332
+
333
+ // The preload is single-use (see the test above), so each dev call needs its own preload;
334
+ // preloading itself is not rate-limited.
335
+ const statuses = [];
336
+ for (let i = 0; i < 15 && !statuses.includes(429); i++) {
337
+ const preload = await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
338
+ expect(preload).to.have.status(200);
339
+ await new Promise((resolve) => setTimeout(resolve, 200)); // preload writes to Redis without awaiting
340
+
341
+ const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ i: i });
342
+ statuses.push(res.status);
343
+ }
344
+ expect(statuses).to.include(429);
345
+ expect(statuses.filter((s) => s !== 429 && s !== 202)).to.deep.equal([]);
346
+ });
347
+ });