-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamelogic_test.go
157 lines (140 loc) · 4.12 KB
/
gamelogic_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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"testing"
"github.com/sushovande/qr-mixer-game/qrpb"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
func GetSyntheticStateRow(usernameno int, level int64) *StateRow {
sr := NewStateRow()
sr.Cookie = fmt.Sprintf("cookie-%v", usernameno)
sr.Username = fmt.Sprintf("username-%v", usernameno)
sr.State = &qrpb.GameState{
UserLevel: proto.Int64(level),
Life: proto.Int64(3),
}
sr.UserInfo = &qrpb.GUser{
Username: proto.String(sr.Username),
Name: proto.String(fmt.Sprintf("name-%v", usernameno)),
}
return &sr
}
func TestStepRegularQuestion(t *testing.T) {
env, err := createEnv(":memory:")
if err != nil {
t.Fatal(err)
}
defer env.db.Close()
setupSynthetic(env.cgo, 10)
u1 := GetSyntheticStateRow(1, 6)
AddUser(env.db, u1)
mr, err := env.Step(u1.State, "qrcode-6")
if err != nil {
t.Fatal(err)
}
if mr.actionString != "Correct!" {
t.Errorf("expected progress. got: %v", mr.actionString)
}
mr, err = env.Step(u1.State, "qrcode-9")
if err != nil {
t.Fatal(err)
}
if mr.actionString != "Lost a Life!" {
t.Errorf("expected loss. got: %v", mr.actionString)
}
}
func TestGetMetalOnLevel9(t *testing.T) {
env, err := createEnv(":memory:")
if err != nil {
t.Fatal(err)
}
defer env.db.Close()
setupSynthetic(env.cgo, 10)
u1 := GetSyntheticStateRow(1, 9)
AddUser(env.db, u1)
// On level 9, if we answer correctly, we will reach level 10.
// We don't have a metal yet, so we should always get a metal here
mr, _ := env.Step(u1.State, "qrcode-10")
if !(mr.newState.GetHasAl() || mr.newState.GetHasCu()) {
ps, _ := prototext.Marshal(mr.newState)
t.Errorf("expected to have either al or cu. got: %v", string(ps))
}
}
func TestEndgameGrabMetal(t *testing.T) {
env, err := createEnv(":memory:")
if err != nil {
t.Fatal(err)
}
defer env.db.Close()
setupSynthetic(env.cgo, 10)
u1 := GetSyntheticStateRow(1, 20)
u1.State.HasAl = proto.Bool(true)
u1.State.HasSn = proto.Bool(true)
AddUser(env.db, u1)
u2 := GetSyntheticStateRow(2, 19)
u2.State.HasAl = proto.Bool(true)
u2.State.HasZn = proto.Bool(true)
AddUser(env.db, u2)
u4 := GetSyntheticStateRow(4, 20)
u4.State.HasCu = proto.Bool(true)
u4.State.HasSn = proto.Bool(true)
AddUser(env.db, u4)
// u1 is on level 20. On scanning u2, they should grab zinc
mr, err := env.Step(u1.State, "qrcode-2")
if err != nil {
t.Fatal(err)
}
if mr.actionString != "Grabbed Metal!" {
t.Errorf("expected metal. got: %v", mr.actionString)
}
if !mr.newState.GetHasZn() {
ps, _ := prototext.Marshal(mr.newState)
t.Errorf("expected to have zinc. got: %v", string(ps))
}
// u1 has just copper missing. If they scan u4, they should progress
u1.State = mr.newState
mr, _ = env.Step(u1.State, "qrcode-4")
if mr.actionString != "Grabbed Metal!" {
t.Errorf("expected metal. got: %v", mr.actionString)
}
if !mr.newState.GetHasSn() {
ps, _ := prototext.Marshal(mr.newState)
t.Errorf("expected to have zinc. got: %v", string(ps))
}
if mr.newState.GetUserLevel() != 21 {
ps, _ := prototext.Marshal(mr.newState)
t.Errorf("expected to reach level 21. got: %v", string(ps))
}
}
func TestAnyPersonQuestion(t *testing.T) {
env, err := createEnv(":memory:")
if err != nil {
t.Fatal(err)
}
defer env.db.Close()
setupSynthetic(env.cgo, 10)
u1 := GetSyntheticStateRow(1, 19)
AddUser(env.db, u1)
// On level 19, all answers should be accepted
mr, err := env.Step(u1.State, "qrcode-12")
if err != nil {
t.Fatal(err)
}
if mr.actionString != "Correct!" {
t.Errorf("expected progress. got: %v", mr.actionString)
}
}