telegabber/telegram/utils_test.go

541 lines
13 KiB
Go
Raw Normal View History

2019-11-30 00:41:22 +00:00
package telegram
import (
"testing"
"time"
"dev.narayana.im/narayana/telegabber/config"
2019-12-11 22:48:35 +00:00
"dev.narayana.im/narayana/telegabber/persistence"
2022-01-17 20:45:40 +00:00
"github.com/zelenin/go-tdlib/client"
2019-11-30 00:41:22 +00:00
)
const testTimeFormat string = "15:04 02/01/2006"
2019-11-30 00:41:22 +00:00
func TestOnlineStatus(t *testing.T) {
2022-02-09 16:32:55 +00:00
c := Client{
DelayedStatuses: make(map[int64]*DelayedStatus),
}
show, status, presenceType := (&c).userStatusToText(client.UserStatus(&client.UserStatusOnline{}), 0)
if show != "" || status != "Online" || presenceType != "" {
2019-11-30 00:41:22 +00:00
t.Errorf("Wrong online status: %v, %v", show, status)
}
}
func TestOnlineRecently(t *testing.T) {
2022-02-09 16:32:55 +00:00
show, status, presenceType := (&Client{}).userStatusToText(client.UserStatus(&client.UserStatusRecently{}), 0)
if show != "dnd" || status != "Last seen recently" || presenceType != "" {
2019-11-30 00:41:22 +00:00
t.Errorf("Wrong recently status: %v, %v", show, status)
}
}
func TestOnlineOfflineAway(t *testing.T) {
2022-04-01 16:29:55 +00:00
location := time.FixedZone("UTC+1", 3600)
2019-12-11 22:48:35 +00:00
timestamp := time.Now().In(location).Unix() - 3599
tm := time.Unix(timestamp, 0).In(location)
c := &Client{
Session: &persistence.Session{
Timezone: "+01:00",
},
}
2022-02-09 16:32:55 +00:00
show, status, presenceType := c.userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}), 0)
2019-12-11 22:48:35 +00:00
trueStatus := "Last seen at " + tm.Format(testTimeFormat)
2022-02-09 16:32:55 +00:00
if show != "away" || status != trueStatus || presenceType != "" {
t.Errorf("Wrong away status: %v, %v, should be %v", show, status, trueStatus)
2019-11-30 00:41:22 +00:00
}
}
func TestOnlineOfflineXa(t *testing.T) {
timestamp := time.Now().Unix() - 3601
2019-12-11 22:48:35 +00:00
tm := time.Unix(timestamp, 0).UTC()
c := &Client{
Session: &persistence.Session{},
}
2022-02-09 16:32:55 +00:00
show, status, presenceType := c.userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}), 0)
2019-12-11 22:48:35 +00:00
trueStatus := "Last seen at " + tm.Format(testTimeFormat)
2022-02-09 16:32:55 +00:00
if show != "xa" || status != trueStatus || presenceType != "" {
t.Errorf("Wrong xa status: %v, %v, should be %v", show, status, trueStatus)
}
}
func TestFormatMessageOneline(t *testing.T) {
message := client.Message{
Id: 42,
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).formatMessage(0, 0, true, &message)
if text != "42 | | tist" {
t.Errorf("Wrong oneline message formatting: %v", text)
}
}
func TestFormatMessageMultiline(t *testing.T) {
message := client.Message{
Id: 42,
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist\nziz",
},
},
}
text := (&Client{}).formatMessage(0, 0, true, &message)
if text != "42 | | tist" {
t.Errorf("Wrong multiline message formatting: %v", text)
}
}
func TestFormatMessageOnelinePreview(t *testing.T) {
message := client.Message{
Id: 42,
Date: 1200000000,
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist",
},
},
}
2019-12-11 22:48:35 +00:00
c := &Client{
Session: &persistence.Session{},
}
text := c.formatMessage(0, 0, false, &message)
if text != "42 | | 10 Jan 2008 21:20:00 | tist" {
t.Errorf("Wrong oneline preview message formatting: %v", text)
}
}
func TestFormatMessageMultilinePreview(t *testing.T) {
message := client.Message{
Id: 42,
Date: 1200000000,
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist\nziz",
},
},
}
2019-12-11 22:48:35 +00:00
c := &Client{
Session: &persistence.Session{},
}
text := c.formatMessage(0, 0, false, &message)
if text != "42 | | 10 Jan 2008 21:20:00 | tist\nziz" {
t.Errorf("Wrong multiline preview message formatting: %v", text)
}
}
2022-03-14 21:57:03 +00:00
func TestFormatFile(t *testing.T) {
file := client.File{
Size: 23899,
2022-02-09 16:32:55 +00:00
Local: &client.LocalFile{
Path: "c:/Documents and Settings/blabla.jpg",
},
Remote: &client.RemoteFile{
2022-02-09 16:32:55 +00:00
UniqueId: "tist",
},
}
c := Client{
content: &config.TelegramContentConfig{
Link: "localhvost",
},
}
2023-01-16 01:35:13 +00:00
content, link := c.formatFile(&file, false)
if content != ". (23 kbytes) | " {
t.Errorf("Wrong file label: %v", content)
}
2023-01-16 01:35:13 +00:00
if link != "" {
t.Errorf("Wrong file link: %v", link)
}
}
2022-03-14 21:57:03 +00:00
func TestFormatPreview(t *testing.T) {
file := client.File{
Size: 23899,
Local: &client.LocalFile{
Path: "c:/pron/smokovnica_vhsrip.mov",
},
Remote: &client.RemoteFile{
UniqueId: "aZaZaZ",
},
}
c := Client{
content: &config.TelegramContentConfig{
Link: "localhvost",
},
}
2023-01-16 01:35:13 +00:00
content, link := c.formatFile(&file, true)
2022-03-14 21:57:03 +00:00
if content != "" {
t.Errorf("Wrong preview label: %v", content)
}
2023-01-16 01:35:13 +00:00
if link != "" {
t.Errorf("Wrong preview link: %v", link)
}
2022-03-14 21:57:03 +00:00
}
func TestMessageToTextSticker(t *testing.T) {
sticker := client.Message{
Content: &client.MessageSticker{
Sticker: &client.Sticker{
Emoji: "💩",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&sticker, false)
if text != "💩" {
t.Errorf("Not poop")
}
}
func TestMessageToTextGroup(t *testing.T) {
group := client.Message{
Content: &client.MessageBasicGroupChatCreate{},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&group, false)
if text != "has created chat" {
t.Errorf("Who created the group?")
}
}
func TestMessageToTextSupergroup(t *testing.T) {
supergroup := client.Message{
Content: &client.MessageSupergroupChatCreate{},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&supergroup, false)
if text != "has created chat" {
t.Errorf("Who created the supergroup?")
}
}
func TestMessageChatJoin(t *testing.T) {
join := client.Message{
Content: &client.MessageChatJoinByLink{},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&join, false)
if text != "joined chat via invite link" {
t.Errorf("Non-joined")
}
}
func TestMessageChatAddNoMembers(t *testing.T) {
add := client.Message{
Content: &client.MessageChatAddMembers{},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&add, false)
if text != "invited " {
t.Errorf("Invited someone anyway")
}
}
func TestMessageChatChangeTitle(t *testing.T) {
title := client.Message{
Content: &client.MessageChatChangeTitle{
Title: "Anime",
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&title, false)
if text != "chat title set to: Anime" {
t.Errorf("How to patch KDE2 for FreeBSD?")
}
}
func TestMessageLocation(t *testing.T) {
location := client.Message{
Content: &client.MessageLocation{
Location: &client.Location{
Latitude: 50.8,
Longitude: 42.0167,
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&location, false)
if text != "coordinates: 50.8,42.0167 | https://www.google.com/maps/search/50.8,42.0167/" {
t.Errorf("Excuse me, I'm lost")
}
}
func TestMessagePhoto(t *testing.T) {
photo := client.Message{
Content: &client.MessagePhoto{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&photo, false)
if text != "tist" {
t.Errorf("Wrong photo label")
}
}
func TestMessageAudio(t *testing.T) {
audio := client.Message{
Content: &client.MessageAudio{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&audio, false)
if text != "tist" {
t.Errorf("Wrong audio label")
}
}
func TestMessageVideo(t *testing.T) {
video := client.Message{
Content: &client.MessageVideo{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&video, false)
if text != "tist" {
t.Errorf("Wrong video label")
}
}
func TestMessageDocument(t *testing.T) {
document := client.Message{
Content: &client.MessageDocument{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&document, false)
if text != "tist" {
t.Errorf("Wrong document label")
}
}
func TestMessageText(t *testing.T) {
textMessage := client.Message{
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&textMessage, false)
if text != "tist" {
t.Errorf("Wrong text message")
}
}
func TestMessageVoice(t *testing.T) {
voice := client.Message{
Content: &client.MessageVoiceNote{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&voice, false)
if text != "tist" {
t.Errorf("Wrong voice label")
}
}
func TestMessageVideoNote(t *testing.T) {
videoNote := client.Message{
Content: &client.MessageVideoNote{},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&videoNote, false)
if text != "" {
t.Errorf("Wrong video note label")
}
}
func TestMessageAnimation(t *testing.T) {
animation := client.Message{
Content: &client.MessageAnimation{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&animation, false)
if text != "tist" {
t.Errorf("Wrong animation label")
}
}
func TestMessageUnknown(t *testing.T) {
unknown := client.Message{
Content: &client.MessageExpiredPhoto{},
}
2022-02-09 16:32:55 +00:00
text := (&Client{}).messageToText(&unknown, false)
if text != "unknown message (messageExpiredPhoto)" {
t.Errorf("Wrong label for unknown message")
}
}
func TestMessageToPrefix1(t *testing.T) {
message := client.Message{
Id: 42,
IsOutgoing: true,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginHiddenUser{
SenderName: "ziz",
},
},
}
2023-03-08 13:39:43 +00:00
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "", "", nil)
2022-02-09 16:32:55 +00:00
if prefix != "➡ 42 | fwd: ziz" {
t.Errorf("Wrong prefix: %v", prefix)
}
2023-03-08 13:39:43 +00:00
if replyStart != 0 {
t.Errorf("Wrong replyStart: %v", replyStart)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
}
}
func TestMessageToPrefix2(t *testing.T) {
message := client.Message{
Id: 56,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginChannel{
AuthorSignature: "zaz",
},
},
}
2023-03-08 13:39:43 +00:00
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{}}).messageToPrefix(&message, "y.jpg", "", nil)
2022-03-14 21:57:03 +00:00
if prefix != "⬅ 56 | fwd: (zaz) | preview: y.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
2023-03-08 13:39:43 +00:00
if replyStart != 0 {
t.Errorf("Wrong replyStart: %v", replyStart)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
}
}
func TestMessageToPrefix3(t *testing.T) {
message := client.Message{
Id: 56,
ForwardInfo: &client.MessageForwardInfo{
2022-03-14 21:57:03 +00:00
Origin: &client.MessageForwardOriginChannel{
AuthorSignature: "zuz",
},
},
}
2023-03-08 13:39:43 +00:00
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "a.jpg", nil)
2022-03-14 21:57:03 +00:00
if prefix != "< 56 | fwd: (zuz) | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
2023-03-08 13:39:43 +00:00
if replyStart != 0 {
t.Errorf("Wrong replyStart: %v", replyStart)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
}
}
func TestMessageToPrefix4(t *testing.T) {
message := client.Message{
Id: 23,
IsOutgoing: true,
}
2023-03-08 13:39:43 +00:00
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", nil)
if prefix != "> 23" {
t.Errorf("Wrong prefix: %v", prefix)
2019-11-30 00:41:22 +00:00
}
2023-03-08 13:39:43 +00:00
if replyStart != 0 {
t.Errorf("Wrong replyStart: %v", replyStart)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
}
2019-11-30 00:41:22 +00:00
}
2022-03-14 21:57:03 +00:00
func TestMessageToPrefix5(t *testing.T) {
message := client.Message{
Id: 560,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginChat{
AuthorSignature: "zyz",
},
},
}
2023-03-08 13:39:43 +00:00
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "h.jpg", "a.jpg", nil)
2022-03-14 21:57:03 +00:00
if prefix != "< 560 | fwd: (zyz) | preview: h.jpg | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
2023-03-08 13:39:43 +00:00
if replyStart != 0 {
t.Errorf("Wrong replyStart: %v", replyStart)
}
if replyEnd != 0 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
}
2022-03-14 21:57:03 +00:00
}
2023-03-05 08:00:53 +00:00
func TestMessageToPrefix6(t *testing.T) {
message := client.Message{
Id: 23,
IsOutgoing: true,
ReplyToMessageId: 42,
}
reply := client.Message{
Id: 42,
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist",
},
},
}
2023-03-08 13:39:43 +00:00
prefix, replyStart, replyEnd := (&Client{Session: &persistence.Session{AsciiArrows: true}}).messageToPrefix(&message, "", "", &reply)
2023-03-05 08:00:53 +00:00
if prefix != "> 23 | reply: 42 | | tist" {
t.Errorf("Wrong prefix: %v", prefix)
}
2023-03-08 13:39:43 +00:00
if replyStart != 4 {
t.Errorf("Wrong replyStart: %v", replyStart)
}
if replyEnd != 26 {
t.Errorf("Wrong replyEnd: %v", replyEnd)
}
2023-03-05 08:00:53 +00:00
}
func GetSenderIdEmpty(t *testing.T) {
message := client.Message{}
senderId := (&Client{}).getSenderId(&message)
if senderId != 0 {
t.Errorf("Wrong sender id: %v", senderId)
}
}
func GetSenderIdUser(t *testing.T) {
message := client.Message{
SenderId: &client.MessageSenderUser{
UserId: 42,
},
}
senderId := (&Client{}).getSenderId(&message)
if senderId != 42 {
t.Errorf("Wrong sender id: %v", senderId)
}
}
func GetSenderIdChat(t *testing.T) {
message := client.Message{
SenderId: &client.MessageSenderChat{
ChatId: -42,
},
}
senderId := (&Client{}).getSenderId(&message)
if senderId != -42 {
t.Errorf("Wrong sender id: %v", senderId)
}
}