@tiledesk/tiledesk-server 3.0.0 → 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 +37 -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 +14 -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/agentPrompt.js +104 -0
- 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/agentPrompts.js +132 -0
- 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 +154 -0
- 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,116 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const nock = require('nock');
|
|
4
|
+
|
|
5
|
+
const BASE = 'http://sched.test';
|
|
6
|
+
const ENV = { SCHEDULER_SERVICE_URL: BASE, SCHEDULER_API_TOKEN: 'apitok', SCHEDULED_START_TOKEN: 'secret-start' };
|
|
7
|
+
const saved = {};
|
|
8
|
+
|
|
9
|
+
const schedule = { frequency: 'daily', time: '09:30', timezone: 'Europe/Rome' };
|
|
10
|
+
const IDS = { backend: 'scheduler', job_id: 'J1', id_project: 'proj1', chatbot_id: 'bot1' };
|
|
11
|
+
const live = Object.assign({ schedule }, IDS);
|
|
12
|
+
|
|
13
|
+
describe('scheduledStart scheduler backend', function () {
|
|
14
|
+
let backend;
|
|
15
|
+
before(() => {
|
|
16
|
+
Object.keys(ENV).forEach((k) => { saved[k] = process.env[k]; process.env[k] = ENV[k]; });
|
|
17
|
+
backend = require('../services/scheduledStart/schedulerBackend');
|
|
18
|
+
});
|
|
19
|
+
after(() => {
|
|
20
|
+
Object.keys(ENV).forEach((k) => { if (saved[k] === undefined) delete process.env[k]; else process.env[k] = saved[k]; });
|
|
21
|
+
});
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
const done = nock.isDone();
|
|
24
|
+
const pending = nock.pendingMocks();
|
|
25
|
+
nock.cleanAll();
|
|
26
|
+
assert.ok(done, 'pending mocks: ' + pending);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const api = () => nock(BASE, { reqheaders: { authorization: 'Bearer apitok' } });
|
|
30
|
+
|
|
31
|
+
it('activate upserts the auto-start slot with a 5-field cron and the schedule timezone', async () => {
|
|
32
|
+
let body;
|
|
33
|
+
api().post('/jobs/cron', (b) => { body = b; return true; })
|
|
34
|
+
.reply(201, { job: { _id: 'J1' }, created: true });
|
|
35
|
+
const ids = await backend.activate('bot1', { schedule }, 'proj1');
|
|
36
|
+
assert.deepStrictEqual(body, {
|
|
37
|
+
projectId: 'proj1',
|
|
38
|
+
flowId: 'auto-start-bot1',
|
|
39
|
+
target: 'scheduledStart',
|
|
40
|
+
payload: { id_project: 'proj1', chatbot_id: 'bot1' },
|
|
41
|
+
cron: '30 9 * * *',
|
|
42
|
+
timezone: 'Europe/Rome',
|
|
43
|
+
metadata: { source: 'scheduled-start' }
|
|
44
|
+
});
|
|
45
|
+
assert.deepStrictEqual(ids, IDS);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('never sends the start token to the scheduler', async () => {
|
|
49
|
+
let raw;
|
|
50
|
+
api().post('/jobs/cron', (b) => { raw = JSON.stringify(b); return true; }).reply(201, { job: { _id: 'J1' }, created: true });
|
|
51
|
+
await backend.activate('bot1', { schedule }, 'proj1');
|
|
52
|
+
assert.ok(!raw.includes('secret-start'));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('activate surfaces scheduler errors as 502', async () => {
|
|
56
|
+
api().post('/jobs/cron').reply(500, { error: 'boom' });
|
|
57
|
+
await assert.rejects(backend.activate('bot1', { schedule }, 'proj1'), (err) => err.status === 502);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('update re-upserts the slot', async () => {
|
|
61
|
+
api().post('/jobs/cron').reply(200, { job: { _id: 'J1' }, created: false });
|
|
62
|
+
assert.deepStrictEqual(await backend.update(live, { schedule }, 'bot1', 'proj1'), IDS);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('deactivate pauses the slot by flowId and project', async () => {
|
|
66
|
+
api().post('/jobs/auto-start-bot1/pause').query({ projectId: 'proj1' }).reply(200, { job: {} });
|
|
67
|
+
await backend.deactivate(live);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('destroy deletes the slot', async () => {
|
|
71
|
+
api().delete('/jobs/auto-start-bot1').query({ projectId: 'proj1' }).reply(204);
|
|
72
|
+
await backend.destroy(live);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('destroyFor resolves true when the slot is already gone', async () => {
|
|
76
|
+
api().delete('/jobs/auto-start-bot1').query({ projectId: 'proj1' }).reply(404, { error: 'job not found' });
|
|
77
|
+
assert.strictEqual(await backend.destroyFor('bot1', live), true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('destroyFor resolves false when the scheduler fails', async () => {
|
|
81
|
+
api().delete('/jobs/auto-start-bot1').query({ projectId: 'proj1' }).reply(503, { error: 'down' });
|
|
82
|
+
assert.strictEqual(await backend.destroyFor('bot1', live), false);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('destroyFor without live ids finds the project through the scheduler', async () => {
|
|
86
|
+
api().get('/jobs/auto-start-bot1').reply(200, { job: { _id: 'J1', projectId: 'proj1' } });
|
|
87
|
+
api().delete('/jobs/auto-start-bot1').query({ projectId: 'proj1' }).reply(204);
|
|
88
|
+
assert.strictEqual(await backend.destroyFor('bot1', null), true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('destroyFor without live ids and no slot resolves true', async () => {
|
|
92
|
+
api().get('/jobs/auto-start-bot1').reply(404, { error: 'job not found' });
|
|
93
|
+
assert.strictEqual(await backend.destroyFor('bot1', null), true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('offlineFor pauses and resolves true', async () => {
|
|
97
|
+
api().post('/jobs/auto-start-bot1/pause').query({ projectId: 'proj1' }).reply(200, { job: {} });
|
|
98
|
+
assert.strictEqual(await backend.offlineFor('bot1', live), true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('nextRuns returns 3 wall-clock times in the schedule timezone', async () => {
|
|
102
|
+
let body;
|
|
103
|
+
api().post('/schedules/preview', (b) => { body = b; return true; })
|
|
104
|
+
.reply(200, { runs: ['2026-10-14T07:30:00.000Z', '2026-10-15T07:30:00.000Z', '2026-10-16T07:30:00.000Z'] });
|
|
105
|
+
const runs = await backend.nextRuns(live);
|
|
106
|
+
assert.deepStrictEqual(body, { cron: '30 9 * * *', timezone: 'Europe/Rome', count: 3 });
|
|
107
|
+
assert.deepStrictEqual(runs, ['2026-10-14 09:30:00', '2026-10-15 09:30:00', '2026-10-16 09:30:00']);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('nextRuns gives up after a short timeout (null)', async () => {
|
|
111
|
+
api().post('/schedules/preview').delay(4000).reply(200, { runs: [] });
|
|
112
|
+
const t0 = Date.now();
|
|
113
|
+
assert.strictEqual(await backend.nextRuns(live), null);
|
|
114
|
+
assert.ok(Date.now() - t0 < 3900, 'took ' + (Date.now() - t0));
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const nock = require('nock');
|
|
4
|
+
const moment = require('moment-timezone');
|
|
5
|
+
|
|
6
|
+
const BASE = 'http://scheduler.test/dolphinscheduler';
|
|
7
|
+
const P = '/projects/proj1';
|
|
8
|
+
const ENV_KEYS = ['SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT', 'SCHEDULED_START_TOKEN', 'API_URL'];
|
|
9
|
+
const saved = {};
|
|
10
|
+
|
|
11
|
+
const schedule = { frequency: 'daily', time: '09:30', timezone: 'Europe/Rome' };
|
|
12
|
+
const CRON = '0 30 9 * * ? *';
|
|
13
|
+
const ok = (data) => ({ code: 0, msg: 'success', data, success: true });
|
|
14
|
+
|
|
15
|
+
describe('scheduledStartService', function () {
|
|
16
|
+
let service;
|
|
17
|
+
before(() => {
|
|
18
|
+
ENV_KEYS.forEach((k) => { saved[k] = process.env[k]; });
|
|
19
|
+
process.env.SCHEDULER_BASEURL = BASE;
|
|
20
|
+
process.env.SCHEDULER_TOKEN = 'dstoken';
|
|
21
|
+
process.env.SCHEDULER_PROJECT = 'proj1';
|
|
22
|
+
process.env.SCHEDULED_START_TOKEN = 'secret-start';
|
|
23
|
+
process.env.API_URL = 'http://api.test/';
|
|
24
|
+
service = require('../services/scheduledStartService');
|
|
25
|
+
});
|
|
26
|
+
after(() => {
|
|
27
|
+
ENV_KEYS.forEach((k) => { if (saved[k] === undefined) delete process.env[k]; else process.env[k] = saved[k]; });
|
|
28
|
+
});
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
const done = nock.isDone();
|
|
31
|
+
const pending = nock.pendingMocks();
|
|
32
|
+
nock.cleanAll();
|
|
33
|
+
assert.ok(done, 'pending mocks: ' + pending);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
function mockActivate(chatbotId, o = {}) {
|
|
37
|
+
const calls = [];
|
|
38
|
+
const s = nock(BASE, { reqheaders: { token: 'dstoken' } });
|
|
39
|
+
if (!o.existing) {
|
|
40
|
+
s.get(P + '/process-definition').query((q) => q.searchVal === 'auto-start-' + chatbotId)
|
|
41
|
+
.reply(200, ok({ totalList: [] }));
|
|
42
|
+
s.post(P + '/process-definition').query((q) => { calls.push(q); return true; })
|
|
43
|
+
.reply(200, ok({ code: 111, name: 'auto-start-' + chatbotId }));
|
|
44
|
+
} else {
|
|
45
|
+
s.get(P + '/process-definition').query((q) => q.searchVal === 'auto-start-' + chatbotId)
|
|
46
|
+
.reply(200, ok({ totalList: [{ code: 777, name: 'auto-start-' + chatbotId }, { code: 5, name: 'auto-start-' + chatbotId + 'x' }] }));
|
|
47
|
+
}
|
|
48
|
+
s.post(P + '/process-definition/' + (o.existing ? 777 : 111) + '/release').query({ releaseState: 'ONLINE' }).reply(200, ok(true));
|
|
49
|
+
if (o.existing) {
|
|
50
|
+
s.get(P + '/schedules').query((q) => String(q.processDefinitionCode) === '777')
|
|
51
|
+
.reply(200, ok({ totalList: o.existingSchedule ? [{ id: 333 }] : [] }));
|
|
52
|
+
}
|
|
53
|
+
if (o.existing && o.existingSchedule) {
|
|
54
|
+
s.post(P + '/schedules/333/offline').reply(200, ok(true));
|
|
55
|
+
s.put(P + '/schedules/333').query(true).reply(200, ok(true));
|
|
56
|
+
s.post(P + '/schedules/333/online').reply(200, ok(true));
|
|
57
|
+
return calls;
|
|
58
|
+
}
|
|
59
|
+
if (o.onlineFails) {
|
|
60
|
+
s.post(P + '/schedules').query(true).reply(200, ok({ id: 222 }));
|
|
61
|
+
s.post(P + '/schedules/222/online').reply(500, { code: 1, msg: 'boom' });
|
|
62
|
+
s.delete(P + '/schedules/222').reply(200, ok(true));
|
|
63
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true));
|
|
64
|
+
s.delete(P + '/process-definition/111').reply(200, ok(true));
|
|
65
|
+
return calls;
|
|
66
|
+
}
|
|
67
|
+
if (o.scheduleFails) {
|
|
68
|
+
s.post(P + '/schedules').query(true).reply(500, { code: 1, msg: 'boom' });
|
|
69
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true));
|
|
70
|
+
s.delete(P + '/process-definition/111').reply(200, ok(true));
|
|
71
|
+
} else {
|
|
72
|
+
s.post(P + '/schedules').query((q) => { calls.push(q); return true; }).reply(200, ok({ id: 222 }));
|
|
73
|
+
s.post(P + '/schedules/222/online').reply(200, ok(true));
|
|
74
|
+
}
|
|
75
|
+
return calls;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
it('activate creates definition, releases, creates schedule, schedules online', async () => {
|
|
79
|
+
const calls = mockActivate('bot1');
|
|
80
|
+
const res = await service.activate('bot1', { schedule });
|
|
81
|
+
assert.deepStrictEqual(res, { workflow_code: 111, schedule_id: 222 });
|
|
82
|
+
const def = calls[0];
|
|
83
|
+
assert.strictEqual(def.name, 'auto-start-bot1');
|
|
84
|
+
const task = JSON.parse(def.taskDefinitionJson)[0];
|
|
85
|
+
assert.strictEqual(task.taskParams.url, 'http://api.test/webhook/scheduled-start');
|
|
86
|
+
assert.strictEqual(task.taskType, 'HTTP');
|
|
87
|
+
const hp = task.taskParams.httpParams;
|
|
88
|
+
assert.ok(hp.find((p) => p.prop === 'id_project' && p.httpParametersType === 'BODY' && p.value === '${id_project}'));
|
|
89
|
+
assert.ok(hp.find((p) => p.prop === 'chatbot_id' && p.httpParametersType === 'BODY' && p.value === '${chatbot_id}'));
|
|
90
|
+
assert.ok(hp.find((p) => p.prop === 'x-auth-token' && p.httpParametersType === 'HEADERS' && p.value === '${token}'));
|
|
91
|
+
const gp = JSON.parse(def.globalParams);
|
|
92
|
+
assert.deepStrictEqual(gp.map((g) => g.prop).sort(), ['chatbot_id', 'id_project', 'token']);
|
|
93
|
+
assert.strictEqual(gp.find((g) => g.prop === 'token').value, 'secret-start');
|
|
94
|
+
assert.strictEqual(gp.find((g) => g.prop === 'chatbot_id').value, 'bot1');
|
|
95
|
+
const sch = JSON.parse(calls[1].schedule);
|
|
96
|
+
assert.strictEqual(sch.crontab, CRON);
|
|
97
|
+
assert.strictEqual(sch.timezoneId, 'Europe/Rome');
|
|
98
|
+
assert.ok(sch.endTime.startsWith('2099-12-31'));
|
|
99
|
+
assert.strictEqual(String(calls[1].processDefinitionCode), '111');
|
|
100
|
+
assert.strictEqual(calls[1].failureStrategy, 'CONTINUE');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('sends startTime as the current wall-clock time in the schedule timezone', async () => {
|
|
104
|
+
// DolphinScheduler reads startTime in timezoneId: a UTC "now" west of UTC starts the
|
|
105
|
+
// trigger hours late and skips the first run (seen live on DS 3.2.2).
|
|
106
|
+
const calls = mockActivate('bot1');
|
|
107
|
+
const before = moment.tz('America/New_York');
|
|
108
|
+
await service.activate('bot1', { schedule: { ...schedule, timezone: 'America/New_York' } });
|
|
109
|
+
const after = moment.tz('America/New_York');
|
|
110
|
+
const start = moment.tz(JSON.parse(calls[1].schedule).startTime, 'YYYY-MM-DD HH:mm:ss', 'America/New_York');
|
|
111
|
+
assert.ok(start.isValid());
|
|
112
|
+
assert.ok(!start.isBefore(before.clone().startOf('second')) && !start.isAfter(after),
|
|
113
|
+
'startTime ' + start.format() + ' is not now in America/New_York');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('activate rolls back the definition when schedule creation fails (502)', async () => {
|
|
117
|
+
mockActivate('bot1', { scheduleFails: true });
|
|
118
|
+
await assert.rejects(() => service.activate('bot1', { schedule }), (e) => e.status === 502);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('update with known ids: offline, update, online', async () => {
|
|
122
|
+
const s = nock(BASE);
|
|
123
|
+
s.post(P + '/schedules/222/offline').reply(200, ok(true));
|
|
124
|
+
let q;
|
|
125
|
+
s.put(P + '/schedules/222').query((x) => { q = x; return true; }).reply(200, ok(true));
|
|
126
|
+
s.post(P + '/schedules/222/online').reply(200, ok(true));
|
|
127
|
+
const res = await service.update({ workflow_code: 111, schedule_id: 222 },
|
|
128
|
+
{ schedule: { ...schedule, timezone: 'UTC' } });
|
|
129
|
+
assert.deepStrictEqual(res, { workflow_code: 111, schedule_id: 222 });
|
|
130
|
+
const sch = JSON.parse(q.schedule);
|
|
131
|
+
assert.strictEqual(sch.timezoneId, 'UTC');
|
|
132
|
+
assert.strictEqual(sch.crontab, CRON);
|
|
133
|
+
assert.strictEqual(String(q.processDefinitionCode), '111');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('update with ids DS reports as missing (200 + code) falls back to activate, reusing the definition', async () => {
|
|
137
|
+
nock(BASE).post(P + '/schedules/222/offline').reply(200, { code: 50003, msg: 'schedule 222 does not exist' });
|
|
138
|
+
nock(BASE).put(P + '/schedules/222').query(true).reply(200, { code: 50003, msg: 'schedule 222 does not exist' });
|
|
139
|
+
mockActivate('bot1', { existing: true });
|
|
140
|
+
const res = await service.update({ workflow_code: 111, schedule_id: 222 }, { schedule }, 'bot1');
|
|
141
|
+
assert.deepStrictEqual(res, { workflow_code: 777, schedule_id: 222 });
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('activate on a reused definition with an existing schedule updates it, never creates', async () => {
|
|
145
|
+
mockActivate('bot1', { existing: true, existingSchedule: true });
|
|
146
|
+
const res = await service.activate('bot1', { schedule });
|
|
147
|
+
assert.deepStrictEqual(res, { workflow_code: 777, schedule_id: 333 });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('activate on a reused definition without a schedule creates one', async () => {
|
|
151
|
+
mockActivate('bot1', { existing: true });
|
|
152
|
+
const res = await service.activate('bot1', { schedule });
|
|
153
|
+
assert.deepStrictEqual(res, { workflow_code: 777, schedule_id: 222 });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('activate compensates when schedule online fails (schedule + definition removed)', async () => {
|
|
157
|
+
mockActivate('bot1', { onlineFails: true });
|
|
158
|
+
await assert.rejects(() => service.activate('bot1', { schedule }), (e) => e.status === 502);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('activate deletes a freshly created definition when release ONLINE fails', async () => {
|
|
162
|
+
const s = nock(BASE);
|
|
163
|
+
s.get(P + '/process-definition').query(true).reply(200, ok({ totalList: [] }));
|
|
164
|
+
s.post(P + '/process-definition').query(true).reply(200, ok({ code: 111 }));
|
|
165
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'ONLINE' }).reply(500, { code: 1, msg: 'x' });
|
|
166
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true));
|
|
167
|
+
s.delete(P + '/process-definition/111').reply(200, ok(true));
|
|
168
|
+
await assert.rejects(() => service.activate('bot1', { schedule }), (e) => e.status === 502);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('update that fails (non not-found) puts the previous schedule back online and rethrows', async () => {
|
|
172
|
+
const s = nock(BASE);
|
|
173
|
+
s.post(P + '/schedules/222/offline').reply(200, ok(true));
|
|
174
|
+
s.put(P + '/schedules/222').query(true).reply(500, { code: 1, msg: 'db down' });
|
|
175
|
+
s.post(P + '/schedules/222/online').reply(200, ok(true));
|
|
176
|
+
await assert.rejects(() => service.update({ workflow_code: 111, schedule_id: 222 }, { schedule }, 'bot1'),
|
|
177
|
+
(e) => e.status === 502);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('two consecutive no-change updates never touch process-definition and keep ids', async () => {
|
|
181
|
+
const s = nock(BASE);
|
|
182
|
+
for (let i = 0; i < 2; i++) {
|
|
183
|
+
s.post(P + '/schedules/222/offline').reply(200, ok(true));
|
|
184
|
+
s.put(P + '/schedules/222').query(true).reply(200, ok(true));
|
|
185
|
+
s.post(P + '/schedules/222/online').reply(200, ok(true));
|
|
186
|
+
}
|
|
187
|
+
const live = { workflow_code: 111, schedule_id: 222 };
|
|
188
|
+
assert.deepStrictEqual(await service.update(live, { schedule }, 'bot1'), live);
|
|
189
|
+
assert.deepStrictEqual(await service.update(live, { schedule }, 'bot1'), live);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('destroy tolerates DS not-found reported as HTTP 200 with a non-zero code', async () => {
|
|
193
|
+
const s = nock(BASE);
|
|
194
|
+
s.post(P + '/schedules/222/offline').reply(200, { code: 1, msg: 'schedule does not exist' });
|
|
195
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'OFFLINE' }).reply(200, { code: 1, msg: 'process definition does not exist' });
|
|
196
|
+
s.delete(P + '/process-definition/111').reply(200, { code: 1, msg: 'process definition 111 does not exist' });
|
|
197
|
+
await service.destroy({ workflow_code: 111, schedule_id: 222 });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('update without ids activates', async () => {
|
|
201
|
+
mockActivate('bot1');
|
|
202
|
+
const res = await service.update({}, { schedule }, 'bot1');
|
|
203
|
+
assert.deepStrictEqual(res, { workflow_code: 111, schedule_id: 222 });
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('deactivate takes the schedule offline only', async () => {
|
|
207
|
+
nock(BASE).post(P + '/schedules/222/offline').reply(200, ok(true));
|
|
208
|
+
await service.deactivate({ workflow_code: 111, schedule_id: 222 });
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('destroy: schedule offline, definition offline, delete', async () => {
|
|
212
|
+
const s = nock(BASE);
|
|
213
|
+
s.post(P + '/schedules/222/offline').reply(200, ok(true));
|
|
214
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true));
|
|
215
|
+
s.delete(P + '/process-definition/111').reply(200, ok(true));
|
|
216
|
+
await service.destroy({ workflow_code: 111, schedule_id: 222 });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('destroy with unknown ids resolves without calls', async () => {
|
|
220
|
+
await service.destroy({});
|
|
221
|
+
await service.destroy(null);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('treats success:false on HTTP 200 as an error', async () => {
|
|
225
|
+
nock(BASE).post(P + '/schedules/9/offline').reply(200, { code: 50003, msg: 'nope', success: false });
|
|
226
|
+
const client = require('../services/dolphinSchedulerClient');
|
|
227
|
+
await assert.rejects(() => client.scheduleOffline(9), /nope/);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('nextRuns returns 3 dates, null on error', async () => {
|
|
231
|
+
nock(BASE).post(P + '/schedules/preview').query(true)
|
|
232
|
+
.reply(200, ok(['2026-10-02 09:30:00', '2026-10-03 09:30:00', '2026-10-04 09:30:00', '2026-10-05 09:30:00']));
|
|
233
|
+
assert.strictEqual((await service.nextRuns({ schedule })).length, 3);
|
|
234
|
+
nock(BASE).post(P + '/schedules/preview').query(true).reply(500, { code: 1, msg: 'x' });
|
|
235
|
+
assert.strictEqual(await service.nextRuns({ schedule }), null);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('nextRuns gives up after a short timeout (null)', async () => {
|
|
239
|
+
nock(BASE).post(P + '/schedules/preview').query(true).delay(4500)
|
|
240
|
+
.reply(200, ok(['2026-10-02 09:30:00']));
|
|
241
|
+
const t0 = Date.now();
|
|
242
|
+
assert.strictEqual(await service.nextRuns({ schedule }), null);
|
|
243
|
+
assert.ok(Date.now() - t0 < 4400, 'took ' + (Date.now() - t0));
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe('destroyFor / offlineFor', () => {
|
|
247
|
+
const lookup = (s, chatbotId, schedules) => {
|
|
248
|
+
s.get(P + '/process-definition').query((q) => q.searchVal === 'auto-start-' + chatbotId)
|
|
249
|
+
.reply(200, ok({ totalList: [{ code: 888, name: 'auto-start-' + chatbotId }] }));
|
|
250
|
+
s.get(P + '/schedules').query((q) => String(q.processDefinitionCode) === '888')
|
|
251
|
+
.reply(200, ok({ totalList: schedules }));
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
it('destroyFor uses the live ids when present', async () => {
|
|
255
|
+
const s = nock(BASE);
|
|
256
|
+
s.post(P + '/schedules/222/offline').reply(200, ok(true));
|
|
257
|
+
s.post(P + '/process-definition/111/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true));
|
|
258
|
+
s.delete(P + '/process-definition/111').reply(200, ok(true));
|
|
259
|
+
assert.strictEqual(await service.destroyFor('bot1', { workflow_code: 111, schedule_id: 222 }), true);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('destroyFor falls back to the workflow name when there are no live ids', async () => {
|
|
263
|
+
const s = nock(BASE);
|
|
264
|
+
lookup(s, 'bot1', [{ id: 333 }]);
|
|
265
|
+
s.post(P + '/schedules/333/offline').reply(200, ok(true));
|
|
266
|
+
s.post(P + '/process-definition/888/release').query({ releaseState: 'OFFLINE' }).reply(200, ok(true));
|
|
267
|
+
s.delete(P + '/process-definition/888').reply(200, ok(true));
|
|
268
|
+
assert.strictEqual(await service.destroyFor('bot1', { error: 'x' }), true);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('destroyFor never throws: returns false when the scheduler fails', async () => {
|
|
272
|
+
nock(BASE).get(P + '/process-definition').query(true).reply(500, { code: 1, msg: 'boom' });
|
|
273
|
+
assert.strictEqual(await service.destroyFor('bot1', null), false);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('offlineFor takes the schedules found by name offline when there are no live ids', async () => {
|
|
277
|
+
const s = nock(BASE);
|
|
278
|
+
lookup(s, 'bot1', [{ id: 333 }]);
|
|
279
|
+
s.post(P + '/schedules/333/offline').reply(200, ok(true));
|
|
280
|
+
assert.strictEqual(await service.offlineFor('bot1', null), true);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('offlineFor is a no-op (true) when no workflow exists and never throws on failure', async () => {
|
|
284
|
+
nock(BASE).get(P + '/process-definition').query(true).reply(200, ok({ totalList: [] }));
|
|
285
|
+
assert.strictEqual(await service.offlineFor('bot1', {}), true);
|
|
286
|
+
nock(BASE).get(P + '/process-definition').query(true).reply(500, { code: 1, msg: 'boom' });
|
|
287
|
+
assert.strictEqual(await service.offlineFor('bot1', {}), false);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { Webhook } = require('../models/webhook');
|
|
12
|
+
const Faq = require('../models/faq');
|
|
13
|
+
const { setupProjectWithBot, waitFor } = require('./webhookConversationTestUtils');
|
|
14
|
+
|
|
15
|
+
async function setupWithStartPoint() {
|
|
16
|
+
const ctx = await setupProjectWithBot(server);
|
|
17
|
+
const block_id = ctx.startIntent.intent_id;
|
|
18
|
+
const res = await chai.request(server)
|
|
19
|
+
.put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/webhook')
|
|
20
|
+
.auth(ctx.email, ctx.pwd)
|
|
21
|
+
.send({ block_id: block_id });
|
|
22
|
+
expect(res).to.have.status(200);
|
|
23
|
+
return Object.assign(ctx, { block_id });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const startPoints = async (ctx) => (await Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean().exec()).start_points || [];
|
|
27
|
+
|
|
28
|
+
describe('StartPointsCleanup', function () {
|
|
29
|
+
|
|
30
|
+
this.timeout(30000);
|
|
31
|
+
|
|
32
|
+
it('removes the start point when its box is deleted via intentId', async () => {
|
|
33
|
+
const ctx = await setupWithStartPoint();
|
|
34
|
+
|
|
35
|
+
const res = await chai.request(server)
|
|
36
|
+
.delete('/' + ctx.project._id + '/faq/intentId' + ctx.block_id + '?id_faq_kb=' + ctx.chatbot_id)
|
|
37
|
+
.auth(ctx.email, ctx.pwd);
|
|
38
|
+
expect(res).to.have.status(200);
|
|
39
|
+
|
|
40
|
+
await waitFor(async () => (await startPoints(ctx)).length === 0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('removes the start point when its box is deleted via _id', async () => {
|
|
44
|
+
const ctx = await setupWithStartPoint();
|
|
45
|
+
|
|
46
|
+
const res = await chai.request(server)
|
|
47
|
+
.delete('/' + ctx.project._id + '/faq/' + ctx.startIntent._id)
|
|
48
|
+
.auth(ctx.email, ctx.pwd);
|
|
49
|
+
expect(res).to.have.status(200);
|
|
50
|
+
|
|
51
|
+
await waitFor(async () => (await startPoints(ctx)).length === 0);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('keeps the start point when another box is deleted', async () => {
|
|
55
|
+
const ctx = await setupWithStartPoint();
|
|
56
|
+
const other = await Faq.findOne({ id_faq_kb: ctx.chatbot_id, intent_id: { $ne: ctx.block_id } }).lean().exec();
|
|
57
|
+
expect(other, 'the blank template has a second box').to.exist;
|
|
58
|
+
|
|
59
|
+
const res = await chai.request(server)
|
|
60
|
+
.delete('/' + ctx.project._id + '/faq/intentId' + other.intent_id + '?id_faq_kb=' + ctx.chatbot_id)
|
|
61
|
+
.auth(ctx.email, ctx.pwd);
|
|
62
|
+
expect(res).to.have.status(200);
|
|
63
|
+
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
65
|
+
const kept = await startPoints(ctx);
|
|
66
|
+
expect(kept).to.have.length(1);
|
|
67
|
+
expect(kept[0].block_id).to.equal(ctx.block_id);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
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 webhookService = require('../services/webhookService');
|
|
12
|
+
const messageService = require('../services/messageService');
|
|
13
|
+
const requestService = require('../services/requestService');
|
|
14
|
+
const triggerEventEmitter = require('../pubmodules/trigger/event/triggerEventEmitter');
|
|
15
|
+
const Faq = require('../models/faq');
|
|
16
|
+
const Faq_kb = require('../models/faq_kb');
|
|
17
|
+
const { Webhook } = require('../models/webhook');
|
|
18
|
+
const { setupProjectWithBot, waitFor } = require('./webhookConversationTestUtils');
|
|
19
|
+
|
|
20
|
+
// Pins behaviours that the start points feature must not change.
|
|
21
|
+
describe('StartPointsRegression', function () {
|
|
22
|
+
|
|
23
|
+
this.timeout(30000);
|
|
24
|
+
|
|
25
|
+
describe('automation webhook', () => {
|
|
26
|
+
let originalRun;
|
|
27
|
+
let calls;
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
originalRun = webhookService.run;
|
|
31
|
+
calls = [];
|
|
32
|
+
// stub of the tybot /block call. Note: the real run() short-circuits under
|
|
33
|
+
// NODE_ENV=test, so the sync/async handling inside run is not covered here;
|
|
34
|
+
// the route -> run contract (webhook, payload, dev) is.
|
|
35
|
+
webhookService.run = async (webhook, payload, dev) => {
|
|
36
|
+
calls.push({ webhook, payload, dev });
|
|
37
|
+
return { answer: 'stubbed tybot response' };
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
afterEach(() => { webhookService.run = originalRun; });
|
|
41
|
+
|
|
42
|
+
async function createAutomation(ctx) {
|
|
43
|
+
const res = await chai.request(server)
|
|
44
|
+
.post('/' + ctx.project._id + '/webhooks/')
|
|
45
|
+
.auth(ctx.email, ctx.pwd)
|
|
46
|
+
.send({ chatbot_id: ctx.chatbot_id, block_id: ctx.startIntent.intent_id });
|
|
47
|
+
expect(res).to.have.status(200);
|
|
48
|
+
return res.body;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
it('sync (async:false) answers the tybot response', async () => {
|
|
52
|
+
const ctx = await setupProjectWithBot(server);
|
|
53
|
+
const created = await createAutomation(ctx);
|
|
54
|
+
const upd = await chai.request(server)
|
|
55
|
+
.put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id)
|
|
56
|
+
.auth(ctx.email, ctx.pwd).send({ async: false });
|
|
57
|
+
expect(upd.body.async).to.equal(false);
|
|
58
|
+
|
|
59
|
+
const res = await chai.request(server).post('/webhook/' + created.webhook_id).send({ a: 1 });
|
|
60
|
+
|
|
61
|
+
expect(res).to.have.status(200);
|
|
62
|
+
expect(res.body).to.deep.equal({ answer: 'stubbed tybot response' });
|
|
63
|
+
expect(calls).to.have.length(1);
|
|
64
|
+
expect(calls[0].webhook.block_id).to.equal(ctx.startIntent.intent_id);
|
|
65
|
+
expect(calls[0].webhook.async).to.equal(false);
|
|
66
|
+
expect(calls[0].payload.a).to.equal(1);
|
|
67
|
+
expect(calls[0].payload.webhook_http_method).to.equal('POST');
|
|
68
|
+
expect(calls[0].payload.request_id).to.match(new RegExp('^automation-request-' + ctx.project._id + '-.+-' + created.webhook_id + '$'));
|
|
69
|
+
expect(calls[0].dev).to.equal(undefined);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('async answers 200', async () => {
|
|
73
|
+
const ctx = await setupProjectWithBot(server);
|
|
74
|
+
const created = await createAutomation(ctx);
|
|
75
|
+
expect(created.async).to.equal(true);
|
|
76
|
+
|
|
77
|
+
const res = await chai.request(server).post('/webhook/' + created.webhook_id).send({ a: 1 });
|
|
78
|
+
|
|
79
|
+
expect(res).to.have.status(200);
|
|
80
|
+
expect(calls).to.have.length(1);
|
|
81
|
+
expect(calls[0].webhook.mode).to.equal(undefined);
|
|
82
|
+
expect(calls[0].webhook.async).to.equal(true);
|
|
83
|
+
expect(res.body).to.deep.equal({ answer: 'stubbed tybot response' });
|
|
84
|
+
expect(calls[0].payload.a).to.equal(1);
|
|
85
|
+
expect(calls[0].payload.webhook_http_method).to.equal('POST');
|
|
86
|
+
expect(calls[0].payload.request_id).to.match(new RegExp('^automation-request-' + ctx.project._id + '-.+-' + created.webhook_id + '$'));
|
|
87
|
+
expect(calls[0].dev).to.equal(undefined);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('publish', () => {
|
|
92
|
+
afterEach(() => { server.set('chatbot_service', new (require('../services/chatbotService').ChatbotService)()); });
|
|
93
|
+
|
|
94
|
+
it('keeps the start point block resolvable in the published copy', async () => {
|
|
95
|
+
const ctx = await setupProjectWithBot(server);
|
|
96
|
+
const block_id = ctx.startIntent.intent_id;
|
|
97
|
+
const put = await chai.request(server)
|
|
98
|
+
.put('/' + ctx.project._id + '/webhooks/' + ctx.chatbot_id + '/start_points/webhook')
|
|
99
|
+
.auth(ctx.email, ctx.pwd).send({ block_id });
|
|
100
|
+
expect(put).to.have.status(200);
|
|
101
|
+
|
|
102
|
+
// The real fork calls API_URL over HTTP, so it is replaced by an equivalent
|
|
103
|
+
// that goes through the same exportjson/importjson routes on the in-process server.
|
|
104
|
+
const auth = (r) => r.auth(ctx.email, ctx.pwd);
|
|
105
|
+
// Only fork/setModified are replaced: publish() itself (which on master-COLLAUDO lives
|
|
106
|
+
// in ChatbotService) runs for real.
|
|
107
|
+
const { ChatbotService } = require('../services/chatbotService');
|
|
108
|
+
server.set('chatbot_service', new (class extends ChatbotService {
|
|
109
|
+
async fork(id_faq_kb) {
|
|
110
|
+
const exported = await auth(chai.request(server).get('/' + ctx.project._id + '/faq_kb/exportjson/' + id_faq_kb));
|
|
111
|
+
expect(exported).to.have.status(200);
|
|
112
|
+
const created = await auth(chai.request(server).post('/' + ctx.project._id + '/faq_kb'))
|
|
113
|
+
.send({ name: 'published', type: 'tilebot', language: 'en', template: 'blank' });
|
|
114
|
+
const forked_id = created.body._id;
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
116
|
+
const imported = await auth(chai.request(server).post('/' + ctx.project._id + '/faq_kb/importjson/' + forked_id + '?replace=true'))
|
|
117
|
+
.send(JSON.parse(exported.text));
|
|
118
|
+
expect(imported).to.have.status(200);
|
|
119
|
+
return { bot_id: forked_id };
|
|
120
|
+
}
|
|
121
|
+
async setModified() { return true; }
|
|
122
|
+
})());
|
|
123
|
+
|
|
124
|
+
const res = await auth(chai.request(server).put('/' + ctx.project._id + '/faq_kb/' + ctx.chatbot_id + '/publish'));
|
|
125
|
+
expect(res).to.have.status(200);
|
|
126
|
+
|
|
127
|
+
const published_id = res.body.bot_id;
|
|
128
|
+
const copy = await waitFor(() => Faq.findOne({ id_faq_kb: published_id, intent_id: block_id }).lean().exec());
|
|
129
|
+
expect(copy.intent_id).to.equal(block_id);
|
|
130
|
+
|
|
131
|
+
const webhook = await Webhook.findOne({ chatbot_id: ctx.chatbot_id }).lean().exec();
|
|
132
|
+
expect(webhook.start_points).to.have.length(1);
|
|
133
|
+
expect(webhook.start_points[0].block_id).to.equal(block_id);
|
|
134
|
+
const original = await Faq_kb.findById(ctx.chatbot_id).lean().exec();
|
|
135
|
+
expect(original.url).to.contain(published_id);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('widget start', () => {
|
|
140
|
+
it('the rulesTrigger bot launch sends /start without attributes.action', async () => {
|
|
141
|
+
// A full trigger run needs the rules engine and a live request; the
|
|
142
|
+
// 'request.bot.launch' handler is exercised directly through triggerEventEmitter
|
|
143
|
+
// with messageService/requestService stubbed.
|
|
144
|
+
const originalSend = messageService.send;
|
|
145
|
+
const originalAdd = requestService.addParticipantByRequestId;
|
|
146
|
+
const sent = [];
|
|
147
|
+
messageService.send = function () { sent.push(Array.from(arguments)); return Promise.resolve({}); };
|
|
148
|
+
requestService.addParticipantByRequestId = () => Promise.resolve({});
|
|
149
|
+
try {
|
|
150
|
+
const obj = (o) => Object.assign({ toObject: () => o }, o);
|
|
151
|
+
triggerEventEmitter.emit('request.bot.launch', {
|
|
152
|
+
eventKey: 'request.create',
|
|
153
|
+
event: { request_id: 'support-group-widget-1', id_project: 'p1' },
|
|
154
|
+
trigger: obj({}),
|
|
155
|
+
action: obj({ parameters: { member: 'bot_123' } })
|
|
156
|
+
});
|
|
157
|
+
await waitFor(() => sent.length > 0);
|
|
158
|
+
} finally {
|
|
159
|
+
messageService.send = originalSend;
|
|
160
|
+
requestService.addParticipantByRequestId = originalAdd;
|
|
161
|
+
}
|
|
162
|
+
const args = sent[0];
|
|
163
|
+
expect(args[3]).to.equal('/start');
|
|
164
|
+
const options = args[6];
|
|
165
|
+
expect(options).to.deep.equal({ subtype: 'info', updateconversation: false });
|
|
166
|
+
expect(options.attributes).to.equal(undefined);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
});
|