@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.
- package/.circleci/config.yml +1 -1
- package/CHANGELOG.md +34 -0
- package/Dockerfile +3 -2
- package/Dockerfile-en +3 -2
- package/Dockerfile-jobs +3 -2
- package/Dockerfile-profiler +3 -2
- package/README.md +15 -0
- package/app.js +12 -1
- package/bin/quota-reset +68 -0
- package/bin/quota-restore +86 -0
- package/channels/chat21/chat21Handler.js +50 -34
- package/channels/chat21/chat21JoinTracker.js +43 -0
- package/config/database.js +5 -5
- package/config/native_mcp/native_mcp_servers.json +1 -0
- package/controllers/chatbot.controller.js +80 -27
- package/controllers/intents.controller.js +42 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
- package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
- package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
- package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
- package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
- package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
- package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
- package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
- package/event/botEvent.js +12 -1
- package/event/emailEvent.js +3 -0
- package/event/messageEvent.js +44 -0
- package/jobs.js +10 -0
- package/jobsManager.js +19 -0
- package/middleware/errorHandler.js +6 -2
- package/models/analyticResult.js +1 -1
- package/models/channelConstants.js +3 -2
- package/models/quotaUsage.js +71 -0
- package/models/request.js +6 -0
- package/models/requestSnapshot.js +12 -0
- package/models/routerLogger.js +1 -1
- package/models/webhook.js +44 -0
- package/package.json +7 -5
- package/pubmodules/emailNotification/requestNotification.js +14 -22
- package/pubmodules/events/test/eventRoute.js +7 -6
- package/pubmodules/rules/conciergeBot.js +9 -4
- package/routes/auth.js +1 -12
- package/routes/campaigns.js +0 -13
- package/routes/dataTable.js +0 -3
- package/routes/faq_kb.js +12 -1
- package/routes/messagesRoot.js +0 -1
- package/routes/quotes.js +38 -1
- package/routes/request.js +81 -5
- package/routes/users.js +0 -1
- package/routes/webhook.js +160 -8
- package/routes/webhooks.js +276 -6
- package/routes/widgetLoader.js +6 -3
- package/services/QuoteManager.js +77 -16
- package/services/aiService.js +1 -1
- package/services/chatbotService.js +55 -1
- package/services/departmentService.js +60 -43
- package/services/dolphinSchedulerClient.js +116 -0
- package/services/emailService.js +6 -1
- package/services/hitlParticipantListener.js +260 -0
- package/services/participantInviteService.js +302 -0
- package/services/participantRemoveService.js +79 -0
- package/services/quotaSnapshotService.js +1167 -0
- package/services/requestService.js +102 -144
- package/services/scheduledStart/dolphinBackend.js +246 -0
- package/services/scheduledStart/schedulerBackend.js +101 -0
- package/services/scheduledStart/schedulerServiceClient.js +41 -0
- package/services/scheduledStartPublishListener.js +186 -0
- package/services/scheduledStartRules.js +173 -0
- package/services/scheduledStartService.js +130 -0
- package/services/startPointCleanupListener.js +30 -0
- package/services/startPointService.js +158 -0
- package/services/testWsService.js +1 -4
- package/services/updateRequestSnapshotQueued.js +8 -9
- package/services/webhookConversationService.js +185 -0
- package/services/webhookSettingsValidator.js +61 -0
- package/template/email/checkpointReachedEmail.html +5 -0
- package/test/agentPromptsRoute.test.js +3 -2
- package/test/agentTokensQuota.js +237 -0
- package/test/botEventHitlFallback.js +19 -0
- package/test/chat21HandlerJoinWiring.js +59 -0
- package/test/dataTablesRoute.test.js +10 -2
- package/test/dateUtils.test.js +0 -3
- package/test/departmentService.js +8 -12
- package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
- package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
- package/test/faqRoute.js +6 -1
- package/test/faqkbRoute.js +14 -10
- package/test/faqkbV3Route.js +1 -1
- package/test/filepRoute.js +4 -50
- package/test/hitlParticipantListener.js +446 -0
- package/test/hitlQueueWiring.js +191 -0
- package/test/hitlWaitingReset.js +124 -0
- package/test/inviteParticipantsRoute.js +492 -0
- package/test/kbRoute.js +1 -1
- package/test/messageRoute.js +33 -21
- package/test/nativemcp.test.js +7 -1
- package/test/openaiRoute.js +2 -8
- package/test/publishFailLoud.test.js +414 -0
- package/test/quotaSnapshots.js +1044 -0
- package/test/removeParticipantsRoute.js +186 -0
- package/test/requestService.js +24 -16
- package/test/requestServiceRemoveParticipant.js +144 -0
- package/test/requestVisibility.test.js +186 -0
- package/test/scheduledStartFacade.js +184 -0
- package/test/scheduledStartHandover.js +95 -0
- package/test/scheduledStartPublish.js +320 -0
- package/test/scheduledStartRoute.js +163 -0
- package/test/scheduledStartRules.js +177 -0
- package/test/scheduledStartRun.js +299 -0
- package/test/scheduledStartSchedulerBackend.js +116 -0
- package/test/scheduledStartService.js +290 -0
- package/test/startPointsCleanup.js +69 -0
- package/test/startPointsRegression.js +169 -0
- package/test/startPointsRoute.js +158 -0
- package/test/webhookConversationRoute.js +347 -0
- package/test/webhookConversationService.js +261 -0
- package/test/webhookConversationTestUtils.js +55 -0
- package/utils/aiUtils.js +95 -1
- package/utils/intentFieldNames.js +192 -0
- package/utils/quotaCliUtil.js +43 -0
- package/utils/requestVisibility.js +355 -0
- package/websocket/webSocketServer.js +36 -31
- /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
process.env.LOG_LEVEL = 'error';
|
|
3
|
+
|
|
4
|
+
const chai = require('chai');
|
|
5
|
+
const chaiHttp = require('chai-http');
|
|
6
|
+
const nock = require('nock');
|
|
7
|
+
const server = require('../app');
|
|
8
|
+
const expect = chai.expect;
|
|
9
|
+
chai.use(chaiHttp);
|
|
10
|
+
|
|
11
|
+
const Faq = require('../models/faq');
|
|
12
|
+
const { Webhook } = require('../models/webhook');
|
|
13
|
+
const service = require('../services/scheduledStartService');
|
|
14
|
+
const publishListener = require('../services/scheduledStartPublishListener');
|
|
15
|
+
const { setupProjectWithBot } = require('./webhookConversationTestUtils');
|
|
16
|
+
|
|
17
|
+
const SCHED = 'http://sched.test';
|
|
18
|
+
const DS = 'http://ds.test/dolphinscheduler';
|
|
19
|
+
const DSP = '/projects/dsproj';
|
|
20
|
+
const SCHEDULE = { frequency: 'daily', time: '09:00', timezone: 'Europe/Rome' };
|
|
21
|
+
const SCHEDULER_ENV = { SCHEDULED_START_BACKEND: 'scheduler', SCHEDULER_SERVICE_URL: SCHED, SCHEDULER_API_TOKEN: 'apitok', SCHEDULED_START_TOKEN: 'start' };
|
|
22
|
+
const DOLPHIN_ENV = { SCHEDULER_BASEURL: DS, SCHEDULER_TOKEN: 'dstoken', SCHEDULER_PROJECT: 'dsproj' };
|
|
23
|
+
const ALL = Object.keys(SCHEDULER_ENV).concat(Object.keys(DOLPHIN_ENV));
|
|
24
|
+
const ok = (data) => ({ code: 0, msg: 'success', data, success: true });
|
|
25
|
+
|
|
26
|
+
const dsLive = { workflow_code: 777, schedule_id: 9, schedule: SCHEDULE, enabled: true };
|
|
27
|
+
const schedIds = { backend: 'scheduler', job_id: 'J1', id_project: 'proj1', chatbot_id: 'bot1' };
|
|
28
|
+
|
|
29
|
+
describe('scheduledStartService facade', function () {
|
|
30
|
+
this.timeout(60000);
|
|
31
|
+
let saved;
|
|
32
|
+
|
|
33
|
+
const setEnv = (vars) => Object.keys(vars).forEach((k) => { process.env[k] = vars[k]; });
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
saved = {};
|
|
36
|
+
ALL.forEach((k) => { saved[k] = process.env[k]; delete process.env[k]; });
|
|
37
|
+
});
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
ALL.forEach((k) => { if (saved[k] === undefined) delete process.env[k]; else process.env[k] = saved[k]; });
|
|
40
|
+
const done = nock.isDone();
|
|
41
|
+
const pending = nock.pendingMocks();
|
|
42
|
+
nock.cleanAll();
|
|
43
|
+
expect(done, 'pending mocks: ' + pending).to.equal(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const upsert = () => nock(SCHED, { reqheaders: { authorization: 'Bearer apitok' } })
|
|
47
|
+
.post('/jobs/cron').reply(201, { job: { _id: 'J1' }, created: true });
|
|
48
|
+
const dsDestroy = (status = 200) => {
|
|
49
|
+
const s = nock(DS, { reqheaders: { token: 'dstoken' } });
|
|
50
|
+
s.post(DSP + '/schedules/9/offline').reply(status, ok(true));
|
|
51
|
+
s.post(DSP + '/process-definition/777/release').query({ releaseState: 'OFFLINE' }).reply(status, ok(true));
|
|
52
|
+
s.delete(DSP + '/process-definition/777').reply(status, ok(true));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
describe('hasLive / liveIds', () => {
|
|
56
|
+
it('only counts ids of the selected backend', () => {
|
|
57
|
+
setEnv(SCHEDULER_ENV);
|
|
58
|
+
expect(service.hasLive(dsLive)).to.equal(false);
|
|
59
|
+
expect(service.hasLive(schedIds)).to.equal(true);
|
|
60
|
+
delete process.env.SCHEDULED_START_BACKEND;
|
|
61
|
+
expect(service.hasLive(dsLive)).to.equal(true);
|
|
62
|
+
expect(service.hasLive(schedIds)).to.equal(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('extracts the ids of either backend', () => {
|
|
66
|
+
expect(service.liveIds(Object.assign({ enabled: true }, dsLive))).to.deep.equal({ workflow_code: 777, schedule_id: 9 });
|
|
67
|
+
expect(service.liveIds(Object.assign({ enabled: true }, schedIds))).to.deep.equal(schedIds);
|
|
68
|
+
expect(service.liveIds(null)).to.deep.equal({});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('dolphin → scheduler handover', () => {
|
|
73
|
+
it('update creates the scheduler slot and destroys the DolphinScheduler workflow', async () => {
|
|
74
|
+
setEnv(SCHEDULER_ENV); setEnv(DOLPHIN_ENV);
|
|
75
|
+
upsert(); dsDestroy();
|
|
76
|
+
expect(await service.update(dsLive, { schedule: SCHEDULE }, 'bot1', 'proj1')).to.deep.equal(schedIds);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('a failing DolphinScheduler cleanup never fails the update', async () => {
|
|
80
|
+
setEnv(SCHEDULER_ENV); setEnv(DOLPHIN_ENV);
|
|
81
|
+
upsert();
|
|
82
|
+
nock(DS).post(DSP + '/schedules/9/offline').reply(500, { code: 1, msg: 'x' })
|
|
83
|
+
.post(DSP + '/process-definition/777/release').query(true).reply(500, { code: 1, msg: 'x' })
|
|
84
|
+
.delete(DSP + '/process-definition/777').reply(500, { code: 1, msg: 'x' });
|
|
85
|
+
// the DolphinScheduler ids are kept so the next call retries the cleanup
|
|
86
|
+
expect(await service.update(dsLive, { schedule: SCHEDULE }, 'bot1', 'proj1'))
|
|
87
|
+
.to.deep.equal(Object.assign({}, schedIds, { workflow_code: 777, schedule_id: 9 }));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('skips the DolphinScheduler cleanup when its env is gone', async () => {
|
|
91
|
+
setEnv(SCHEDULER_ENV);
|
|
92
|
+
upsert();
|
|
93
|
+
expect(await service.update(dsLive, { schedule: SCHEDULE }, 'bot1', 'proj1')).to.deep.equal(schedIds);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('destroyFor with DolphinScheduler ids also cleans the workflow', async () => {
|
|
97
|
+
setEnv(SCHEDULER_ENV); setEnv(DOLPHIN_ENV);
|
|
98
|
+
nock(SCHED).get('/jobs/auto-start-bot1').reply(404, { error: 'job not found' });
|
|
99
|
+
dsDestroy();
|
|
100
|
+
expect(await service.destroyFor('bot1', dsLive)).to.equal(true);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('scheduler → dolphin rollback', () => {
|
|
105
|
+
it('removes the scheduler slot so the bot is not started twice', async () => {
|
|
106
|
+
setEnv(SCHEDULER_ENV); delete process.env.SCHEDULED_START_BACKEND; setEnv(DOLPHIN_ENV);
|
|
107
|
+
const s = nock(DS, { reqheaders: { token: 'dstoken' } });
|
|
108
|
+
s.get(DSP + '/process-definition').query(true).reply(200, ok({ totalList: [] }));
|
|
109
|
+
s.post(DSP + '/process-definition').query(true).reply(200, ok({ code: 111 }));
|
|
110
|
+
s.post(DSP + '/process-definition/111/release').query({ releaseState: 'ONLINE' }).reply(200, ok(true));
|
|
111
|
+
s.post(DSP + '/schedules').query(true).reply(200, ok({ id: 5 }));
|
|
112
|
+
s.post(DSP + '/schedules/5/online').reply(200, ok(true));
|
|
113
|
+
nock(SCHED).delete('/jobs/auto-start-bot1').query({ projectId: 'proj1' }).reply(204);
|
|
114
|
+
const live = Object.assign({ schedule: SCHEDULE }, schedIds);
|
|
115
|
+
expect(await service.update(live, { schedule: SCHEDULE }, 'bot1', 'proj1')).to.deep.equal({ workflow_code: 111, schedule_id: 5 });
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('publish listener', () => {
|
|
120
|
+
async function setupLive(scheduled_live) {
|
|
121
|
+
const ctx = await setupProjectWithBot(server);
|
|
122
|
+
ctx.block_id = ctx.startIntent.intent_id;
|
|
123
|
+
ctx.published_id = 'pub' + Date.now() + Math.floor(Math.random() * 10000);
|
|
124
|
+
const res = await chai.request(server).put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled')
|
|
125
|
+
.auth(ctx.email, ctx.pwd).send({ block_id: ctx.block_id, enabled: true, schedule: SCHEDULE, mapping: { source_name: 'cron' } });
|
|
126
|
+
expect(res).to.have.status(200);
|
|
127
|
+
await Faq.create({ id_faq_kb: ctx.published_id, id_project: ctx.project._id.toString(), intent_id: ctx.block_id, question: 'q', answer: 'a', createdBy: ctx.user._id.toString() });
|
|
128
|
+
if (scheduled_live) await Webhook.updateOne({ chatbot_id: ctx.chatbot_id }, { $set: { scheduled_live } });
|
|
129
|
+
return ctx;
|
|
130
|
+
}
|
|
131
|
+
const stored = async (ctx) => (await Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean().exec()).scheduled_live;
|
|
132
|
+
|
|
133
|
+
it('replaces DolphinScheduler ids with the scheduler slot', async () => {
|
|
134
|
+
setEnv(SCHEDULER_ENV);
|
|
135
|
+
const ctx = await setupLive(dsLive);
|
|
136
|
+
const project = ctx.project._id.toString();
|
|
137
|
+
nock(SCHED).post('/jobs/cron', (b) => b.projectId === project && b.flowId === 'auto-start-' + ctx.chatbot_id)
|
|
138
|
+
.reply(201, { job: { _id: 'J9' }, created: true });
|
|
139
|
+
await publishListener.sync(project, ctx.chatbot_id, ctx.published_id);
|
|
140
|
+
const live = await stored(ctx);
|
|
141
|
+
expect(live).to.include({ backend: 'scheduler', job_id: 'J9', id_project: project, chatbot_id: ctx.chatbot_id, enabled: true });
|
|
142
|
+
expect(live).to.not.have.property('workflow_code');
|
|
143
|
+
expect(live).to.not.have.property('schedule_id');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('migrate moves the live snapshot, not the draft, to the scheduler', async () => {
|
|
147
|
+
setEnv(SCHEDULER_ENV); setEnv(DOLPHIN_ENV);
|
|
148
|
+
const live = Object.assign({}, dsLive, { block_id: 'b1', mapping: { source_name: 'cron', payload: {} }, published_bot_id: 'p1' });
|
|
149
|
+
const ctx = await setupLive(live);
|
|
150
|
+
// unpublished draft edit: must not go live through the migration
|
|
151
|
+
await chai.request(server).put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled').auth(ctx.email, ctx.pwd)
|
|
152
|
+
.send({ block_id: ctx.block_id, enabled: true, schedule: { frequency: 'daily', time: '18:45', timezone: 'Asia/Tokyo' }, mapping: { source_name: 'cron' } });
|
|
153
|
+
const project = ctx.project._id.toString();
|
|
154
|
+
let body;
|
|
155
|
+
nock(SCHED).post('/jobs/cron', (b) => { body = b; return true; }).reply(201, { job: { _id: 'J9' }, created: true });
|
|
156
|
+
nock(DS).post(DSP + '/schedules/9/offline').reply(200, ok(true))
|
|
157
|
+
.post(DSP + '/process-definition/777/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true))
|
|
158
|
+
.delete(DSP + '/process-definition/777').reply(200, ok(true));
|
|
159
|
+
const res = await chai.request(server).post('/' + project + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled/migrate').auth(ctx.email, ctx.pwd);
|
|
160
|
+
expect(res).to.have.status(200);
|
|
161
|
+
expect(body).to.include({ cron: '0 9 * * *', timezone: 'Europe/Rome' });
|
|
162
|
+
const stored = await Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean().exec();
|
|
163
|
+
expect(stored.scheduled_live).to.include({ backend: 'scheduler', job_id: 'J9', block_id: 'b1', published_bot_id: 'p1', enabled: true });
|
|
164
|
+
expect(stored.scheduled_live).to.not.have.property('workflow_code');
|
|
165
|
+
expect(stored.scheduled_live.schedule).to.deep.equal(SCHEDULE);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('migrate is a no-op without a live schedule', async () => {
|
|
169
|
+
setEnv(SCHEDULER_ENV);
|
|
170
|
+
const ctx = await setupLive(null);
|
|
171
|
+
const res = await chai.request(server).post('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/scheduled/migrate').auth(ctx.email, ctx.pwd);
|
|
172
|
+
expect(res).to.have.status(200);
|
|
173
|
+
expect(res.body.scheduled_live).to.equal(null);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('stores the error when the scheduler service is down', async () => {
|
|
177
|
+
setEnv(SCHEDULER_ENV);
|
|
178
|
+
const ctx = await setupLive(null);
|
|
179
|
+
nock(SCHED).post('/jobs/cron').reply(503, { error: 'scheduler queue unavailable' });
|
|
180
|
+
await publishListener.sync(ctx.project._id.toString(), ctx.chatbot_id, ctx.published_id);
|
|
181
|
+
expect((await stored(ctx)).error).to.match(/scheduler queue unavailable/);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
// Facade handover between backends; no database needed (both schedulers are mocked with nock).
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const nock = require('nock');
|
|
5
|
+
|
|
6
|
+
const SCHED = 'http://sched.test';
|
|
7
|
+
const DS = 'http://ds.test/dolphinscheduler';
|
|
8
|
+
const DSP = '/projects/dsproj';
|
|
9
|
+
const SCHEDULE = { frequency: 'daily', time: '09:00', timezone: 'Europe/Rome' };
|
|
10
|
+
const SCHEDULER_ENV = { SCHEDULED_START_BACKEND: 'scheduler', SCHEDULER_SERVICE_URL: SCHED, SCHEDULER_API_TOKEN: 'apitok', SCHEDULED_START_TOKEN: 'start' };
|
|
11
|
+
const DOLPHIN_ENV = { SCHEDULER_BASEURL: DS, SCHEDULER_TOKEN: 'dstoken', SCHEDULER_PROJECT: 'dsproj' };
|
|
12
|
+
const ALL = Object.keys(SCHEDULER_ENV).concat(Object.keys(DOLPHIN_ENV));
|
|
13
|
+
const ok = (data) => ({ code: 0, msg: 'success', data, success: true });
|
|
14
|
+
const fail = { code: 1, msg: 'internal error' };
|
|
15
|
+
|
|
16
|
+
const DS_IDS = { workflow_code: 777, schedule_id: 9 };
|
|
17
|
+
const SCHED_IDS = { backend: 'scheduler', job_id: 'J1', id_project: 'proj1', chatbot_id: 'bot1' };
|
|
18
|
+
const dsLive = Object.assign({ schedule: SCHEDULE, enabled: true }, DS_IDS);
|
|
19
|
+
|
|
20
|
+
describe('scheduledStart handover', function () {
|
|
21
|
+
let service;
|
|
22
|
+
let saved;
|
|
23
|
+
const setEnv = (vars) => Object.keys(vars).forEach((k) => { process.env[k] = vars[k]; });
|
|
24
|
+
|
|
25
|
+
before(() => { service = require('../services/scheduledStartService'); });
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
saved = {};
|
|
28
|
+
ALL.forEach((k) => { saved[k] = process.env[k]; delete process.env[k]; });
|
|
29
|
+
setEnv(SCHEDULER_ENV);
|
|
30
|
+
setEnv(DOLPHIN_ENV);
|
|
31
|
+
});
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
ALL.forEach((k) => { if (saved[k] === undefined) delete process.env[k]; else process.env[k] = saved[k]; });
|
|
34
|
+
const done = nock.isDone();
|
|
35
|
+
const pending = nock.pendingMocks();
|
|
36
|
+
nock.cleanAll();
|
|
37
|
+
assert.ok(done, 'pending mocks: ' + pending);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const upsert = () => nock(SCHED).post('/jobs/cron').reply(201, { job: { _id: 'J1' }, created: true });
|
|
41
|
+
const dsDestroy = (status) => nock(DS)
|
|
42
|
+
.post(DSP + '/schedules/9/offline').reply(status, status === 200 ? ok(true) : fail)
|
|
43
|
+
.post(DSP + '/process-definition/777/release').query({ releaseState: 'OFFLINE' }).reply(status, status === 200 ? ok(true) : fail)
|
|
44
|
+
.delete(DSP + '/process-definition/777').reply(status, status === 200 ? ok(true) : fail);
|
|
45
|
+
|
|
46
|
+
it('keeps the DolphinScheduler ids when their cleanup fails', async () => {
|
|
47
|
+
upsert(); dsDestroy(500);
|
|
48
|
+
const ids = await service.update(dsLive, { schedule: SCHEDULE }, 'bot1', 'proj1');
|
|
49
|
+
assert.deepStrictEqual(ids, Object.assign({}, SCHED_IDS, DS_IDS));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('drops the DolphinScheduler ids once their cleanup succeeds', async () => {
|
|
53
|
+
upsert(); dsDestroy(200);
|
|
54
|
+
assert.deepStrictEqual(await service.update(dsLive, { schedule: SCHEDULE }, 'bot1', 'proj1'), SCHED_IDS);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('retries a failed cleanup on the next update', async () => {
|
|
58
|
+
const both = Object.assign({ schedule: SCHEDULE, enabled: true }, SCHED_IDS, DS_IDS);
|
|
59
|
+
assert.strictEqual(service.hasLive(both), true);
|
|
60
|
+
assert.deepStrictEqual(service.liveIds(both), Object.assign({}, SCHED_IDS, DS_IDS));
|
|
61
|
+
upsert(); dsDestroy(200);
|
|
62
|
+
assert.deepStrictEqual(await service.update(both, { schedule: SCHEDULE }, 'bot1', 'proj1'), SCHED_IDS);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('deactivate reports the ids to keep', async () => {
|
|
66
|
+
const both = Object.assign({ schedule: SCHEDULE, enabled: true }, SCHED_IDS, DS_IDS);
|
|
67
|
+
nock(SCHED).post('/jobs/auto-start-bot1/pause').query({ projectId: 'proj1' }).reply(200, { job: {} });
|
|
68
|
+
dsDestroy(500);
|
|
69
|
+
assert.deepStrictEqual(await service.deactivate(both), Object.assign({}, SCHED_IDS, DS_IDS));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('destroyFor is not done while the other backend still holds the schedule', async () => {
|
|
73
|
+
nock(SCHED).get('/jobs/auto-start-bot1').reply(404, { error: 'job not found' });
|
|
74
|
+
dsDestroy(500);
|
|
75
|
+
assert.strictEqual(await service.destroyFor('bot1', dsLive), false);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('destroyFor without ids also removes the DolphinScheduler workflow found by name', async () => {
|
|
79
|
+
nock(SCHED).get('/jobs/auto-start-bot1').reply(404, { error: 'job not found' });
|
|
80
|
+
nock(DS)
|
|
81
|
+
.get(DSP + '/process-definition').query((q) => q.searchVal === 'auto-start-bot1').reply(200, ok({ totalList: [{ code: 777, name: 'auto-start-bot1' }] }))
|
|
82
|
+
.get(DSP + '/schedules').query(true).reply(200, ok({ totalList: [{ id: 9 }] }));
|
|
83
|
+
dsDestroy(200);
|
|
84
|
+
assert.strictEqual(await service.destroyFor('bot1', null), true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('offlineFor without ids also takes the DolphinScheduler schedule offline', async () => {
|
|
88
|
+
nock(SCHED).get('/jobs/auto-start-bot1').reply(404, { error: 'job not found' });
|
|
89
|
+
nock(DS)
|
|
90
|
+
.get(DSP + '/process-definition').query((q) => q.searchVal === 'auto-start-bot1').reply(200, ok({ totalList: [{ code: 777, name: 'auto-start-bot1' }] }))
|
|
91
|
+
.get(DSP + '/schedules').query(true).reply(200, ok({ totalList: [{ id: 9 }] }))
|
|
92
|
+
.post(DSP + '/schedules/9/offline').reply(200, ok(true));
|
|
93
|
+
assert.strictEqual(await service.offlineFor('bot1', null), true);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
process.env.LOG_LEVEL = 'error';
|
|
3
|
+
|
|
4
|
+
const chai = require('chai');
|
|
5
|
+
const chaiHttp = require('chai-http');
|
|
6
|
+
const server = require('../app');
|
|
7
|
+
const expect = chai.expect;
|
|
8
|
+
chai.use(chaiHttp);
|
|
9
|
+
|
|
10
|
+
const Faq = require('../models/faq');
|
|
11
|
+
const Faq_kb = require('../models/faq_kb');
|
|
12
|
+
const { Webhook } = require('../models/webhook');
|
|
13
|
+
const scheduledStartService = require('../services/scheduledStartService');
|
|
14
|
+
const publishListener = require('../services/scheduledStartPublishListener');
|
|
15
|
+
const botEvent = require('../event/botEvent');
|
|
16
|
+
const { setupProjectWithBot, waitFor } = require('./webhookConversationTestUtils');
|
|
17
|
+
|
|
18
|
+
const ENV = ['SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT', 'SCHEDULED_START_TOKEN'];
|
|
19
|
+
const SCHEDULE = { frequency: 'daily', time: '09:00', timezone: 'Europe/Rome' };
|
|
20
|
+
|
|
21
|
+
describe('ScheduledStartPublish', function () {
|
|
22
|
+
this.timeout(60000);
|
|
23
|
+
|
|
24
|
+
let saved;
|
|
25
|
+
let original;
|
|
26
|
+
let calls;
|
|
27
|
+
let behaviour;
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
saved = {};
|
|
31
|
+
ENV.forEach((k) => { saved[k] = process.env[k]; process.env[k] = k === 'SCHEDULER_BASEURL' ? 'http://scheduler.test' : 'x'; });
|
|
32
|
+
original = {};
|
|
33
|
+
calls = [];
|
|
34
|
+
behaviour = {};
|
|
35
|
+
['activate', 'update', 'deactivate', 'destroy', 'nextRuns', 'destroyFor', 'offlineFor'].forEach((m) => {
|
|
36
|
+
original[m] = scheduledStartService[m];
|
|
37
|
+
scheduledStartService[m] = async function () {
|
|
38
|
+
calls.push({ m, args: Array.from(arguments) });
|
|
39
|
+
if (m === 'destroyFor' || m === 'offlineFor') return !behaviour.throw;
|
|
40
|
+
if (behaviour.throw) throw new Error('scheduler down');
|
|
41
|
+
if (m === 'activate' || m === 'update') return { workflow_code: 11, schedule_id: 22 };
|
|
42
|
+
if (m === 'nextRuns') return behaviour.nextRunsNull ? null : ['2030-01-01 09:00:00'];
|
|
43
|
+
return undefined;
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
ENV.forEach((k) => { if (saved[k] === undefined) delete process.env[k]; else process.env[k] = saved[k]; });
|
|
49
|
+
Object.keys(original).forEach((m) => { scheduledStartService[m] = original[m]; });
|
|
50
|
+
server.set('chatbot_service', new (require('../services/chatbotService').ChatbotService)());
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const names = () => calls.map((c) => c.m);
|
|
54
|
+
const url = (ctx, rest) => '/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + (rest || '');
|
|
55
|
+
const auth = (ctx, r) => r.auth(ctx.email, ctx.pwd);
|
|
56
|
+
const webhook = (ctx) => Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean().exec();
|
|
57
|
+
|
|
58
|
+
async function setup({ withDraft = true, enabled = true, withBox = true } = {}) {
|
|
59
|
+
const ctx = await setupProjectWithBot(server);
|
|
60
|
+
ctx.block_id = ctx.startIntent.intent_id;
|
|
61
|
+
ctx.published_id = 'pub' + Date.now() + Math.floor(Math.random() * 10000);
|
|
62
|
+
if (withDraft) {
|
|
63
|
+
const res = await auth(ctx, chai.request(server).put(url(ctx, '/start_points/scheduled')))
|
|
64
|
+
.send({ block_id: ctx.block_id, enabled, schedule: SCHEDULE, mapping: { source_name: 'cron' } });
|
|
65
|
+
expect(res).to.have.status(200);
|
|
66
|
+
}
|
|
67
|
+
if (withBox) {
|
|
68
|
+
await Faq.create({ id_faq_kb: ctx.published_id, id_project: ctx.project._id.toString(), intent_id: ctx.block_id, question: 'q', answer: 'a', createdBy: ctx.user._id.toString() });
|
|
69
|
+
}
|
|
70
|
+
return ctx;
|
|
71
|
+
}
|
|
72
|
+
const sync = (ctx) => publishListener.sync(ctx.project._id.toString(), ctx.chatbot_id, ctx.published_id);
|
|
73
|
+
|
|
74
|
+
describe('sync', () => {
|
|
75
|
+
it('activates the first time and stores the live snapshot', async () => {
|
|
76
|
+
const ctx = await setup();
|
|
77
|
+
const live = await sync(ctx);
|
|
78
|
+
expect(names()).to.deep.equal(['activate']);
|
|
79
|
+
expect(calls[0].args[0]).to.equal(ctx.chatbot_id);
|
|
80
|
+
expect(calls[0].args[1].schedule).to.deep.include(SCHEDULE);
|
|
81
|
+
const stored = (await webhook(ctx)).scheduled_live;
|
|
82
|
+
expect(stored).to.include({ enabled: true, block_id: ctx.block_id, published_bot_id: ctx.published_id, workflow_code: 11, schedule_id: 22 });
|
|
83
|
+
expect(stored.published_at).to.exist;
|
|
84
|
+
expect(stored.mapping.source_name).to.equal('cron');
|
|
85
|
+
expect(stored.mapping.payload).to.deep.equal({});
|
|
86
|
+
expect(live.workflow_code).to.equal(11);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('two syncs unchanged: second is an update, never a second activate', async () => {
|
|
90
|
+
const ctx = await setup();
|
|
91
|
+
await sync(ctx);
|
|
92
|
+
await sync(ctx);
|
|
93
|
+
expect(names()).to.deep.equal(['activate', 'update']);
|
|
94
|
+
expect(calls[1].args[0]).to.include({ workflow_code: 11, schedule_id: 22 });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('draft disabled -> deactivate and live.enabled false', async () => {
|
|
98
|
+
const ctx = await setup();
|
|
99
|
+
await sync(ctx);
|
|
100
|
+
calls.length = 0;
|
|
101
|
+
await auth(ctx, chai.request(server).put(url(ctx, '/start_points/scheduled'))).send({ block_id: ctx.block_id, enabled: false });
|
|
102
|
+
await sync(ctx);
|
|
103
|
+
expect(names()).to.deep.equal(['deactivate']);
|
|
104
|
+
const stored = (await webhook(ctx)).scheduled_live;
|
|
105
|
+
expect(stored.enabled).to.equal(false);
|
|
106
|
+
expect(stored.schedule_id).to.equal(22);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('box missing from the published copy -> destroy and live unset', async () => {
|
|
110
|
+
const ctx = await setup();
|
|
111
|
+
await sync(ctx);
|
|
112
|
+
calls.length = 0;
|
|
113
|
+
const live = await publishListener.sync(ctx.project._id.toString(), ctx.chatbot_id, 'otherpublishedcopy');
|
|
114
|
+
expect(names()).to.deep.equal(['destroy']);
|
|
115
|
+
expect(live).to.equal(null);
|
|
116
|
+
expect((await webhook(ctx)).scheduled_live).to.equal(undefined);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('draft removed via the faq delete then sync -> destroy with the old ids', async () => {
|
|
120
|
+
const ctx = await setup();
|
|
121
|
+
await sync(ctx);
|
|
122
|
+
calls.length = 0;
|
|
123
|
+
const del = await auth(ctx, chai.request(server).delete('/' + ctx.project._id + '/faq/' + ctx.startIntent._id));
|
|
124
|
+
expect(del).to.have.status(200);
|
|
125
|
+
await waitFor(async () => ((await webhook(ctx)).start_points || []).length === 0);
|
|
126
|
+
expect((await webhook(ctx)).scheduled_live).to.exist;
|
|
127
|
+
await sync(ctx);
|
|
128
|
+
expect(names()).to.deep.equal(['destroy']);
|
|
129
|
+
expect(calls[0].args[0]).to.include({ workflow_code: 11, schedule_id: 22 });
|
|
130
|
+
expect((await webhook(ctx)).scheduled_live).to.equal(undefined);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('no draft and no live -> no service call', async () => {
|
|
134
|
+
const ctx = await setup({ withDraft: false });
|
|
135
|
+
expect(await sync(ctx)).to.equal(null);
|
|
136
|
+
const ctx2 = await setup();
|
|
137
|
+
await Webhook.updateOne({ chatbot_id: ctx2.chatbot_id }, { $set: { start_points: [] } });
|
|
138
|
+
await sync(ctx2);
|
|
139
|
+
expect(calls).to.have.length(0);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('scheduler failure is stored in scheduled_live.error and sync resolves', async () => {
|
|
143
|
+
const ctx = await setup();
|
|
144
|
+
behaviour.throw = true;
|
|
145
|
+
const live = await sync(ctx);
|
|
146
|
+
expect(live.error).to.equal('scheduler down');
|
|
147
|
+
expect((await webhook(ctx)).scheduled_live.error).to.equal('scheduler down');
|
|
148
|
+
behaviour.throw = false;
|
|
149
|
+
await sync(ctx);
|
|
150
|
+
const stored = (await webhook(ctx)).scheduled_live;
|
|
151
|
+
expect(stored.error).to.equal(undefined);
|
|
152
|
+
expect(stored.workflow_code).to.equal(11);
|
|
153
|
+
expect(names()).to.deep.equal(['activate', 'activate']);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('events', () => {
|
|
158
|
+
it('publish of a chatbot without scheduled start point makes no scheduler call', async () => {
|
|
159
|
+
const ctx = await setupProjectWithBot(server);
|
|
160
|
+
await publishListener.onPublish({ chatbot: { _id: ctx.chatbot_id, id_project: ctx.project._id.toString() }, publishedBotId: 'x' });
|
|
161
|
+
expect(calls).to.have.length(0);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('publish route answers 200 and goes live even if the scheduler throws', async () => {
|
|
165
|
+
const ctx = await setup({ withBox: false });
|
|
166
|
+
behaviour.throw = true;
|
|
167
|
+
const { ChatbotService } = require('../services/chatbotService');
|
|
168
|
+
server.set('chatbot_service', new (class extends ChatbotService {
|
|
169
|
+
async fork(id_faq_kb) {
|
|
170
|
+
const exported = await auth(ctx, chai.request(server).get('/' + ctx.project._id + '/faq_kb/exportjson/' + id_faq_kb));
|
|
171
|
+
const created = await auth(ctx, chai.request(server).post('/' + ctx.project._id + '/faq_kb'))
|
|
172
|
+
.send({ name: 'published', type: 'tilebot', language: 'en', template: 'blank' });
|
|
173
|
+
const forked_id = created.body._id;
|
|
174
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
175
|
+
await auth(ctx, chai.request(server).post('/' + ctx.project._id + '/faq_kb/importjson/' + forked_id + '?replace=true')).send(JSON.parse(exported.text));
|
|
176
|
+
return { bot_id: forked_id };
|
|
177
|
+
}
|
|
178
|
+
async setModified() { return true; }
|
|
179
|
+
})());
|
|
180
|
+
const res = await auth(ctx, chai.request(server).put('/' + ctx.project._id + '/faq_kb/' + ctx.chatbot_id + '/publish'));
|
|
181
|
+
expect(res).to.have.status(200);
|
|
182
|
+
await waitFor(async () => ((await webhook(ctx)).scheduled_live || {}).error);
|
|
183
|
+
expect((await webhook(ctx)).scheduled_live.error).to.equal('scheduler down');
|
|
184
|
+
|
|
185
|
+
behaviour.throw = false;
|
|
186
|
+
const retry = await auth(ctx, chai.request(server).post(url(ctx, '/start_points/scheduled/sync')));
|
|
187
|
+
expect(retry).to.have.status(200);
|
|
188
|
+
expect(retry.body.scheduled_live.workflow_code).to.equal(11);
|
|
189
|
+
expect(retry.body.scheduled_live.published_bot_id).to.equal(res.body.bot_id);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('faqbot.delete destroys the live schedule', async () => {
|
|
193
|
+
const ctx = await setup();
|
|
194
|
+
await sync(ctx);
|
|
195
|
+
calls.length = 0;
|
|
196
|
+
botEvent.emit('faqbot.delete', { _id: ctx.chatbot_id });
|
|
197
|
+
await waitFor(() => calls.length > 0);
|
|
198
|
+
expect(names()).to.deep.equal(['destroyFor']);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe('delete events', () => {
|
|
203
|
+
it('trashing the chatbot (PUT trashed:true -> faqbot.deleted) cleans the scheduler even though the webhook record is already gone', async () => {
|
|
204
|
+
const ctx = await setup();
|
|
205
|
+
await sync(ctx);
|
|
206
|
+
calls.length = 0;
|
|
207
|
+
const res = await auth(ctx, chai.request(server).put('/' + ctx.project._id + '/faq_kb/' + ctx.chatbot_id)).send({ trashed: true });
|
|
208
|
+
expect(res).to.have.status(200);
|
|
209
|
+
await waitFor(() => calls.length > 0);
|
|
210
|
+
expect(names()).to.deep.equal(['destroyFor']);
|
|
211
|
+
expect(calls[0].args[0]).to.equal(ctx.chatbot_id);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('faqbot.deleted destroys the schedule and unsets scheduled_live', async () => {
|
|
215
|
+
const ctx = await setup();
|
|
216
|
+
await sync(ctx);
|
|
217
|
+
calls.length = 0;
|
|
218
|
+
await publishListener.onDeleted({ chatbot: { _id: ctx.chatbot_id, id_project: ctx.project._id.toString() } });
|
|
219
|
+
expect(names()).to.deep.equal(['destroyFor']);
|
|
220
|
+
expect(calls[0].args[1].workflow_code).to.equal(11);
|
|
221
|
+
expect((await webhook(ctx)).scheduled_live).to.equal(undefined);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('is idempotent when both faqbot.delete and faqbot.deleted fire', async () => {
|
|
225
|
+
const ctx = await setup();
|
|
226
|
+
await sync(ctx);
|
|
227
|
+
calls.length = 0;
|
|
228
|
+
const chatbot = { _id: ctx.chatbot_id, id_project: ctx.project._id.toString() };
|
|
229
|
+
await publishListener.onDelete(chatbot);
|
|
230
|
+
await publishListener.onDeleted({ chatbot });
|
|
231
|
+
expect(names()).to.deep.equal(['destroyFor']);
|
|
232
|
+
expect((await webhook(ctx)).scheduled_live).to.equal(undefined);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('ignores published copies (root_id set)', async () => {
|
|
236
|
+
const ctx = await setup();
|
|
237
|
+
await sync(ctx);
|
|
238
|
+
calls.length = 0;
|
|
239
|
+
await publishListener.onDeleted({ chatbot: { _id: ctx.chatbot_id, id_project: ctx.project._id.toString(), root_id: 'other' } });
|
|
240
|
+
expect(calls).to.have.length(0);
|
|
241
|
+
expect((await webhook(ctx)).scheduled_live).to.not.equal(undefined);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('keeps scheduled_live when the scheduler cleanup fails', async () => {
|
|
245
|
+
const ctx = await setup();
|
|
246
|
+
await sync(ctx);
|
|
247
|
+
behaviour.throw = true;
|
|
248
|
+
await publishListener.onDeleted({ chatbot: { _id: ctx.chatbot_id, id_project: ctx.project._id.toString() } });
|
|
249
|
+
expect((await webhook(ctx)).scheduled_live).to.not.equal(undefined);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('routes', () => {
|
|
254
|
+
it('DELETE webhook destroys the schedule before deleting', async () => {
|
|
255
|
+
const ctx = await setup();
|
|
256
|
+
await sync(ctx);
|
|
257
|
+
calls.length = 0;
|
|
258
|
+
const res = await auth(ctx, chai.request(server).delete(url(ctx)));
|
|
259
|
+
expect(res).to.have.status(200);
|
|
260
|
+
expect(names()).to.deep.equal(['destroyFor']);
|
|
261
|
+
expect(await webhook(ctx)).to.equal(null);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('DELETE delete/:webhook_id destroys the schedule before deleting', async () => {
|
|
265
|
+
const ctx = await setup();
|
|
266
|
+
await sync(ctx);
|
|
267
|
+
const wh = await webhook(ctx);
|
|
268
|
+
calls.length = 0;
|
|
269
|
+
const res = await auth(ctx, chai.request(server).delete('/' + ctx.project._id + '/webhooks/delete/' + wh.webhook_id));
|
|
270
|
+
expect(res).to.have.status(200);
|
|
271
|
+
expect(names()).to.deep.equal(['destroyFor']);
|
|
272
|
+
expect(await webhook(ctx)).to.equal(null);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('DELETE answers 502 and keeps the webhook when the scheduler cleanup fails', async () => {
|
|
276
|
+
const ctx = await setup();
|
|
277
|
+
await sync(ctx);
|
|
278
|
+
behaviour.throw = true;
|
|
279
|
+
const res = await auth(ctx, chai.request(server).delete(url(ctx)));
|
|
280
|
+
expect(res).to.have.status(502);
|
|
281
|
+
expect(await webhook(ctx)).to.not.equal(null);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('GET omits next_runs when the preview fails', async () => {
|
|
285
|
+
const ctx = await setup();
|
|
286
|
+
await sync(ctx);
|
|
287
|
+
behaviour.nextRunsNull = true;
|
|
288
|
+
const res = await auth(ctx, chai.request(server).get(url(ctx)));
|
|
289
|
+
expect(res).to.have.status(200);
|
|
290
|
+
expect(res.body).to.not.have.property('next_runs');
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('sync route is 503 when not configured and requires admin', async () => {
|
|
294
|
+
const ctx = await setup();
|
|
295
|
+
const noAuth = await chai.request(server).post(url(ctx, '/start_points/scheduled/sync'));
|
|
296
|
+
expect(noAuth.status).to.be.oneOf([401, 403]);
|
|
297
|
+
delete process.env.SCHEDULED_START_TOKEN;
|
|
298
|
+
const res = await auth(ctx, chai.request(server).post(url(ctx, '/start_points/scheduled/sync')));
|
|
299
|
+
expect(res).to.have.status(503);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('sync route returns scheduled_live using the newest published copy', async () => {
|
|
303
|
+
const ctx = await setup({ withBox: false });
|
|
304
|
+
const copy = await Faq_kb.create({ name: 'copy', id_project: ctx.project._id.toString(), root_id: ctx.chatbot_id, trashed: true, publishedAt: Date.now(), createdBy: ctx.user._id.toString(), type: 'tilebot' });
|
|
305
|
+
await Faq.create({ id_faq_kb: copy._id.toString(), id_project: ctx.project._id.toString(), intent_id: ctx.block_id, question: 'q', answer: 'a', createdBy: ctx.user._id.toString() });
|
|
306
|
+
const res = await auth(ctx, chai.request(server).post(url(ctx, '/start_points/scheduled/sync')));
|
|
307
|
+
expect(res).to.have.status(200);
|
|
308
|
+
expect(res.body.scheduled_live.published_bot_id).to.equal(copy._id.toString());
|
|
309
|
+
expect(names()).to.deep.equal(['activate']);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('GET returns next_runs from nextRuns', async () => {
|
|
313
|
+
const ctx = await setup();
|
|
314
|
+
await sync(ctx);
|
|
315
|
+
const res = await auth(ctx, chai.request(server).get(url(ctx)));
|
|
316
|
+
expect(res).to.have.status(200);
|
|
317
|
+
expect(res.body.next_runs).to.deep.equal(['2030-01-01 09:00:00']);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|