-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelp_internal_test.go
More file actions
220 lines (170 loc) · 8.85 KB
/
help_internal_test.go
File metadata and controls
220 lines (170 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package slackscot
import (
"fmt"
"github.com/alexandre-normand/slackscot/config"
"github.com/alexandre-normand/slackscot/schedule"
"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
type TestCmdMatcher struct {
prefix string
}
func NewTestCmdMatcher(prefix string) *TestCmdMatcher {
r := new(TestCmdMatcher)
r.prefix = prefix
return r
}
func (pc *TestCmdMatcher) IsCmd(msg slack.Msg) bool {
return strings.HasPrefix(msg.Text, pc.prefix)
}
func (pc *TestCmdMatcher) IsBot(msg slack.Msg) bool {
return strings.HasPrefix(msg.Text, pc.prefix)
}
func (pc *TestCmdMatcher) UsagePrefix() string {
return pc.prefix
}
func (pc *TestCmdMatcher) TrimPrefix(text string) string {
return strings.TrimPrefix(text, pc.prefix)
}
func (pc *TestCmdMatcher) String() string {
return fmt.Sprintf("Prefixed-Command{%v}", pc.prefix)
}
func newPluginWithActionsOfAllTypes(hidden bool) (p *Plugin) {
p = new(Plugin)
p.Name = "thank"
p.NamespaceCommands = true
p.Commands = []ActionDefinition{{
Hidden: hidden,
Match: func(m *IncomingMessage) bool {
return strings.HasPrefix(m.NormalizedText, "@user")
},
Usage: "<someone of something to thank>",
Description: "Format a thank you note",
Answer: func(m *IncomingMessage) *Answer {
return nil
}}}
p.HearActions = []ActionDefinition{{
Hidden: hidden,
Match: func(m *IncomingMessage) bool {
return strings.Contains(m.NormalizedText, "chickadee")
},
Usage: "say `chickadee` and hear a chirp",
Description: "Chirp when hearing people talk about chickadees",
Answer: func(m *IncomingMessage) *Answer {
return nil
}}}
p.ScheduledActions = []ScheduledActionDefinition{{Hidden: hidden, Schedule: schedule.Definition{Interval: 30, Unit: schedule.Seconds}, Description: "Sends a heartbeat every 30 seconds", Action: func() {}}}
return p
}
func TestHelpWithNamespacingEnabled(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(false))
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
assert.False(t, cmd.Match(&IncomingMessage{NormalizedText: " help"}))
require.True(t, cmd.Match(&IncomingMessage{NormalizedText: "help"}))
assert.True(t, cmd.Match(&IncomingMessage{NormalizedText: "help and something else"}))
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n\n"+
"I currently support the following commands:\n\t• `thank <someone of something to thank>` - Format a thank you note\n\nAnd listen for the following:\n"+
"\t• `say `chickadee` and hear a chirp` - Chirp when hearing people talk about chickadees\n\nAnd do those things periodically:\n"+
"\t• [`thank`] `Every 30 seconds` (`Local`) - Sends a heartbeat every 30 seconds\n", a.Text)
}
func TestHelpWithNamespacingDisabled(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults(), OptionNoPluginNamespacing())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(false))
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n\n"+
"I currently support the following commands:\n\t• `<someone of something to thank>` - Format a thank you note\n\nAnd listen for the following:\n"+
"\t• `say `chickadee` and hear a chirp` - Chirp when hearing people talk about chickadees\n\nAnd do those things periodically:\n"+
"\t• [`thank`] `Every 30 seconds` (`Local`) - Sends a heartbeat every 30 seconds\n", a.Text)
}
func TestHelpWithHiddenActions(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults(), OptionNoPluginNamespacing())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(true))
s.cmdMatcher = NewTestCmdMatcher("")
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n", a.Text)
}
func TestHelpWithNamespacingEnabledWithBlankPrefixCommandOption(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(false))
s.cmdMatcher = NewTestCmdMatcher("")
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
assert.False(t, cmd.Match(&IncomingMessage{NormalizedText: " help"}))
require.True(t, cmd.Match(&IncomingMessage{NormalizedText: "help"}))
assert.True(t, cmd.Match(&IncomingMessage{NormalizedText: "help and something else"}))
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n\n"+
"I currently support the following commands:\n\t• `thank <someone of something to thank>` - Format a thank you note\n\nAnd listen for the following:\n"+
"\t• `say `chickadee` and hear a chirp` - Chirp when hearing people talk about chickadees\n\nAnd do those things periodically:\n"+
"\t• [`thank`] `Every 30 seconds` (`Local`) - Sends a heartbeat every 30 seconds\n", a.Text)
}
func TestHelpWithNamespacingEnabledWithCommandOptionPrefix(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(false))
s.cmdMatcher = NewTestCmdMatcher("!!")
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
assert.False(t, cmd.Match(&IncomingMessage{NormalizedText: " help"}))
require.True(t, cmd.Match(&IncomingMessage{NormalizedText: "help"}))
assert.True(t, cmd.Match(&IncomingMessage{NormalizedText: "help and something else"}))
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n\n"+
"I currently support the following commands:\n\t• `!!thank <someone of something to thank>` - Format a thank you note\n\nAnd listen for the following:\n"+
"\t• `say `chickadee` and hear a chirp` - Chirp when hearing people talk about chickadees\n\nAnd do those things periodically:\n"+
"\t• [`thank`] `Every 30 seconds` (`Local`) - Sends a heartbeat every 30 seconds\n", a.Text)
}
func TestHelpWithNamespacingDisabledWithBlankPrefixCommandOption(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults(), OptionNoPluginNamespacing())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(false))
s.cmdMatcher = NewTestCmdMatcher("")
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n\n"+
"I currently support the following commands:\n\t• `<someone of something to thank>` - Format a thank you note\n\nAnd listen for the following:\n"+
"\t• `say `chickadee` and hear a chirp` - Chirp when hearing people talk about chickadees\n\nAnd do those things periodically:\n"+
"\t• [`thank`] `Every 30 seconds` (`Local`) - Sends a heartbeat every 30 seconds\n", a.Text)
}
func TestHelpWithNamespacingDisabledWithCommandOptionPrefix(t *testing.T) {
s, err := New("robert", config.NewViperWithDefaults(), OptionNoPluginNamespacing())
s.RegisterPlugin(newPluginWithActionsOfAllTypes(false))
s.cmdMatcher = NewTestCmdMatcher("!!")
require.NoError(t, err)
help := s.newHelpPlugin("1.0.0")
help.UserInfoFinder = &userInfoFinder{}
cmd := help.Commands[0]
a := cmd.Answer(&IncomingMessage{NormalizedText: "help"})
require.NotNil(t, a)
assert.Equal(t, "🤝 Hi, `Daniel Quinn`! I'm `robert` (engine `v1.0.0`) and I listen to the team's chat and provides automated functions :genie:.\n\n"+
"I currently support the following commands:\n\t• `!!<someone of something to thank>` - Format a thank you note\n\nAnd listen for the following:\n"+
"\t• `say `chickadee` and hear a chirp` - Chirp when hearing people talk about chickadees\n\nAnd do those things periodically:\n"+
"\t• [`thank`] `Every 30 seconds` (`Local`) - Sends a heartbeat every 30 seconds\n", a.Text)
}