forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring_display_test.go
144 lines (130 loc) · 5.24 KB
/
scoring_display_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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)
func TestScoringDisplay(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("/displays/scoring/invalidalliance")
assert.Equal(t, 500, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Invalid alliance")
recorder = getHttpResponse("/displays/scoring/red")
assert.Equal(t, 200, recorder.Code)
recorder = getHttpResponse("/displays/scoring/blue")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Scoring - Untitled Event - Cheesy Arena")
}
func TestScoringDisplayWebsocket(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()
server, wsUrl := startTestServer()
defer server.Close()
_, _, err = websocket.DefaultDialer.Dial(wsUrl+"/displays/scoring/blorpy/websocket", nil)
assert.NotNil(t, err)
redConn, _, err := websocket.DefaultDialer.Dial(wsUrl+"/displays/scoring/red/websocket", nil)
assert.Nil(t, err)
defer redConn.Close()
redWs := &Websocket{redConn, new(sync.Mutex)}
blueConn, _, err := websocket.DefaultDialer.Dial(wsUrl+"/displays/scoring/blue/websocket", nil)
assert.Nil(t, err)
defer blueConn.Close()
blueWs := &Websocket{blueConn, new(sync.Mutex)}
// Should receive a score update right after connection.
readWebsocketType(t, redWs, "score")
readWebsocketType(t, redWs, "matchTime")
readWebsocketType(t, blueWs, "score")
readWebsocketType(t, blueWs, "matchTime")
// Send a match worth of scoring commands in.
redWs.Write("defenseCrossed", "2")
blueWs.Write("autoDefenseReached", nil)
redWs.Write("highGoal", nil)
redWs.Write("highGoal", nil)
redWs.Write("lowGoal", nil)
redWs.Write("defenseCrossed", "5")
blueWs.Write("defenseCrossed", "1")
redWs.Write("undoHighGoal", nil)
redWs.Write("commit", nil)
blueWs.Write("autoDefenseReached", nil)
blueWs.Write("commit", nil)
redWs.Write("uncommitAuto", nil)
redWs.Write("autoDefenseReached", nil)
redWs.Write("defenseCrossed", "2")
redWs.Write("commit", nil)
for i := 0; i < 11; i++ {
readWebsocketType(t, redWs, "score")
}
for i := 0; i < 4; i++ {
readWebsocketType(t, blueWs, "score")
}
assert.Equal(t, [5]int{0, 2, 0, 0, 1}, mainArena.redRealtimeScore.CurrentScore.AutoDefensesCrossed)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.AutoDefensesReached)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.AutoHighGoals)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.AutoLowGoals)
assert.Equal(t, [5]int{1, 0, 0, 0, 0}, mainArena.blueRealtimeScore.CurrentScore.AutoDefensesCrossed)
assert.Equal(t, 2, mainArena.blueRealtimeScore.CurrentScore.AutoDefensesReached)
redWs.Write("defenseCrossed", "2")
blueWs.Write("autoDefenseReached", nil)
redWs.Write("highGoal", nil)
redWs.Write("highGoal", nil)
redWs.Write("lowGoal", nil)
redWs.Write("defenseCrossed", "5")
blueWs.Write("defenseCrossed", "3")
blueWs.Write("challenge", nil)
blueWs.Write("scale", nil)
blueWs.Write("undoChallenge", nil)
redWs.Write("challenge", nil)
redWs.Write("defenseCrossed", "3")
redWs.Write("undoHighGoal", nil)
for i := 0; i < 8; i++ {
readWebsocketType(t, redWs, "score")
}
for i := 0; i < 5; i++ {
readWebsocketType(t, blueWs, "score")
}
// Make sure auto scores haven't changed in teleop.
assert.Equal(t, [5]int{0, 2, 0, 0, 1}, mainArena.redRealtimeScore.CurrentScore.AutoDefensesCrossed)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.AutoDefensesReached)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.AutoHighGoals)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.AutoLowGoals)
assert.Equal(t, [5]int{1, 0, 0, 0, 0}, mainArena.blueRealtimeScore.CurrentScore.AutoDefensesCrossed)
assert.Equal(t, 2, mainArena.blueRealtimeScore.CurrentScore.AutoDefensesReached)
assert.Equal(t, [5]int{0, 0, 1, 0, 1}, mainArena.redRealtimeScore.CurrentScore.DefensesCrossed)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.HighGoals)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.LowGoals)
assert.Equal(t, 1, mainArena.redRealtimeScore.CurrentScore.Challenges)
assert.Equal(t, [5]int{0, 0, 1, 0, 0}, mainArena.blueRealtimeScore.CurrentScore.DefensesCrossed)
assert.Equal(t, 0, mainArena.blueRealtimeScore.CurrentScore.Challenges)
assert.Equal(t, 1, mainArena.blueRealtimeScore.CurrentScore.Scales)
// Test committing logic.
redWs.Write("commitMatch", nil)
readWebsocketType(t, redWs, "error")
mainArena.MatchState = POST_MATCH
redWs.Write("commitMatch", nil)
blueWs.Write("commitMatch", nil)
readWebsocketType(t, redWs, "score")
readWebsocketType(t, blueWs, "score")
// Load another match to reset the results.
mainArena.ResetMatch()
mainArena.LoadTestMatch()
readWebsocketType(t, redWs, "score")
readWebsocketType(t, blueWs, "score")
assert.Equal(t, *NewRealtimeScore(), *mainArena.redRealtimeScore)
assert.Equal(t, *NewRealtimeScore(), *mainArena.blueRealtimeScore)
}