Repository navigation
Expand file tree
/
Copy path_helpers.js
More file actions
116 lines (101 loc) · 2.21 KB
/
Copy path_helpers.js
File metadata and controls
116 lines (101 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// @ts-check
const { Telegraf } = require('..')
/** @type {import("../types").Message['from']} */
const from = {
id: 1,
is_bot: false,
first_name: 'Enrico',
last_name: 'Fermi',
}
/** @type {import("../types").Message['chat']} */
const chat = {
id: 1,
type: 'private',
first_name: 'Enrico',
last_name: 'Fermi',
}
let update_id = 1
let message_id = 1
/** @type { { message: { text:() => import("../types").Update.MessageUpdate<import("../types").Message.TextMessage> } } } */
const Fixtures = {
message: {
text: () => ({
update_id: update_id++,
message: {
message_id: message_id++,
date: Date.now(),
from: { ...from },
chat: { ...chat },
text: 'foo',
},
}),
},
}
const SyncStore = () => {
const map = new Map()
return {
map,
store: {
get: (name) => map.get(name),
set: (name, value) => void map.set(name, value),
delete: (name) => void map.delete(name),
},
}
}
const AsyncStore = () => {
const map = new Map()
return {
map,
store: {
async get(name) {
// API requests take time!
await randSleep(50)
return map.get(name)
},
async set(name, value) {
// API requests take time!
await randSleep(50)
map.set(name, value)
},
async delete(name) {
// API requests take time!
await randSleep(50)
map.delete(name)
},
},
}
}
const genericAsyncMiddleware = async (ctx, next) => {
await randSleep(200)
return await next()
}
/** @returns {any} */
function createBot() {
const bot = new Telegraf('')
bot.botInfo = {
id: 42,
is_bot: true,
username: 'bot',
first_name: 'Bot',
can_join_groups: true,
can_read_all_group_messages: true,
supports_inline_queries: true,
}
return bot
}
/** @type {(n: number) => Promise<void>} */
const sleep = (t) => new Promise((r) => setTimeout(r, t))
/** @type {(n: number) => number} */
const rand = (n) => Math.ceil(Math.random() * n)
/** @type {(n: number) => Promise<void>} */
const randSleep = (n) => sleep(rand(n))
module.exports = {
SyncStore,
AsyncStore,
Fixtures,
rand,
sleep,
createBot,
randSleep,
genericAsyncMiddleware,
}