Skip to content

Commit 43ac934

Browse files
committed
test(api): add tests for command logging
Refs: #130
1 parent f9ccea9 commit 43ac934

File tree

2 files changed

+211
-1
lines changed

2 files changed

+211
-1
lines changed

api/slash_commands.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ func (c *SlashCommandManager) computeSubCommandStringFromInteractionData(
231231
}
232232

233233
if option.Type != discordgo.ApplicationCommandOptionSubCommandGroup {
234-
return "<unexpected option type>"
234+
// This is not a subcommand for sure.
235+
return ""
235236
}
236237

237238
if len(option.Options) == 0 {

api/slash_commands_test.go

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package api
2020

2121
import (
22+
"github.com/bwmarrin/discordgo"
2223
"github.com/lazybytez/jojo-discord-bot/api/entities"
2324
"github.com/stretchr/testify/suite"
2425
"testing"
@@ -38,6 +39,214 @@ func (suite *SlashCommandManagerTestSuite) SetupTest() {
3839
suite.slashCommandManager = SlashCommandManager{owner: suite.owningComponent}
3940
}
4041

42+
func (suite *SlashCommandManagerTestSuite) TestComputeFullCommandStringFromInteractionData() {
43+
tables := []struct {
44+
input discordgo.ApplicationCommandInteractionData
45+
expected string
46+
}{
47+
{
48+
input: discordgo.ApplicationCommandInteractionData{
49+
ID: "123451234512345",
50+
Name: "dice",
51+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
52+
{
53+
Type: discordgo.ApplicationCommandOptionInteger,
54+
Name: "dice-side-number",
55+
Value: 5,
56+
},
57+
},
58+
},
59+
expected: "dice",
60+
},
61+
{
62+
input: discordgo.ApplicationCommandInteractionData{
63+
ID: "123451234512345",
64+
Name: "jojo",
65+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
66+
{
67+
Type: discordgo.ApplicationCommandOptionSubCommand,
68+
Name: "sync-commands",
69+
Value: &discordgo.ApplicationCommandInteractionDataOption{
70+
Name: "sync-commands",
71+
},
72+
},
73+
},
74+
},
75+
expected: "jojo sync-commands",
76+
},
77+
{
78+
input: discordgo.ApplicationCommandInteractionData{
79+
ID: "123451234512345",
80+
Name: "jojo",
81+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
82+
{
83+
Type: discordgo.ApplicationCommandOptionSubCommand,
84+
Name: "sync-commands",
85+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
86+
{
87+
Type: discordgo.ApplicationCommandOptionString,
88+
Name: "some-random-option",
89+
Value: "test",
90+
},
91+
{
92+
Type: discordgo.ApplicationCommandOptionInteger,
93+
Name: "some-second-option",
94+
Value: 2,
95+
},
96+
},
97+
},
98+
},
99+
},
100+
expected: "jojo sync-commands",
101+
},
102+
{
103+
input: discordgo.ApplicationCommandInteractionData{
104+
ID: "123451234512345",
105+
Name: "jojo",
106+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
107+
{
108+
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
109+
Name: "module",
110+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
111+
{
112+
Type: discordgo.ApplicationCommandOptionSubCommand,
113+
Name: "list",
114+
},
115+
},
116+
},
117+
},
118+
},
119+
expected: "jojo module list",
120+
},
121+
{
122+
input: discordgo.ApplicationCommandInteractionData{
123+
ID: "123451234512345",
124+
Name: "jojo",
125+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
126+
{
127+
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
128+
Name: "module",
129+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
130+
{
131+
Type: discordgo.ApplicationCommandOptionSubCommand,
132+
Name: "enable",
133+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
134+
{
135+
Type: discordgo.ApplicationCommandOptionString,
136+
Name: "module",
137+
Value: "ping_pong",
138+
},
139+
},
140+
},
141+
},
142+
},
143+
},
144+
},
145+
expected: "jojo module enable",
146+
},
147+
}
148+
149+
for _, table := range tables {
150+
result := suite.slashCommandManager.computeFullCommandStringFromInteractionData(table.input)
151+
152+
suite.Equal(table.expected, result)
153+
}
154+
}
155+
156+
func (suite *SlashCommandManagerTestSuite) TestComputeConfiguredOptionsString() {
157+
tables := []struct {
158+
input []*discordgo.ApplicationCommandInteractionDataOption
159+
expected string
160+
}{
161+
{
162+
input: []*discordgo.ApplicationCommandInteractionDataOption{
163+
{
164+
Type: discordgo.ApplicationCommandOptionInteger,
165+
Name: "dice-side-number",
166+
Value: 5,
167+
},
168+
},
169+
expected: "dice-side-number=5",
170+
},
171+
{
172+
input: []*discordgo.ApplicationCommandInteractionDataOption{
173+
{
174+
Type: discordgo.ApplicationCommandOptionSubCommand,
175+
Name: "sync-commands",
176+
Value: &discordgo.ApplicationCommandInteractionDataOption{
177+
Name: "sync-commands",
178+
},
179+
},
180+
},
181+
expected: "",
182+
},
183+
{
184+
input: []*discordgo.ApplicationCommandInteractionDataOption{
185+
{
186+
Type: discordgo.ApplicationCommandOptionSubCommand,
187+
Name: "sync-commands",
188+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
189+
{
190+
Type: discordgo.ApplicationCommandOptionString,
191+
Name: "some-random-option",
192+
Value: "test",
193+
},
194+
{
195+
Type: discordgo.ApplicationCommandOptionInteger,
196+
Name: "some-second-option",
197+
Value: 2,
198+
},
199+
},
200+
},
201+
},
202+
expected: "some-random-option=test; some-second-option=2",
203+
},
204+
{
205+
input: []*discordgo.ApplicationCommandInteractionDataOption{
206+
{
207+
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
208+
Name: "module",
209+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
210+
{
211+
Type: discordgo.ApplicationCommandOptionSubCommand,
212+
Name: "list",
213+
},
214+
},
215+
},
216+
},
217+
expected: "",
218+
},
219+
{
220+
input: []*discordgo.ApplicationCommandInteractionDataOption{
221+
{
222+
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
223+
Name: "module",
224+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
225+
{
226+
Type: discordgo.ApplicationCommandOptionSubCommand,
227+
Name: "enable",
228+
Options: []*discordgo.ApplicationCommandInteractionDataOption{
229+
{
230+
Type: discordgo.ApplicationCommandOptionString,
231+
Name: "module",
232+
Value: "ping_pong",
233+
},
234+
},
235+
},
236+
},
237+
},
238+
},
239+
expected: "module=ping_pong",
240+
},
241+
}
242+
243+
for _, table := range tables {
244+
result := suite.slashCommandManager.computeConfiguredOptionsString(table.input)
245+
246+
suite.Equal(table.expected, result)
247+
}
248+
}
249+
41250
func (suite *SlashCommandManagerTestSuite) TestGetCommandsForComponentWithoutMatchingCommands() {
42251
testComponentCode := entities.ComponentCode("no_commands_component")
43252
componentCommandMap = map[string]*Command{

0 commit comments

Comments
 (0)