Repository navigation
Expand file tree
/
Copy pathbot_test.go
More file actions
148 lines (127 loc) · 4.42 KB
/
Copy pathbot_test.go
File metadata and controls
148 lines (127 loc) · 4.42 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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package slackbot
import (
"errors"
"testing"
"time"
"github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1"
"github.com/stretchr/testify/require"
)
type fakeBackend struct {
messages []string
attachments []string
listenErr error
}
func (b *fakeBackend) SendMessage(text string, _ string) {
b.messages = append(b.messages, text)
}
func (b *fakeBackend) SendAttachment(filename, _ string, _ string) error {
b.attachments = append(b.attachments, filename)
return nil
}
func (b *fakeBackend) Listen(BotCommandRunner) error {
return b.listenErr
}
func TestHelp(t *testing.T) {
bot, err := NewTestBot()
require.NoError(t, err)
bot.AddCommand("date", NewExecCommand("/bin/date", nil, true, "Show the current date", &config{}))
bot.AddCommand("utc", NewExecCommand("/bin/date", []string{"-u"}, true, "Show the current date (utc)", &config{}))
msg := bot.HelpMessage()
if msg == "" {
t.Fatal("No help message")
}
t.Logf("Help:\n%s", msg)
}
func TestPausedBlocksCommand(t *testing.T) {
backend := &fakeBackend{}
bot := NewBot(NewConfig(false, true), "testbot", "", backend)
ran := false
bot.AddCommand("ping", NewFuncCommand(func(_ string, _ []string) (string, error) {
ran = true
return "pong", nil
}, "Ping", bot.Config()))
err := bot.RunCommand([]string{"ping"}, "chan")
require.NoError(t, err)
require.False(t, ran)
require.Equal(t, []string{"I can't do that, I'm paused."}, backend.messages)
}
func TestIgnorePauseRunsCommandWhilePaused(t *testing.T) {
backend := &fakeBackend{}
bot := NewBot(NewConfig(false, true), "testbot", "", backend)
ranCh := make(chan struct{})
bot.AddCommand("ping", NewFuncCommand(func(_ string, _ []string) (string, error) {
close(ranCh)
return "pong", nil
}, "Ping", bot.Config()))
err := bot.RunCommand([]string{"ping", "--ignore-pause"}, "chan")
require.NoError(t, err)
select {
case <-ranCh:
case <-time.After(5 * time.Second):
t.Fatal("command did not run with --ignore-pause")
}
require.True(t, bot.Config().Paused(), "bot should remain paused")
}
func TestCommandPanicIsContained(t *testing.T) {
backend := &fakeBackend{}
bot := NewBot(NewConfig(false, false), "testbot", "", backend)
command := NewFuncCommand(func(_ string, _ []string) (string, error) {
panic("boom")
}, "Panic", bot.Config())
bot.run([]string{"panic"}, command, "chan")
require.Equal(t, []string{`Oops, there was an internal error running "panic".`}, backend.messages)
}
func TestSendAttachment(t *testing.T) {
backend := &fakeBackend{}
bot := NewBot(NewConfig(false, false), "testbot", "", backend)
require.NoError(t, bot.SendAttachment("/tmp/build.log", "build output", "chan"))
require.Equal(t, []string{"/tmp/build.log"}, backend.attachments)
}
func TestListenReturnsBackendError(t *testing.T) {
expectedErr := errors.New("listen failed")
backend := &fakeBackend{listenErr: expectedErr}
bot := NewBot(NewConfig(false, false), "testbot", "", backend)
require.ErrorIs(t, bot.Listen(), expectedErr)
}
func TestParseInput(t *testing.T) {
args := parseInput(`!keybot dumplog "release promote"`)
if args[0] != "!keybot" || args[1] != "dumplog" || args[2] != `release promote` {
t.Fatal("Invalid parse")
}
}
func TestAdvertisedCommands(t *testing.T) {
bot, err := NewTestBot()
require.NoError(t, err)
bot.AddCommand("date", NewExecCommand("/bin/date", nil, true, "Show the current date", &config{}))
bot.SetHelp("help body")
bot.AddAdvertisements(chat1.UserBotCommandInput{
Name: "build",
Description: "Build things",
Usage: "<target>",
})
commands := bot.AdvertisedCommands()
if len(commands) != 3 {
t.Fatalf("expected 3 advertised commands, got %d", len(commands))
}
if commands[0].Name != "testbot help" {
t.Fatalf("expected help command first, got %q", commands[0].Name)
}
if commands[0].ExtendedDescription == nil || commands[0].ExtendedDescription.DesktopBody != "help body" {
t.Fatalf("unexpected help extended description: %+v", commands[0].ExtendedDescription)
}
if commands[1] != (chat1.UserBotCommandInput{
Name: "testbot date",
Description: "Show the current date",
}) {
t.Fatalf("unexpected builtin command: %+v", commands[1])
}
if commands[2] != (chat1.UserBotCommandInput{
Name: "testbot build",
Description: "Build things",
Usage: "<target>",
}) {
t.Fatalf("unexpected extra command: %+v", commands[2])
}
}