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,616 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, beforeEach, mock, type Mock } from 'bun:test';
|
|
2
|
-
import { BotEventType, TypedEventEmitter } from '../../../src/types/events';
|
|
3
|
-
import { IStorageClient } from '../../../src/types/storage';
|
|
4
|
-
import { ActionKey } from '../../../src/types/action';
|
|
5
|
-
import { Milliseconds } from '../../../src/types/timeValues';
|
|
6
|
-
import { InlineQueryActionProcessor } from '../../../src/services/actionProcessors/inlineQueryActionProcessor';
|
|
7
|
-
import { InlineQueryAction } from '../../../src/entities/actions/inlineQueryAction';
|
|
8
|
-
import { BotResponse } from '../../../src/types/response';
|
|
9
|
-
import {
|
|
10
|
-
createMockStorage,
|
|
11
|
-
createMockScheduler,
|
|
12
|
-
createMockTelegramApi,
|
|
13
|
-
type MockScheduler
|
|
14
|
-
} from './processorTestHelpers';
|
|
15
|
-
import { IncomingInlineQuery } from '../../../src/dtos/incomingQuery';
|
|
16
|
-
|
|
17
|
-
// =============================================================================
|
|
18
|
-
// Mock Types for InlineQueryActionProcessor Tests
|
|
19
|
-
// =============================================================================
|
|
20
|
-
|
|
21
|
-
// Mock for telegram.on() handler
|
|
22
|
-
type InlineQueryEventHandler = (params: {
|
|
23
|
-
inlineQuery: {
|
|
24
|
-
id: string;
|
|
25
|
-
query: string;
|
|
26
|
-
from: { id: number };
|
|
27
|
-
};
|
|
28
|
-
}) => void;
|
|
29
|
-
|
|
30
|
-
// Mock TelegramBot interface with call tracking
|
|
31
|
-
interface MockTelegramBot {
|
|
32
|
-
on: (event: string, handler: InlineQueryEventHandler) => void;
|
|
33
|
-
getOnCallCount: () => number;
|
|
34
|
-
getOnLastArgs: () => [string, InlineQueryEventHandler] | undefined;
|
|
35
|
-
hasRegisteredEvent: (eventName: string) => boolean;
|
|
36
|
-
getEventHandler: (eventName: string) => InlineQueryEventHandler | undefined;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function createMockTelegramBot(): MockTelegramBot {
|
|
40
|
-
const handlers = new Map<string, InlineQueryEventHandler>();
|
|
41
|
-
const onMock = mock((event: string, handler: InlineQueryEventHandler) => {
|
|
42
|
-
handlers.set(event, handler);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
on: onMock,
|
|
47
|
-
getOnCallCount: () => onMock.mock.calls.length,
|
|
48
|
-
getOnLastArgs: () =>
|
|
49
|
-
onMock.mock.calls.at(-1) as
|
|
50
|
-
| [string, InlineQueryEventHandler]
|
|
51
|
-
| undefined,
|
|
52
|
-
hasRegisteredEvent: (eventName: string) => handlers.has(eventName),
|
|
53
|
-
getEventHandler: (eventName: string) => handlers.get(eventName)
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Mock InlineQueryAction with call tracking
|
|
58
|
-
interface MockInlineQueryAction {
|
|
59
|
-
key: ActionKey;
|
|
60
|
-
name: string;
|
|
61
|
-
pattern: RegExp;
|
|
62
|
-
isActiveProvider: (ctx: unknown) => boolean;
|
|
63
|
-
handler: (ctx: unknown) => Promise<void>;
|
|
64
|
-
exec: Mock<(ctx: unknown) => Promise<BotResponse[]>>;
|
|
65
|
-
getExecCallCount: () => number;
|
|
66
|
-
getExecLastArgs: () => unknown[] | undefined;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function createMockInlineQueryAction(
|
|
70
|
-
name: string,
|
|
71
|
-
pattern: RegExp = /test/,
|
|
72
|
-
execResult: BotResponse[] = []
|
|
73
|
-
): MockInlineQueryAction {
|
|
74
|
-
const execMock: Mock<(ctx: unknown) => Promise<BotResponse[]>> = mock(() =>
|
|
75
|
-
Promise.resolve(execResult)
|
|
76
|
-
);
|
|
77
|
-
return {
|
|
78
|
-
key: `inline:${name}` as ActionKey,
|
|
79
|
-
name,
|
|
80
|
-
pattern,
|
|
81
|
-
isActiveProvider: () => true,
|
|
82
|
-
handler: () => Promise.resolve(),
|
|
83
|
-
exec: execMock,
|
|
84
|
-
getExecCallCount: () => execMock.mock.calls.length,
|
|
85
|
-
getExecLastArgs: () => execMock.mock.calls.at(-1)
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// =============================================================================
|
|
90
|
-
// InlineQueryActionProcessor Tests
|
|
91
|
-
// =============================================================================
|
|
92
|
-
|
|
93
|
-
describe('InlineQueryActionProcessor', () => {
|
|
94
|
-
let processor: InlineQueryActionProcessor;
|
|
95
|
-
let eventEmitter: TypedEventEmitter;
|
|
96
|
-
let storage: IStorageClient;
|
|
97
|
-
let scheduler: MockScheduler;
|
|
98
|
-
|
|
99
|
-
beforeEach(() => {
|
|
100
|
-
eventEmitter = new TypedEventEmitter();
|
|
101
|
-
storage = createMockStorage();
|
|
102
|
-
scheduler = createMockScheduler();
|
|
103
|
-
processor = new InlineQueryActionProcessor(
|
|
104
|
-
'inline-bot',
|
|
105
|
-
storage,
|
|
106
|
-
scheduler,
|
|
107
|
-
eventEmitter
|
|
108
|
-
);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('constructor', () => {
|
|
112
|
-
test('should create processor with bot name', () => {
|
|
113
|
-
expect(processor).toBeDefined();
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('initialize', () => {
|
|
118
|
-
test('should not create task when no inline queries', () => {
|
|
119
|
-
const mockApi = createMockTelegramApi();
|
|
120
|
-
const mockTelegram = createMockTelegramBot();
|
|
121
|
-
|
|
122
|
-
processor.initialize(
|
|
123
|
-
mockApi,
|
|
124
|
-
mockTelegram as unknown as Parameters<
|
|
125
|
-
typeof processor.initialize
|
|
126
|
-
>[1],
|
|
127
|
-
[],
|
|
128
|
-
100 as Milliseconds
|
|
129
|
-
);
|
|
130
|
-
|
|
131
|
-
expect(scheduler.createTaskCallCount()).toBe(0);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
test('should set up message handler when inline queries provided', () => {
|
|
135
|
-
const mockApi = createMockTelegramApi();
|
|
136
|
-
const mockTelegram = createMockTelegramBot();
|
|
137
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
138
|
-
'test-inline',
|
|
139
|
-
/test/
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
processor.initialize(
|
|
143
|
-
mockApi,
|
|
144
|
-
mockTelegram as unknown as Parameters<
|
|
145
|
-
typeof processor.initialize
|
|
146
|
-
>[1],
|
|
147
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
148
|
-
100 as Milliseconds
|
|
149
|
-
);
|
|
150
|
-
|
|
151
|
-
// Should register inline_query handler
|
|
152
|
-
expect(mockTelegram.getOnCallCount()).toBeGreaterThan(0);
|
|
153
|
-
expect(mockTelegram.hasRegisteredEvent('inline_query')).toBe(true);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test('should create periodic task for processing queries', () => {
|
|
157
|
-
const mockApi = createMockTelegramApi();
|
|
158
|
-
const mockTelegram = createMockTelegramBot();
|
|
159
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
160
|
-
'test-inline',
|
|
161
|
-
/test/
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
processor.initialize(
|
|
165
|
-
mockApi,
|
|
166
|
-
mockTelegram as unknown as Parameters<
|
|
167
|
-
typeof processor.initialize
|
|
168
|
-
>[1],
|
|
169
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
170
|
-
200 as Milliseconds
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
expect(scheduler.createTaskCallCount()).toBe(1);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
test('should store inline queries for processing', () => {
|
|
177
|
-
const mockApi = createMockTelegramApi();
|
|
178
|
-
const mockTelegram = createMockTelegramBot();
|
|
179
|
-
const mockInlineAction1 = createMockInlineQueryAction(
|
|
180
|
-
'test-inline-1',
|
|
181
|
-
/test1/
|
|
182
|
-
);
|
|
183
|
-
const mockInlineAction2 = createMockInlineQueryAction(
|
|
184
|
-
'test-inline-2',
|
|
185
|
-
/test2/
|
|
186
|
-
);
|
|
187
|
-
|
|
188
|
-
processor.initialize(
|
|
189
|
-
mockApi,
|
|
190
|
-
mockTelegram as unknown as Parameters<
|
|
191
|
-
typeof processor.initialize
|
|
192
|
-
>[1],
|
|
193
|
-
[
|
|
194
|
-
mockInlineAction1 as unknown as InlineQueryAction,
|
|
195
|
-
mockInlineAction2 as unknown as InlineQueryAction
|
|
196
|
-
],
|
|
197
|
-
100 as Milliseconds
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
// Handler registered via telegram.on
|
|
201
|
-
expect(mockTelegram.getOnCallCount()).toBeGreaterThan(0);
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
describe('edge cases', () => {
|
|
206
|
-
test('should handle no inline query actions', () => {
|
|
207
|
-
const localScheduler = createMockScheduler();
|
|
208
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
209
|
-
'no-inline-bot',
|
|
210
|
-
createMockStorage(),
|
|
211
|
-
localScheduler,
|
|
212
|
-
new TypedEventEmitter()
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
const mockApi = createMockTelegramApi();
|
|
216
|
-
const mockTelegram = createMockTelegramBot();
|
|
217
|
-
|
|
218
|
-
localProcessor.initialize(
|
|
219
|
-
mockApi,
|
|
220
|
-
mockTelegram as unknown as Parameters<
|
|
221
|
-
typeof localProcessor.initialize
|
|
222
|
-
>[1],
|
|
223
|
-
[], // No inline actions
|
|
224
|
-
100 as Milliseconds
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
// Should not register any handlers
|
|
228
|
-
expect(mockTelegram.getOnCallCount()).toBe(0);
|
|
229
|
-
expect(localScheduler.createTaskCallCount()).toBe(0);
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
describe('query processing', () => {
|
|
234
|
-
test('should emit processing started event when query received', () => {
|
|
235
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
236
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
237
|
-
'query-bot',
|
|
238
|
-
createMockStorage(),
|
|
239
|
-
createMockScheduler(),
|
|
240
|
-
localEventEmitter
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
const processingEvents: unknown[] = [];
|
|
244
|
-
localEventEmitter.on(
|
|
245
|
-
BotEventType.inlineQueryRecieved,
|
|
246
|
-
(_ts, data) => {
|
|
247
|
-
processingEvents.push(data);
|
|
248
|
-
}
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
const mockApi = createMockTelegramApi();
|
|
252
|
-
const mockTelegram = createMockTelegramBot();
|
|
253
|
-
|
|
254
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
255
|
-
'query-action',
|
|
256
|
-
/query/
|
|
257
|
-
);
|
|
258
|
-
|
|
259
|
-
localProcessor.initialize(
|
|
260
|
-
mockApi,
|
|
261
|
-
mockTelegram as unknown as Parameters<
|
|
262
|
-
typeof localProcessor.initialize
|
|
263
|
-
>[1],
|
|
264
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
265
|
-
50 as Milliseconds
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
const inlineQueryHandler =
|
|
269
|
-
mockTelegram.getEventHandler('inline_query');
|
|
270
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
271
|
-
|
|
272
|
-
// Simulate receiving an inline query
|
|
273
|
-
if (inlineQueryHandler) {
|
|
274
|
-
inlineQueryHandler({
|
|
275
|
-
inlineQuery: {
|
|
276
|
-
id: 'query-1',
|
|
277
|
-
query: 'test query',
|
|
278
|
-
from: { id: 12345 }
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
expect(processingEvents.length).toBe(1);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
test('should emit abort event when second query from same user received', () => {
|
|
287
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
288
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
289
|
-
'abort-bot',
|
|
290
|
-
createMockStorage(),
|
|
291
|
-
createMockScheduler(),
|
|
292
|
-
localEventEmitter
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
const abortingEvents: unknown[] = [];
|
|
296
|
-
localEventEmitter.on(
|
|
297
|
-
BotEventType.inlineProcessingAborting,
|
|
298
|
-
(_ts, data) => {
|
|
299
|
-
abortingEvents.push(data);
|
|
300
|
-
}
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
const mockApi = createMockTelegramApi();
|
|
304
|
-
const mockTelegram = createMockTelegramBot();
|
|
305
|
-
|
|
306
|
-
// Create a slow action that allows us to trigger a second query during processing
|
|
307
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
308
|
-
'abort-action',
|
|
309
|
-
/abort/
|
|
310
|
-
);
|
|
311
|
-
|
|
312
|
-
localProcessor.initialize(
|
|
313
|
-
mockApi,
|
|
314
|
-
mockTelegram as unknown as Parameters<
|
|
315
|
-
typeof localProcessor.initialize
|
|
316
|
-
>[1],
|
|
317
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
318
|
-
50 as Milliseconds
|
|
319
|
-
);
|
|
320
|
-
|
|
321
|
-
const inlineQueryHandler =
|
|
322
|
-
mockTelegram.getEventHandler('inline_query');
|
|
323
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
324
|
-
|
|
325
|
-
if (inlineQueryHandler) {
|
|
326
|
-
// First query
|
|
327
|
-
inlineQueryHandler({
|
|
328
|
-
inlineQuery: {
|
|
329
|
-
id: 'query-1',
|
|
330
|
-
query: 'first query',
|
|
331
|
-
from: { id: 99999 }
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
// Second query from same user (should trigger abort of first in pending queue)
|
|
336
|
-
inlineQueryHandler({
|
|
337
|
-
inlineQuery: {
|
|
338
|
-
id: 'query-2',
|
|
339
|
-
query: 'second query',
|
|
340
|
-
from: { id: 99999 }
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// The abort event is only emitted for queries in processing (not pending)
|
|
346
|
-
// Since we haven't started the processing task, the first query is just removed from pending
|
|
347
|
-
// So no abort event should be emitted yet
|
|
348
|
-
expect(abortingEvents.length).toBe(0);
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
test('should filter pending queries when receiving duplicate user query', () => {
|
|
352
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
353
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
354
|
-
'filter-bot',
|
|
355
|
-
createMockStorage(),
|
|
356
|
-
createMockScheduler(),
|
|
357
|
-
localEventEmitter
|
|
358
|
-
);
|
|
359
|
-
|
|
360
|
-
const startedEvents: unknown[] = [];
|
|
361
|
-
localEventEmitter.on(
|
|
362
|
-
BotEventType.inlineQueryRecieved,
|
|
363
|
-
(_ts, data) => {
|
|
364
|
-
startedEvents.push(data);
|
|
365
|
-
}
|
|
366
|
-
);
|
|
367
|
-
|
|
368
|
-
const mockApi = createMockTelegramApi();
|
|
369
|
-
const mockTelegram = createMockTelegramBot();
|
|
370
|
-
|
|
371
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
372
|
-
'filter-action',
|
|
373
|
-
/filter/
|
|
374
|
-
);
|
|
375
|
-
|
|
376
|
-
localProcessor.initialize(
|
|
377
|
-
mockApi,
|
|
378
|
-
mockTelegram as unknown as Parameters<
|
|
379
|
-
typeof localProcessor.initialize
|
|
380
|
-
>[1],
|
|
381
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
382
|
-
50 as Milliseconds
|
|
383
|
-
);
|
|
384
|
-
|
|
385
|
-
const inlineQueryHandler =
|
|
386
|
-
mockTelegram.getEventHandler('inline_query');
|
|
387
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
388
|
-
|
|
389
|
-
if (inlineQueryHandler) {
|
|
390
|
-
// Multiple queries from same user
|
|
391
|
-
inlineQueryHandler({
|
|
392
|
-
inlineQuery: {
|
|
393
|
-
id: 'query-1',
|
|
394
|
-
query: 'first query',
|
|
395
|
-
from: { id: 12345 }
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
inlineQueryHandler({
|
|
400
|
-
inlineQuery: {
|
|
401
|
-
id: 'query-2',
|
|
402
|
-
query: 'second query',
|
|
403
|
-
from: { id: 12345 }
|
|
404
|
-
}
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
inlineQueryHandler({
|
|
408
|
-
inlineQuery: {
|
|
409
|
-
id: 'query-3',
|
|
410
|
-
query: 'third query',
|
|
411
|
-
from: { id: 12345 }
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// All three queries should trigger start events
|
|
417
|
-
expect(startedEvents.length).toBe(3);
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
test('should emit inlineQueryRecieved with query data', () => {
|
|
421
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
422
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
423
|
-
'query-emit-bot',
|
|
424
|
-
createMockStorage(),
|
|
425
|
-
createMockScheduler(),
|
|
426
|
-
localEventEmitter
|
|
427
|
-
);
|
|
428
|
-
|
|
429
|
-
const receivedQueries: Array<{ query: IncomingInlineQuery }> = [];
|
|
430
|
-
localEventEmitter.on(
|
|
431
|
-
BotEventType.inlineQueryRecieved,
|
|
432
|
-
(_ts, data) => {
|
|
433
|
-
receivedQueries.push(data);
|
|
434
|
-
}
|
|
435
|
-
);
|
|
436
|
-
|
|
437
|
-
const mockApi = createMockTelegramApi();
|
|
438
|
-
const mockTelegram = createMockTelegramBot();
|
|
439
|
-
|
|
440
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
441
|
-
'emit-action',
|
|
442
|
-
/test/
|
|
443
|
-
);
|
|
444
|
-
|
|
445
|
-
localProcessor.initialize(
|
|
446
|
-
mockApi,
|
|
447
|
-
mockTelegram as unknown as Parameters<
|
|
448
|
-
typeof localProcessor.initialize
|
|
449
|
-
>[1],
|
|
450
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
451
|
-
50 as Milliseconds
|
|
452
|
-
);
|
|
453
|
-
|
|
454
|
-
const inlineQueryHandler =
|
|
455
|
-
mockTelegram.getEventHandler('inline_query');
|
|
456
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
457
|
-
|
|
458
|
-
if (inlineQueryHandler) {
|
|
459
|
-
inlineQueryHandler({
|
|
460
|
-
inlineQuery: {
|
|
461
|
-
id: 'test-query-1',
|
|
462
|
-
query: 'test search',
|
|
463
|
-
from: { id: 99999 }
|
|
464
|
-
}
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
expect(receivedQueries.length).toBe(1);
|
|
469
|
-
expect(receivedQueries[0].query.queryId).toBe('test-query-1');
|
|
470
|
-
expect(receivedQueries[0].query.query).toBe('test search');
|
|
471
|
-
expect(receivedQueries[0].query.userId).toBe(99999);
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
test('should emit inlineProcessingAborting when second query from same user arrives', () => {
|
|
475
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
476
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
477
|
-
'abort-emit-bot',
|
|
478
|
-
createMockStorage(),
|
|
479
|
-
createMockScheduler(),
|
|
480
|
-
localEventEmitter
|
|
481
|
-
);
|
|
482
|
-
|
|
483
|
-
const mockApi = createMockTelegramApi();
|
|
484
|
-
const mockTelegram = createMockTelegramBot();
|
|
485
|
-
|
|
486
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
487
|
-
'abort-action',
|
|
488
|
-
/abort/
|
|
489
|
-
);
|
|
490
|
-
|
|
491
|
-
localProcessor.initialize(
|
|
492
|
-
mockApi,
|
|
493
|
-
mockTelegram as unknown as Parameters<
|
|
494
|
-
typeof localProcessor.initialize
|
|
495
|
-
>[1],
|
|
496
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
497
|
-
50 as Milliseconds
|
|
498
|
-
);
|
|
499
|
-
|
|
500
|
-
const inlineQueryHandler =
|
|
501
|
-
mockTelegram.getEventHandler('inline_query');
|
|
502
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
503
|
-
|
|
504
|
-
// The abort event only fires if a query is already in processing
|
|
505
|
-
// This would happen if we manually trigger processing between queries
|
|
506
|
-
// But in this test, queries are just queued, not processed yet
|
|
507
|
-
// So we just verify both queries are received
|
|
508
|
-
if (inlineQueryHandler) {
|
|
509
|
-
inlineQueryHandler({
|
|
510
|
-
inlineQuery: {
|
|
511
|
-
id: 'query-1',
|
|
512
|
-
query: 'first',
|
|
513
|
-
from: { id: 55555 }
|
|
514
|
-
}
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
inlineQueryHandler({
|
|
518
|
-
inlineQuery: {
|
|
519
|
-
id: 'query-2',
|
|
520
|
-
query: 'second',
|
|
521
|
-
from: { id: 55555 }
|
|
522
|
-
}
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
// Both queries are received and queued (not yet in processing)
|
|
527
|
-
// The abort event is for when a query is in processing
|
|
528
|
-
// and another query arrives from the same user
|
|
529
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
test('should abort previous query when new query from same user received', () => {
|
|
533
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
534
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
535
|
-
'abort-signal-bot',
|
|
536
|
-
createMockStorage(),
|
|
537
|
-
createMockScheduler(),
|
|
538
|
-
localEventEmitter
|
|
539
|
-
);
|
|
540
|
-
|
|
541
|
-
const mockApi = createMockTelegramApi();
|
|
542
|
-
const mockTelegram = createMockTelegramBot();
|
|
543
|
-
|
|
544
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
545
|
-
'signal-action',
|
|
546
|
-
/signal/
|
|
547
|
-
);
|
|
548
|
-
|
|
549
|
-
localProcessor.initialize(
|
|
550
|
-
mockApi,
|
|
551
|
-
mockTelegram as unknown as Parameters<
|
|
552
|
-
typeof localProcessor.initialize
|
|
553
|
-
>[1],
|
|
554
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
555
|
-
50 as Milliseconds
|
|
556
|
-
);
|
|
557
|
-
|
|
558
|
-
const inlineQueryHandler =
|
|
559
|
-
mockTelegram.getEventHandler('inline_query');
|
|
560
|
-
expect(inlineQueryHandler).toBeDefined();
|
|
561
|
-
|
|
562
|
-
if (inlineQueryHandler) {
|
|
563
|
-
inlineQueryHandler({
|
|
564
|
-
inlineQuery: {
|
|
565
|
-
id: 'query-1',
|
|
566
|
-
query: 'first',
|
|
567
|
-
from: { id: 77777 }
|
|
568
|
-
}
|
|
569
|
-
});
|
|
570
|
-
|
|
571
|
-
inlineQueryHandler({
|
|
572
|
-
inlineQuery: {
|
|
573
|
-
id: 'query-2',
|
|
574
|
-
query: 'second',
|
|
575
|
-
from: { id: 77777 }
|
|
576
|
-
}
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
// The first query's abort controller should have been called
|
|
581
|
-
// This can be verified through the inlineProcessingAborted event
|
|
582
|
-
// which would be emitted when the aborted query throws AbortError
|
|
583
|
-
});
|
|
584
|
-
|
|
585
|
-
test('should emit inlineProcessingFinished after processing completes', () => {
|
|
586
|
-
const localEventEmitter = new TypedEventEmitter();
|
|
587
|
-
const localProcessor = new InlineQueryActionProcessor(
|
|
588
|
-
'finish-bot',
|
|
589
|
-
createMockStorage(),
|
|
590
|
-
createMockScheduler(),
|
|
591
|
-
localEventEmitter
|
|
592
|
-
);
|
|
593
|
-
|
|
594
|
-
const mockApi = createMockTelegramApi();
|
|
595
|
-
const mockTelegram = createMockTelegramBot();
|
|
596
|
-
|
|
597
|
-
const mockInlineAction = createMockInlineQueryAction(
|
|
598
|
-
'finish-action',
|
|
599
|
-
/finish/
|
|
600
|
-
);
|
|
601
|
-
|
|
602
|
-
localProcessor.initialize(
|
|
603
|
-
mockApi,
|
|
604
|
-
mockTelegram as unknown as Parameters<
|
|
605
|
-
typeof localProcessor.initialize
|
|
606
|
-
>[1],
|
|
607
|
-
[mockInlineAction as unknown as InlineQueryAction],
|
|
608
|
-
50 as Milliseconds
|
|
609
|
-
);
|
|
610
|
-
|
|
611
|
-
// Verify that inlineProcessingFinished would be emitted
|
|
612
|
-
// when the periodic task completes all processing
|
|
613
|
-
expect(localProcessor).toBeDefined();
|
|
614
|
-
});
|
|
615
|
-
});
|
|
616
|
-
});
|