fix: markdown parse mode breaking some plugins
This commit is contained in:
		
							parent
							
								
									35c14ce8a8
								
							
						
					
					
						commit
						0edf41c792
					
				
					 5 changed files with 140 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -75,6 +75,7 @@ func (p *TwitterExpander) OnMessage(msg *model.Message, config map[string]interf
 | 
			
		|||
		Chat:    msg.Chat,
 | 
			
		||||
		ReplyTo: msg.ID,
 | 
			
		||||
		Channel: msg.Channel,
 | 
			
		||||
		Raw:     map[string]interface{}{"parse_mode": ""},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	action := &model.MessageAction{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										120
									
								
								internal/plugin/social/twitter_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								internal/plugin/social/twitter_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,120 @@
 | 
			
		|||
package social
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"git.nakama.town/fmartingr/butterrobot/internal/model"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestTwitterExpander_OnMessage(t *testing.T) {
 | 
			
		||||
	plugin := NewTwitterExpander()
 | 
			
		||||
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name     string
 | 
			
		||||
		input    string
 | 
			
		||||
		config   map[string]interface{}
 | 
			
		||||
		expected string
 | 
			
		||||
		hasReply bool
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name:     "Twitter URL with default domain",
 | 
			
		||||
			input:    "https://twitter.com/user/status/123456789",
 | 
			
		||||
			config:   map[string]interface{}{},
 | 
			
		||||
			expected: "https://fxtwitter.com/user/status/123456789",
 | 
			
		||||
			hasReply: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:     "X.com URL with custom domain",
 | 
			
		||||
			input:    "https://x.com/elonmusk/status/987654321",
 | 
			
		||||
			config:   map[string]interface{}{"domain": "vxtwitter.com"},
 | 
			
		||||
			expected: "https://vxtwitter.com/elonmusk/status/987654321",
 | 
			
		||||
			hasReply: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:     "Twitter URL with tracking parameters",
 | 
			
		||||
			input:    "https://twitter.com/openai/status/555?ref_src=twsrc%5Etfw&s=20",
 | 
			
		||||
			config:   map[string]interface{}{},
 | 
			
		||||
			expected: "https://fxtwitter.com/openai/status/555",
 | 
			
		||||
			hasReply: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:     "www.twitter.com URL",
 | 
			
		||||
			input:    "https://www.twitter.com/user/status/789",
 | 
			
		||||
			config:   map[string]interface{}{"domain": "nitter.net"},
 | 
			
		||||
			expected: "https://nitter.net/user/status/789",
 | 
			
		||||
			hasReply: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:     "Mixed text with Twitter URL",
 | 
			
		||||
			input:    "Check this out: https://twitter.com/user/status/123 amazing!",
 | 
			
		||||
			config:   map[string]interface{}{},
 | 
			
		||||
			expected: "Check this out: https://fxtwitter.com/user/status/123 amazing!",
 | 
			
		||||
			hasReply: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:     "No Twitter URLs",
 | 
			
		||||
			input:    "Just some regular text https://youtube.com/watch?v=abc",
 | 
			
		||||
			config:   map[string]interface{}{},
 | 
			
		||||
			expected: "",
 | 
			
		||||
			hasReply: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:     "Empty message",
 | 
			
		||||
			input:    "",
 | 
			
		||||
			config:   map[string]interface{}{},
 | 
			
		||||
			expected: "",
 | 
			
		||||
			hasReply: false,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
		t.Run(tt.name, func(t *testing.T) {
 | 
			
		||||
			msg := &model.Message{
 | 
			
		||||
				ID:   "test_msg",
 | 
			
		||||
				Text: tt.input,
 | 
			
		||||
				Chat: "test_chat",
 | 
			
		||||
				Channel: &model.Channel{
 | 
			
		||||
					ID:                1,
 | 
			
		||||
					Platform:          "telegram",
 | 
			
		||||
					PlatformChannelID: "test_chat",
 | 
			
		||||
				},
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			actions := plugin.OnMessage(msg, tt.config, nil)
 | 
			
		||||
 | 
			
		||||
			if !tt.hasReply {
 | 
			
		||||
				if len(actions) != 0 {
 | 
			
		||||
					t.Errorf("Expected no actions, got %d", len(actions))
 | 
			
		||||
				}
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if len(actions) != 1 {
 | 
			
		||||
				t.Errorf("Expected 1 action, got %d", len(actions))
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			action := actions[0]
 | 
			
		||||
			if action.Type != model.ActionSendMessage {
 | 
			
		||||
				t.Errorf("Expected ActionSendMessage, got %s", action.Type)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if action.Message == nil {
 | 
			
		||||
				t.Error("Expected message in action, got nil")
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if action.Message.Text != tt.expected {
 | 
			
		||||
				t.Errorf("Expected '%s', got '%s'", tt.expected, action.Message.Text)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if action.Message.ReplyTo != msg.ID {
 | 
			
		||||
				t.Errorf("Expected ReplyTo '%s', got '%s'", msg.ID, action.Message.ReplyTo)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if action.Message.Raw == nil || action.Message.Raw["parse_mode"] != "" {
 | 
			
		||||
				t.Error("Expected parse_mode to be empty string to disable markdown parsing")
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue