Skip to content

Commit 4ed48d0

Browse files
committed
add tests
1 parent d9d5f9f commit 4ed48d0

File tree

3 files changed

+295
-8
lines changed

3 files changed

+295
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "commonjs",
33
"name": "typescript-telegram-bot-api",
4-
"version": "0.3.4",
4+
"version": "0.3.5",
55
"description": "Telegram Bot API wrapper for Node.js written in TypeScript",
66
"repository": "github:Borodin/typescript-telegram-bot-api",
77
"main": "dist/index.js",

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,13 +2145,13 @@ export class TelegramBot extends EventEmitter {
21452145
* ## setStickerSetThumbnail
21462146
* Use this method to set the thumbnail of a regular or mask sticker set. The format of the thumbnail file must match
21472147
* the format of the stickers in the set. Returns True on success.
2148-
* @see https://core.telegram.org/bots/api#setstickersettitle
2148+
* @see https://core.telegram.org/bots/api#setstickersetthumbnail
21492149
*/
21502150
async setStickerSetThumbnail(options: {
21512151
name: string;
21522152
user_id: number;
21532153
thumbnail?: InputFile | string;
2154-
format?: InputSticker['format'];
2154+
format: InputSticker['format'];
21552155
}): Promise<true> {
21562156
return await this.callApi('setStickerSetThumbnail', options);
21572157
}
@@ -2379,7 +2379,7 @@ export class TelegramBot extends EventEmitter {
23792379
protect_content?: boolean;
23802380
message_effect_id?: string;
23812381
reply_parameters?: ReplyParameters;
2382-
reply_markup: InlineKeyboardMarkup;
2382+
reply_markup?: InlineKeyboardMarkup;
23832383
}): Promise<Message> {
23842384
return await this.callApi('sendGame', {
23852385
...options,

tests/index.test.ts

Lines changed: 291 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createReadStream } from 'fs';
33
import { readFile } from 'fs/promises';
44
import { FileOptions, TelegramBot } from '../src';
55
import { TelegramError } from '../src/errors';
6-
import { ForumTopic, File, User, StickerSet } from '../src/types';
6+
import { ForumTopic, File, User, StickerSet, Update } from '../src/types';
77

88
const TOKEN = process.env.TEST_TELEGRAM_TOKEN as string;
99
const USERID = parseInt(process.env.TEST_USER_ID as string);
@@ -53,11 +53,35 @@ describe('TelegramBot', () => {
5353
});
5454
});
5555

56+
describe('.processUpdate()', () => {
57+
const update: Update = {
58+
update_id: 1,
59+
message: {
60+
message_id: 1,
61+
chat: { id: 1, type: 'private' },
62+
text: '/start',
63+
date: 0,
64+
},
65+
};
66+
67+
it('should emit message event', () => {
68+
const spy = jest.spyOn(bot, 'emit');
69+
bot.processUpdate(update);
70+
expect(spy).toHaveBeenCalledWith('message', update.message);
71+
});
72+
73+
it('should emit message text event', () => {
74+
const spy = jest.spyOn(bot, 'emit');
75+
bot.processUpdate(update);
76+
expect(spy).toHaveBeenCalledWith('message:text', update.message);
77+
});
78+
});
79+
5680
describe('.startPolling()', () => {
5781
it('should start polling', async () => {
5882
expect(() => bot.startPolling()).not.toThrow();
5983
await new Promise((resolve) => setTimeout(resolve, 5000));
60-
bot.stopPolling();
84+
await bot.stopPolling();
6185
});
6286
});
6387

@@ -134,12 +158,28 @@ describe('.logOut()', () => {
134158
it('should have a logOut method', () => {
135159
expect(bot.logOut).toBeDefined();
136160
});
161+
162+
it('should log out the bot', async () => {
163+
await expect(
164+
new TelegramBot({
165+
botToken: '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11',
166+
}).logOut(),
167+
).rejects.toThrow('401 Unauthorized');
168+
});
137169
});
138170

139171
describe('.close()', () => {
140172
it('should have a close method', () => {
141173
expect(bot.close).toBeDefined();
142174
});
175+
176+
it('should close the bot', async () => {
177+
await expect(
178+
new TelegramBot({
179+
botToken: '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11',
180+
}).close(),
181+
).rejects.toThrow('401 Unauthorized');
182+
});
143183
});
144184

145185
describe('.sendMessage()', () => {
@@ -1782,7 +1822,7 @@ describe('.answerInlineQuery()', () => {
17821822
},
17831823
],
17841824
}),
1785-
).rejects.toThrow('400 Bad Request: USER_BOT_INVALID');
1825+
).rejects.toThrow('400 Bad Request: query is too old and response timeout expired or query ID is invalid');
17861826
});
17871827
});
17881828

@@ -1945,6 +1985,7 @@ describe('.createNewStickerSet()', () => {
19451985
user_id: USERID,
19461986
name: `new_sticker_set_by_${me.username}`,
19471987
title: 'NewStickerSet',
1988+
sticker_type: 'regular',
19481989
stickers: [
19491990
{
19501991
emoji_list: ['🐶'],
@@ -2036,7 +2077,136 @@ describe('.addStickerToSet()', () => {
20362077
});
20372078
});
20382079

2039-
describe('.setStickerPositionInSet()', () => {
2080+
describe(`setStickerEmojiList(), setStickerKeywords(), setStickerSetTitle(),
2081+
setStickerSetThumbnail(), setStickerMaskPosition(), .replaceStickerInSet()`, () => {
2082+
let stickerSet = null as null | StickerSet;
2083+
let me = null as null | User;
2084+
2085+
beforeAll(async () => {
2086+
me = await bot.getMe();
2087+
await bot.addStickerToSet({
2088+
user_id: USERID,
2089+
name: `sticker_set_by_${me.username}`,
2090+
sticker: {
2091+
emoji_list: ['👻'],
2092+
sticker: createReadStream('tests/data/sticker.webp'),
2093+
format: 'static',
2094+
},
2095+
});
2096+
stickerSet = await bot.getStickerSet({
2097+
name: `sticker_set_by_${me.username}`,
2098+
});
2099+
});
2100+
2101+
it('should set sticker emoji list', async () => {
2102+
expect(stickerSet).not.toBeNull();
2103+
expect(me).not.toBeNull();
2104+
if (stickerSet && me) {
2105+
await expect(
2106+
bot.setStickerEmojiList({
2107+
sticker: stickerSet.stickers[0].file_id,
2108+
emoji_list: ['🐶', '🐕'],
2109+
}),
2110+
).resolves.toBe(true);
2111+
}
2112+
});
2113+
2114+
it('should set sticker keywords', async () => {
2115+
expect(stickerSet).not.toBeNull();
2116+
expect(me).not.toBeNull();
2117+
if (stickerSet && me) {
2118+
await expect(
2119+
bot.setStickerKeywords({
2120+
sticker: stickerSet.stickers[0].file_id,
2121+
keywords: ['dog', 'puppy'],
2122+
}),
2123+
).resolves.toBe(true);
2124+
}
2125+
});
2126+
2127+
it('should set sticker title', async () => {
2128+
expect(stickerSet).not.toBeNull();
2129+
expect(me).not.toBeNull();
2130+
if (stickerSet && me) {
2131+
await expect(
2132+
bot.setStickerSetTitle({
2133+
name: `sticker_set_by_${me.username}`,
2134+
title: 'Dog Sticker',
2135+
}),
2136+
).resolves.toBe(true);
2137+
}
2138+
});
2139+
2140+
it('should set sticker set thumbnail', async () => {
2141+
expect(stickerSet).not.toBeNull();
2142+
expect(me).not.toBeNull();
2143+
if (stickerSet && me) {
2144+
await expect(
2145+
bot.setStickerSetThumbnail({
2146+
name: `sticker_set_by_${me.username}`,
2147+
user_id: USERID,
2148+
thumbnail: createReadStream('tests/data/video_emoji.webm'),
2149+
format: 'video',
2150+
}),
2151+
).resolves.toBe(true);
2152+
}
2153+
});
2154+
2155+
it('should set sticker mask position', async () => {
2156+
expect(stickerSet).not.toBeNull();
2157+
expect(me).not.toBeNull();
2158+
if (stickerSet && me) {
2159+
await expect(
2160+
bot.setStickerMaskPosition({
2161+
sticker: stickerSet.stickers[0].file_id,
2162+
mask_position: {
2163+
point: 'eyes',
2164+
x_shift: 0.5,
2165+
y_shift: 0.5,
2166+
scale: 1,
2167+
},
2168+
}),
2169+
).rejects.toThrow('400 Bad Request: STICKER_MASK_COORDS_NOT_SUPPORTED');
2170+
}
2171+
});
2172+
2173+
it('should replace sticker in set', async () => {
2174+
expect(stickerSet).not.toBeNull();
2175+
expect(me).not.toBeNull();
2176+
if (stickerSet && me) {
2177+
await expect(
2178+
bot.replaceStickerInSet({
2179+
old_sticker: stickerSet.stickers[0].file_id,
2180+
user_id: USERID,
2181+
name: `sticker_set_by_${me.username}`,
2182+
sticker: {
2183+
emoji_list: ['🐶'],
2184+
sticker: createReadStream('tests/data/sticker.webp'),
2185+
format: 'static',
2186+
},
2187+
}),
2188+
).resolves.toBe(true);
2189+
}
2190+
});
2191+
2192+
afterAll(async () => {
2193+
if (me) {
2194+
const stickerSet = await bot.getStickerSet({
2195+
name: `sticker_set_by_${me.username}`,
2196+
});
2197+
2198+
await Promise.all(
2199+
stickerSet.stickers.map(async (sticker) => {
2200+
await bot.deleteStickerFromSet({
2201+
sticker: sticker.file_id,
2202+
});
2203+
}),
2204+
);
2205+
}
2206+
});
2207+
});
2208+
2209+
describe('.setStickerPositionInSet(), setCustomEmojiStickerSetThumbnail()', () => {
20402210
let me = null as null | User;
20412211
let stickerSet = null as null | StickerSet;
20422212

@@ -2079,6 +2249,19 @@ describe('.setStickerPositionInSet()', () => {
20792249
}
20802250
});
20812251

2252+
it('should set custom emoji sticker set thumbnail', async () => {
2253+
expect(stickerSet).not.toBeNull();
2254+
expect(me).not.toBeNull();
2255+
if (stickerSet && me) {
2256+
await expect(
2257+
bot.setCustomEmojiStickerSetThumbnail({
2258+
name: `video_emoji_by_${me.username}`,
2259+
custom_emoji_id: '',
2260+
}),
2261+
).resolves.toBe(true);
2262+
}
2263+
});
2264+
20822265
afterAll(async () => {
20832266
if (me) {
20842267
await bot.deleteStickerSet({
@@ -2087,3 +2270,107 @@ describe('.setStickerPositionInSet()', () => {
20872270
}
20882271
});
20892272
});
2273+
2274+
describe('.sendGame()', () => {
2275+
it('should send game', async () => {
2276+
await expect(
2277+
bot.sendGame({
2278+
chat_id: USERID,
2279+
game_short_name: 'game',
2280+
}),
2281+
).resolves.toHaveProperty('game.title', 'game');
2282+
});
2283+
});
2284+
2285+
describe('.setGameScore()', () => {
2286+
let message_id = null as null | number;
2287+
2288+
beforeAll(async () => {
2289+
const message = await bot.sendGame({
2290+
chat_id: USERID,
2291+
game_short_name: 'game',
2292+
});
2293+
message_id = message.message_id;
2294+
});
2295+
2296+
it('should set game score', async () => {
2297+
expect(message_id).not.toBeNull();
2298+
if (message_id) {
2299+
await expect(
2300+
bot.setGameScore({
2301+
user_id: USERID,
2302+
score: Math.floor(Math.random() * 10000),
2303+
chat_id: USERID,
2304+
message_id: message_id,
2305+
force: true,
2306+
}),
2307+
).resolves.toHaveProperty('game.title', 'game');
2308+
}
2309+
});
2310+
});
2311+
2312+
describe('.setGameScore()', () => {
2313+
let message_id = null as null | number;
2314+
beforeAll(async () => {
2315+
const message = await bot.sendGame({
2316+
chat_id: USERID,
2317+
game_short_name: 'game',
2318+
});
2319+
message_id = message.message_id;
2320+
});
2321+
2322+
it('should set game score', async () => {
2323+
expect(message_id).not.toBeNull();
2324+
await expect(
2325+
bot.setGameScore({
2326+
user_id: USERID,
2327+
score: Math.floor(Math.random() * 10000),
2328+
chat_id: USERID,
2329+
message_id: message_id as number,
2330+
force: true,
2331+
}),
2332+
).resolves.toHaveProperty('game.title', 'game');
2333+
});
2334+
});
2335+
2336+
describe('.getGameHighScores()', () => {
2337+
let message_id = null as null | number;
2338+
2339+
beforeAll(async () => {
2340+
const message = await bot.sendGame({
2341+
chat_id: USERID,
2342+
game_short_name: 'game',
2343+
});
2344+
message_id = message.message_id;
2345+
});
2346+
2347+
it('should get game high scores', async () => {
2348+
expect(message_id).not.toBeNull();
2349+
await expect(
2350+
bot.getGameHighScores({
2351+
user_id: USERID,
2352+
chat_id: USERID,
2353+
message_id: message_id as number,
2354+
}),
2355+
).resolves.toBeInstanceOf(Array);
2356+
});
2357+
});
2358+
2359+
describe('.setPassportDataErrors()', () => {
2360+
it('should set passport data errors', async () => {
2361+
await expect(
2362+
bot.setPassportDataErrors({
2363+
user_id: USERID,
2364+
errors: [
2365+
{
2366+
source: 'data',
2367+
type: 'passport',
2368+
field_name: 'name',
2369+
data_hash: '78EJbNOmoK3Axtg7yzA9cA==',
2370+
message: 'Error message',
2371+
},
2372+
],
2373+
}),
2374+
).rejects.toThrow('400 Bad Request: DATA_HASH_SIZE_INVALID');
2375+
});
2376+
});

0 commit comments

Comments
 (0)