telegabber/telegram/utils_test.go

523 lines
12 KiB
Go
Raw Permalink 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) {
2019-12-11 22:48:35 +00:00
show, status := (&Client{}).userStatusToText(client.UserStatus(&client.UserStatusOnline{}))
2019-11-30 00:41:22 +00:00
if show != "" || status != "Online" {
t.Errorf("Wrong online status: %v, %v", show, status)
}
}
func TestOnlineRecently(t *testing.T) {
2019-12-11 22:48:35 +00:00
show, status := (&Client{}).userStatusToText(client.UserStatus(&client.UserStatusRecently{}))
2019-11-30 00:41:22 +00:00
if show != "dnd" || status != "Last seen recently" {
t.Errorf("Wrong recently status: %v, %v", show, status)
}
}
func TestOnlineOfflineAway(t *testing.T) {
2019-12-11 22:48:35 +00:00
location, _ := time.LoadLocation("Europe/Berlin")
timestamp := time.Now().In(location).Unix() - 3599
tm := time.Unix(timestamp, 0).In(location)
c := &Client{
Session: &persistence.Session{
Timezone: "+01:00",
},
}
show, status := c.userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}))
trueStatus := "Last seen at " + tm.Format(testTimeFormat)
if show != "away" || status != trueStatus {
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{},
}
show, status := c.userStatusToText(client.UserStatus(&client.UserStatusOffline{WasOnline: int32(timestamp)}))
trueStatus := "Last seen at " + tm.Format(testTimeFormat)
if show != "xa" || status != trueStatus {
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)
}
}
func TestFormatContent(t *testing.T) {
file := client.File{
Size: 23899,
Remote: &client.RemoteFile{
Id: "tist",
},
}
c := Client{
content: &config.TelegramContentConfig{
Link: "localhvost",
},
}
content := c.formatContent(&file, "a.jpg")
if content != "a.jpg (23 kbytes) | localhvost/b0896d9e9f1de7d2af59b080c3f0947b838c5c6c64f71c68a4b690a15de2ccf8.jpg" {
t.Errorf("Wrong file label: %v", content)
}
}
func TestMessageToTextSticker(t *testing.T) {
sticker := client.Message{
Content: &client.MessageSticker{
Sticker: &client.Sticker{
Emoji: "💩",
},
},
}
text := (&Client{}).messageToText(&sticker)
if text != "💩" {
t.Errorf("Not poop")
}
}
func TestMessageToTextGroup(t *testing.T) {
group := client.Message{
Content: &client.MessageBasicGroupChatCreate{},
}
text := (&Client{}).messageToText(&group)
if text != "has created chat" {
t.Errorf("Who created the group?")
}
}
func TestMessageToTextSupergroup(t *testing.T) {
supergroup := client.Message{
Content: &client.MessageSupergroupChatCreate{},
}
text := (&Client{}).messageToText(&supergroup)
if text != "has created chat" {
t.Errorf("Who created the supergroup?")
}
}
func TestMessageChatJoin(t *testing.T) {
join := client.Message{
Content: &client.MessageChatJoinByLink{},
}
text := (&Client{}).messageToText(&join)
if text != "joined chat via invite link" {
t.Errorf("Non-joined")
}
}
func TestMessageChatAddNoMembers(t *testing.T) {
add := client.Message{
Content: &client.MessageChatAddMembers{},
}
text := (&Client{}).messageToText(&add)
if text != "invited " {
t.Errorf("Invited someone anyway")
}
}
func TestMessageChatChangeTitle(t *testing.T) {
title := client.Message{
Content: &client.MessageChatChangeTitle{
Title: "Anime",
},
}
text := (&Client{}).messageToText(&title)
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,
},
},
}
text := (&Client{}).messageToText(&location)
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",
},
},
}
text := (&Client{}).messageToText(&photo)
if text != "tist" {
t.Errorf("Wrong photo label")
}
}
func TestMessageAudio(t *testing.T) {
audio := client.Message{
Content: &client.MessageAudio{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).messageToText(&audio)
if text != "tist" {
t.Errorf("Wrong audio label")
}
}
func TestMessageVideo(t *testing.T) {
video := client.Message{
Content: &client.MessageVideo{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).messageToText(&video)
if text != "tist" {
t.Errorf("Wrong video label")
}
}
func TestMessageDocument(t *testing.T) {
document := client.Message{
Content: &client.MessageDocument{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).messageToText(&document)
if text != "tist" {
t.Errorf("Wrong document label")
}
}
func TestMessageText(t *testing.T) {
textMessage := client.Message{
Content: &client.MessageText{
Text: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).messageToText(&textMessage)
if text != "tist" {
t.Errorf("Wrong text message")
}
}
func TestMessageVoice(t *testing.T) {
voice := client.Message{
Content: &client.MessageVoiceNote{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).messageToText(&voice)
if text != "tist" {
t.Errorf("Wrong voice label")
}
}
func TestMessageVideoNote(t *testing.T) {
videoNote := client.Message{
Content: &client.MessageVideoNote{},
}
text := (&Client{}).messageToText(&videoNote)
if text != "" {
t.Errorf("Wrong video note label")
}
}
func TestMessageAnimation(t *testing.T) {
animation := client.Message{
Content: &client.MessageAnimation{
Caption: &client.FormattedText{
Text: "tist",
},
},
}
text := (&Client{}).messageToText(&animation)
if text != "tist" {
t.Errorf("Wrong animation label")
}
}
func TestMessageUnknown(t *testing.T) {
unknown := client.Message{
Content: &client.MessageExpiredPhoto{},
}
text := (&Client{}).messageToText(&unknown)
if text != "unknown message (messageExpiredPhoto)" {
t.Errorf("Wrong label for unknown message")
}
}
func TestContentToFilenameSticker(t *testing.T) {
sticker := client.MessageSticker{
Sticker: &client.Sticker{},
}
_, filename := (&Client{}).contentToFilename(&sticker)
if filename != "sticker.webp" {
t.Errorf("Wrong sticker filename")
}
}
func TestContentToFilenameVoice(t *testing.T) {
voice := client.MessageVoiceNote{
VoiceNote: &client.VoiceNote{
Duration: 56,
},
}
_, filename := (&Client{}).contentToFilename(&voice)
if filename != "voice note (56 s.).oga" {
t.Errorf("Wrong voice note filename")
}
}
func TestContentToFilenameVideoNote(t *testing.T) {
video := client.MessageVideoNote{
VideoNote: &client.VideoNote{
Duration: 56,
},
}
_, filename := (&Client{}).contentToFilename(&video)
if filename != "video note (56 s.).mp4" {
t.Errorf("Wrong video note filename")
}
}
func TestContentToFilenameAnimation(t *testing.T) {
animation := client.MessageAnimation{
Animation: &client.Animation{},
}
_, filename := (&Client{}).contentToFilename(&animation)
if filename != "animation.mp4" {
t.Errorf("Wrong animation filename")
}
}
func TestContentToFilenamePhoto(t *testing.T) {
photo := client.MessagePhoto{
Photo: &client.Photo{
Sizes: []*client.PhotoSize{
&client.PhotoSize{
Photo: &client.File{},
},
&client.PhotoSize{
Photo: &client.File{
Id: 56,
},
},
},
},
}
_, filename := (&Client{}).contentToFilename(&photo)
if filename != "56.jpg" {
t.Errorf("Wrong photo filename")
}
}
func TestContentToFilenamePhotoNoSizes(t *testing.T) {
photo := client.MessagePhoto{
Photo: &client.Photo{
Sizes: []*client.PhotoSize{},
},
}
_, filename := (&Client{}).contentToFilename(&photo)
if filename != "" {
t.Errorf("Wrong filename of sizeless photo")
}
}
func TestContentToFilenameAudio(t *testing.T) {
audio := client.MessageAudio{
Audio: &client.Audio{
FileName: "swine.mp3",
},
}
_, filename := (&Client{}).contentToFilename(&audio)
if filename != "swine.mp3" {
t.Errorf("Not oinking, shame on you!")
}
}
func TestContentToFilenameVideo(t *testing.T) {
video := client.MessageVideo{
Video: &client.Video{
FileName: "swine.3gp",
},
}
_, filename := (&Client{}).contentToFilename(&video)
if filename != "swine.3gp" {
t.Errorf("Not pigdancing, shame on you!")
}
}
func TestContentToFilenameDocument(t *testing.T) {
document := client.MessageDocument{
Document: &client.Document{
FileName: "swine.doc",
},
}
_, filename := (&Client{}).contentToFilename(&document)
if filename != "swine.doc" {
t.Errorf("Not hoofstomping, shame on you!")
}
}
func TestContentToFilenameUnknown(t *testing.T) {
unknown := client.MessageExpiredPhoto{}
_, filename := (&Client{}).contentToFilename(&unknown)
if filename != "" {
t.Errorf("Wrong filename of unknown content")
}
}
func TestMessageToPrefix1(t *testing.T) {
message := client.Message{
Id: 42,
IsOutgoing: true,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginHiddenUser{
SenderName: "ziz",
},
},
}
prefix := (&Client{}).messageToPrefix(&message, "")
if prefix != "➡ 42 | fwd: anonymous (ziz)" {
t.Errorf("Wrong prefix: %v", prefix)
}
}
func TestMessageToPrefix2(t *testing.T) {
message := client.Message{
Id: 56,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginChannel{
AuthorSignature: "zaz",
},
},
}
prefix := (&Client{}).messageToPrefix(&message, "")
if prefix != "⬅ 56 | fwd: (zaz)" {
t.Errorf("Wrong prefix: %v", prefix)
}
}
func TestMessageToPrefix3(t *testing.T) {
message := client.Message{
Id: 56,
ForwardInfo: &client.MessageForwardInfo{
Origin: &client.MessageForwardOriginChannel{},
},
}
prefix := (&Client{}).messageToPrefix(&message, "a.jpg")
if prefix != "⬅ 56 | fwd: | file: a.jpg" {
t.Errorf("Wrong prefix: %v", prefix)
}
}
func TestMessageToPrefix4(t *testing.T) {
message := client.Message{
Id: 23,
IsOutgoing: true,
}
prefix := (&Client{}).messageToPrefix(&message, "")
if prefix != "➡ 23" {
t.Errorf("Wrong prefix: %v", prefix)
2019-11-30 00:41:22 +00:00
}
}