spark-server/internal/module/admin/logic/map_test.go
2026-09-21 15:42:16 +08:00

64 lines
2.7 KiB
Go

package logic
import (
"testing"
"time"
adminmodel "spark-server/internal/module/admin/model"
msgentity "spark-server/internal/module/message/entity"
)
func TestAdminMessageHelpers(t *testing.T) {
if msgTypeName(msgentity.TypeText) != "text" || msgTypeName(msgentity.Type(99)) != "type_99" {
t.Fatal("type name")
}
if roleName(1) != "owner" || roleName(2) != "admin" || roleName(3) != "member" {
t.Fatal("role")
}
if reportReasonName(1) != "spam" || reportReasonName(2) != "harassment" || reportReasonName(3) != "fake" || reportReasonName(0) != "other" {
t.Fatal("reason name")
}
if parseReportReason("spam") != 1 || parseReportReason("骚扰") != 2 || parseReportReason("3") != 3 || parseReportReason("x") != 4 {
t.Fatal("parse reason")
}
mustPreview(t, msgentity.NewText("hi"), msgentity.TypeText, "hi")
mustPreview(t, msgentity.NewImage(&msgentity.Image{Key: "chat/20260917/1/99.jpg", Mime: "image/jpeg"}), msgentity.TypeImage, "[图片]")
mustPreview(t, msgentity.NewVoice(&msgentity.Voice{Key: "chat/20260917/1/a.m4a", DurationMS: 1000}), msgentity.TypeVoice, "[语音]")
mustPreview(t, msgentity.NewVideo(&msgentity.Video{Key: "chat/20260917/1/a.mp4"}), msgentity.TypeVideo, "[视频]")
mustPreview(t, msgentity.NewEmoji(&msgentity.Emoji{Pack: "default", ID: "smile"}), msgentity.TypeEmoji, "[表情]")
mustPreview(t, msgentity.NewGIF(&msgentity.GIF{Key: "chat/20260917/1/a.gif"}), msgentity.TypeGIF, "[GIF]")
mustPreview(t, msgentity.NewFile(&msgentity.File{Key: "chat/20260917/1/a.pdf", Name: "a.pdf", Size: 1}), msgentity.TypeFile, "[文件] a.pdf")
mustPreview(t, msgentity.NewLocation(&msgentity.Location{Lat: 1, Lng: 2, Name: "cafe"}), msgentity.TypeLocation, "[位置] cafe")
mustPreview(t, msgentity.NewSystem(&msgentity.System{Kind: "join", Body: "joined"}), msgentity.TypeSystem, "joined")
if previewText([]byte("not-json"), msgentity.TypeText) != "" {
t.Fatal("bad content")
}
v := messageView(adminmodel.MessageRow{
ID: 42, SenderID: 7, ReceiverID: 8, GroupID: 9, MessageType: int16(msgentity.TypeText),
Content: mustMarshal(t, msgentity.NewText("needle")), Status: 2, CreatedAt: time.Unix(1, 0).UTC(), ChatType: "private",
})
if !v.Revoked || !messageMatch(v, "needle") || !messageMatch(v, "42") || !messageMatch(v, "text") || !messageMatch(v, "7") {
t.Fatalf("%+v", v)
}
if messageMatch(v, "zzz") {
t.Fatal("no match")
}
}
func mustMarshal(t *testing.T, c *msgentity.Content) []byte {
t.Helper()
raw, err := c.Marshal()
if err != nil {
t.Fatal(err)
}
return raw
}
func mustPreview(t *testing.T, c *msgentity.Content, typ msgentity.Type, want string) {
t.Helper()
got := previewText(mustMarshal(t, c), typ)
if got != want {
t.Fatalf("preview %q want %q", got, want)
}
}