chz-telegram-bot 0.7.13 → 0.7.15
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/dist/dtos/propertyProviderSets.d.ts +0 -1
- package/dist/dtos/propertyProviderSets.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.d.ts +1 -1
- package/dist/entities/actions/commandAction.d.ts.map +1 -1
- package/dist/entities/actions/commandAction.js +2 -2
- package/dist/helpers/builders/commandActionBuilder.d.ts.map +1 -1
- package/dist/helpers/builders/commandActionBuilder.js +2 -3
- package/package.json +4 -1
- package/eslint.config.ts +0 -62
- package/src/builtin/helpAction.ts +0 -17
- package/src/dtos/chatHistoryMessage.ts +0 -22
- package/src/dtos/chatInfo.ts +0 -12
- package/src/dtos/commandTriggerCheckResult.ts +0 -40
- package/src/dtos/cooldownInfo.ts +0 -10
- package/src/dtos/incomingMessage.ts +0 -71
- package/src/dtos/incomingQuery.ts +0 -14
- package/src/dtos/messageInfo.ts +0 -15
- package/src/dtos/propertyProviderSets.ts +0 -21
- package/src/dtos/replyInfo.ts +0 -9
- package/src/dtos/responses/delay.ts +0 -28
- package/src/dtos/responses/imageMessage.ts +0 -41
- package/src/dtos/responses/inlineQueryResponse.ts +0 -26
- package/src/dtos/responses/reaction.ts +0 -30
- package/src/dtos/responses/textMessage.ts +0 -44
- package/src/dtos/responses/unpin.ts +0 -27
- package/src/dtos/responses/videoMessage.ts +0 -41
- package/src/dtos/userInfo.ts +0 -8
- package/src/entities/actions/commandAction.ts +0 -275
- package/src/entities/actions/inlineQueryAction.ts +0 -83
- package/src/entities/actions/replyCaptureAction.ts +0 -110
- package/src/entities/actions/scheduledAction.ts +0 -182
- package/src/entities/botInstance.ts +0 -92
- package/src/entities/cachedStateFactory.ts +0 -14
- package/src/entities/context/baseContext.ts +0 -111
- package/src/entities/context/chatContext.ts +0 -135
- package/src/entities/context/inlineQueryContext.ts +0 -63
- package/src/entities/context/messageContext.ts +0 -250
- package/src/entities/context/replyContext.ts +0 -260
- package/src/entities/states/actionStateBase.ts +0 -6
- package/src/entities/taskRecord.ts +0 -11
- package/src/helpers/builders/commandActionBuilder.ts +0 -214
- package/src/helpers/builders/inlineQueryActionBuilder.ts +0 -71
- package/src/helpers/builders/scheduledActionBuilder.ts +0 -143
- package/src/helpers/mapUtils.ts +0 -28
- package/src/helpers/noop.ts +0 -20
- package/src/helpers/objectFromEntries.ts +0 -7
- package/src/helpers/timeConvertions.ts +0 -13
- package/src/helpers/toArray.ts +0 -3
- package/src/helpers/traceFactory.ts +0 -11
- package/src/index.ts +0 -33
- package/src/main.ts +0 -76
- package/src/services/actionProcessingService.ts +0 -125
- package/src/services/actionProcessors/baseProcessor.ts +0 -67
- package/src/services/actionProcessors/commandActionProcessor.ts +0 -231
- package/src/services/actionProcessors/inlineQueryActionProcessor.ts +0 -165
- package/src/services/actionProcessors/scheduledActionProcessor.ts +0 -136
- package/src/services/jsonFileStorage.ts +0 -181
- package/src/services/nodeTimeoutScheduler.ts +0 -79
- package/src/services/responseProcessingQueue.ts +0 -57
- package/src/services/telegramApi.ts +0 -278
- package/src/types/action.ts +0 -15
- package/src/types/actionState.ts +0 -4
- package/src/types/cachedValueAccessor.ts +0 -1
- package/src/types/capture.ts +0 -33
- package/src/types/commandCondition.ts +0 -9
- package/src/types/commandTrigger.ts +0 -1
- package/src/types/events.ts +0 -286
- package/src/types/externalAliases.ts +0 -18
- package/src/types/handlers.ts +0 -26
- package/src/types/inputFile.ts +0 -4
- package/src/types/messageSendingOptions.ts +0 -10
- package/src/types/messageTypes.ts +0 -21
- package/src/types/propertyProvider.ts +0 -14
- package/src/types/response.ts +0 -51
- package/src/types/scheduler.ts +0 -20
- package/src/types/storage.ts +0 -23
- package/src/types/timeValues.ts +0 -33
- package/src/types/trace.ts +0 -5
- package/tests/dtos/commandTriggerCheckResult.test.ts +0 -301
- package/tests/entities/actions/inlineQueryAction.test.ts +0 -359
- package/tests/entities/actions/replyCaptureAction.test.ts +0 -501
- package/tests/entities/cachedStateFactory.test.ts +0 -98
- package/tests/entities/context/chatContext.test.ts +0 -606
- package/tests/entities/context/messageContext.test.ts +0 -370
- package/tests/entities/states/actionStateBase.test.ts +0 -138
- package/tests/entities/taskRecord.test.ts +0 -195
- package/tests/helpers/mapUtils.test.ts +0 -163
- package/tests/helpers/timeConvertions.test.ts +0 -129
- package/tests/services/actionProcessors/baseActionProcessor.test.ts +0 -359
- package/tests/services/actionProcessors/commandActionProcessor.test.ts +0 -268
- package/tests/services/actionProcessors/inlineQueryActionProcessor.test.ts +0 -616
- package/tests/services/actionProcessors/processorTestHelpers.ts +0 -147
- package/tests/services/actionProcessors/scheduledActionProcessor.test.ts +0 -153
- package/tests/services/jsonFileStorage.test.ts +0 -927
- package/tests/services/nodeTimeoutScheduler.test.ts +0 -421
- package/tests/services/responseProcessingQueue.test.ts +0 -388
- package/tsconfig.build.json +0 -8
- package/tsconfig.json +0 -118
|
@@ -1,606 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, mock } from 'bun:test';
|
|
2
|
-
import { ChatContextInternal } from '../../../src/entities/context/chatContext';
|
|
3
|
-
import { ActionStateBase } from '../../../src/entities/states/actionStateBase';
|
|
4
|
-
import { TypedEventEmitter } from '../../../src/types/events';
|
|
5
|
-
import { TextMessage } from '../../../src/dtos/responses/textMessage';
|
|
6
|
-
import { ImageMessage } from '../../../src/dtos/responses/imageMessage';
|
|
7
|
-
import { VideoMessage } from '../../../src/dtos/responses/videoMessage';
|
|
8
|
-
import { UnpinResponse } from '../../../src/dtos/responses/unpin';
|
|
9
|
-
import { DelayResponse } from '../../../src/dtos/responses/delay';
|
|
10
|
-
import { Milliseconds } from '../../../src/types/timeValues';
|
|
11
|
-
import { ActionKey } from '../../../src/types/action';
|
|
12
|
-
import { ChatInfo } from '../../../src/dtos/chatInfo';
|
|
13
|
-
import { TraceId } from '../../../src/types/trace';
|
|
14
|
-
import { ScheduledAction } from '../../../src/entities/actions/scheduledAction';
|
|
15
|
-
import { createMockScheduler } from '../../services/actionProcessors/processorTestHelpers';
|
|
16
|
-
import { IStorageClient } from '../../../src/types/storage';
|
|
17
|
-
|
|
18
|
-
// Create a mock storage with configurable load response
|
|
19
|
-
function createMockStorage(
|
|
20
|
-
loadResponse: Record<number, ActionStateBase> = {}
|
|
21
|
-
): IStorageClient {
|
|
22
|
-
return {
|
|
23
|
-
load: mock(() => Promise.resolve(loadResponse)),
|
|
24
|
-
close: mock(() => Promise.resolve()),
|
|
25
|
-
getActionState: mock(() =>
|
|
26
|
-
Promise.resolve({ lastExecutedDate: 0, pinnedMessages: [] })
|
|
27
|
-
),
|
|
28
|
-
saveActionExecutionResult: mock(() => Promise.resolve()),
|
|
29
|
-
updateStateFor: mock(() => Promise.resolve())
|
|
30
|
-
} as unknown as IStorageClient;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function createMockAction(): ScheduledAction<ActionStateBase> {
|
|
34
|
-
// Create a minimal mock that satisfies ScheduledAction interface
|
|
35
|
-
return {
|
|
36
|
-
key: 'scheduled:test-action' as ActionKey,
|
|
37
|
-
name: 'test-action',
|
|
38
|
-
stateConstructor: () => new ActionStateBase(),
|
|
39
|
-
exec: mock(() => Promise.resolve([])),
|
|
40
|
-
// These properties are required by ScheduledAction but we mock them
|
|
41
|
-
timeinHoursProvider: () => 12,
|
|
42
|
-
activeProvider: () => true,
|
|
43
|
-
chatsWhitelistProvider: () => [],
|
|
44
|
-
cachedState: new Map(),
|
|
45
|
-
cachedStateFactories: new Map(),
|
|
46
|
-
handler: mock(() => Promise.resolve())
|
|
47
|
-
} as unknown as ScheduledAction<ActionStateBase>;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function createChatContext(): ChatContextInternal<
|
|
51
|
-
ActionStateBase,
|
|
52
|
-
ScheduledAction<ActionStateBase>
|
|
53
|
-
> {
|
|
54
|
-
const storage = createMockStorage();
|
|
55
|
-
const scheduler = createMockScheduler();
|
|
56
|
-
const eventEmitter = new TypedEventEmitter();
|
|
57
|
-
const action = createMockAction();
|
|
58
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
59
|
-
|
|
60
|
-
const ctx = new ChatContextInternal<
|
|
61
|
-
ActionStateBase,
|
|
62
|
-
ScheduledAction<ActionStateBase>
|
|
63
|
-
>(
|
|
64
|
-
storage,
|
|
65
|
-
scheduler,
|
|
66
|
-
eventEmitter,
|
|
67
|
-
action,
|
|
68
|
-
chatInfo,
|
|
69
|
-
'trace-123' as TraceId,
|
|
70
|
-
'TestBot'
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
return ctx;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
describe('ChatContextInternal', () => {
|
|
77
|
-
describe('constructor', () => {
|
|
78
|
-
test('should set storage', () => {
|
|
79
|
-
const ctx = createChatContext();
|
|
80
|
-
|
|
81
|
-
expect(ctx.storage).toBeDefined();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('should set scheduler', () => {
|
|
85
|
-
const ctx = createChatContext();
|
|
86
|
-
|
|
87
|
-
expect(ctx.scheduler).toBeDefined();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test('should set eventEmitter', () => {
|
|
91
|
-
const ctx = createChatContext();
|
|
92
|
-
|
|
93
|
-
expect(ctx.observability.eventEmitter).toBeInstanceOf(
|
|
94
|
-
TypedEventEmitter
|
|
95
|
-
);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test('should have isInitialized as false by default', () => {
|
|
99
|
-
// Note: isInitialized property no longer exists as it was removed from public API
|
|
100
|
-
// All required properties are now set at construction time
|
|
101
|
-
const storage = createMockStorage();
|
|
102
|
-
const scheduler = createMockScheduler();
|
|
103
|
-
const eventEmitter = new TypedEventEmitter();
|
|
104
|
-
const action = createMockAction();
|
|
105
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
106
|
-
|
|
107
|
-
const ctx = new ChatContextInternal<ActionStateBase>(
|
|
108
|
-
storage,
|
|
109
|
-
scheduler,
|
|
110
|
-
eventEmitter,
|
|
111
|
-
action,
|
|
112
|
-
chatInfo,
|
|
113
|
-
'trace-123' as TraceId,
|
|
114
|
-
'TestBot'
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
// Verify context is properly initialized with all properties
|
|
118
|
-
expect(ctx.action).toBeDefined();
|
|
119
|
-
expect(ctx.chatInfo).toBeDefined();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
test('should have empty responses by default', () => {
|
|
123
|
-
const ctx = createChatContext();
|
|
124
|
-
|
|
125
|
-
expect(ctx.responses).toEqual([]);
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe('send.text', () => {
|
|
130
|
-
test('should add TextMessage to responses', () => {
|
|
131
|
-
const ctx = createChatContext();
|
|
132
|
-
|
|
133
|
-
ctx.send.text('Hello, world!');
|
|
134
|
-
|
|
135
|
-
expect(ctx.responses.length).toBe(1);
|
|
136
|
-
expect(ctx.responses[0]).toBeInstanceOf(TextMessage);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test('should set correct text content', () => {
|
|
140
|
-
const ctx = createChatContext();
|
|
141
|
-
|
|
142
|
-
ctx.send.text('Test message');
|
|
143
|
-
|
|
144
|
-
const response = ctx.responses[0] as TextMessage;
|
|
145
|
-
expect(response.content).toBe('Test message');
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test('should set chatInfo on response', () => {
|
|
149
|
-
const ctx = createChatContext();
|
|
150
|
-
|
|
151
|
-
ctx.send.text('Test');
|
|
152
|
-
|
|
153
|
-
const response = ctx.responses[0] as TextMessage;
|
|
154
|
-
expect(response.chatInfo).toBe(ctx.chatInfo);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
test('should set traceId on response', () => {
|
|
158
|
-
const ctx = createChatContext();
|
|
159
|
-
|
|
160
|
-
ctx.send.text('Test');
|
|
161
|
-
|
|
162
|
-
const response = ctx.responses[0] as TextMessage;
|
|
163
|
-
expect(response.traceId).toBe(ctx.observability.traceId);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test('should set action on response', () => {
|
|
167
|
-
const ctx = createChatContext();
|
|
168
|
-
|
|
169
|
-
ctx.send.text('Test');
|
|
170
|
-
|
|
171
|
-
const response = ctx.responses[0] as TextMessage;
|
|
172
|
-
expect(response.action).toBe(ctx.action);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test('should support sending options', () => {
|
|
176
|
-
const ctx = createChatContext();
|
|
177
|
-
|
|
178
|
-
ctx.send.text('Test', { pin: true });
|
|
179
|
-
|
|
180
|
-
const response = ctx.responses[0] as TextMessage;
|
|
181
|
-
expect(response.shouldPin).toBe(true);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
test('should return capture controller', () => {
|
|
185
|
-
const ctx = createChatContext();
|
|
186
|
-
|
|
187
|
-
const controller = ctx.send.text('Test');
|
|
188
|
-
|
|
189
|
-
expect(controller.captureReplies).toBeDefined();
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('should add multiple text messages', () => {
|
|
193
|
-
const ctx = createChatContext();
|
|
194
|
-
|
|
195
|
-
ctx.send.text('First');
|
|
196
|
-
ctx.send.text('Second');
|
|
197
|
-
ctx.send.text('Third');
|
|
198
|
-
|
|
199
|
-
expect(ctx.responses.length).toBe(3);
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
describe('send.image', () => {
|
|
204
|
-
test('should add ImageMessage to responses', () => {
|
|
205
|
-
const ctx = createChatContext();
|
|
206
|
-
|
|
207
|
-
ctx.send.image('test-image');
|
|
208
|
-
|
|
209
|
-
expect(ctx.responses.length).toBe(1);
|
|
210
|
-
expect(ctx.responses[0]).toBeInstanceOf(ImageMessage);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test('should set correct image source path', () => {
|
|
214
|
-
const ctx = createChatContext();
|
|
215
|
-
|
|
216
|
-
ctx.send.image('my-image');
|
|
217
|
-
|
|
218
|
-
const response = ctx.responses[0] as ImageMessage;
|
|
219
|
-
expect(response.content.source).toContain('my-image.png');
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
test('should return capture controller', () => {
|
|
223
|
-
const ctx = createChatContext();
|
|
224
|
-
|
|
225
|
-
const controller = ctx.send.image('test');
|
|
226
|
-
|
|
227
|
-
expect(controller.captureReplies).toBeDefined();
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
test('should support sending options', () => {
|
|
231
|
-
const ctx = createChatContext();
|
|
232
|
-
|
|
233
|
-
ctx.send.image('test', { pin: true });
|
|
234
|
-
|
|
235
|
-
const response = ctx.responses[0] as ImageMessage;
|
|
236
|
-
expect(response.shouldPin).toBe(true);
|
|
237
|
-
});
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
describe('send.video', () => {
|
|
241
|
-
test('should add VideoMessage to responses', () => {
|
|
242
|
-
const ctx = createChatContext();
|
|
243
|
-
|
|
244
|
-
ctx.send.video('test-video');
|
|
245
|
-
|
|
246
|
-
expect(ctx.responses.length).toBe(1);
|
|
247
|
-
expect(ctx.responses[0]).toBeInstanceOf(VideoMessage);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
test('should set correct video source path', () => {
|
|
251
|
-
const ctx = createChatContext();
|
|
252
|
-
|
|
253
|
-
ctx.send.video('my-video');
|
|
254
|
-
|
|
255
|
-
const response = ctx.responses[0] as VideoMessage;
|
|
256
|
-
expect(response.content.source).toContain('my-video.mp4');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
test('should return capture controller', () => {
|
|
260
|
-
const ctx = createChatContext();
|
|
261
|
-
|
|
262
|
-
const controller = ctx.send.video('test');
|
|
263
|
-
|
|
264
|
-
expect(controller.captureReplies).toBeDefined();
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
describe('unpinMessage', () => {
|
|
269
|
-
test('should add UnpinResponse to responses', () => {
|
|
270
|
-
const ctx = createChatContext();
|
|
271
|
-
|
|
272
|
-
ctx.unpinMessage(123);
|
|
273
|
-
|
|
274
|
-
expect(ctx.responses.length).toBe(1);
|
|
275
|
-
expect(ctx.responses[0]).toBeInstanceOf(UnpinResponse);
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
test('should set correct message id', () => {
|
|
279
|
-
const ctx = createChatContext();
|
|
280
|
-
|
|
281
|
-
ctx.unpinMessage(456);
|
|
282
|
-
|
|
283
|
-
const response = ctx.responses[0] as UnpinResponse;
|
|
284
|
-
expect(response.messageId).toBe(456);
|
|
285
|
-
});
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
describe('wait', () => {
|
|
289
|
-
test('should add DelayResponse to responses', () => {
|
|
290
|
-
const ctx = createChatContext();
|
|
291
|
-
|
|
292
|
-
ctx.wait(1000 as Milliseconds);
|
|
293
|
-
|
|
294
|
-
expect(ctx.responses.length).toBe(1);
|
|
295
|
-
expect(ctx.responses[0]).toBeInstanceOf(DelayResponse);
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test('should set correct delay duration', () => {
|
|
299
|
-
const delay = 5000 as Milliseconds;
|
|
300
|
-
const ctx = createChatContext();
|
|
301
|
-
|
|
302
|
-
ctx.wait(delay);
|
|
303
|
-
|
|
304
|
-
const response = ctx.responses[0] as DelayResponse;
|
|
305
|
-
expect(response.delay).toBe(delay);
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
describe('responses ordering', () => {
|
|
310
|
-
test('should maintain order of different response types', () => {
|
|
311
|
-
const ctx = createChatContext();
|
|
312
|
-
|
|
313
|
-
ctx.send.text('Hello');
|
|
314
|
-
ctx.wait(100 as Milliseconds);
|
|
315
|
-
ctx.send.image('test');
|
|
316
|
-
ctx.send.video('clip');
|
|
317
|
-
ctx.unpinMessage(1);
|
|
318
|
-
|
|
319
|
-
expect(ctx.responses.length).toBe(5);
|
|
320
|
-
expect(ctx.responses[0]).toBeInstanceOf(TextMessage);
|
|
321
|
-
expect(ctx.responses[1]).toBeInstanceOf(DelayResponse);
|
|
322
|
-
expect(ctx.responses[2]).toBeInstanceOf(ImageMessage);
|
|
323
|
-
expect(ctx.responses[3]).toBeInstanceOf(VideoMessage);
|
|
324
|
-
expect(ctx.responses[4]).toBeInstanceOf(UnpinResponse);
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
describe('capture controller', () => {
|
|
329
|
-
test('captureReplies should add capture to response', () => {
|
|
330
|
-
const ctx = createChatContext();
|
|
331
|
-
|
|
332
|
-
const controller = ctx.send.text('Choose an option');
|
|
333
|
-
controller.captureReplies(['yes', 'no'], async () => {});
|
|
334
|
-
|
|
335
|
-
const response = ctx.responses[0] as TextMessage;
|
|
336
|
-
expect(response.captures.length).toBe(1);
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
test('captureReplies should set triggers', () => {
|
|
340
|
-
const ctx = createChatContext();
|
|
341
|
-
const triggers = ['yes', 'no', /maybe/];
|
|
342
|
-
|
|
343
|
-
const controller = ctx.send.text('Choose');
|
|
344
|
-
controller.captureReplies(triggers, async () => {});
|
|
345
|
-
|
|
346
|
-
const response = ctx.responses[0] as TextMessage;
|
|
347
|
-
expect(response.captures[0].trigger).toBe(triggers);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
test('captureReplies should set handler', () => {
|
|
351
|
-
const ctx = createChatContext();
|
|
352
|
-
const handler = async () => {};
|
|
353
|
-
|
|
354
|
-
const controller = ctx.send.text('Choose');
|
|
355
|
-
controller.captureReplies(['yes'], handler);
|
|
356
|
-
|
|
357
|
-
const response = ctx.responses[0] as TextMessage;
|
|
358
|
-
expect(response.captures[0].handler).toBe(handler);
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
test('captureReplies should create default AbortController if not provided', () => {
|
|
362
|
-
const ctx = createChatContext();
|
|
363
|
-
|
|
364
|
-
const controller = ctx.send.text('Choose');
|
|
365
|
-
controller.captureReplies(['yes'], async () => {});
|
|
366
|
-
|
|
367
|
-
const response = ctx.responses[0] as TextMessage;
|
|
368
|
-
expect(response.captures[0].abortController).toBeInstanceOf(
|
|
369
|
-
AbortController
|
|
370
|
-
);
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
test('captureReplies should use provided AbortController', () => {
|
|
374
|
-
const ctx = createChatContext();
|
|
375
|
-
const abortController = new AbortController();
|
|
376
|
-
|
|
377
|
-
const controller = ctx.send.text('Choose');
|
|
378
|
-
controller.captureReplies(['yes'], async () => {}, abortController);
|
|
379
|
-
|
|
380
|
-
const response = ctx.responses[0] as TextMessage;
|
|
381
|
-
expect(response.captures[0].abortController).toBe(abortController);
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
test('captureReplies should set action reference', () => {
|
|
385
|
-
const ctx = createChatContext();
|
|
386
|
-
|
|
387
|
-
const controller = ctx.send.text('Choose');
|
|
388
|
-
controller.captureReplies(['yes'], async () => {});
|
|
389
|
-
|
|
390
|
-
const response = ctx.responses[0] as TextMessage;
|
|
391
|
-
expect(response.captures[0].action).toBe(ctx.action);
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
describe('actionKey getter', () => {
|
|
396
|
-
test('should return action key', () => {
|
|
397
|
-
const ctx = createChatContext();
|
|
398
|
-
|
|
399
|
-
expect(ctx.actionKey).toBe('scheduled:test-action' as ActionKey);
|
|
400
|
-
});
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
describe('loadStateOf', () => {
|
|
404
|
-
test('should call storage.load with the action', () => {
|
|
405
|
-
const loadMock = mock(() => Promise.resolve({}));
|
|
406
|
-
const storage: IStorageClient = {
|
|
407
|
-
load: loadMock,
|
|
408
|
-
close: mock(() => Promise.resolve()),
|
|
409
|
-
getActionState: mock(() =>
|
|
410
|
-
Promise.resolve({ lastExecutedDate: 0, pinnedMessages: [] })
|
|
411
|
-
),
|
|
412
|
-
saveActionExecutionResult: mock(() => Promise.resolve()),
|
|
413
|
-
updateStateFor: mock(() => Promise.resolve())
|
|
414
|
-
} as unknown as IStorageClient;
|
|
415
|
-
|
|
416
|
-
const scheduler = createMockScheduler();
|
|
417
|
-
const eventEmitter = new TypedEventEmitter();
|
|
418
|
-
const action = createMockAction();
|
|
419
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
420
|
-
|
|
421
|
-
const ctx = new ChatContextInternal<
|
|
422
|
-
ActionStateBase,
|
|
423
|
-
ScheduledAction<ActionStateBase>
|
|
424
|
-
>(
|
|
425
|
-
storage,
|
|
426
|
-
scheduler,
|
|
427
|
-
eventEmitter,
|
|
428
|
-
action,
|
|
429
|
-
chatInfo,
|
|
430
|
-
'trace-123' as TraceId,
|
|
431
|
-
'TestBot'
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
const baseAction = createMockAction();
|
|
435
|
-
const otherAction = Object.assign(
|
|
436
|
-
Object.create(Object.getPrototypeOf(baseAction) as object),
|
|
437
|
-
baseAction,
|
|
438
|
-
{
|
|
439
|
-
key: 'scheduled:other-action' as ActionKey
|
|
440
|
-
}
|
|
441
|
-
) as ScheduledAction<ActionStateBase>;
|
|
442
|
-
|
|
443
|
-
ctx.loadStateOf(otherAction);
|
|
444
|
-
|
|
445
|
-
expect(loadMock).toHaveBeenCalledWith(otherAction);
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
test('should return state for current chat if it exists', () => {
|
|
449
|
-
const existingState = new ActionStateBase();
|
|
450
|
-
existingState.lastExecutedDate = 12345;
|
|
451
|
-
|
|
452
|
-
const storage: IStorageClient = {
|
|
453
|
-
load: mock(() => ({ 12345: existingState })),
|
|
454
|
-
close: mock(() => Promise.resolve()),
|
|
455
|
-
getActionState: mock(() => ({
|
|
456
|
-
lastExecutedDate: 0,
|
|
457
|
-
pinnedMessages: []
|
|
458
|
-
})),
|
|
459
|
-
saveActionExecutionResult: mock(() => Promise.resolve()),
|
|
460
|
-
updateStateFor: mock(() => Promise.resolve())
|
|
461
|
-
} as unknown as IStorageClient;
|
|
462
|
-
|
|
463
|
-
const scheduler = createMockScheduler();
|
|
464
|
-
const eventEmitter = new TypedEventEmitter();
|
|
465
|
-
const action = createMockAction();
|
|
466
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
467
|
-
|
|
468
|
-
const ctx = new ChatContextInternal<
|
|
469
|
-
ActionStateBase,
|
|
470
|
-
ScheduledAction<ActionStateBase>
|
|
471
|
-
>(
|
|
472
|
-
storage,
|
|
473
|
-
scheduler,
|
|
474
|
-
eventEmitter,
|
|
475
|
-
action,
|
|
476
|
-
chatInfo,
|
|
477
|
-
'trace-123' as TraceId,
|
|
478
|
-
'TestBot'
|
|
479
|
-
);
|
|
480
|
-
|
|
481
|
-
const otherAction = createMockAction();
|
|
482
|
-
const result = ctx.loadStateOf(otherAction);
|
|
483
|
-
|
|
484
|
-
expect(result.lastExecutedDate).toBe(12345);
|
|
485
|
-
});
|
|
486
|
-
|
|
487
|
-
test('should return new state from constructor if no state exists', () => {
|
|
488
|
-
const storage: IStorageClient = {
|
|
489
|
-
load: mock(() => Promise.resolve({})),
|
|
490
|
-
close: mock(() => Promise.resolve()),
|
|
491
|
-
getActionState: mock(() =>
|
|
492
|
-
Promise.resolve({ lastExecutedDate: 0, pinnedMessages: [] })
|
|
493
|
-
),
|
|
494
|
-
saveActionExecutionResult: mock(() => Promise.resolve()),
|
|
495
|
-
updateStateFor: mock(() => Promise.resolve())
|
|
496
|
-
} as unknown as IStorageClient;
|
|
497
|
-
|
|
498
|
-
const scheduler = createMockScheduler();
|
|
499
|
-
const eventEmitter = new TypedEventEmitter();
|
|
500
|
-
const action = createMockAction();
|
|
501
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
502
|
-
|
|
503
|
-
const ctx = new ChatContextInternal<
|
|
504
|
-
ActionStateBase,
|
|
505
|
-
ScheduledAction<ActionStateBase>
|
|
506
|
-
>(
|
|
507
|
-
storage,
|
|
508
|
-
scheduler,
|
|
509
|
-
eventEmitter,
|
|
510
|
-
action,
|
|
511
|
-
chatInfo,
|
|
512
|
-
'trace-123' as TraceId,
|
|
513
|
-
'TestBot'
|
|
514
|
-
);
|
|
515
|
-
|
|
516
|
-
const otherAction = createMockAction();
|
|
517
|
-
const result = ctx.loadStateOf(otherAction);
|
|
518
|
-
|
|
519
|
-
expect(result.lastExecutedDate).toBe(0);
|
|
520
|
-
expect(result.pinnedMessages).toEqual([]);
|
|
521
|
-
});
|
|
522
|
-
|
|
523
|
-
test('should return frozen state', () => {
|
|
524
|
-
const existingState = new ActionStateBase();
|
|
525
|
-
|
|
526
|
-
const storage: IStorageClient = {
|
|
527
|
-
load: mock(() => Promise.resolve({ 12345: existingState })),
|
|
528
|
-
close: mock(() => Promise.resolve()),
|
|
529
|
-
getActionState: mock(() =>
|
|
530
|
-
Promise.resolve({ lastExecutedDate: 0, pinnedMessages: [] })
|
|
531
|
-
),
|
|
532
|
-
saveActionExecutionResult: mock(() => Promise.resolve()),
|
|
533
|
-
updateStateFor: mock(() => Promise.resolve())
|
|
534
|
-
} as unknown as IStorageClient;
|
|
535
|
-
|
|
536
|
-
const scheduler = createMockScheduler();
|
|
537
|
-
const eventEmitter = new TypedEventEmitter();
|
|
538
|
-
const action = createMockAction();
|
|
539
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
540
|
-
|
|
541
|
-
const ctx = new ChatContextInternal<
|
|
542
|
-
ActionStateBase,
|
|
543
|
-
ScheduledAction<ActionStateBase>
|
|
544
|
-
>(
|
|
545
|
-
storage,
|
|
546
|
-
scheduler,
|
|
547
|
-
eventEmitter,
|
|
548
|
-
action,
|
|
549
|
-
chatInfo,
|
|
550
|
-
'trace-123' as TraceId,
|
|
551
|
-
'TestBot'
|
|
552
|
-
);
|
|
553
|
-
|
|
554
|
-
const otherAction = createMockAction();
|
|
555
|
-
const result = ctx.loadStateOf(otherAction);
|
|
556
|
-
|
|
557
|
-
expect(Object.isFrozen(result)).toBe(true);
|
|
558
|
-
});
|
|
559
|
-
});
|
|
560
|
-
|
|
561
|
-
describe('updateStateOf', () => {
|
|
562
|
-
test('should call storage.updateStateFor', async () => {
|
|
563
|
-
const updateStateForMock = mock(() => Promise.resolve());
|
|
564
|
-
const storage: IStorageClient = {
|
|
565
|
-
load: mock(() => Promise.resolve({})),
|
|
566
|
-
close: mock(() => Promise.resolve()),
|
|
567
|
-
getActionState: mock(() =>
|
|
568
|
-
Promise.resolve({ lastExecutedDate: 0, pinnedMessages: [] })
|
|
569
|
-
),
|
|
570
|
-
saveActionExecutionResult: mock(() => Promise.resolve()),
|
|
571
|
-
updateStateFor: updateStateForMock
|
|
572
|
-
} as unknown as IStorageClient;
|
|
573
|
-
|
|
574
|
-
const scheduler = createMockScheduler();
|
|
575
|
-
const eventEmitter = new TypedEventEmitter();
|
|
576
|
-
const action = createMockAction();
|
|
577
|
-
const chatInfo = new ChatInfo(12345, 'Test Chat', []);
|
|
578
|
-
|
|
579
|
-
const ctx = new ChatContextInternal<
|
|
580
|
-
ActionStateBase,
|
|
581
|
-
ScheduledAction<ActionStateBase>
|
|
582
|
-
>(
|
|
583
|
-
storage,
|
|
584
|
-
scheduler,
|
|
585
|
-
eventEmitter,
|
|
586
|
-
action,
|
|
587
|
-
chatInfo,
|
|
588
|
-
'trace-123' as TraceId,
|
|
589
|
-
'TestBot'
|
|
590
|
-
);
|
|
591
|
-
|
|
592
|
-
const otherAction = createMockAction();
|
|
593
|
-
const mutation = (_state: ActionStateBase): Promise<void> => {
|
|
594
|
-
return Promise.resolve();
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
await ctx.updateStateOf(otherAction, mutation);
|
|
598
|
-
|
|
599
|
-
expect(updateStateForMock).toHaveBeenCalledWith(
|
|
600
|
-
otherAction,
|
|
601
|
-
ctx.chatInfo.id,
|
|
602
|
-
mutation
|
|
603
|
-
);
|
|
604
|
-
});
|
|
605
|
-
});
|
|
606
|
-
});
|