From 97adf57cc01ece6ef04360ba2b790199dad83bd2 Mon Sep 17 00:00:00 2001 From: Alix-007 <267018309+Alix-007@users.noreply.github.com> Date: Tue, 31 Mar 2026 06:03:51 +0800 Subject: [PATCH] test(telegram): dedupe reply-context handleMessage tests --- pkg/channels/telegram/telegram_test.go | 131 +++++++------------------ 1 file changed, 36 insertions(+), 95 deletions(-) diff --git a/pkg/channels/telegram/telegram_test.go b/pkg/channels/telegram/telegram_test.go index 42ca109b3..7ea2f7305 100644 --- a/pkg/channels/telegram/telegram_test.go +++ b/pkg/channels/telegram/telegram_test.go @@ -674,7 +674,14 @@ func TestHandleMessage_EmptyContent_Ignored(t *testing.T) { } } -func TestHandleMessage_ReplyCarriesMetadataAndQuotedContext(t *testing.T) { +func handleReplyMessageAndGetInbound( + t *testing.T, + userText string, + messageID int, + replyToMessage *telego.Message, +) bus.InboundMessage { + t.Helper() + messageBus := bus.NewMessageBus() ch := &TelegramChannel{ BaseChannel: channels.NewBaseChannel("telegram", nil, messageBus, nil), @@ -683,8 +690,8 @@ func TestHandleMessage_ReplyCarriesMetadataAndQuotedContext(t *testing.T) { } msg := &telego.Message{ - Text: "what do you think?", - MessageID: 21, + Text: userText, + MessageID: messageID, Chat: telego.Chat{ ID: 12345, Type: "private", @@ -693,13 +700,7 @@ func TestHandleMessage_ReplyCarriesMetadataAndQuotedContext(t *testing.T) { ID: 10, FirstName: "Bob", }, - ReplyToMessage: &telego.Message{ - MessageID: 99, - Text: "this is the old message", - From: &telego.User{ - Username: "alice", - }, - }, + ReplyToMessage: replyToMessage, } err := ch.handleMessage(context.Background(), msg) @@ -707,6 +708,18 @@ func TestHandleMessage_ReplyCarriesMetadataAndQuotedContext(t *testing.T) { inbound, ok := <-messageBus.InboundChan() require.True(t, ok) + return inbound +} + +func TestHandleMessage_ReplyCarriesMetadataAndQuotedContext(t *testing.T) { + inbound := handleReplyMessageAndGetInbound(t, "what do you think?", 21, &telego.Message{ + MessageID: 99, + Text: "this is the old message", + From: &telego.User{ + Username: "alice", + }, + }) + assert.Equal(t, "99", inbound.Metadata["reply_to_message_id"]) assert.Equal( t, @@ -716,108 +729,36 @@ func TestHandleMessage_ReplyCarriesMetadataAndQuotedContext(t *testing.T) { } func TestHandleMessage_ReplyWithoutTextOnlySetsReplyMetadata(t *testing.T) { - messageBus := bus.NewMessageBus() - ch := &TelegramChannel{ - BaseChannel: channels.NewBaseChannel("telegram", nil, messageBus, nil), - chatIDs: make(map[string]int64), - ctx: context.Background(), - } + inbound := handleReplyMessageAndGetInbound(t, "ping", 22, &telego.Message{ + MessageID: 100, + }) - msg := &telego.Message{ - Text: "ping", - MessageID: 22, - Chat: telego.Chat{ - ID: 12345, - Type: "private", - }, - From: &telego.User{ - ID: 10, - FirstName: "Bob", - }, - ReplyToMessage: &telego.Message{ - MessageID: 100, - }, - } - - err := ch.handleMessage(context.Background(), msg) - require.NoError(t, err) - - inbound, ok := <-messageBus.InboundChan() - require.True(t, ok) assert.Equal(t, "100", inbound.Metadata["reply_to_message_id"]) assert.Equal(t, "ping", inbound.Content) } func TestHandleMessage_ReplyCommandKeepsCommandPrefix(t *testing.T) { - messageBus := bus.NewMessageBus() - ch := &TelegramChannel{ - BaseChannel: channels.NewBaseChannel("telegram", nil, messageBus, nil), - chatIDs: make(map[string]int64), - ctx: context.Background(), - } - - msg := &telego.Message{ - Text: "/new", - MessageID: 23, - Chat: telego.Chat{ - ID: 12345, - Type: "private", - }, + inbound := handleReplyMessageAndGetInbound(t, "/new", 23, &telego.Message{ + MessageID: 101, + Text: "old context text", From: &telego.User{ - ID: 10, - FirstName: "Bob", + Username: "alice", }, - ReplyToMessage: &telego.Message{ - MessageID: 101, - Text: "old context text", - From: &telego.User{ - Username: "alice", - }, - }, - } + }) - err := ch.handleMessage(context.Background(), msg) - require.NoError(t, err) - - inbound, ok := <-messageBus.InboundChan() - require.True(t, ok) assert.Equal(t, "101", inbound.Metadata["reply_to_message_id"]) assert.Equal(t, "/new", inbound.Content) } func TestHandleMessage_ReplyBangCommandKeepsCommandPrefix(t *testing.T) { - messageBus := bus.NewMessageBus() - ch := &TelegramChannel{ - BaseChannel: channels.NewBaseChannel("telegram", nil, messageBus, nil), - chatIDs: make(map[string]int64), - ctx: context.Background(), - } - - msg := &telego.Message{ - Text: "!new", - MessageID: 24, - Chat: telego.Chat{ - ID: 12345, - Type: "private", - }, + inbound := handleReplyMessageAndGetInbound(t, "!new", 24, &telego.Message{ + MessageID: 102, + Text: "old context text", From: &telego.User{ - ID: 10, - FirstName: "Bob", + Username: "alice", }, - ReplyToMessage: &telego.Message{ - MessageID: 102, - Text: "old context text", - From: &telego.User{ - Username: "alice", - }, - }, - } + }) - err := ch.handleMessage(context.Background(), msg) - require.NoError(t, err) - - inbound, ok := <-messageBus.InboundChan() - require.True(t, ok) assert.Equal(t, "102", inbound.Metadata["reply_to_message_id"]) assert.Equal(t, "!new", inbound.Content) }