@tiledesk/tiledesk-server 3.0.1 → 3.2.1
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 +47 -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/dataTable.js +16 -0
- package/models/location.js +6 -12
- package/models/quotaUsage.js +71 -0
- package/models/request.js +71 -61
- package/models/requestSnapshot.js +12 -0
- package/models/routerLogger.js +1 -1
- package/models/webhook.js +44 -0
- package/package.json +9 -7
- 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 +23 -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/dataTableService.js +436 -42
- 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 +649 -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/dataTableUtils.js +99 -9
- 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,237 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
process.env.QUOTES_ENABLED = 'true';
|
|
3
|
+
|
|
4
|
+
const projectService = require('../services/projectService');
|
|
5
|
+
const userService = require('../services/userService');
|
|
6
|
+
const Project = require('../models/project');
|
|
7
|
+
const { QuoteManager, PLANS_LIST } = require('../services/QuoteManager');
|
|
8
|
+
const { computeAgentTokens, AGENT_TOKENS_RATES } = require('../utils/aiUtils');
|
|
9
|
+
|
|
10
|
+
let chai = require('chai');
|
|
11
|
+
let chaiHttp = require('chai-http');
|
|
12
|
+
let server = require('../app');
|
|
13
|
+
let should = chai.should();
|
|
14
|
+
let expect = chai.expect;
|
|
15
|
+
|
|
16
|
+
chai.use(chaiHttp);
|
|
17
|
+
|
|
18
|
+
const projectMock = require('./mock/projectMock');
|
|
19
|
+
|
|
20
|
+
let log = false;
|
|
21
|
+
|
|
22
|
+
function createUserAndProject(name) {
|
|
23
|
+
let email = "test-agent-tokens-" + Date.now() + "-" + Math.floor(Math.random() * 100000) + "@email.com";
|
|
24
|
+
let pwd = "pwd";
|
|
25
|
+
return userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => {
|
|
26
|
+
return projectService.create(name, savedUser._id).then((savedProject) => {
|
|
27
|
+
return { email, pwd, savedUser, savedProject };
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('AgentTokensQuota', function () {
|
|
33
|
+
|
|
34
|
+
describe('computeAgentTokens', function () {
|
|
35
|
+
|
|
36
|
+
const rates = { per_usd: 1000000, fallback_multiplier: 1 };
|
|
37
|
+
|
|
38
|
+
it('default rates are 1000000 per USD and fallback multiplier 1', function () {
|
|
39
|
+
expect(AGENT_TOKENS_RATES.per_usd).to.equal(1000000);
|
|
40
|
+
expect(AGENT_TOKENS_RATES.fallback_multiplier).to.equal(1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('converts a USD cost with ceil', function () {
|
|
44
|
+
expect(computeAgentTokens({ tokens: 500, cost: { amount: 0.0123451, currency: 'USD' } }, rates))
|
|
45
|
+
.to.deep.equal({ counted: 12346, mode: 'cost' });
|
|
46
|
+
expect(computeAgentTokens({ tokens: 500, cost: { amount: 0.5, currency: 'usd' } }, rates).counted).to.equal(500000);
|
|
47
|
+
expect(computeAgentTokens({ cost: { amount: 0, currency: 'USD' } }, rates)).to.deep.equal({ counted: 0, mode: 'cost' });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('falls back to tokens * multiplier without a usable cost', function () {
|
|
51
|
+
expect(computeAgentTokens({ tokens: 300 }, rates)).to.deep.equal({ counted: 300, mode: 'tokens' });
|
|
52
|
+
expect(computeAgentTokens({ tokens: 300, cost: null }, rates).counted).to.equal(300);
|
|
53
|
+
expect(computeAgentTokens({ tokens: 300, cost: { amount: 1, currency: 'EUR' } }, rates).counted).to.equal(300);
|
|
54
|
+
expect(computeAgentTokens({ tokens: 300, cost: { amount: "1", currency: 'USD' } }, rates).counted).to.equal(300);
|
|
55
|
+
expect(computeAgentTokens({ tokens: 300 }, { per_usd: 1000000, fallback_multiplier: 2.5 }).counted).to.equal(750);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('does not add 1 to exact micro-dollar amounts', function () {
|
|
59
|
+
for (let n of [1, 3, 7, 10, 29, 57, 123, 1001, 2124, 99999, 123457]) {
|
|
60
|
+
let amount = n * 1e-6;
|
|
61
|
+
expect(computeAgentTokens({ cost: { amount: amount, currency: 'USD' } }, rates).counted, String(amount)).to.equal(n);
|
|
62
|
+
}
|
|
63
|
+
expect(computeAgentTokens({ cost: { amount: 0.1 + 0.2, currency: 'USD' } }, rates).counted).to.equal(300000);
|
|
64
|
+
expect(computeAgentTokens({ cost: { amount: 0.0000011, currency: 'USD' } }, rates).counted).to.equal(2);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('rounds the fallback path up', function () {
|
|
68
|
+
expect(computeAgentTokens({ tokens: 3 }, { per_usd: 1000000, fallback_multiplier: 0.5 }).counted).to.equal(2);
|
|
69
|
+
expect(computeAgentTokens({ tokens: 10.2 }, rates).counted).to.equal(11);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('rejects values over the sanity caps or not finite', function () {
|
|
73
|
+
expect(computeAgentTokens({ tokens: 1e9 }, rates).counted).to.equal(1e9);
|
|
74
|
+
expect(computeAgentTokens({ tokens: 1e9 + 1 }, rates).error).to.be.a('string');
|
|
75
|
+
expect(computeAgentTokens({ cost: { amount: 1e4, currency: 'USD' } }, rates).counted).to.equal(1e10);
|
|
76
|
+
expect(computeAgentTokens({ cost: { amount: 1e303, currency: 'USD' } }, rates).error).to.be.a('string');
|
|
77
|
+
expect(computeAgentTokens({ tokens: 10, cost: { amount: 10001, currency: 'USD' } }, rates).error).to.be.a('string');
|
|
78
|
+
expect(computeAgentTokens({ tokens: 1e9 }, { per_usd: 1, fallback_multiplier: 1e308 }).error).to.be.a('string');
|
|
79
|
+
expect(computeAgentTokens({ cost: { amount: 1e4, currency: 'USD' } }, { per_usd: 1e306, fallback_multiplier: 1 }).error).to.be.a('string');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('rejects invalid bodies', function () {
|
|
83
|
+
expect(computeAgentTokens({}, rates).error).to.be.a('string');
|
|
84
|
+
expect(computeAgentTokens({ tokens: -1 }, rates).error).to.be.a('string');
|
|
85
|
+
expect(computeAgentTokens({ tokens: NaN }, rates).error).to.be.a('string');
|
|
86
|
+
expect(computeAgentTokens({ tokens: "12" }, rates).error).to.be.a('string');
|
|
87
|
+
expect(computeAgentTokens({ cost: { amount: 1, currency: 'EUR' } }, rates).error).to.be.a('string');
|
|
88
|
+
expect(computeAgentTokens({ tokens: 10, cost: { amount: -1, currency: 'USD' } }, rates).error).to.be.a('string');
|
|
89
|
+
expect(computeAgentTokens({ tokens: 10, cost: "1 USD" }, rates).error).to.be.a('string');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('invalid env values fall back to defaults', function () {
|
|
93
|
+
const path = require.resolve('../utils/aiUtils');
|
|
94
|
+
const saved = require.cache[path];
|
|
95
|
+
const env = { per: process.env.AGENT_TOKENS_PER_USD, fb: process.env.AGENT_TOKENS_FALLBACK_MULTIPLIER };
|
|
96
|
+
try {
|
|
97
|
+
delete require.cache[path];
|
|
98
|
+
process.env.AGENT_TOKENS_PER_USD = 'abc';
|
|
99
|
+
process.env.AGENT_TOKENS_FALLBACK_MULTIPLIER = '-3';
|
|
100
|
+
expect(require('../utils/aiUtils').AGENT_TOKENS_RATES).to.deep.equal({ per_usd: 1000000, fallback_multiplier: 1 });
|
|
101
|
+
|
|
102
|
+
delete require.cache[path];
|
|
103
|
+
process.env.AGENT_TOKENS_PER_USD = '2000000';
|
|
104
|
+
process.env.AGENT_TOKENS_FALLBACK_MULTIPLIER = '3';
|
|
105
|
+
expect(require('../utils/aiUtils').AGENT_TOKENS_RATES).to.deep.equal({ per_usd: 2000000, fallback_multiplier: 3 });
|
|
106
|
+
} finally {
|
|
107
|
+
if (env.per === undefined) delete process.env.AGENT_TOKENS_PER_USD; else process.env.AGENT_TOKENS_PER_USD = env.per;
|
|
108
|
+
if (env.fb === undefined) delete process.env.AGENT_TOKENS_FALLBACK_MULTIPLIER; else process.env.AGENT_TOKENS_FALLBACK_MULTIPLIER = env.fb;
|
|
109
|
+
require.cache[path] = saved;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('plan limits', function () {
|
|
115
|
+
|
|
116
|
+
it('agent_tokens equals tokens for every plan', function () {
|
|
117
|
+
Object.keys(PLANS_LIST).forEach((plan) => {
|
|
118
|
+
expect(PLANS_LIST[plan].agent_tokens, plan).to.equal(PLANS_LIST[plan].tokens);
|
|
119
|
+
});
|
|
120
|
+
expect(PLANS_LIST.SANDBOX.agent_tokens).to.equal(100000);
|
|
121
|
+
expect(PLANS_LIST.BASIC.agent_tokens).to.equal(2000000);
|
|
122
|
+
expect(PLANS_LIST.STARTER.agent_tokens).to.equal(2000000);
|
|
123
|
+
expect(PLANS_LIST.PREMIUM.agent_tokens).to.equal(5000000);
|
|
124
|
+
expect(PLANS_LIST.PRO.agent_tokens).to.equal(5000000);
|
|
125
|
+
expect(PLANS_LIST.TEAM.agent_tokens).to.equal(10000000);
|
|
126
|
+
expect(PLANS_LIST.BUSINESS.agent_tokens).to.equal(10000000);
|
|
127
|
+
expect(PLANS_LIST.CUSTOM.agent_tokens).to.equal(10000000);
|
|
128
|
+
expect(PLANS_LIST.FREE_TRIAL.agent_tokens).to.equal(5000000);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('getPlanLimits returns agent_tokens and merges the project override', async function () {
|
|
132
|
+
const qm = new QuoteManager({ tdCache: {} });
|
|
133
|
+
let basic = await qm.getPlanLimits(projectMock.mockProjectBasicPlan);
|
|
134
|
+
expect(basic.agent_tokens).to.equal(2000000);
|
|
135
|
+
let premium = await qm.getPlanLimits(projectMock.mockProjectPremiumPlan);
|
|
136
|
+
expect(premium.agent_tokens).to.equal(5000000);
|
|
137
|
+
|
|
138
|
+
let overridden = JSON.parse(JSON.stringify(projectMock.mockProjectBasicPlan));
|
|
139
|
+
overridden.profile.quotes = { agent_tokens: 42 };
|
|
140
|
+
let limits = await qm.getPlanLimits(overridden);
|
|
141
|
+
expect(limits.agent_tokens).to.equal(42);
|
|
142
|
+
expect(limits.tokens).to.equal(2000000);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('routes', function () {
|
|
147
|
+
|
|
148
|
+
it('incr agent_tokens with a USD cost counts ceil(amount * 1e6)', async function () {
|
|
149
|
+
const { email, pwd, savedProject } = await createUserAndProject("agent-tokens-cost");
|
|
150
|
+
let res = await chai.request(server)
|
|
151
|
+
.post('/' + savedProject._id + '/quotes/incr/agent_tokens')
|
|
152
|
+
.auth(email, pwd)
|
|
153
|
+
.send({ tokens: 1500, input_tokens: 1000, output_tokens: 500, cost: { amount: 0.0021234, currency: 'USD' }, model: 'openai/gpt-4o', source: 'vibe-coder' });
|
|
154
|
+
if (log) { console.log(res.body); }
|
|
155
|
+
res.should.have.status(200);
|
|
156
|
+
expect(res.body.counted).to.equal(2124);
|
|
157
|
+
expect(res.body.currentQuote).to.equal(2124);
|
|
158
|
+
expect(res.body.key).to.match(new RegExp('^quotes:agent_tokens:' + savedProject._id + ':'));
|
|
159
|
+
expect(res.body.message).to.equal('value incremented for key ' + res.body.key);
|
|
160
|
+
|
|
161
|
+
res = await chai.request(server)
|
|
162
|
+
.post('/' + savedProject._id + '/quotes/incr/agent_tokens')
|
|
163
|
+
.auth(email, pwd)
|
|
164
|
+
.send({ tokens: 300, cost: null });
|
|
165
|
+
res.should.have.status(200);
|
|
166
|
+
expect(res.body.counted).to.equal(300);
|
|
167
|
+
expect(res.body.currentQuote).to.equal(2424);
|
|
168
|
+
|
|
169
|
+
// the chatbot tokens quota is untouched
|
|
170
|
+
res = await chai.request(server).post('/' + savedProject._id + '/quotes').auth(email, pwd).send({});
|
|
171
|
+
res.should.have.status(200);
|
|
172
|
+
expect(res.body.quotes.tokens.quote).to.equal(0);
|
|
173
|
+
expect(res.body.quotes.agent_tokens).to.deep.equal({ quote: 2424 });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('incr agent_tokens rejects invalid bodies with 400', async function () {
|
|
177
|
+
const { email, pwd, savedProject } = await createUserAndProject("agent-tokens-400");
|
|
178
|
+
const bodies = [{}, { tokens: 1e10 }, { cost: { amount: 1e303, currency: 'USD' } }, { tokens: -5 }, { tokens: "abc" }, { cost: { amount: 1, currency: 'EUR' } }, { tokens: 1, cost: { amount: -1, currency: 'USD' } }];
|
|
179
|
+
for (let body of bodies) {
|
|
180
|
+
let res = await chai.request(server)
|
|
181
|
+
.post('/' + savedProject._id + '/quotes/incr/agent_tokens')
|
|
182
|
+
.auth(email, pwd)
|
|
183
|
+
.send(body);
|
|
184
|
+
expect(res.status, JSON.stringify(body)).to.equal(400);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('incr of an unknown type answers 400', async function () {
|
|
189
|
+
const { email, pwd, savedProject } = await createUserAndProject("agent-tokens-unknown");
|
|
190
|
+
let res = await chai.request(server)
|
|
191
|
+
.post('/' + savedProject._id + '/quotes/incr/requests')
|
|
192
|
+
.auth(email, pwd)
|
|
193
|
+
.send({ tokens: 10 });
|
|
194
|
+
res.should.have.status(400);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('incr tokens keeps the existing behaviour', async function () {
|
|
198
|
+
const { email, pwd, savedProject } = await createUserAndProject("agent-tokens-tokens");
|
|
199
|
+
let res = await chai.request(server)
|
|
200
|
+
.post('/' + savedProject._id + '/quotes/incr/tokens')
|
|
201
|
+
.auth(email, pwd)
|
|
202
|
+
.send({ tokens: 128, model: 'unknown-model-xyz' });
|
|
203
|
+
res.should.have.status(200);
|
|
204
|
+
expect(res.body.currentQuote).to.equal(128);
|
|
205
|
+
expect(res.body.key).to.match(new RegExp('^quotes:tokens:' + savedProject._id + ':'));
|
|
206
|
+
expect(res.body).to.not.have.property('counted');
|
|
207
|
+
|
|
208
|
+
res = await chai.request(server).post('/' + savedProject._id + '/quotes').auth(email, pwd).send({});
|
|
209
|
+
expect(res.body.quotes.tokens.quote).to.equal(128);
|
|
210
|
+
expect(res.body.quotes.agent_tokens.quote).to.equal(0);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('GET agent_tokens availability honours profile.quotes.agent_tokens', async function () {
|
|
214
|
+
const { email, pwd, savedProject } = await createUserAndProject("agent-tokens-avail");
|
|
215
|
+
await Project.findByIdAndUpdate(savedProject._id, { "profile.quotes": { agent_tokens: 1000 } });
|
|
216
|
+
|
|
217
|
+
let res = await chai.request(server).get('/' + savedProject._id + '/quotes/agent_tokens').auth(email, pwd);
|
|
218
|
+
res.should.have.status(200);
|
|
219
|
+
expect(res.body.isAvailable).to.equal(true);
|
|
220
|
+
|
|
221
|
+
res = await chai.request(server)
|
|
222
|
+
.post('/' + savedProject._id + '/quotes/incr/agent_tokens')
|
|
223
|
+
.auth(email, pwd)
|
|
224
|
+
.send({ tokens: 10, cost: { amount: 0.001, currency: 'USD' } });
|
|
225
|
+
res.should.have.status(200);
|
|
226
|
+
expect(res.body.counted).to.equal(1000);
|
|
227
|
+
|
|
228
|
+
res = await chai.request(server).get('/' + savedProject._id + '/quotes/agent_tokens').auth(email, pwd);
|
|
229
|
+
res.should.have.status(200);
|
|
230
|
+
expect(res.body.isAvailable).to.equal(false);
|
|
231
|
+
|
|
232
|
+
// the chatbot tokens quota is still available
|
|
233
|
+
res = await chai.request(server).get('/' + savedProject._id + '/quotes/tokens').auth(email, pwd);
|
|
234
|
+
expect(res.body.isAvailable).to.equal(true);
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
process.env.LOG_LEVEL = 'error';
|
|
3
|
+
|
|
4
|
+
const expect = require('chai').expect;
|
|
5
|
+
// faq_kb and botEvent require each other: load the model first, or faq_kb keeps an empty
|
|
6
|
+
// botEvent when this file is the first to load them and later publish tests fail.
|
|
7
|
+
require('../models/faq_kb');
|
|
8
|
+
const botEvent = require('../event/botEvent');
|
|
9
|
+
|
|
10
|
+
describe('botEvent.shouldForwardSystemMessage', () => {
|
|
11
|
+
it('forwards /start and the HITL fallback system message only', () => {
|
|
12
|
+
expect(botEvent.shouldForwardSystemMessage({ sender: 'system', text: '/start' })).to.equal(true);
|
|
13
|
+
expect(botEvent.shouldForwardSystemMessage({ sender: 'system', text: '\\start' })).to.equal(true);
|
|
14
|
+
expect(botEvent.shouldForwardSystemMessage({ sender: 'system', text: 'x', attributes: { hitl_fallback: true, action: '#noone-id' } })).to.equal(true);
|
|
15
|
+
expect(botEvent.shouldForwardSystemMessage({ sender: 'system', text: 'x', attributes: { action: '#noone-id' } })).to.equal(false);
|
|
16
|
+
expect(botEvent.shouldForwardSystemMessage({ sender: 'system', text: 'x', attributes: { hitl_fallback: true } })).to.equal(false);
|
|
17
|
+
expect(botEvent.shouldForwardSystemMessage({ sender: 'system', text: 'x' })).to.equal(false);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
// chat21Handler.listen() must route request.participants.join through onParticipantsJoin, which
|
|
3
|
+
// registers the join in chat21JoinTracker; the HITL invite route waits on that registration.
|
|
4
|
+
// listen() is disabled in tests (channelManager): here it is called once and every listener it
|
|
5
|
+
// adds is removed afterwards so the rest of the suite is unaffected.
|
|
6
|
+
const assert = require('assert');
|
|
7
|
+
|
|
8
|
+
// Required lazily (in before): loading the event modules ahead of the app breaks a require cycle
|
|
9
|
+
// for the test files that load after this one.
|
|
10
|
+
let EMITTERS, requestEvent, chat21, chat21JoinTracker, chat21Handler;
|
|
11
|
+
|
|
12
|
+
function snapshot() {
|
|
13
|
+
return EMITTERS.map((emitter) => new Map(emitter.eventNames().map((e) => [e, emitter.listeners(e).slice()])));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('chat21Handler participants.join wiring', function () {
|
|
17
|
+
let before;
|
|
18
|
+
let originalJoin;
|
|
19
|
+
|
|
20
|
+
// eslint-disable-next-line no-undef
|
|
21
|
+
this.beforeAll(() => {
|
|
22
|
+
EMITTERS = ['messageEvent', 'authEvent', 'botEvent', 'requestEvent', 'groupEvent', 'leadEvent']
|
|
23
|
+
.map((name) => require('../event/' + name));
|
|
24
|
+
requestEvent = require('../event/requestEvent');
|
|
25
|
+
chat21 = require('../channels/chat21/chat21Client');
|
|
26
|
+
chat21JoinTracker = require('../channels/chat21/chat21JoinTracker');
|
|
27
|
+
chat21Handler = require('../channels/chat21/chat21Handler');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
originalJoin = chat21.groups.join;
|
|
32
|
+
chat21.groups.join = () => Promise.resolve({ ok: true });
|
|
33
|
+
before = snapshot();
|
|
34
|
+
chat21Handler.listen();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
chat21.groups.join = originalJoin;
|
|
39
|
+
EMITTERS.forEach((emitter, i) => {
|
|
40
|
+
emitter.eventNames().forEach((e) => {
|
|
41
|
+
const kept = before[i].get(e) || [];
|
|
42
|
+
emitter.listeners(e).filter((l) => !kept.includes(l)).forEach((l) => emitter.removeListener(e, l));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('registers the join in chat21JoinTracker while the event is emitted', async () => {
|
|
48
|
+
const added = requestEvent.listeners('request.participants.join').filter((l) => !(before[3].get('request.participants.join') || []).includes(l));
|
|
49
|
+
assert.strictEqual(added.length, 1, 'listen() adds one request.participants.join listener');
|
|
50
|
+
|
|
51
|
+
const request = { request_id: 'support-group-wiring-' + Date.now(), id_project: 'p1', channelOutbound: { name: 'chat21' } };
|
|
52
|
+
added[0]({ request: request, member: 'agent-1' });
|
|
53
|
+
|
|
54
|
+
const pending = chat21JoinTracker.get(request.request_id, 'agent-1');
|
|
55
|
+
assert.ok(pending, 'join registered in chat21JoinTracker synchronously');
|
|
56
|
+
const settled = await pending;
|
|
57
|
+
assert.strictEqual(settled.ok, true);
|
|
58
|
+
});
|
|
59
|
+
});
|