forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_play_test.go
450 lines (411 loc) · 16.5 KB
/
match_play_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"bytes"
"fmt"
"github.com/gorilla/websocket"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"log"
"sync"
"testing"
"time"
)
func TestMatchPlay(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
match1 := Match{Type: "practice", DisplayName: "1", Status: "complete", Winner: "R"}
match2 := Match{Type: "practice", DisplayName: "2"}
match3 := Match{Type: "qualification", DisplayName: "1", Status: "complete", Winner: "B"}
match4 := Match{Type: "elimination", DisplayName: "SF1-1", Status: "complete", Winner: "T"}
match5 := Match{Type: "elimination", DisplayName: "SF1-2"}
db.CreateMatch(&match1)
db.CreateMatch(&match2)
db.CreateMatch(&match3)
db.CreateMatch(&match4)
db.CreateMatch(&match5)
// Check that all matches are listed on the page.
recorder := getHttpResponse("/match_play")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "P1")
assert.Contains(t, recorder.Body.String(), "P2")
assert.Contains(t, recorder.Body.String(), "Q1")
assert.Contains(t, recorder.Body.String(), "SF1-1")
assert.Contains(t, recorder.Body.String(), "SF1-2")
}
func TestMatchPlayLoad(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
db.CreateTeam(&Team{Id: 101})
db.CreateTeam(&Team{Id: 102})
db.CreateTeam(&Team{Id: 103})
db.CreateTeam(&Team{Id: 104})
db.CreateTeam(&Team{Id: 105})
db.CreateTeam(&Team{Id: 106})
match := Match{Type: "elimination", DisplayName: "QF4-3", Status: "complete", Winner: "R", Red1: 101,
Red2: 102, Red3: 103, Blue1: 104, Blue2: 105, Blue3: 106}
db.CreateMatch(&match)
recorder := getHttpResponse("/match_play")
assert.Equal(t, 200, recorder.Code)
assert.NotContains(t, recorder.Body.String(), "101")
assert.NotContains(t, recorder.Body.String(), "102")
assert.NotContains(t, recorder.Body.String(), "103")
assert.NotContains(t, recorder.Body.String(), "104")
assert.NotContains(t, recorder.Body.String(), "105")
assert.NotContains(t, recorder.Body.String(), "106")
// Load the match and check for the team numbers again.
recorder = getHttpResponse(fmt.Sprintf("/match_play/%d/load", match.Id))
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/match_play")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "101")
assert.Contains(t, recorder.Body.String(), "102")
assert.Contains(t, recorder.Body.String(), "103")
assert.Contains(t, recorder.Body.String(), "104")
assert.Contains(t, recorder.Body.String(), "105")
assert.Contains(t, recorder.Body.String(), "106")
// Load a test match.
recorder = getHttpResponse("/match_play/0/load")
assert.Equal(t, 302, recorder.Code)
recorder = getHttpResponse("/match_play")
assert.Equal(t, 200, recorder.Code)
assert.NotContains(t, recorder.Body.String(), "101")
assert.NotContains(t, recorder.Body.String(), "102")
assert.NotContains(t, recorder.Body.String(), "103")
assert.NotContains(t, recorder.Body.String(), "104")
assert.NotContains(t, recorder.Body.String(), "105")
assert.NotContains(t, recorder.Body.String(), "106")
}
func TestMatchPlayShowResult(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
recorder := getHttpResponse("/match_play/1/show_result")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Invalid match")
match := Match{Type: "qualification", DisplayName: "1", Status: "complete"}
db.CreateMatch(&match)
recorder = getHttpResponse(fmt.Sprintf("/match_play/%d/show_result", match.Id))
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "No result found")
db.CreateMatchResult(&MatchResult{MatchId: match.Id})
recorder = getHttpResponse(fmt.Sprintf("/match_play/%d/show_result", match.Id))
assert.Equal(t, 302, recorder.Code)
assert.Equal(t, match.Id, mainArena.savedMatch.Id)
assert.Equal(t, match.Id, mainArena.savedMatchResult.MatchId)
}
func TestMatchPlayErrors(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
// Load an invalid match.
recorder := getHttpResponse("/match_play/1114/load")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Invalid match")
}
func TestCommitMatch(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
// Committing test match should do nothing.
match := &Match{Id: 0, Type: "test", Red1: 101, Red2: 102, Red3: 103, Blue1: 104, Blue2: 105, Blue3: 106}
err = CommitMatchScore(match, &MatchResult{MatchId: match.Id}, false)
assert.Nil(t, err)
matchResult, err := db.GetMatchResultForMatch(match.Id)
assert.Nil(t, err)
assert.Nil(t, matchResult)
// Committing the same match more than once should create a second match result record.
match.Id = 1
match.Type = "qualification"
db.CreateMatch(match)
matchResult = &MatchResult{MatchId: match.Id, BlueScore: Score{AutoDefensesReached: 2}}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
assert.Equal(t, 1, matchResult.PlayNumber)
match, _ = db.GetMatchById(1)
assert.Equal(t, "B", match.Winner)
matchResult = &MatchResult{MatchId: match.Id, RedScore: Score{AutoDefensesReached: 1}}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
assert.Equal(t, 2, matchResult.PlayNumber)
match, _ = db.GetMatchById(1)
assert.Equal(t, "R", match.Winner)
matchResult = &MatchResult{MatchId: match.Id}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
assert.Equal(t, 3, matchResult.PlayNumber)
match, _ = db.GetMatchById(1)
assert.Equal(t, "T", match.Winner)
// Verify TBA and STEMtv publishing by checking the log for the expected failure messages.
tbaBaseUrl = "fakeurl"
stemTvBaseUrl = "fakeurl"
eventSettings.TbaPublishingEnabled = true
eventSettings.StemTvPublishingEnabled = true
var writer bytes.Buffer
log.SetOutput(&writer)
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
time.Sleep(time.Millisecond * 10) // Allow some time for the asynchronous publishing to happen.
assert.Contains(t, writer.String(), "Failed to publish matches")
assert.Contains(t, writer.String(), "Failed to publish rankings")
assert.Contains(t, writer.String(), "Failed to publish match video split to STEMtv")
}
func TestCommitEliminationTie(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
match := &Match{Id: 0, Type: "qualification", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5, Blue3: 6}
db.CreateMatch(match)
matchResult := &MatchResult{MatchId: match.Id, RedScore: Score{HighGoals: 1, Fouls: []Foul{Foul{}}}}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
match, _ = db.GetMatchById(1)
assert.Equal(t, "T", match.Winner)
match.Type = "elimination"
db.SaveMatch(match)
CommitMatchScore(match, matchResult, false)
match, _ = db.GetMatchById(1)
assert.Equal(t, "T", match.Winner) // No elimination tiebreakers.
}
func TestCommitCards(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
// Check that a yellow card sticks with a team.
team := &Team{Id: 5}
db.CreateTeam(team)
match := &Match{Id: 0, Type: "qualification", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5, Blue3: 6}
db.CreateMatch(match)
matchResult := &MatchResult{MatchId: match.Id, BlueCards: map[string]string{"5": "yellow"}}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
team, _ = db.GetTeamById(5)
assert.True(t, team.YellowCard)
// Check that editing a match result removes a yellow card from a team.
matchResult = &MatchResult{MatchId: match.Id}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
team, _ = db.GetTeamById(5)
assert.False(t, team.YellowCard)
// Check that a red card causes a yellow card to stick with a team.
matchResult = &MatchResult{MatchId: match.Id, BlueCards: map[string]string{"5": "red"}}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
team, _ = db.GetTeamById(5)
assert.True(t, team.YellowCard)
// Check that a red card in eliminations zeroes out the score.
createTestAlliances(db, 2)
match.Type = "elimination"
db.SaveMatch(match)
*matchResult = buildTestMatchResult(match.Id, 10)
matchResult.RedCards = map[string]string{"1": "red"}
err = CommitMatchScore(match, matchResult, false)
assert.Nil(t, err)
assert.Equal(t, 0, matchResult.RedScoreSummary().Score)
assert.Equal(t, 113, matchResult.BlueScoreSummary().Score)
}
func TestMatchPlayWebsocketCommands(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
db.CreateTeam(&Team{Id: 254})
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
server, wsUrl := startTestServer()
defer server.Close()
conn, _, err := websocket.DefaultDialer.Dial(wsUrl+"/match_play/websocket", nil)
assert.Nil(t, err)
defer conn.Close()
ws := &Websocket{conn, new(sync.Mutex)}
// Should get a few status updates right after connection.
readWebsocketType(t, ws, "status")
readWebsocketType(t, ws, "matchTiming")
readWebsocketType(t, ws, "matchTime")
readWebsocketType(t, ws, "realtimeScore")
readWebsocketType(t, ws, "setAudienceDisplay")
readWebsocketType(t, ws, "scoringStatus")
readWebsocketType(t, ws, "setAllianceStationDisplay")
// Test that a server-side error is communicated to the client.
ws.Write("nonexistenttype", nil)
assert.Contains(t, readWebsocketError(t, ws), "Invalid message type")
// Test match setup commands.
ws.Write("substituteTeam", nil)
assert.Contains(t, readWebsocketError(t, ws), "Invalid alliance station")
ws.Write("substituteTeam", map[string]interface{}{"team": 254, "position": "B5"})
assert.Contains(t, readWebsocketError(t, ws), "Invalid alliance station")
ws.Write("substituteTeam", map[string]interface{}{"team": 254, "position": "B1"})
readWebsocketType(t, ws, "status")
assert.Equal(t, 254, mainArena.currentMatch.Blue1)
ws.Write("substituteTeam", map[string]interface{}{"team": 0, "position": "B1"})
readWebsocketType(t, ws, "status")
assert.Equal(t, 0, mainArena.currentMatch.Blue1)
ws.Write("toggleBypass", nil)
assert.Contains(t, readWebsocketError(t, ws), "Failed to parse")
ws.Write("toggleBypass", "R4")
assert.Contains(t, readWebsocketError(t, ws), "Invalid alliance station")
ws.Write("toggleBypass", "R3")
readWebsocketType(t, ws, "status")
assert.Equal(t, true, mainArena.AllianceStations["R3"].Bypass)
ws.Write("toggleBypass", "R3")
readWebsocketType(t, ws, "status")
assert.Equal(t, false, mainArena.AllianceStations["R3"].Bypass)
// Go through match flow.
ws.Write("abortMatch", nil)
assert.Contains(t, readWebsocketError(t, ws), "Cannot abort match")
ws.Write("startMatch", nil)
assert.Contains(t, readWebsocketError(t, ws), "Cannot start match")
mainArena.AllianceStations["R1"].Bypass = true
mainArena.AllianceStations["R2"].Bypass = true
mainArena.AllianceStations["R3"].Bypass = true
mainArena.AllianceStations["B1"].Bypass = true
mainArena.AllianceStations["B2"].Bypass = true
mainArena.AllianceStations["B3"].Bypass = true
ws.Write("startMatch", nil)
readWebsocketType(t, ws, "status")
assert.Equal(t, START_MATCH, mainArena.MatchState)
ws.Write("commitResults", nil)
assert.Contains(t, readWebsocketError(t, ws), "Cannot reset match")
ws.Write("discardResults", nil)
assert.Contains(t, readWebsocketError(t, ws), "Cannot reset match")
ws.Write("abortMatch", nil)
readWebsocketType(t, ws, "status")
readWebsocketType(t, ws, "setAudienceDisplay")
assert.Equal(t, POST_MATCH, mainArena.MatchState)
mainArena.redRealtimeScore.CurrentScore.AutoDefensesReached = 1
mainArena.blueRealtimeScore.CurrentScore.AutoLowGoals = 2
ws.Write("commitResults", nil)
readWebsocketMultiple(t, ws, 3) // reload, realtimeScore, setAllianceStationDisplay
assert.Equal(t, 1, mainArena.savedMatchResult.RedScore.AutoDefensesReached)
assert.Equal(t, 2, mainArena.savedMatchResult.BlueScore.AutoLowGoals)
assert.Equal(t, PRE_MATCH, mainArena.MatchState)
ws.Write("discardResults", nil)
readWebsocketMultiple(t, ws, 3) // reload, realtimeScore, setAllianceStationDisplay
assert.Equal(t, PRE_MATCH, mainArena.MatchState)
// Test changing the displays.
ws.Write("setAudienceDisplay", "logo")
readWebsocketType(t, ws, "setAudienceDisplay")
assert.Equal(t, "logo", mainArena.audienceDisplayScreen)
ws.Write("setAllianceStationDisplay", "logo")
readWebsocketType(t, ws, "setAllianceStationDisplay")
assert.Equal(t, "logo", mainArena.allianceStationDisplayScreen)
}
func TestMatchPlayWebsocketNotifications(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
db.CreateTeam(&Team{Id: 254})
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
server, wsUrl := startTestServer()
defer server.Close()
conn, _, err := websocket.DefaultDialer.Dial(wsUrl+"/match_play/websocket", nil)
assert.Nil(t, err)
defer conn.Close()
ws := &Websocket{conn, new(sync.Mutex)}
// Should get a few status updates right after connection.
readWebsocketType(t, ws, "status")
readWebsocketType(t, ws, "matchTiming")
readWebsocketType(t, ws, "matchTime")
readWebsocketType(t, ws, "realtimeScore")
readWebsocketType(t, ws, "setAudienceDisplay")
readWebsocketType(t, ws, "scoringStatus")
mainArena.AllianceStations["R1"].Bypass = true
mainArena.AllianceStations["R2"].Bypass = true
mainArena.AllianceStations["R3"].Bypass = true
mainArena.AllianceStations["B1"].Bypass = true
mainArena.AllianceStations["B2"].Bypass = true
mainArena.AllianceStations["B3"].Bypass = true
mainArena.StartMatch()
mainArena.Update()
messages := readWebsocketMultiple(t, ws, 4)
statusReceived, matchTime := getStatusMatchTime(t, messages)
assert.Equal(t, true, statusReceived)
assert.Equal(t, 2, matchTime.MatchState)
assert.Equal(t, 0, matchTime.MatchTimeSec)
_, ok := messages["setAudienceDisplay"]
assert.True(t, ok)
_, ok = messages["setAllianceStationDisplay"]
assert.True(t, ok)
mainArena.scoringStatusNotifier.Notify(nil)
readWebsocketType(t, ws, "scoringStatus")
// Should get a tick notification when an integer second threshold is crossed.
mainArena.matchStartTime = time.Now().Add(-time.Second + 10*time.Millisecond) // Not crossed yet
mainArena.Update()
mainArena.matchStartTime = time.Now().Add(-time.Second - 10*time.Millisecond) // Crossed
mainArena.Update()
mainArena.matchStartTime = time.Now().Add(-2*time.Second + 10*time.Millisecond) // Not crossed yet
mainArena.Update()
mainArena.matchStartTime = time.Now().Add(-2*time.Second - 10*time.Millisecond) // Crossed
mainArena.Update()
err = mapstructure.Decode(readWebsocketType(t, ws, "matchTime"), &matchTime)
assert.Nil(t, err)
assert.Equal(t, 2, matchTime.MatchState)
assert.Equal(t, 1, matchTime.MatchTimeSec)
err = mapstructure.Decode(readWebsocketType(t, ws, "matchTime"), &matchTime)
assert.Nil(t, err)
assert.Equal(t, 2, matchTime.MatchState)
assert.Equal(t, 2, matchTime.MatchTimeSec)
// Check across a match state boundary.
mainArena.matchStartTime = time.Now().Add(-time.Duration(mainArena.matchTiming.AutoDurationSec) * time.Second)
mainArena.Update()
statusReceived, matchTime = readWebsocketStatusMatchTime(t, ws)
assert.Equal(t, true, statusReceived)
assert.Equal(t, 3, matchTime.MatchState)
assert.Equal(t, mainArena.matchTiming.AutoDurationSec, matchTime.MatchTimeSec)
}
// Handles the status and matchTime messages arriving in either order.
func readWebsocketStatusMatchTime(t *testing.T, ws *Websocket) (bool, MatchTimeMessage) {
return getStatusMatchTime(t, readWebsocketMultiple(t, ws, 2))
}
func getStatusMatchTime(t *testing.T, messages map[string]interface{}) (bool, MatchTimeMessage) {
_, statusReceived := messages["status"]
message, ok := messages["matchTime"]
var matchTime MatchTimeMessage
if assert.True(t, ok) {
err := mapstructure.Decode(message, &matchTime)
assert.Nil(t, err)
}
return statusReceived, matchTime
}