-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_test.ts
111 lines (86 loc) · 3.16 KB
/
utils_test.ts
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
import { assertEquals, assertNotEquals } from "@std/assert";
import { MessageEntity } from "grammy/types.ts";
import {
escape,
fixText,
fixTrans,
hasButton,
isZh,
removeButton,
replaceButton,
unparse,
} from "./utils.ts";
Deno.test("removeButton", () => {
const replyMarkup = {
inline_keyboard: [[{ text: "x", callback_data: "y" }]],
};
removeButton(replyMarkup, "y");
assertEquals(replyMarkup.inline_keyboard[0], undefined);
});
Deno.test("replaceButton", () => {
const replyMarkup = {
inline_keyboard: [[{ text: "x", callback_data: "y" }]],
};
replaceButton(replyMarkup, "y", () => "y", "x");
assertEquals(replyMarkup.inline_keyboard[0][0].text, "y");
assertEquals(replyMarkup.inline_keyboard[0][0].callback_data, "x");
});
Deno.test("hasButton", () => {
const replyMarkup = {
inline_keyboard: [[{ text: "x", callback_data: "y" }]],
};
assertEquals(hasButton(replyMarkup, "y"), true);
assertNotEquals(hasButton(replyMarkup, "x"), true);
});
Deno.test("escape", () => {
const escaped = escape('<a href="https://tginfo.me">Telegram Info</a>');
assertEquals(
escaped,
"<a href="https://tginfo.me">Telegram Info</a>",
);
});
Deno.test("unparse", () => {
const text =
`Pavel Durov is the luckiest Russian ever according to this a>ticle.
— sendMessage and editMessageText are the most used Bot API methods.
"Probably soon, but not today." — said Telegram on Twitter.
This text is underlined and a spoiler!`;
const entities: MessageEntity[] = [
{ type: "bold", offset: 0, length: 11 },
{
type: "text_link",
url: "http://telegra.ph/?fb_cli_id=%15%12",
offset: 54,
length: 12,
},
{ type: "code", offset: 73, length: 11 },
{ type: "code", offset: 89, length: 15 },
{ type: "italic", offset: 141, length: 31 },
{ type: "spoiler", offset: 202, length: 38 },
{ type: "underline", offset: 202, length: 38 },
];
const html = unparse(text, entities);
assertEquals(
html,
`<b>Pavel Durov</b> is the luckiest Russian ever according to <a href="http://telegra.ph/?fb_cli_id=%15%12">this a>ticle</a>.
— <code>sendMessage</code> and <code>editMessageText</code> are the most used Bot API methods.
<i>"Probably soon, but not today."</i> — said Telegram on Twitter.
<span class="tg-spoiler"><u>This text is underlined and a spoiler!</u></span>`,
);
});
Deno.test("fixTrans", () => {
const trans =
`< b>Lê em ji wan re gotin:< / b> < i >" Ma hûn nizanîbû ku Telegramê zêdetir rêzê li bikarînerên xwe digire? < a href = "https://telegram.org" >Herin aniha dakişînin!< /a>"</i >微信`;
const result = fixTrans(trans, true);
const expected =
`<b>Lê em ji wan re gotin:</b> <i>" Ma hûn nizanîbû ku Telegramê zêdetir rêzê li bikarînerên xwe digire? <a href="https://telegram.org">Herin aniha dakişînin!</a>"</i>Telegram`;
assertEquals(result, expected);
});
Deno.test("fixText", () => {
assertEquals(fixText("TelegRam", true), "WeChat");
});
Deno.test("isZh", () => {
assertEquals(isZh("zh"), true);
assertEquals(isZh("zh-CN"), true);
assertEquals(isZh("zh-HK"), true);
});