github.com/diamondburned/arikawa/v2@v2.1.0/api/send_test.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/diamondburned/arikawa/v2/discord"
     9  	"github.com/diamondburned/arikawa/v2/utils/sendpart"
    10  )
    11  
    12  func TestMarshalAllowedMentions(t *testing.T) {
    13  	t.Run("parse nothing", func(t *testing.T) {
    14  		var data = SendMessageData{
    15  			AllowedMentions: &AllowedMentions{
    16  				Parse: []AllowedMentionType{},
    17  			},
    18  		}
    19  
    20  		if j := mustMarshal(t, data); j != `{"allowed_mentions":{"parse":[]}}` {
    21  			t.Fatal("Unexpected JSON:", j)
    22  		}
    23  	})
    24  
    25  	t.Run("allow everything", func(t *testing.T) {
    26  		var data = SendMessageData{
    27  			Content: "a",
    28  		}
    29  
    30  		if j := mustMarshal(t, data); j != `{"content":"a"}` {
    31  			t.Fatal("Unexpected JSON:", j)
    32  		}
    33  	})
    34  
    35  	t.Run("allow certain user IDs", func(t *testing.T) {
    36  		var data = SendMessageData{
    37  			AllowedMentions: &AllowedMentions{
    38  				Users: []discord.UserID{1, 2},
    39  			},
    40  		}
    41  
    42  		if j := mustMarshal(t, data); j != `{"allowed_mentions":{"parse":null,"users":["1","2"]}}` {
    43  			t.Fatal("Unexpected JSON:", j)
    44  		}
    45  	})
    46  }
    47  
    48  func TestVerifyAllowedMentions(t *testing.T) {
    49  	t.Run("invalid", func(t *testing.T) {
    50  		var am = AllowedMentions{
    51  			Parse: []AllowedMentionType{AllowEveryoneMention, AllowUserMention},
    52  			Users: []discord.UserID{69, 420},
    53  		}
    54  
    55  		err := am.Verify()
    56  		errMustContain(t, err, "Users slice is not empty")
    57  	})
    58  
    59  	t.Run("users too long", func(t *testing.T) {
    60  		var am = AllowedMentions{
    61  			Users: make([]discord.UserID, 101),
    62  		}
    63  
    64  		err := am.Verify()
    65  		errMustContain(t, err, "users slice length 101 is over 100")
    66  	})
    67  
    68  	t.Run("roles too long", func(t *testing.T) {
    69  		var am = AllowedMentions{
    70  			Roles: make([]discord.RoleID, 101),
    71  		}
    72  
    73  		err := am.Verify()
    74  		errMustContain(t, err, "roles slice length 101 is over 100")
    75  	})
    76  
    77  	t.Run("valid", func(t *testing.T) {
    78  		var am = AllowedMentions{
    79  			Parse: []AllowedMentionType{AllowEveryoneMention, AllowUserMention},
    80  			Roles: []discord.RoleID{1337},
    81  			Users: []discord.UserID{},
    82  		}
    83  
    84  		if err := am.Verify(); err != nil {
    85  			t.Fatal("Unexpected error:", err)
    86  		}
    87  	})
    88  }
    89  
    90  func TestSendMessage(t *testing.T) {
    91  	send := func(data SendMessageData) error {
    92  		// A nil client will cause a panic.
    93  		defer func() {
    94  			recover()
    95  		}()
    96  
    97  		// shouldn't matter
    98  		client := (*Client)(nil)
    99  		_, err := client.SendMessageComplex(0, data)
   100  		return err
   101  	}
   102  
   103  	t.Run("empty", func(t *testing.T) {
   104  		var empty = SendMessageData{
   105  			Content: "",
   106  			Embed:   nil,
   107  		}
   108  
   109  		if err := send(empty); err != ErrEmptyMessage {
   110  			t.Fatal("Unexpected error:", err)
   111  		}
   112  	})
   113  
   114  	t.Run("files only", func(t *testing.T) {
   115  		var empty = SendMessageData{
   116  			Files: []sendpart.File{{Name: "test.jpg"}},
   117  		}
   118  
   119  		if err := send(empty); err != nil {
   120  			t.Fatal("Unexpected error:", err)
   121  		}
   122  	})
   123  
   124  	t.Run("invalid allowed mentions", func(t *testing.T) {
   125  		var data = SendMessageData{
   126  			Content: "hime arikawa",
   127  			AllowedMentions: &AllowedMentions{
   128  				Parse: []AllowedMentionType{AllowEveryoneMention, AllowUserMention},
   129  				Users: []discord.UserID{69, 420},
   130  			},
   131  		}
   132  
   133  		err := send(data)
   134  		errMustContain(t, err, "allowedMentions error")
   135  	})
   136  
   137  	t.Run("invalid embed", func(t *testing.T) {
   138  		var data = SendMessageData{
   139  			Embed: &discord.Embed{
   140  				// max 256
   141  				Title: spaces(257),
   142  			},
   143  		}
   144  
   145  		err := send(data)
   146  		errMustContain(t, err, "embed error")
   147  	})
   148  }
   149  
   150  func errMustContain(t *testing.T, err error, contains string) {
   151  	// mark function as helper so line traces are accurate.
   152  	t.Helper()
   153  
   154  	if err != nil && strings.Contains(err.Error(), contains) {
   155  		return
   156  	}
   157  	t.Fatal("Unexpected error:", err)
   158  }
   159  
   160  func spaces(length int) string {
   161  	return strings.Repeat(" ", length)
   162  }
   163  
   164  func mustMarshal(t *testing.T, v interface{}) string {
   165  	t.Helper()
   166  
   167  	j, err := json.Marshal(v)
   168  	if err != nil {
   169  		t.Fatal("Failed to marshal data:", err)
   170  	}
   171  	return string(j)
   172  }