package gateway import ( "encoding/json" "testing" ) func TestMessageEventFromID(t *testing.T) { e := MessageEvent{SenderID: "bob"} if e.FromID() != "bob" { t.Errorf("FromID = %q", e.FromID()) } } func TestMessageEventMinimalText(t *testing.T) { e := MessageEvent{ Type: MsgText, Text: "hi", ChannelID: "c1", Platform: "web", SenderID: "u1", } data, err := json.Marshal(e) if err != nil { t.Fatal(err) } var decoded MessageEvent if err := json.Unmarshal(data, &decoded); err != nil { t.Fatal(err) } if decoded.Type != MsgText { t.Errorf("Type = %q", decoded.Type) } if decoded.Text != "hi" { t.Errorf("Text = %q", decoded.Text) } if decoded.Type.IsEphemeral() { t.Error("text should not be ephemeral") } } func TestMessageEventMediaAttachment(t *testing.T) { e := MessageEvent{ Type: MsgImage, Text: "look at this photo", ChannelID: "c1", Platform: "telegram", SenderID: "u1", ID: "msg-1", Media: []MediaAttachment{ { Type: "image", FileID: "file-abc", MIMEType: "image/png", FileSize: new(int64(102400)), Width: new(800), Height: new(600), }, }, } data, err := json.Marshal(e) if err != nil { t.Fatal(err) } var decoded MessageEvent if err := json.Unmarshal(data, &decoded); err != nil { t.Fatal(err) } if len(decoded.Media) != 1 { t.Fatalf("expected 1 media, got %d", len(decoded.Media)) } m := decoded.Media[0] if m.FileID != "file-abc" { t.Errorf("FileID = %q", m.FileID) } if m.Width == nil || *m.Width != 800 { t.Errorf("Width = %d", m.Width) } } func TestMessageEventReplyContext(t *testing.T) { e := MessageEvent{ Type: MsgText, Text: "yes", ChannelID: "c1", Platform: "discord", SenderID: "u2", ReplyToID: "msg-99", ThreadID: "thread-1", } if e.ReplyToID != "msg-99" { t.Errorf("ReplyToID = %q", e.ReplyToID) } if e.ThreadID != "thread-1" { t.Errorf("ThreadID = %q", e.ThreadID) } } func TestMessageEventReaction(t *testing.T) { e := MessageEvent{ Type: MsgReaction, ChannelID: "c1", Platform: "telegram", SenderID: "u1", Reaction: "👍", ReactedToID: "msg-5", } if !e.Type.IsEphemeral() { t.Error("reaction should be ephemeral") } if e.Reaction != "👍" { t.Errorf("Reaction = %q", e.Reaction) } } func TestMessageEventCallback(t *testing.T) { e := MessageEvent{ Type: MsgCallback, ChannelID: "c1", Platform: "telegram", SenderID: "u1", CallbackData: `{"action":"approve","id":42}`, } if !e.Type.IsRich() { t.Error("callback should be rich") } } func TestMessageEventEditDelete(t *testing.T) { e := MessageEvent{ Type: MsgEdited, ChannelID: "c1", Platform: "telegram", SenderID: "u1", ID: "msg-1", EditedFrom: "original text", Text: "updated text", } if e.EditedFrom != "original text" { t.Errorf("EditedFrom = %q", e.EditedFrom) } if e.Text != "updated text" { t.Errorf("Text = %q", e.Text) } del := MessageEvent{ Type: MsgDeleted, ChannelID: "c1", Platform: "telegram", SenderID: "u1", ID: "msg-1", } if !del.Type.IsEphemeral() { t.Error("deleted should be ephemeral") } } func TestMessageEventTimestamp(t *testing.T) { e := MessageEvent{ Type: MsgText, ChannelID: "c1", Platform: "telegram", SenderID: "u1", Timestamp: 1234567890000, } if e.Timestamp != 1234567890000 { t.Errorf("Timestamp = %d", e.Timestamp) } } func TestMessageEventRawPayload(t *testing.T) { e := MessageEvent{ Type: MsgSystem, ChannelID: "c1", Platform: "telegram", SenderID: "", Raw: map[string]any{ "action": "member_joined", "user": "newguy", }, } data, err := json.Marshal(e) if err != nil { t.Fatal(err) } var decoded MessageEvent if err := json.Unmarshal(data, &decoded); err != nil { t.Fatal(err) } if decoded.Raw["action"] != "member_joined" { t.Errorf("Raw[action] = %v", decoded.Raw["action"]) } } func TestMessageEventMediaAttachmentZeroValue(t *testing.T) { var m MediaAttachment if m.FileID != "" { t.Error("zero MediaAttachment should have empty FileID") } if m.Type != "" { t.Error("zero MediaAttachment should have empty Type") } } func TestMessageEventDocExample(t *testing.T) { // Exercise: full photo message as an adapter would construct it. e := MessageEvent{ Type: MsgImage, Text: "check out this screenshot", ID: "1734567890123", ChannelID: "-1001234567890", Platform: "telegram", SenderID: "987654321", SenderName: "Alice", ReplyToID: "1734567890000", Media: []MediaAttachment{{ Type: "image", FileID: "AgACAgIAAxkBAA...", MIMEType: "image/png", FileSize: new(int64(245760)), Width: new(1920), Height: new(1080), }}, Timestamp: 1734567890123, } if e.Type != MsgImage { t.Error("expected image type") } if !e.Type.IsMedia() { t.Error("image should be media") } if e.Type.IsEphemeral() { t.Error("image should not be ephemeral") } if e.SenderName != "Alice" { t.Errorf("SenderName = %q", e.SenderName) } }