-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
238 lines (221 loc) Β· 11.2 KB
/
main_test.go
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
"io"
"os"
"reflect"
"testing"
"time"
)
func Test_parseAndFormatMessage(t *testing.T) {
// Initialize and consume on 2 channels, so we don't block on the channel insert (and therefore sleep al goroutines)
// There is probaly a better way to do this
commands = make(chan string)
go func() {
for range commands {
}
}()
discordActivities = make(chan discordgo.Activity)
go func() {
for range discordActivities {
}
}()
type args struct {
message string
config botConfig
}
config = botConfig{sendJoinLeave: true}
tests := []struct {
name string
args args
want string
}{
{"JOIN-show", args{message: "2022-02-01 15:31:19 [JOIN] Mattie joined the game", config: botConfig{sendJoinLeave: true}}, ":green_circle: | `Mattie` joined the game!"},
{"JOIN-hide", args{message: "2022-02-01 15:31:19 [JOIN] Mattie joined the game", config: botConfig{sendJoinLeave: false}}, ""},
{"LEAVE-show", args{message: "2022-02-01 15:31:30 [LEAVE] Mattie left the game", config: botConfig{sendJoinLeave: true}}, ":red_circle: | `Mattie` left the game!"},
{"LEAVE-hide", args{message: "2022-02-01 15:31:30 [LEAVE] Mattie left the game", config: botConfig{sendJoinLeave: false}}, ""},
{"CHAT", args{message: "2022-02-01 15:31:30 [CHAT] Mattie: Some chat message"}, ":speech_left: | `Mattie`: Some chat message"},
// Messages below are generated by the companion mod, but I still want them to go through the normal flow!
{"PLAYER_DIED", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie]\""}, ":skull: | Player died: `Mattie` (unknown cause)"},
// Player died messages
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:locomotive]\""}, ":skull: | Player died: `Mattie`, cause: `locomotive` (hahaha!)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:cargo-wagon]\""}, ":skull: | Player died: `Mattie`, cause: `cargo-wagon` (hahaha! how the hell did you do that?!?!)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:big worm]\""}, ":skull: | Player died: `Mattie`, cause: `big worm`"},
{"PLAYER_DIED_CAUSE", args{message: "[[FactoriGOChatBot]: \"11083545 [PLAYER_DIED:Vance307:behemoth-spitter]\""}, ":skull: | Player died: `Vance307`, cause: `behemoth-spitter`"},
// Player died (with counts, companion mod > 0.6.0)
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:locomotive:10:50]\""}, ":skull: | Player died: `Mattie`, cause: `locomotive` (hahaha!) (10 times out of 50 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:cargo-wagon:10:50]\""}, ":skull: | Player died: `Mattie`, cause: `cargo-wagon` (hahaha! how the hell did you do that?!?!) (10 times out of 50 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Vance307:behemoth-spitter:1:1]\""}, ":skull: | Player died: `Vance307`, cause: `behemoth-spitter` (1 times out of 1 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:character:1:2]\""}, ":skull: | Player died: `Mattie`, cause: `character` (1 times out of 2 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"36777 [PLAYER_DIED:Mattie:small-biter:0:4]\""}, ":skull: | Player died: `Mattie`, cause: `small-biter` (0 times out of 4 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"13934261 [PLAYER_DIED:Mattie::1:15]\""}, ":skull: | Player died: `Mattie`, cause: `unknown` (1 times out of 15 deaths)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"13934261 [PLAYER_DIED:Mattie:SomeOtherPlayerNickNameHere:1:15]\""}, ":skull: | Player died: `Mattie`, cause: `SomeOtherPlayerNickNameHere` (1 times out of 15 deaths)"},
// Research
{"RESEARCH_STARTED", args{message: "[FactoriGOChatBot]: \"3045105 [RESEARCH_STARTED:nuclear-power]\""}, ":microscope: | Research started: `nuclear-power`"},
{"RESEARCH_FINISHED", args{message: "[FactoriGOChatBot]: \"3229214 [RESEARCH_FINISHED:nuclear-power]\""}, ":microscope: | Research finished: `nuclear-power`"},
// Rocket
{"ROCKET_LAUNCHED_1", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:1]\""}, ":rocket: :rocket: :rocket: A rocket has been launched! (1 times)"},
{"ROCKET_LAUNCHED_5", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:5]\""}, ":rocket: :rocket: :rocket: A rocket has been launched! (5 times)"},
{"ROCKET_LAUNCHED_10", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:10]\""}, ":rocket: :rocket: :rocket: A rocket has been launched! (10 times)"},
{"ROCKET_LAUNCHED_50", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:50]\""}, ":rocket: :rocket: :rocket: A rocket has been launched! (50 times)"},
{"ROCKET_LAUNCHED_100", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:100]\""}, ":rocket: :rocket: :rocket: A rocket has been launched! (100 times)"},
{"ROCKET_LAUNCHED_500", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:500]\""}, ":rocket: :rocket: :rocket: A rocket has been launched! (500 times)"},
{"ROCKET_LAUNCHED_11", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:11]\""}, ""},
{"ROCKET_LAUNCHED_101", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:101]\""}, ""},
{"ROCKET_LAUNCHED_220", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:220]\""}, ""},
{"ROCKET_LAUNCHED_1337", args{message: "[FactoriGOChatBot]: \"12393460 [ROCKET_LAUNCHED:1337]\""}, ""},
// Corrupted messages (as I don't know yet how to fix the file read, so it will have a single line guaranteed
{"CORRUPT", args{message: "[FactoriGOChatBot]: \"2852569 [foobar]\""}, ""},
// Messages I want to ignore
{"GPS-hide", args{message: "2022-04-14 19:41:54 [CHAT] Mattie: [gps=98,69]"}, ""},
{"GPS-show", args{message: "2022-04-14 19:41:54 [CHAT] Mattie: [gps=98,69]", config: botConfig{sendGPSPing: true}}, ":map: | `Mattie`: [gps=98,69]"},
{"message-loop-issue-#28", args{message: "2024-10-31 20:15:53 [CHAT] <server>: [color=#7289DA][Discord][Mattie]: test123[/color]", config: botConfig{achievementMode: true}}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config = tt.args.config
if got := parseAndFormatMessage(tt.args.message); got != tt.want {
t.Errorf("parseAndFormatMessage() = '%v', want '%v'", got, tt.want)
}
})
}
close(commands)
}
func Test_parseDiscordMessage(t *testing.T) {
type args struct {
message string
}
tests := []struct {
name string
args args
want []string
}{
{"Normal chat", args{message: "test"}, []string{"test"}},
{"Wave emoji", args{message: "π"}, []string{"**waving-hand**"}},
{"Sweat", args{message: "π"}, []string{"**downcast-face-with-sweat**"}},
{"Grin", args{message: "π"}, []string{"**grinning-face**"}},
{"Smile (but slightly)", args{message: "π"}, []string{"**slightly-smiling-face**"}},
{"Cool guy", args{message: "π"}, []string{"**smiling-face-with-sunglasses**"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseDiscordMessage(tt.args.message); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseDiscordMessage() = %v, want %v", got, tt.want)
}
})
}
}
// This is more of an integration test, it actually creates a file, there is probably a better way
func Test_readFactorioLogFile(t *testing.T) {
log = logrus.New()
log.Out = io.Discard
// Prepare channel (readFactorioLogFile will publish to this one)
messagesToDiscord = make(chan string, 10)
// First write a file with some lines so that we have a baseline
d1 := []byte("this\nis\na\n\test\n")
err := os.WriteFile("test.txt", d1, 0644)
if err != nil {
t.Fatal(err)
}
defer func() {
_ = os.Remove("test.txt")
}()
// Start the tail process and after some time stop it (so that the program finishes)
go readFactorioLogFile("test.txt")
time.Sleep(100 * time.Millisecond) // Give the tail stuff some time to "activate"
go func() {
<-time.After(100 * time.Millisecond)
_ = tailFile.Stop()
}()
// Write / append something (e.g. a new line written by factorio)
f, err := os.OpenFile("test.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
write := "2022-02-01 15:31:30 [CHAT] Mattie: Some chat message"
want := ":speech_left: | `Mattie`: Some chat message"
if _, err = f.WriteString(write); err != nil {
panic(err)
}
_ = f.Close()
str := <-messagesToDiscord
if got := str; !reflect.DeepEqual(got, want) {
t.Errorf("readFactorioLogFile() = '%v', want '%v'", got, want)
}
close(messagesToDiscord)
}
// MockRconClient is a mock implementation of the rcon.Client
type MockRconClient struct {
mock.Mock
}
func (m *MockRconClient) Execute(command string) (string, error) {
args := m.Called(command)
return args.String(0), args.Error(1)
}
func Test_sendMessageToFactorio(t *testing.T) {
log = logrus.New()
log.Out = io.Discard
mockRconClient := new(MockRconClient)
type args struct {
message string
achievementMode bool
}
tests := []struct {
name string
args args
want string
}{
{"Test achivement mode", args{message: "test", achievementMode: true}, "[color=#7289DA][Discord]test[/color]"},
{"Test nicer layout mode", args{message: "test", achievementMode: false}, "/silent-command game.print('[color=#7289DA][Discord]test[/color]')"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
messagesToFactorio = make(chan string)
go sendMessageToFactorio(mockRconClient)
config.achievementMode = tt.args.achievementMode
mockRconClient.On("Execute", tt.want).Return("some response", nil)
messagesToFactorio <- tt.args.message
mockRconClient.AssertCalled(t, "Execute", tt.want)
close(messagesToFactorio)
})
}
}
func Test_updatePlayerCount(t *testing.T) {
log = logrus.New()
log.Out = io.Discard
type args struct {
playersOnline string
expected string
}
tests := []struct {
name string
args args
}{
{"0 players online", args{playersOnline: "Online players (0):", expected: "the world burn"}},
{"1 players online", args{playersOnline: "Online players (1):", expected: "the factory grow"}},
{"123 players online", args{playersOnline: "Online players (123):", expected: "the factory grow"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
discordActivities = make(chan discordgo.Activity, 1)
mockRconClient := new(MockRconClient)
// Return the amount of players online when Execute is called
mockRconClient.On("Execute", "/players online count").Return(tt.args.playersOnline, nil)
// Execute the funtion tested
updatePlayerCount(mockRconClient)
// Make sure we did a call to the RCON client
mockRconClient.AssertCalled(t, "Execute", "/players online count")
// Read back the activity from the channel, and make sure it is what we expect
activity := <-discordActivities
if activity.Type != discordgo.ActivityTypeWatching {
t.Errorf("updatePlayerCount() = '%v', want '%v'", activity.Type, discordgo.ActivityTypeWatching)
}
if activity.Name != tt.args.expected {
t.Errorf("updatePlayerCount() = '%v', want '%v'", activity.Name, tt.args.expected)
}
close(discordActivities)
})
}
}