Files
drive/tests/test_link_parser.py
T
2026-10-18 23:48:22 +05:30

67 lines
2.5 KiB
Python

import pytest
from link_parser import LinkError, internal_channel_id, marked_channel_id, parse_message_link
@pytest.mark.parametrize(
"url",
[
"https://t.me/c/3909337424/14",
"https://t.me/c/3909337424/14/",
"https://t.me/c/3909337424/14?single",
"https://www.t.me/c/3909337424/14",
"http://t.me/c/3909337424/14#frag",
"t.me/c/3909337424/14",
"https://telegram.me/c/3909337424/14",
"https://t.me/c/3909337424/7/14", # thread segment
" https://t.me/c/3909337424/14 ",
],
)
def test_private_links(url):
link = parse_message_link(url)
assert link.kind == "private"
assert link.channel == "3909337424"
assert link.message_id == 14
assert link.channel_ref == "-1003909337424"
@pytest.mark.parametrize("url", ["https://t.me/my_channel/14", "https://t.me/s/my_channel/14", "t.me/My_Channel/14?single"])
def test_public_links(url):
link = parse_message_link(url)
assert link.kind == "public" and link.message_id == 14
assert link.channel.lower() == "my_channel"
assert link.channel_ref.lower() == "my_channel"
@pytest.mark.parametrize(
"text,fragment",
[
("", "send a Telegram message link"),
("hello", "not a Telegram"),
("https://example.com/c/1/2", "not a Telegram"),
("https://t.me/c/3909337424", "must look like"),
("https://t.me/c/abc/14", "must look like"),
("https://t.me/c/3909337424/0", "positive"),
("https://t.me/c/3909337424/x14", "must look like"),
("https://t.me/+AbCdEf123", "Invite links"),
("https://t.me/joinchat/AbCdEf", "Invite links"),
("https://t.me/my_channel", "not to a message"),
("https://t.me/addstickers/pack/1", "Unsupported"),
("https://t.me/ab/14", "Unsupported"),
("ftp://t.me/c/1/2", "not a Telegram"),
("https://t.me/c/3909337424/14 https://t.me/c/3909337424/15", "only the link"),
],
)
def test_rejected_links(text, fragment):
with pytest.raises(LinkError) as err:
parse_message_link(text)
assert fragment.lower() in str(err.value).lower()
def test_marked_id_follows_telegram_convention():
# -100 prefix is only a coincidence of 10-digit IDs; the real rule is -(10**12 + id)
assert marked_channel_id(3909337424) == -1003909337424
assert marked_channel_id(12345) == -1000000012345
assert internal_channel_id(-1000000012345) == 12345
assert parse_message_link("https://t.me/c/12345/9").channel_ref == "-1000000012345"