v4/interactions/cmd_roleypoly_test.go
2025-03-26 21:08:15 -07:00

71 lines
1.8 KiB
Go

package interactions_test
import (
"fmt"
"net/url"
"testing"
"git.sapphic.engineer/roleypoly/v4/interactions"
"github.com/stretchr/testify/assert"
)
func TestCmdRoleypoly(t *testing.T) {
i, dcm, _ := makeInteractions(t)
ix := mkInteraction("roleypoly")
commands := []interactions.InteractionCommand{
{ID: "1", Name: "pick-role"},
{ID: "2", Name: "pickable-roles"},
{ID: "3", Name: "remove-role"},
}
dcm.MockResponse("GET", "/applications/*/commands", 200, commands)
ir, err := i.CmdRoleypoly(ix)
assert.Nil(t, err)
embed := ir.Data.Embeds[0]
// test that the button is cool
button := ir.Data.Components[0]
assert.Equal(t, fmt.Sprintf("%s/s/guild-id", i.PublicBaseURL), button.URL)
u, _ := url.Parse(i.PublicBaseURL)
hostname := u.Host
assert.Equal(t, fmt.Sprintf("Pick roles on %s", hostname), button.Label)
// test the command mentions
tests := map[string]string{
"See all the roles": "</2:pickable-roles>",
"Pick a role": "</1:pick-role>",
"Remove a role": "</3:remove-role>",
}
for _, field := range embed.Fields {
assert.Equal(t, tests[field.Name], field.Value)
}
// test weird name cases
// not weird
ix.Member.Nick = "Doll"
ix.Member.User.GlobalName = "Dolly"
ix.Member.User.Username = "41666."
ir, err = i.CmdRoleypoly(ix)
assert.Nil(t, err)
assert.Equal(t, ":beginner: Hey there, Doll", ir.Data.Embeds[0].Title)
// no nick
ix.Member.Nick = ""
ix.Member.User.GlobalName = "Dolly"
ix.Member.User.Username = "41666."
ir, err = i.CmdRoleypoly(ix)
assert.Nil(t, err)
assert.Equal(t, ":beginner: Hey there, Dolly", ir.Data.Embeds[0].Title)
// no globalname
ix.Member.Nick = ""
ix.Member.User.GlobalName = ""
ix.Member.User.Username = "41666."
ir, err = i.CmdRoleypoly(ix)
assert.Nil(t, err)
assert.Equal(t, ":beginner: Hey there, 41666.", ir.Data.Embeds[0].Title)
}