Skip to content

Commit 5a38d88

Browse files
authored
add more tests to mux (#446)
1 parent 8c15340 commit 5a38d88

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

handler/mux_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,62 @@ func TestMiddlewareMux(t *testing.T) {
245245
assert.Equal(t, d.expectedVars3, dataVars3)
246246
}
247247
}
248+
249+
func TestMux(t *testing.T) {
250+
buttonFooData, err := os.ReadFile("testdata/mux/button_foo_component.json")
251+
assert.NoError(t, err)
252+
253+
buttonFooBarData, err := os.ReadFile("testdata/mux/button_foo_bar_component.json")
254+
assert.NoError(t, err)
255+
256+
data := []struct {
257+
data []byte
258+
expected *discord.InteractionResponse
259+
}{
260+
{
261+
data: buttonFooData,
262+
expected: &discord.InteractionResponse{
263+
Type: discord.InteractionResponseTypeCreateMessage,
264+
Data: discord.MessageCreate{
265+
Content: "/foo/",
266+
},
267+
},
268+
},
269+
{
270+
data: buttonFooBarData,
271+
expected: &discord.InteractionResponse{
272+
Type: discord.InteractionResponseTypeCreateMessage,
273+
Data: discord.MessageCreate{
274+
Content: "/foo/bar",
275+
},
276+
},
277+
},
278+
}
279+
280+
mux := New()
281+
mux.Route("/foo", func(r Router) {
282+
r.ButtonComponent("/", func(data discord.ButtonInteractionData, e *ComponentEvent) error {
283+
return e.CreateMessage(discord.MessageCreate{
284+
Content: "/foo/",
285+
})
286+
})
287+
r.ButtonComponent("/bar", func(data discord.ButtonInteractionData, e *ComponentEvent) error {
288+
return e.CreateMessage(discord.MessageCreate{
289+
Content: "/foo/bar",
290+
})
291+
})
292+
})
293+
294+
for _, d := range data {
295+
interaction, err := discord.UnmarshalInteraction(d.data)
296+
assert.NoError(t, err)
297+
298+
recorder := NewRecorder()
299+
mux.OnEvent(&events.InteractionCreate{
300+
GenericEvent: events.NewGenericEvent(nil, 0, 0),
301+
Interaction: interaction,
302+
Respond: recorder.Respond,
303+
})
304+
assert.Equal(t, d.expected, recorder.Response)
305+
}
306+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"version": 1,
3+
"type": 3,
4+
"token": "unique_interaction_token",
5+
"message": {
6+
"type": 0,
7+
"tts": false,
8+
"timestamp": "2021-05-19T02:12:51.710000+00:00",
9+
"pinned": false,
10+
"mentions": [],
11+
"mention_roles": [],
12+
"mention_everyone": false,
13+
"id": "844397162624450620",
14+
"flags": 0,
15+
"embeds": [],
16+
"edited_timestamp": null,
17+
"content": "This is a message with components.",
18+
"components": [
19+
{
20+
"type": 1,
21+
"components": [
22+
{
23+
"type": 2,
24+
"label": "Click me!",
25+
"style": 1,
26+
"custom_id": "/foo/bar"
27+
}
28+
]
29+
}
30+
],
31+
"channel_id": "345626669114982402",
32+
"author": {
33+
"username": "Mason",
34+
"public_flags": 131141,
35+
"id": "53908232506183680",
36+
"discriminator": "1337",
37+
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432"
38+
},
39+
"attachments": []
40+
},
41+
"member": {
42+
"user": {
43+
"username": "Mason",
44+
"public_flags": 131141,
45+
"id": "53908232506183680",
46+
"discriminator": "1337",
47+
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432"
48+
},
49+
"roles": [
50+
"290926798626357999"
51+
],
52+
"premium_since": null,
53+
"permissions": "17179869183",
54+
"pending": false,
55+
"nick": null,
56+
"mute": false,
57+
"joined_at": "2017-03-13T19:19:14.040000+00:00",
58+
"is_pending": false,
59+
"deaf": false,
60+
"avatar": null
61+
},
62+
"id": "846462639134605312",
63+
"guild_id": "290926798626357999",
64+
"data": {
65+
"custom_id": "/foo/bar",
66+
"component_type": 2
67+
},
68+
"channel_id": "345626669114982999",
69+
"application_id": "290926444748734465"
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"version": 1,
3+
"type": 3,
4+
"token": "unique_interaction_token",
5+
"message": {
6+
"type": 0,
7+
"tts": false,
8+
"timestamp": "2021-05-19T02:12:51.710000+00:00",
9+
"pinned": false,
10+
"mentions": [],
11+
"mention_roles": [],
12+
"mention_everyone": false,
13+
"id": "844397162624450620",
14+
"flags": 0,
15+
"embeds": [],
16+
"edited_timestamp": null,
17+
"content": "This is a message with components.",
18+
"components": [
19+
{
20+
"type": 1,
21+
"components": [
22+
{
23+
"type": 2,
24+
"label": "Click me!",
25+
"style": 1,
26+
"custom_id": "/foo"
27+
}
28+
]
29+
}
30+
],
31+
"channel_id": "345626669114982402",
32+
"author": {
33+
"username": "Mason",
34+
"public_flags": 131141,
35+
"id": "53908232506183680",
36+
"discriminator": "1337",
37+
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432"
38+
},
39+
"attachments": []
40+
},
41+
"member": {
42+
"user": {
43+
"username": "Mason",
44+
"public_flags": 131141,
45+
"id": "53908232506183680",
46+
"discriminator": "1337",
47+
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432"
48+
},
49+
"roles": [
50+
"290926798626357999"
51+
],
52+
"premium_since": null,
53+
"permissions": "17179869183",
54+
"pending": false,
55+
"nick": null,
56+
"mute": false,
57+
"joined_at": "2017-03-13T19:19:14.040000+00:00",
58+
"is_pending": false,
59+
"deaf": false,
60+
"avatar": null
61+
},
62+
"id": "846462639134605312",
63+
"guild_id": "290926798626357999",
64+
"data": {
65+
"custom_id": "/foo",
66+
"component_type": 2
67+
},
68+
"channel_id": "345626669114982999",
69+
"application_id": "290926444748734465"
70+
}

0 commit comments

Comments
 (0)