@mtcute/test 0.1.1 → 0.2.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/cjs/client.d.ts +14 -3
- package/cjs/client.js +39 -21
- package/cjs/client.js.map +1 -1
- package/cjs/client.test.d.ts +1 -0
- package/cjs/client.test.js +32 -0
- package/cjs/client.test.js.map +1 -0
- package/cjs/crypto.d.ts +6 -0
- package/cjs/crypto.js +176 -0
- package/cjs/crypto.js.map +1 -0
- package/cjs/index.d.ts +2 -0
- package/cjs/index.js +2 -0
- package/cjs/index.js.map +1 -1
- package/cjs/schema.js +25 -2
- package/cjs/schema.js.map +1 -1
- package/cjs/storage-test.d.ts +24 -0
- package/cjs/storage-test.js +345 -0
- package/cjs/storage-test.js.map +1 -0
- package/cjs/storage.d.ts +2 -1
- package/cjs/storage.js +2 -1
- package/cjs/storage.js.map +1 -1
- package/cjs/storage.test.d.ts +1 -0
- package/cjs/storage.test.js +43 -0
- package/cjs/storage.test.js.map +1 -0
- package/cjs/stub.test.d.ts +1 -0
- package/cjs/stub.test.js +55 -0
- package/cjs/stub.test.js.map +1 -0
- package/cjs/transport.test.d.ts +1 -0
- package/cjs/transport.test.js +37 -0
- package/cjs/transport.test.js.map +1 -0
- package/cjs/utils.test.d.ts +1 -0
- package/cjs/utils.test.js +16 -0
- package/cjs/utils.test.js.map +1 -0
- package/esm/client.d.ts +14 -3
- package/esm/client.js +40 -22
- package/esm/client.js.map +1 -1
- package/esm/client.test.d.ts +1 -0
- package/esm/client.test.js +30 -0
- package/esm/client.test.js.map +1 -0
- package/esm/crypto.d.ts +6 -0
- package/esm/crypto.js +168 -0
- package/esm/crypto.js.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -1
- package/esm/schema.js +2 -2
- package/esm/schema.js.map +1 -1
- package/esm/storage-test.d.ts +24 -0
- package/esm/storage-test.js +337 -0
- package/esm/storage-test.js.map +1 -0
- package/esm/storage.d.ts +2 -1
- package/esm/storage.js +2 -1
- package/esm/storage.js.map +1 -1
- package/esm/storage.test.d.ts +1 -0
- package/esm/storage.test.js +41 -0
- package/esm/storage.test.js.map +1 -0
- package/esm/stub.test.d.ts +1 -0
- package/esm/stub.test.js +50 -0
- package/esm/stub.test.js.map +1 -0
- package/esm/transport.test.d.ts +1 -0
- package/esm/transport.test.js +35 -0
- package/esm/transport.test.js.map +1 -0
- package/esm/utils.test.d.ts +1 -0
- package/esm/utils.test.js +14 -0
- package/esm/utils.test.js.map +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import Long from 'long';
|
|
2
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { defaultProductionDc, hexEncode, LogManager } from '@mtcute/core/utils.js';
|
|
4
|
+
import { __tlReaderMap } from '@mtcute/tl/binary/reader.js';
|
|
5
|
+
import { __tlWriterMap } from '@mtcute/tl/binary/writer.js';
|
|
6
|
+
import { createStub } from './stub.js';
|
|
7
|
+
export const stubPeerUser = {
|
|
8
|
+
id: 123123,
|
|
9
|
+
accessHash: Long.fromBits(123, 456),
|
|
10
|
+
type: 'user',
|
|
11
|
+
username: 'some_user',
|
|
12
|
+
phone: '78005553535',
|
|
13
|
+
full: createStub('user', { id: 123123 }),
|
|
14
|
+
};
|
|
15
|
+
const peerUserInput = {
|
|
16
|
+
_: 'inputPeerUser',
|
|
17
|
+
userId: 123123,
|
|
18
|
+
accessHash: Long.fromBits(123, 456),
|
|
19
|
+
};
|
|
20
|
+
const peerChannel = {
|
|
21
|
+
id: -1001183945448,
|
|
22
|
+
accessHash: Long.fromBits(666, 555),
|
|
23
|
+
type: 'channel',
|
|
24
|
+
username: 'some_channel',
|
|
25
|
+
full: createStub('channel', { id: 123123 }),
|
|
26
|
+
};
|
|
27
|
+
const peerChannelInput = {
|
|
28
|
+
_: 'inputPeerChannel',
|
|
29
|
+
channelId: 1183945448,
|
|
30
|
+
accessHash: Long.fromBits(666, 555),
|
|
31
|
+
};
|
|
32
|
+
function maybeHexEncode(x) {
|
|
33
|
+
if (x == null)
|
|
34
|
+
return null;
|
|
35
|
+
return hexEncode(x);
|
|
36
|
+
}
|
|
37
|
+
export function testStorage(s, params) {
|
|
38
|
+
beforeAll(async () => {
|
|
39
|
+
const logger = new LogManager();
|
|
40
|
+
logger.level = 0;
|
|
41
|
+
s.setup?.(logger, __tlReaderMap, __tlWriterMap);
|
|
42
|
+
await s.load?.();
|
|
43
|
+
});
|
|
44
|
+
afterAll(() => s.destroy?.());
|
|
45
|
+
beforeEach(() => s.reset(true));
|
|
46
|
+
describe('default dc', () => {
|
|
47
|
+
it('should store', async () => {
|
|
48
|
+
await s.setDefaultDcs(defaultProductionDc);
|
|
49
|
+
expect(await s.getDefaultDcs()).toEqual(defaultProductionDc);
|
|
50
|
+
});
|
|
51
|
+
it('should remove', async () => {
|
|
52
|
+
await s.setDefaultDcs(null);
|
|
53
|
+
expect(await s.getDefaultDcs()).toBeNull();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('auth keys', () => {
|
|
57
|
+
beforeAll(() => void vi.useFakeTimers());
|
|
58
|
+
afterAll(() => void vi.useRealTimers());
|
|
59
|
+
const key2 = new Uint8Array(256).fill(0x42);
|
|
60
|
+
const key3 = new Uint8Array(256).fill(0x43);
|
|
61
|
+
const key2i0 = new Uint8Array(256).fill(0x44);
|
|
62
|
+
const key2i1 = new Uint8Array(256).fill(0x45);
|
|
63
|
+
const key3i0 = new Uint8Array(256).fill(0x46);
|
|
64
|
+
const key3i1 = new Uint8Array(256).fill(0x47);
|
|
65
|
+
it('should store perm auth key', async () => {
|
|
66
|
+
await s.setAuthKeyFor(2, key2);
|
|
67
|
+
await s.setAuthKeyFor(3, key3);
|
|
68
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(2))).toEqual(hexEncode(key2));
|
|
69
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3));
|
|
70
|
+
});
|
|
71
|
+
it('should store temp auth keys', async () => {
|
|
72
|
+
const expire = Date.now() + 1000;
|
|
73
|
+
await s.setTempAuthKeyFor(2, 0, key2i0, expire);
|
|
74
|
+
await s.setTempAuthKeyFor(2, 1, key2i1, expire);
|
|
75
|
+
await s.setTempAuthKeyFor(3, 0, key3i0, expire);
|
|
76
|
+
await s.setTempAuthKeyFor(3, 1, key3i1, expire);
|
|
77
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(2, 0))).toEqual(hexEncode(key2i0));
|
|
78
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(2, 1))).toEqual(hexEncode(key2i1));
|
|
79
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(3, 0))).toEqual(hexEncode(key3i0));
|
|
80
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(3, 1))).toEqual(hexEncode(key3i1));
|
|
81
|
+
});
|
|
82
|
+
it('should expire temp auth keys', async () => {
|
|
83
|
+
const expire = Date.now() + 1000;
|
|
84
|
+
await s.setTempAuthKeyFor(2, 0, key2i0, expire);
|
|
85
|
+
await s.setTempAuthKeyFor(2, 1, key2i1, expire);
|
|
86
|
+
await s.setTempAuthKeyFor(3, 0, key3i0, expire);
|
|
87
|
+
await s.setTempAuthKeyFor(3, 1, key3i1, expire);
|
|
88
|
+
vi.advanceTimersByTime(10000);
|
|
89
|
+
expect(await s.getAuthKeyFor(2, 0)).toBeNull();
|
|
90
|
+
expect(await s.getAuthKeyFor(2, 1)).toBeNull();
|
|
91
|
+
expect(await s.getAuthKeyFor(3, 0)).toBeNull();
|
|
92
|
+
expect(await s.getAuthKeyFor(3, 1)).toBeNull();
|
|
93
|
+
});
|
|
94
|
+
it('should remove auth keys', async () => {
|
|
95
|
+
const expire = Date.now() + 1000;
|
|
96
|
+
await s.setTempAuthKeyFor(2, 0, key2i0, expire);
|
|
97
|
+
await s.setTempAuthKeyFor(2, 1, key2i1, expire);
|
|
98
|
+
await s.setAuthKeyFor(2, key2);
|
|
99
|
+
await s.setAuthKeyFor(3, key3);
|
|
100
|
+
await s.setAuthKeyFor(2, null);
|
|
101
|
+
await s.setTempAuthKeyFor(2, 0, null, 0);
|
|
102
|
+
await s.setTempAuthKeyFor(2, 1, null, 0);
|
|
103
|
+
expect(await s.getAuthKeyFor(2)).toBeNull();
|
|
104
|
+
expect(await s.getAuthKeyFor(2, 0)).toBeNull();
|
|
105
|
+
expect(await s.getAuthKeyFor(2, 1)).toBeNull();
|
|
106
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3)); // should not be removed
|
|
107
|
+
});
|
|
108
|
+
it('should remove all auth keys with dropAuthKeysFor', async () => {
|
|
109
|
+
const expire = Date.now() + 1000;
|
|
110
|
+
await s.setTempAuthKeyFor(2, 0, key2i0, expire);
|
|
111
|
+
await s.setTempAuthKeyFor(2, 1, key2i1, expire);
|
|
112
|
+
await s.setAuthKeyFor(2, key2);
|
|
113
|
+
await s.setAuthKeyFor(3, key3);
|
|
114
|
+
await s.dropAuthKeysFor(2);
|
|
115
|
+
expect(await s.getAuthKeyFor(2)).toBeNull();
|
|
116
|
+
expect(await s.getAuthKeyFor(2, 0)).toBeNull();
|
|
117
|
+
expect(await s.getAuthKeyFor(2, 1)).toBeNull();
|
|
118
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3)); // should not be removed
|
|
119
|
+
});
|
|
120
|
+
it('should not reset auth keys on reset()', async () => {
|
|
121
|
+
await s.setAuthKeyFor(2, key2);
|
|
122
|
+
await s.setAuthKeyFor(3, key3);
|
|
123
|
+
s.reset();
|
|
124
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(2))).toEqual(hexEncode(key2));
|
|
125
|
+
expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3));
|
|
126
|
+
});
|
|
127
|
+
it('should reset auth keys on reset(true)', async () => {
|
|
128
|
+
await s.setAuthKeyFor(2, key2);
|
|
129
|
+
await s.setAuthKeyFor(3, key3);
|
|
130
|
+
s.reset(true);
|
|
131
|
+
expect(await s.getAuthKeyFor(2)).toBeNull();
|
|
132
|
+
expect(await s.getAuthKeyFor(3)).toBeNull();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
describe('peers', () => {
|
|
136
|
+
it('should cache and return peers', async () => {
|
|
137
|
+
await s.updatePeers([stubPeerUser, peerChannel]);
|
|
138
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
139
|
+
expect(await s.getPeerById(stubPeerUser.id)).toEqual(peerUserInput);
|
|
140
|
+
expect(await s.getPeerById(peerChannel.id)).toEqual(peerChannelInput);
|
|
141
|
+
});
|
|
142
|
+
it('should cache and return peers by username', async () => {
|
|
143
|
+
await s.updatePeers([stubPeerUser, peerChannel]);
|
|
144
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
145
|
+
expect(await s.getPeerByUsername(stubPeerUser.username)).toEqual(peerUserInput);
|
|
146
|
+
expect(await s.getPeerByUsername(peerChannel.username)).toEqual(peerChannelInput);
|
|
147
|
+
});
|
|
148
|
+
it('should cache and return peers by phone', async () => {
|
|
149
|
+
await s.updatePeers([stubPeerUser]);
|
|
150
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
151
|
+
expect(await s.getPeerByPhone(stubPeerUser.phone)).toEqual(peerUserInput);
|
|
152
|
+
});
|
|
153
|
+
if (!params?.skipEntityOverwrite) {
|
|
154
|
+
it('should overwrite existing cached peers', async () => {
|
|
155
|
+
await s.updatePeers([stubPeerUser]);
|
|
156
|
+
await s.updatePeers([{ ...stubPeerUser, username: 'whatever' }]);
|
|
157
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
158
|
+
expect(await s.getPeerById(stubPeerUser.id)).toEqual(peerUserInput);
|
|
159
|
+
expect(await s.getPeerByUsername(stubPeerUser.username)).toBeNull();
|
|
160
|
+
expect(await s.getPeerByUsername('whatever')).toEqual(peerUserInput);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
it('should cache full peer info', async () => {
|
|
164
|
+
await s.updatePeers([stubPeerUser, peerChannel]);
|
|
165
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
166
|
+
expect({
|
|
167
|
+
...(await s.getFullPeerById(stubPeerUser.id)),
|
|
168
|
+
usernames: [],
|
|
169
|
+
restrictionReason: [],
|
|
170
|
+
}).toEqual(stubPeerUser.full);
|
|
171
|
+
expect({
|
|
172
|
+
...(await s.getFullPeerById(peerChannel.id)),
|
|
173
|
+
usernames: [],
|
|
174
|
+
restrictionReason: [],
|
|
175
|
+
}).toEqual(peerChannel.full);
|
|
176
|
+
});
|
|
177
|
+
describe('min peers', () => {
|
|
178
|
+
it('should generate *FromMessage constructors from reference messages', async () => {
|
|
179
|
+
await s.updatePeers([peerChannel]);
|
|
180
|
+
await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456);
|
|
181
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
182
|
+
expect(await s.getPeerById(stubPeerUser.id)).toEqual({
|
|
183
|
+
_: 'inputPeerUserFromMessage',
|
|
184
|
+
peer: peerChannelInput,
|
|
185
|
+
msgId: 456,
|
|
186
|
+
userId: stubPeerUser.id,
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
it('should handle cases when referenced chat is not available', async () => {
|
|
190
|
+
// this shouldn't really happen, but the storage should be able to handle it
|
|
191
|
+
await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456);
|
|
192
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
193
|
+
expect(await s.getPeerById(stubPeerUser.id)).toEqual(null);
|
|
194
|
+
});
|
|
195
|
+
it('should return full peer if it gets available', async () => {
|
|
196
|
+
await s.updatePeers([peerChannel]);
|
|
197
|
+
await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456);
|
|
198
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
199
|
+
await s.updatePeers([stubPeerUser]);
|
|
200
|
+
await s.save?.();
|
|
201
|
+
expect(await s.getPeerById(stubPeerUser.id)).toEqual(peerUserInput);
|
|
202
|
+
});
|
|
203
|
+
it('should handle cases when referenced message is deleted', async () => {
|
|
204
|
+
await s.updatePeers([peerChannel]);
|
|
205
|
+
await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456);
|
|
206
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
207
|
+
await s.deleteReferenceMessages(peerChannel.id, [456]);
|
|
208
|
+
await s.save?.();
|
|
209
|
+
expect(await s.getPeerById(stubPeerUser.id)).toEqual(null);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
describe('current user', () => {
|
|
214
|
+
const self = {
|
|
215
|
+
userId: 123123,
|
|
216
|
+
isBot: false,
|
|
217
|
+
};
|
|
218
|
+
it('should store and return self info', async () => {
|
|
219
|
+
await s.setSelf(self);
|
|
220
|
+
expect(await s.getSelf()).toEqual(self);
|
|
221
|
+
});
|
|
222
|
+
it('should remove self info', async () => {
|
|
223
|
+
await s.setSelf(self);
|
|
224
|
+
await s.setSelf(null);
|
|
225
|
+
expect(await s.getSelf()).toBeNull();
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
describe('updates state', () => {
|
|
229
|
+
it('should store and return updates state', async () => {
|
|
230
|
+
await s.setUpdatesPts(1);
|
|
231
|
+
await s.setUpdatesQts(2);
|
|
232
|
+
await s.setUpdatesDate(3);
|
|
233
|
+
await s.setUpdatesSeq(4);
|
|
234
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
235
|
+
expect(await s.getUpdatesState()).toEqual([1, 2, 3, 4]);
|
|
236
|
+
});
|
|
237
|
+
it('should store and return channel pts', async () => {
|
|
238
|
+
await s.setManyChannelPts(new Map([
|
|
239
|
+
[1, 2],
|
|
240
|
+
[3, 4],
|
|
241
|
+
]));
|
|
242
|
+
await s.save?.(); // update-related methods are batched, so we need to save
|
|
243
|
+
expect(await s.getChannelPts(1)).toEqual(2);
|
|
244
|
+
expect(await s.getChannelPts(3)).toEqual(4);
|
|
245
|
+
expect(await s.getChannelPts(2)).toBeNull();
|
|
246
|
+
});
|
|
247
|
+
it('should be null after reset', async () => {
|
|
248
|
+
expect(await s.getUpdatesState()).toBeNull();
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
params?.customTests?.(s);
|
|
252
|
+
}
|
|
253
|
+
export function testStateStorage(s) {
|
|
254
|
+
beforeAll(async () => {
|
|
255
|
+
const logger = new LogManager();
|
|
256
|
+
logger.level = 0;
|
|
257
|
+
s.setup?.(logger, __tlReaderMap, __tlWriterMap);
|
|
258
|
+
await s.load?.();
|
|
259
|
+
});
|
|
260
|
+
afterAll(() => s.destroy?.());
|
|
261
|
+
beforeEach(() => s.reset());
|
|
262
|
+
describe('key-value state', () => {
|
|
263
|
+
beforeAll(() => void vi.useFakeTimers());
|
|
264
|
+
afterAll(() => void vi.useRealTimers());
|
|
265
|
+
it('should store and return state', async () => {
|
|
266
|
+
await s.setState('a', 'b');
|
|
267
|
+
await s.setState('c', 'd');
|
|
268
|
+
await s.setState('e', 'f');
|
|
269
|
+
expect(await s.getState('a')).toEqual('b');
|
|
270
|
+
expect(await s.getState('c')).toEqual('d');
|
|
271
|
+
expect(await s.getState('e')).toEqual('f');
|
|
272
|
+
});
|
|
273
|
+
it('should remove state', async () => {
|
|
274
|
+
await s.setState('a', 'b');
|
|
275
|
+
await s.setState('c', 'd');
|
|
276
|
+
await s.setState('e', 'f');
|
|
277
|
+
await s.deleteState('a');
|
|
278
|
+
await s.deleteState('c');
|
|
279
|
+
await s.deleteState('e');
|
|
280
|
+
expect(await s.getState('a')).toBeNull();
|
|
281
|
+
expect(await s.getState('c')).toBeNull();
|
|
282
|
+
expect(await s.getState('e')).toBeNull();
|
|
283
|
+
});
|
|
284
|
+
it('should expire state', async () => {
|
|
285
|
+
await s.setState('a', 'b', 1);
|
|
286
|
+
await s.setState('c', 'd', 1);
|
|
287
|
+
await s.setState('e', 'f', 1);
|
|
288
|
+
vi.advanceTimersByTime(10000);
|
|
289
|
+
expect(await s.getState('a')).toBeNull();
|
|
290
|
+
expect(await s.getState('c')).toBeNull();
|
|
291
|
+
expect(await s.getState('e')).toBeNull();
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
describe('scenes', () => {
|
|
295
|
+
it('should store and return scenes', async () => {
|
|
296
|
+
await s.setCurrentScene('a', 'b');
|
|
297
|
+
await s.setCurrentScene('c', 'd');
|
|
298
|
+
await s.setCurrentScene('e', 'f');
|
|
299
|
+
expect(await s.getCurrentScene('a')).toEqual('b');
|
|
300
|
+
expect(await s.getCurrentScene('c')).toEqual('d');
|
|
301
|
+
expect(await s.getCurrentScene('e')).toEqual('f');
|
|
302
|
+
});
|
|
303
|
+
it('should remove scenes', async () => {
|
|
304
|
+
await s.setCurrentScene('a', 'b');
|
|
305
|
+
await s.setCurrentScene('c', 'd');
|
|
306
|
+
await s.setCurrentScene('e', 'f');
|
|
307
|
+
await s.deleteCurrentScene('a');
|
|
308
|
+
await s.deleteCurrentScene('c');
|
|
309
|
+
await s.deleteCurrentScene('e');
|
|
310
|
+
expect(await s.getCurrentScene('a')).toBeNull();
|
|
311
|
+
expect(await s.getCurrentScene('c')).toBeNull();
|
|
312
|
+
expect(await s.getCurrentScene('e')).toBeNull();
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
describe('rate limit', () => {
|
|
316
|
+
beforeAll(() => void vi.useFakeTimers());
|
|
317
|
+
afterAll(() => void vi.useRealTimers());
|
|
318
|
+
const check = () => s.getRateLimit('test', 3, 1);
|
|
319
|
+
it('should implement basic rate limiting', async () => {
|
|
320
|
+
vi.setSystemTime(0);
|
|
321
|
+
expect(await check()).toEqual([3, 1000]);
|
|
322
|
+
expect(await check()).toEqual([2, 1000]);
|
|
323
|
+
expect(await check()).toEqual([1, 1000]);
|
|
324
|
+
expect(await check()).toEqual([0, 1000]);
|
|
325
|
+
vi.setSystemTime(1001);
|
|
326
|
+
expect(await check()).toEqual([3, 2001]);
|
|
327
|
+
});
|
|
328
|
+
it('should allow resetting rate limit', async () => {
|
|
329
|
+
vi.setSystemTime(0);
|
|
330
|
+
await check();
|
|
331
|
+
await check();
|
|
332
|
+
await s.resetRateLimit('test');
|
|
333
|
+
expect(await check()).toEqual([3, 1000]);
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=storage-test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-test.js","sourceRoot":"","sources":["../../src/storage-test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAGlF,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAU,UAAU,EAA4B,MAAM,uBAAuB,CAAA;AAEpH,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAE3D,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC,MAAM,CAAC,MAAM,YAAY,GAA8B;IACnD,EAAE,EAAE,MAAM;IACV,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IACnC,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,WAAW;IACrB,KAAK,EAAE,aAAa;IACpB,IAAI,EAAE,UAAU,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;CAC3C,CAAA;AACD,MAAM,aAAa,GAAqB;IACpC,CAAC,EAAE,eAAe;IAClB,MAAM,EAAE,MAAM;IACd,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;CACtC,CAAA;AAED,MAAM,WAAW,GAA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AAO,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA;QAC5C,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,mCAAmC,EAAE,KAAK,IAAI,EAAE;YAC/C,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,CAAA;YAEnB,MAAM,KAAK,EAAE,CAAA;YACb,MAAM,KAAK,EAAE,CAAA;YAEb,MAAM,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAA;YAC9B,MAAM,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA;QAC5C,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC","sourcesContent":["import Long from 'long'\nimport { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'\n\nimport { ITelegramStorage, MaybeAsync } from '@mtcute/core'\nimport { defaultProductionDc, hexEncode, Logger, LogManager, TlReaderMap, TlWriterMap } from '@mtcute/core/utils.js'\nimport { tl } from '@mtcute/tl'\nimport { __tlReaderMap } from '@mtcute/tl/binary/reader.js'\nimport { __tlWriterMap } from '@mtcute/tl/binary/writer.js'\n\nimport { createStub } from './stub.js'\n\nexport const stubPeerUser: ITelegramStorage.PeerInfo = {\n id: 123123,\n accessHash: Long.fromBits(123, 456),\n type: 'user',\n username: 'some_user',\n phone: '78005553535',\n full: createStub('user', { id: 123123 }),\n}\nconst peerUserInput: tl.TypeInputPeer = {\n _: 'inputPeerUser',\n userId: 123123,\n accessHash: Long.fromBits(123, 456),\n}\n\nconst peerChannel: ITelegramStorage.PeerInfo = {\n id: -1001183945448,\n accessHash: Long.fromBits(666, 555),\n type: 'channel',\n username: 'some_channel',\n full: createStub('channel', { id: 123123 }),\n}\n\nconst peerChannelInput: tl.TypeInputPeer = {\n _: 'inputPeerChannel',\n channelId: 1183945448,\n accessHash: Long.fromBits(666, 555),\n}\n\nfunction maybeHexEncode(x: Uint8Array | null): string | null {\n if (x == null) return null\n\n return hexEncode(x)\n}\n\nexport function testStorage<T extends ITelegramStorage>(\n s: T,\n params?: {\n skipEntityOverwrite?: boolean\n customTests?: (s: T) => void\n },\n): void {\n beforeAll(async () => {\n const logger = new LogManager()\n logger.level = 0\n s.setup?.(logger, __tlReaderMap, __tlWriterMap)\n\n await s.load?.()\n })\n\n afterAll(() => s.destroy?.())\n beforeEach(() => s.reset(true))\n\n describe('default dc', () => {\n it('should store', async () => {\n await s.setDefaultDcs(defaultProductionDc)\n expect(await s.getDefaultDcs()).toEqual(defaultProductionDc)\n })\n\n it('should remove', async () => {\n await s.setDefaultDcs(null)\n expect(await s.getDefaultDcs()).toBeNull()\n })\n })\n\n describe('auth keys', () => {\n beforeAll(() => void vi.useFakeTimers())\n afterAll(() => void vi.useRealTimers())\n\n const key2 = new Uint8Array(256).fill(0x42)\n const key3 = new Uint8Array(256).fill(0x43)\n\n const key2i0 = new Uint8Array(256).fill(0x44)\n const key2i1 = new Uint8Array(256).fill(0x45)\n const key3i0 = new Uint8Array(256).fill(0x46)\n const key3i1 = new Uint8Array(256).fill(0x47)\n\n it('should store perm auth key', async () => {\n await s.setAuthKeyFor(2, key2)\n await s.setAuthKeyFor(3, key3)\n\n expect(maybeHexEncode(await s.getAuthKeyFor(2))).toEqual(hexEncode(key2))\n expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3))\n })\n\n it('should store temp auth keys', async () => {\n const expire = Date.now() + 1000\n\n await s.setTempAuthKeyFor(2, 0, key2i0, expire)\n await s.setTempAuthKeyFor(2, 1, key2i1, expire)\n await s.setTempAuthKeyFor(3, 0, key3i0, expire)\n await s.setTempAuthKeyFor(3, 1, key3i1, expire)\n\n expect(maybeHexEncode(await s.getAuthKeyFor(2, 0))).toEqual(hexEncode(key2i0))\n expect(maybeHexEncode(await s.getAuthKeyFor(2, 1))).toEqual(hexEncode(key2i1))\n expect(maybeHexEncode(await s.getAuthKeyFor(3, 0))).toEqual(hexEncode(key3i0))\n expect(maybeHexEncode(await s.getAuthKeyFor(3, 1))).toEqual(hexEncode(key3i1))\n })\n\n it('should expire temp auth keys', async () => {\n const expire = Date.now() + 1000\n\n await s.setTempAuthKeyFor(2, 0, key2i0, expire)\n await s.setTempAuthKeyFor(2, 1, key2i1, expire)\n await s.setTempAuthKeyFor(3, 0, key3i0, expire)\n await s.setTempAuthKeyFor(3, 1, key3i1, expire)\n\n vi.advanceTimersByTime(10000)\n\n expect(await s.getAuthKeyFor(2, 0)).toBeNull()\n expect(await s.getAuthKeyFor(2, 1)).toBeNull()\n expect(await s.getAuthKeyFor(3, 0)).toBeNull()\n expect(await s.getAuthKeyFor(3, 1)).toBeNull()\n })\n\n it('should remove auth keys', async () => {\n const expire = Date.now() + 1000\n\n await s.setTempAuthKeyFor(2, 0, key2i0, expire)\n await s.setTempAuthKeyFor(2, 1, key2i1, expire)\n await s.setAuthKeyFor(2, key2)\n await s.setAuthKeyFor(3, key3)\n\n await s.setAuthKeyFor(2, null)\n await s.setTempAuthKeyFor(2, 0, null, 0)\n await s.setTempAuthKeyFor(2, 1, null, 0)\n\n expect(await s.getAuthKeyFor(2)).toBeNull()\n expect(await s.getAuthKeyFor(2, 0)).toBeNull()\n expect(await s.getAuthKeyFor(2, 1)).toBeNull()\n expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3)) // should not be removed\n })\n\n it('should remove all auth keys with dropAuthKeysFor', async () => {\n const expire = Date.now() + 1000\n\n await s.setTempAuthKeyFor(2, 0, key2i0, expire)\n await s.setTempAuthKeyFor(2, 1, key2i1, expire)\n await s.setAuthKeyFor(2, key2)\n await s.setAuthKeyFor(3, key3)\n\n await s.dropAuthKeysFor(2)\n\n expect(await s.getAuthKeyFor(2)).toBeNull()\n expect(await s.getAuthKeyFor(2, 0)).toBeNull()\n expect(await s.getAuthKeyFor(2, 1)).toBeNull()\n expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3)) // should not be removed\n })\n\n it('should not reset auth keys on reset()', async () => {\n await s.setAuthKeyFor(2, key2)\n await s.setAuthKeyFor(3, key3)\n s.reset()\n\n expect(maybeHexEncode(await s.getAuthKeyFor(2))).toEqual(hexEncode(key2))\n expect(maybeHexEncode(await s.getAuthKeyFor(3))).toEqual(hexEncode(key3))\n })\n\n it('should reset auth keys on reset(true)', async () => {\n await s.setAuthKeyFor(2, key2)\n await s.setAuthKeyFor(3, key3)\n s.reset(true)\n\n expect(await s.getAuthKeyFor(2)).toBeNull()\n expect(await s.getAuthKeyFor(3)).toBeNull()\n })\n })\n\n describe('peers', () => {\n it('should cache and return peers', async () => {\n await s.updatePeers([stubPeerUser, peerChannel])\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getPeerById(stubPeerUser.id)).toEqual(peerUserInput)\n expect(await s.getPeerById(peerChannel.id)).toEqual(peerChannelInput)\n })\n\n it('should cache and return peers by username', async () => {\n await s.updatePeers([stubPeerUser, peerChannel])\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getPeerByUsername(stubPeerUser.username!)).toEqual(peerUserInput)\n expect(await s.getPeerByUsername(peerChannel.username!)).toEqual(peerChannelInput)\n })\n\n it('should cache and return peers by phone', async () => {\n await s.updatePeers([stubPeerUser])\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getPeerByPhone(stubPeerUser.phone!)).toEqual(peerUserInput)\n })\n\n if (!params?.skipEntityOverwrite) {\n it('should overwrite existing cached peers', async () => {\n await s.updatePeers([stubPeerUser])\n await s.updatePeers([{ ...stubPeerUser, username: 'whatever' }])\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getPeerById(stubPeerUser.id)).toEqual(peerUserInput)\n expect(await s.getPeerByUsername(stubPeerUser.username!)).toBeNull()\n expect(await s.getPeerByUsername('whatever')).toEqual(peerUserInput)\n })\n }\n\n it('should cache full peer info', async () => {\n await s.updatePeers([stubPeerUser, peerChannel])\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect({\n ...(await s.getFullPeerById(stubPeerUser.id)),\n usernames: [],\n restrictionReason: [],\n }).toEqual(stubPeerUser.full)\n expect({\n ...(await s.getFullPeerById(peerChannel.id)),\n usernames: [],\n restrictionReason: [],\n }).toEqual(peerChannel.full)\n })\n\n describe('min peers', () => {\n it('should generate *FromMessage constructors from reference messages', async () => {\n await s.updatePeers([peerChannel])\n await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456)\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getPeerById(stubPeerUser.id)).toEqual({\n _: 'inputPeerUserFromMessage',\n peer: peerChannelInput,\n msgId: 456,\n userId: stubPeerUser.id,\n })\n })\n\n it('should handle cases when referenced chat is not available', async () => {\n // this shouldn't really happen, but the storage should be able to handle it\n await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456)\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getPeerById(stubPeerUser.id)).toEqual(null)\n })\n\n it('should return full peer if it gets available', async () => {\n await s.updatePeers([peerChannel])\n await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456)\n await s.save?.() // update-related methods are batched, so we need to save\n\n await s.updatePeers([stubPeerUser])\n await s.save?.()\n\n expect(await s.getPeerById(stubPeerUser.id)).toEqual(peerUserInput)\n })\n\n it('should handle cases when referenced message is deleted', async () => {\n await s.updatePeers([peerChannel])\n await s.saveReferenceMessage(stubPeerUser.id, peerChannel.id, 456)\n await s.save?.() // update-related methods are batched, so we need to save\n\n await s.deleteReferenceMessages(peerChannel.id, [456])\n await s.save?.()\n\n expect(await s.getPeerById(stubPeerUser.id)).toEqual(null)\n })\n })\n })\n\n describe('current user', () => {\n const self: ITelegramStorage.SelfInfo = {\n userId: 123123,\n isBot: false,\n }\n\n it('should store and return self info', async () => {\n await s.setSelf(self)\n expect(await s.getSelf()).toEqual(self)\n })\n\n it('should remove self info', async () => {\n await s.setSelf(self)\n await s.setSelf(null)\n expect(await s.getSelf()).toBeNull()\n })\n })\n\n describe('updates state', () => {\n it('should store and return updates state', async () => {\n await s.setUpdatesPts(1)\n await s.setUpdatesQts(2)\n await s.setUpdatesDate(3)\n await s.setUpdatesSeq(4)\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getUpdatesState()).toEqual([1, 2, 3, 4])\n })\n\n it('should store and return channel pts', async () => {\n await s.setManyChannelPts(\n new Map([\n [1, 2],\n [3, 4],\n ]),\n )\n await s.save?.() // update-related methods are batched, so we need to save\n\n expect(await s.getChannelPts(1)).toEqual(2)\n expect(await s.getChannelPts(3)).toEqual(4)\n expect(await s.getChannelPts(2)).toBeNull()\n })\n\n it('should be null after reset', async () => {\n expect(await s.getUpdatesState()).toBeNull()\n })\n })\n\n params?.customTests?.(s)\n}\n\ninterface IStateStorage {\n setup?(log: Logger, readerMap: TlReaderMap, writerMap: TlWriterMap): void\n\n load?(): MaybeAsync<void>\n\n save?(): MaybeAsync<void>\n\n destroy?(): MaybeAsync<void>\n\n reset(): MaybeAsync<void>\n\n getState(key: string): MaybeAsync<unknown>\n\n setState(key: string, state: unknown, ttl?: number): MaybeAsync<void>\n\n deleteState(key: string): MaybeAsync<void>\n\n getCurrentScene(key: string): MaybeAsync<string | null>\n\n setCurrentScene(key: string, scene: string, ttl?: number): MaybeAsync<void>\n\n deleteCurrentScene(key: string): MaybeAsync<void>\n\n getRateLimit(key: string, limit: number, window: number): MaybeAsync<[number, number]>\n\n resetRateLimit(key: string): MaybeAsync<void>\n}\n\nexport function testStateStorage(s: IStateStorage) {\n beforeAll(async () => {\n const logger = new LogManager()\n logger.level = 0\n s.setup?.(logger, __tlReaderMap, __tlWriterMap)\n\n await s.load?.()\n })\n\n afterAll(() => s.destroy?.())\n beforeEach(() => s.reset())\n\n describe('key-value state', () => {\n beforeAll(() => void vi.useFakeTimers())\n afterAll(() => void vi.useRealTimers())\n\n it('should store and return state', async () => {\n await s.setState('a', 'b')\n await s.setState('c', 'd')\n await s.setState('e', 'f')\n\n expect(await s.getState('a')).toEqual('b')\n expect(await s.getState('c')).toEqual('d')\n expect(await s.getState('e')).toEqual('f')\n })\n\n it('should remove state', async () => {\n await s.setState('a', 'b')\n await s.setState('c', 'd')\n await s.setState('e', 'f')\n\n await s.deleteState('a')\n await s.deleteState('c')\n await s.deleteState('e')\n\n expect(await s.getState('a')).toBeNull()\n expect(await s.getState('c')).toBeNull()\n expect(await s.getState('e')).toBeNull()\n })\n\n it('should expire state', async () => {\n await s.setState('a', 'b', 1)\n await s.setState('c', 'd', 1)\n await s.setState('e', 'f', 1)\n\n vi.advanceTimersByTime(10000)\n\n expect(await s.getState('a')).toBeNull()\n expect(await s.getState('c')).toBeNull()\n expect(await s.getState('e')).toBeNull()\n })\n })\n\n describe('scenes', () => {\n it('should store and return scenes', async () => {\n await s.setCurrentScene('a', 'b')\n await s.setCurrentScene('c', 'd')\n await s.setCurrentScene('e', 'f')\n\n expect(await s.getCurrentScene('a')).toEqual('b')\n expect(await s.getCurrentScene('c')).toEqual('d')\n expect(await s.getCurrentScene('e')).toEqual('f')\n })\n\n it('should remove scenes', async () => {\n await s.setCurrentScene('a', 'b')\n await s.setCurrentScene('c', 'd')\n await s.setCurrentScene('e', 'f')\n\n await s.deleteCurrentScene('a')\n await s.deleteCurrentScene('c')\n await s.deleteCurrentScene('e')\n\n expect(await s.getCurrentScene('a')).toBeNull()\n expect(await s.getCurrentScene('c')).toBeNull()\n expect(await s.getCurrentScene('e')).toBeNull()\n })\n })\n\n describe('rate limit', () => {\n beforeAll(() => void vi.useFakeTimers())\n afterAll(() => void vi.useRealTimers())\n\n const check = () => s.getRateLimit('test', 3, 1)\n\n it('should implement basic rate limiting', async () => {\n vi.setSystemTime(0)\n\n expect(await check()).toEqual([3, 1000])\n expect(await check()).toEqual([2, 1000])\n expect(await check()).toEqual([1, 1000])\n expect(await check()).toEqual([0, 1000])\n\n vi.setSystemTime(1001)\n\n expect(await check()).toEqual([3, 2001])\n })\n\n it('should allow resetting rate limit', async () => {\n vi.setSystemTime(0)\n\n await check()\n await check()\n\n await s.resetRateLimit('test')\n expect(await check()).toEqual([3, 1000])\n })\n })\n}\n"]}
|
package/esm/storage.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ITelegramStorage
|
|
1
|
+
import { ITelegramStorage } from '@mtcute/core';
|
|
2
|
+
import { MemoryStorage } from '@mtcute/core/storage/memory.js';
|
|
2
3
|
import { ICryptoProvider } from '@mtcute/core/utils.js';
|
|
3
4
|
export declare class StubMemoryTelegramStorage extends MemoryStorage implements ITelegramStorage {
|
|
4
5
|
readonly params: {
|
package/esm/storage.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MtArgumentError } from '@mtcute/core';
|
|
2
|
+
import { MemoryStorage } from '@mtcute/core/storage/memory.js';
|
|
2
3
|
import { createAesIgeForMessage } from '@mtcute/core/utils.js';
|
|
3
4
|
export class StubMemoryTelegramStorage extends MemoryStorage {
|
|
4
5
|
constructor(params = {
|
package/esm/storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,eAAe,EAAE,MAAM,cAAc,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAClE,OAAO,EAAE,sBAAsB,EAAmB,MAAM,uBAAuB,CAAA;AAE/E,MAAM,OAAO,yBAA0B,SAAQ,aAAa;IACxD,YACa,SAqBL;QACA,OAAO,EAAE,IAAI;QACb,WAAW,EAAE,IAAI;KACpB;QAED,KAAK,EAAE,CAAA;QA1BE,WAAM,GAAN,MAAM,CAwBd;IAGL,CAAC;IAED,aAAa,CAAC,IAAY,EAAE,SAA8B;QACtD,IAAI,SAAS,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,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CAAA;SAC1D;QAED,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;QACvC,MAAM,aAAa,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;QAEvC,MAAM,GAAG,GAAG,sBAAsB,CAAC,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;QACjE,MAAM,SAAS,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,CAAA;QAE5C,wCAAwC;QACxC,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,UAAU,EAAE,SAAS,CAAC,UAAU,CAAC,CAAA;QACrF,MAAM,MAAM,GAAG,EAAE,CAAC,SAAS,CAAC,EAAE,EAAE,IAAI,CAAC,CAAA;QAErC,OAAO,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,GAAG,MAAM,CAAC,CAAA;IAC9C,CAAC;CACJ","sourcesContent":["import { ITelegramStorage, MtArgumentError } from '@mtcute/core'\nimport { MemoryStorage } from '@mtcute/core/src/storage/memory.js'\nimport { createAesIgeForMessage, ICryptoProvider } from '@mtcute/core/utils.js'\n\nexport class StubMemoryTelegramStorage extends MemoryStorage implements ITelegramStorage {\n constructor(\n readonly params: {\n /**\n * IDs of the DCs for which the storage should have auth keys,\n * or `true` to have keys for all DCs\n *\n * @default true\n */\n hasKeys?: boolean | number[]\n\n /**\n * IDs of the DCs for which the storage should have temp auth keys,\n * or `true` to have keys for all DCs\n *\n * @default true\n */\n hasTempKeys?: boolean | number[]\n\n onLoad?: () => void\n onSave?: () => void\n onDestroy?: () => void\n onReset?: () => void\n } = {\n hasKeys: true,\n hasTempKeys: true,\n },\n ) {\n super()\n }\n\n getAuthKeyFor(dcId: number, tempIndex?: number | undefined): Uint8Array | null {\n if (tempIndex === undefined && this.params.hasKeys) {\n if (this.params.hasKeys === true || this.params.hasKeys.includes(dcId)) {\n return new Uint8Array(256)\n }\n }\n\n if (tempIndex === undefined && this.params.hasTempKeys) {\n if (this.params.hasTempKeys === true || this.params.hasTempKeys.includes(dcId)) {\n return new Uint8Array(256)\n }\n }\n\n return super.getAuthKeyFor(dcId, tempIndex)\n }\n\n load(): void {\n this.params.onLoad?.()\n super.load()\n }\n\n save(): void {\n this.params.onSave?.()\n }\n\n destroy(): void {\n this.params.onDestroy?.()\n super.destroy()\n }\n\n reset(withKeys = false): void {\n this.params?.onReset?.()\n super.reset(withKeys)\n }\n\n decryptOutgoingMessage(crypto: ICryptoProvider, data: Uint8Array, dcId: number, tempIndex?: number | undefined) {\n const key = this.getAuthKeyFor(dcId, tempIndex)\n\n if (!key) {\n throw new MtArgumentError(`No auth key for DC ${dcId}`)\n }\n\n const messageKey = data.subarray(8, 24)\n const encryptedData = data.subarray(24)\n\n const ige = createAesIgeForMessage(crypto, key, messageKey, true)\n const innerData = ige.decrypt(encryptedData)\n\n // skipping all checks because who cares\n const dv = new DataView(innerData.buffer, innerData.byteOffset, innerData.byteLength)\n const length = dv.getUint32(28, true)\n\n return innerData.subarray(32, 32 + length)\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { BaseTelegramClient } from '@mtcute/core';
|
|
3
|
+
import { StubMemoryTelegramStorage } from './storage.js';
|
|
4
|
+
import { createStub } from './stub.js';
|
|
5
|
+
import { StubTelegramTransport } from './transport.js';
|
|
6
|
+
describe('storage stub', () => {
|
|
7
|
+
it('should correctly intercept calls', async () => {
|
|
8
|
+
const log = [];
|
|
9
|
+
const client = new BaseTelegramClient({
|
|
10
|
+
apiId: 0,
|
|
11
|
+
apiHash: '',
|
|
12
|
+
logLevel: 0,
|
|
13
|
+
defaultDcs: {
|
|
14
|
+
main: createStub('dcOption', { ipAddress: '1.2.3.4', port: 1234 }),
|
|
15
|
+
media: createStub('dcOption', { ipAddress: '1.2.3.4', port: 5678 }),
|
|
16
|
+
},
|
|
17
|
+
transport: () => new StubTelegramTransport({
|
|
18
|
+
onMessage: (msg) => {
|
|
19
|
+
if (msg.slice(0, 8).reduce((a, b) => a + b, 0) === 0) {
|
|
20
|
+
// should not happen, since we're providing stub keys
|
|
21
|
+
log.push('unauthed_message');
|
|
22
|
+
}
|
|
23
|
+
setTimeout(() => {
|
|
24
|
+
client.close().catch(() => { });
|
|
25
|
+
}, 10);
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
storage: new StubMemoryTelegramStorage({
|
|
29
|
+
hasKeys: true,
|
|
30
|
+
onLoad: () => log.push('load'),
|
|
31
|
+
onSave: () => log.push('save'),
|
|
32
|
+
onDestroy: () => log.push('destroy'),
|
|
33
|
+
onReset: () => log.push('reset'),
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
await client.connect();
|
|
37
|
+
await client.call({ _: 'help.getConfig' }).catch(() => { }); // ignore "client closed" error
|
|
38
|
+
expect(log).toEqual(['load', 'save', 'destroy']);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=storage.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.test.js","sourceRoot":"","sources":["../../src/storage.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAEtD,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE;QAC9C,MAAM,GAAG,GA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AAO,CAAC;aACnC,CAAC;SACL,CAAC,CAAA;QAEF,MAAM,MAAM,CAAC,OAAO,EAAE,CAAA;QACtB,MAAM,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA,CAAC,+BAA+B;QAE1F,MAAM,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC,CAAA;IACpD,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA","sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport { BaseTelegramClient } from '@mtcute/core'\n\nimport { StubMemoryTelegramStorage } from './storage.js'\nimport { createStub } from './stub.js'\nimport { StubTelegramTransport } from './transport.js'\n\ndescribe('storage stub', () => {\n it('should correctly intercept calls', async () => {\n const log: string[] = []\n\n const client = new BaseTelegramClient({\n apiId: 0,\n apiHash: '',\n logLevel: 0,\n defaultDcs: {\n main: createStub('dcOption', { ipAddress: '1.2.3.4', port: 1234 }),\n media: createStub('dcOption', { ipAddress: '1.2.3.4', port: 5678 }),\n },\n transport: () =>\n new StubTelegramTransport({\n onMessage: (msg) => {\n if (msg.slice(0, 8).reduce((a, b) => a + b, 0) === 0) {\n // should not happen, since we're providing stub keys\n log.push('unauthed_message')\n }\n setTimeout(() => {\n client.close().catch(() => {})\n }, 10)\n },\n }),\n storage: new StubMemoryTelegramStorage({\n hasKeys: true,\n onLoad: () => log.push('load'),\n onSave: () => log.push('save'),\n onDestroy: () => log.push('destroy'),\n onReset: () => log.push('reset'),\n }),\n })\n\n await client.connect()\n await client.call({ _: 'help.getConfig' }).catch(() => {}) // ignore \"client closed\" error\n\n expect(log).toEqual(['load', 'save', 'destroy'])\n })\n})\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/stub.test.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Long from 'long';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { createStub } from './index.js';
|
|
4
|
+
describe('stub', () => {
|
|
5
|
+
it('should correctly generate simple stubs', () => {
|
|
6
|
+
expect(createStub('inputUser', { userId: 123 })).toEqual({
|
|
7
|
+
_: 'inputUser',
|
|
8
|
+
userId: 123,
|
|
9
|
+
accessHash: Long.ZERO,
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
it('should correctly generate stubs for optional fields', () => {
|
|
13
|
+
expect(createStub('updateChannelTooLong')).toEqual({
|
|
14
|
+
_: 'updateChannelTooLong',
|
|
15
|
+
channelId: 0,
|
|
16
|
+
pts: undefined,
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
it('should correctly generate stubs for boolean flags', () => {
|
|
20
|
+
expect(createStub('account.finishTakeoutSession')).toEqual({
|
|
21
|
+
_: 'account.finishTakeoutSession',
|
|
22
|
+
success: false,
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
it('should correctly generate stubs for vectors', () => {
|
|
26
|
+
expect(createStub('messageActionChatAddUser')).toEqual({
|
|
27
|
+
_: 'messageActionChatAddUser',
|
|
28
|
+
users: [],
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
it('should correctly generate stubs for optional vectors', () => {
|
|
32
|
+
expect(createStub('updateChannelPinnedTopics')).toEqual({
|
|
33
|
+
_: 'updateChannelPinnedTopics',
|
|
34
|
+
channelId: 0,
|
|
35
|
+
order: [],
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
it('should correctly generate stubs for nested types', () => {
|
|
39
|
+
expect(createStub('messageActionGroupCallScheduled', { scheduleDate: 123 })).toEqual({
|
|
40
|
+
_: 'messageActionGroupCallScheduled',
|
|
41
|
+
call: {
|
|
42
|
+
_: 'inputGroupCall',
|
|
43
|
+
id: Long.ZERO,
|
|
44
|
+
accessHash: Long.ZERO,
|
|
45
|
+
},
|
|
46
|
+
scheduleDate: 123,
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AC7B,KAAK,EAAE,EAAE;SACZ,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,sDAAsD,EAAE,GAAG,EAAE;QAC5D,MAAM,CAAC,UAAU,CAAC,2BAA2B,CAAC,CAAC,CAAC,OAAO,CAAC;YACpD,CAAC,EAAE,2BAA2B;YAC9B,SAAS,EAAE,CAAC;YACZ,KAAK,EAAE,EAAE;SACZ,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,kDAAkD,EAAE,GAAG,EAAE;QACxD,MAAM,CAAC,UAAU,CAAC,iCAAiC,EAAE,EAAE,YAAY,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;YACjF,CAAC,EAAE,iCAAiC;YACpC,IAAI,EAAE;gBACF,CAAC,EAAE,gBAAgB;gBACnB,EAAE,EAAE,IAAI,CAAC,IAAI;gBACb,UAAU,EAAE,IAAI,CAAC,IAAI;aACxB;YACD,YAAY,EAAE,GAAG;SACpB,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA","sourcesContent":["import Long from 'long'\nimport { describe, expect, it } from 'vitest'\n\nimport { createStub } from './index.js'\n\ndescribe('stub', () => {\n it('should correctly generate simple stubs', () => {\n expect(createStub('inputUser', { userId: 123 })).toEqual({\n _: 'inputUser',\n userId: 123,\n accessHash: Long.ZERO,\n })\n })\n\n it('should correctly generate stubs for optional fields', () => {\n expect(createStub('updateChannelTooLong')).toEqual({\n _: 'updateChannelTooLong',\n channelId: 0,\n pts: undefined,\n })\n })\n\n it('should correctly generate stubs for boolean flags', () => {\n expect(createStub('account.finishTakeoutSession')).toEqual({\n _: 'account.finishTakeoutSession',\n success: false,\n })\n })\n\n it('should correctly generate stubs for vectors', () => {\n expect(createStub('messageActionChatAddUser')).toEqual({\n _: 'messageActionChatAddUser',\n users: [],\n })\n })\n\n it('should correctly generate stubs for optional vectors', () => {\n expect(createStub('updateChannelPinnedTopics')).toEqual({\n _: 'updateChannelPinnedTopics',\n channelId: 0,\n order: [],\n })\n })\n\n it('should correctly generate stubs for nested types', () => {\n expect(createStub('messageActionGroupCallScheduled', { scheduleDate: 123 })).toEqual({\n _: 'messageActionGroupCallScheduled',\n call: {\n _: 'inputGroupCall',\n id: Long.ZERO,\n accessHash: Long.ZERO,\n },\n scheduleDate: 123,\n })\n })\n})\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { BaseTelegramClient } from '@mtcute/core';
|
|
3
|
+
import { MemoryStorage } from '@mtcute/core/storage/memory.js';
|
|
4
|
+
import { createStub } from './stub.js';
|
|
5
|
+
import { StubTelegramTransport } from './transport.js';
|
|
6
|
+
describe('transport stub', () => {
|
|
7
|
+
it('should correctly intercept calls', async () => {
|
|
8
|
+
const log = [];
|
|
9
|
+
const client = new BaseTelegramClient({
|
|
10
|
+
apiId: 0,
|
|
11
|
+
apiHash: '',
|
|
12
|
+
logLevel: 0,
|
|
13
|
+
defaultDcs: {
|
|
14
|
+
main: createStub('dcOption', { ipAddress: '1.2.3.4', port: 1234 }),
|
|
15
|
+
media: createStub('dcOption', { ipAddress: '1.2.3.4', port: 5678 }),
|
|
16
|
+
},
|
|
17
|
+
storage: new MemoryStorage(),
|
|
18
|
+
transport: () => new StubTelegramTransport({
|
|
19
|
+
onConnect: (dc, testMode) => {
|
|
20
|
+
log.push(`connect ${dc.ipAddress}:${dc.port} test=${testMode}`);
|
|
21
|
+
client.close().catch(() => { });
|
|
22
|
+
},
|
|
23
|
+
onMessage(msg) {
|
|
24
|
+
log.push(`message size=${msg.length}`);
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
await client.connect().catch(() => { }); // ignore "client closed" error
|
|
29
|
+
expect(log).toEqual([
|
|
30
|
+
'message size=40',
|
|
31
|
+
'connect 1.2.3.4:1234 test=false',
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
//# sourceMappingURL=transport.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.test.js","sourceRoot":"","sources":["../../src/transport.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAElE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAEtD,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE;QAC9C,MAAM,GAAG,GAAa,EAAE,CAAA;QAExB,MAAM,MAAM,GAAG,IAAI,kBAAkB,CAAC;YAClC,KAAK,EAAE,CAAC;YACR,OAAO,EAAE,EAAE;YACX,QAAQ,EAAE,CAAC;YACX,UAAU,EAAE;gBACR,IAAI,EAAE,UAAU,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;gBAClE,KAAK,EAAE,UAAU,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;aACtE;YACD,OAAO,EAAE,IAAI,aAAa,EAAE;YAC5B,SAAS,EAAE,GAAG,EAAE,CACZ,IAAI,qBAAqB,CAAC;gBACtB,SAAS,EAAE,CAAC,EAAE,EAAE,QAAQ,EAAE,EAAE;oBACxB,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,SAAS,IAAI,EAAE,CAAC,IAAI,SAAS,QAAQ,EAAE,CAAC,CAAA;oBAC/D,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;gBAClC,CAAC;gBACD,SAAS,CAAC,GAAG;oBACT,GAAG,CAAC,IAAI,CAAC,gBAAgB,GAAG,CAAC,MAAM,EAAE,CAAC,CAAA;gBAC1C,CAAC;aACJ,CAAC;SACT,CAAC,CAAA;QAEF,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA,CAAC,+BAA+B;QAEtE,MAAM,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC;YAChB,iBAAiB;YACjB,iCAAiC;SACpC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA","sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport { BaseTelegramClient } from '@mtcute/core'\nimport { MemoryStorage } from '@mtcute/core/src/storage/memory.js'\n\nimport { createStub } from './stub.js'\nimport { StubTelegramTransport } from './transport.js'\n\ndescribe('transport stub', () => {\n it('should correctly intercept calls', async () => {\n const log: string[] = []\n\n const client = new BaseTelegramClient({\n apiId: 0,\n apiHash: '',\n logLevel: 0,\n defaultDcs: {\n main: createStub('dcOption', { ipAddress: '1.2.3.4', port: 1234 }),\n media: createStub('dcOption', { ipAddress: '1.2.3.4', port: 5678 }),\n },\n storage: new MemoryStorage(),\n transport: () =>\n new StubTelegramTransport({\n onConnect: (dc, testMode) => {\n log.push(`connect ${dc.ipAddress}:${dc.port} test=${testMode}`)\n client.close().catch(() => {})\n },\n onMessage(msg) {\n log.push(`message size=${msg.length}`)\n },\n }),\n })\n\n await client.connect().catch(() => {}) // ignore \"client closed\" error\n\n expect(log).toEqual([\n 'message size=40', // req_pq_multi\n 'connect 1.2.3.4:1234 test=false',\n ])\n })\n})\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|