-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity_test.go
More file actions
211 lines (193 loc) · 6.8 KB
/
Copy pathactivity_test.go
File metadata and controls
211 lines (193 loc) · 6.8 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
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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aviorstudio/termcade/internal/registry"
"github.com/aviorstudio/termcade/internal/scores"
)
// The sync is the only place where an arcade that works offline meets an
// account that might not be reachable. Every test here is a way that can go
// wrong without anybody noticing: a run delivered twice, a run silently
// dropped, a local high quietly replaced by a worse one from the account.
// fakeRegistry answers /v1/activity and records what was submitted.
type fakeRegistry struct {
*httptest.Server
submitted []registry.Run
paths []string
// records is what GET /v1/activity returns.
records []registry.Activity
// fail, when set, is the status every submission gets.
fail int
}
func newFakeRegistry(t *testing.T) *fakeRegistry {
t.Helper()
f := &fakeRegistry{}
f.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f.paths = append(f.paths, r.Method+" "+r.URL.Path)
switch {
case r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/v1/activity/"):
if f.fail != 0 {
w.WriteHeader(f.fail)
json.NewEncoder(w).Encode(map[string]string{"message": "no"})
return
}
var run registry.Run
json.NewDecoder(r.Body).Decode(&run)
f.submitted = append(f.submitted, run)
json.NewEncoder(w).Encode(registry.Activity{ID: strings.TrimPrefix(r.URL.Path, "/v1/activity/")})
case r.URL.Path == "/v1/activity":
json.NewEncoder(w).Encode(f.records)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(f.Close)
return f
}
// session points the arcade at the fake registry with a token, in a config
// directory of its own.
func session(t *testing.T, f *fakeRegistry) *scores.Store {
t.Helper()
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("TERMCADE_REGISTRY", f.URL)
if err := registry.SaveSession(registry.Session{
Registry: f.URL, Email: "p@t.dev", Token: "tcc_" + strings.Repeat("A", 43),
}); err != nil {
t.Fatalf("saving a session: %v", err)
}
st, err := scores.Load()
if err != nil {
t.Fatalf("loading scores: %v", err)
}
return st
}
func TestSyncSendsTheQueueAndClearsIt(t *testing.T) {
f := newFakeRegistry(t)
st := session(t, f)
st.Record("aviorstudio/tetris", 900, "1.2.0", true)
st.Record("aviorstudio/asteroid", 10, "", false)
sent, err := syncActivity(st)
if err != nil {
t.Fatalf("sync: %v", err)
}
if sent != 2 {
t.Errorf("sent %d runs, want 2", sent)
}
if got := st.Pending(); len(got) != 0 {
t.Errorf("queue not cleared: %v", got)
}
if len(f.submitted) != 2 {
t.Fatalf("registry saw %d runs", len(f.submitted))
}
if f.submitted[0].Score != 900 || f.submitted[0].Version != "1.2.0" {
t.Errorf("first run went out wrong: %+v", f.submitted[0])
}
if !f.submitted[0].Completed || f.submitted[1].Completed {
t.Errorf("completion not carried: %+v %+v", f.submitted[0], f.submitted[1])
}
// The timestamp is the run's, not the sync's — an offline queue that
// reported "now" on delivery would rewrite when everything happened.
if _, err := time.Parse(time.RFC3339, f.submitted[0].PlayedAt); err != nil {
t.Errorf("played_at %q is not RFC 3339: %v", f.submitted[0].PlayedAt, err)
}
}
// A run stays queued until it is acknowledged, and the retry carries the same
// id — which is the entire reason retrying is safe.
func TestAFailedRunStaysQueuedUnderTheSameID(t *testing.T) {
f := newFakeRegistry(t)
f.fail = http.StatusInternalServerError
st := session(t, f)
st.Record("aviorstudio/tetris", 900, "1.2.0", true)
queued := st.Pending()[0].ID
if sent, err := syncActivity(st); err == nil || sent != 0 {
t.Fatalf("a failing sync reported sent=%d err=%v", sent, err)
}
pending := st.Pending()
if len(pending) != 1 || pending[0].ID != queued {
t.Fatalf("run did not stay queued under its id: %v", pending)
}
f.fail = 0
if sent, err := syncActivity(st); err != nil || sent != 1 {
t.Fatalf("retry: sent=%d err=%v", sent, err)
}
if f.submitted[0].Run != queued {
t.Errorf("retry used a new run id (%s, was %s) — the registry would count two plays",
f.submitted[0].Run, queued)
}
}
// A run for a game the catalog no longer has can never be delivered. Leaving
// it queued would block everything behind it forever.
func TestARunTheRegistryWillNeverAcceptIsDropped(t *testing.T) {
f := newFakeRegistry(t)
f.fail = http.StatusNotFound
st := session(t, f)
st.Record("aviorstudio/gone", 900, "", true)
if _, err := syncActivity(st); err != nil {
t.Fatalf("sync: %v", err)
}
if got := st.Pending(); len(got) != 0 {
t.Errorf("undeliverable run still queued: %v", got)
}
// Its score was already local and stays that way.
if got := st.High("aviorstudio/gone"); got != 900 {
t.Errorf("high = %d, want 900", got)
}
}
func TestSyncMergesTheAccountsRecordWithoutLoweringAnything(t *testing.T) {
f := newFakeRegistry(t)
f.records = []registry.Activity{
// Played higher somewhere else.
{ID: "aviorstudio/asteroid", PersonalBest: 5000,
LastPlayed: time.Now().UTC().Format(time.RFC3339)},
// The account is behind this machine.
{ID: "aviorstudio/tetris", PersonalBest: 10,
LastPlayed: time.Now().UTC().Add(-72 * time.Hour).Format(time.RFC3339)},
}
st := session(t, f)
st.Record("aviorstudio/tetris", 900, "1.2.0", true)
if _, err := syncActivity(st); err != nil {
t.Fatalf("sync: %v", err)
}
if got := st.High("aviorstudio/tetris"); got != 900 {
t.Errorf("local high = %d, want 900 — the account must not lower it", got)
}
if got := st.High("aviorstudio/asteroid"); got != 5000 {
t.Errorf("high = %d, want the 5000 set on another machine", got)
}
}
// Signed out is the ordinary state of an arcade nobody has made an account
// for. It must not be an error, and the queue must survive it.
func TestSyncSignedOutIsNotAFailure(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
st, _ := scores.Load()
st.Record("aviorstudio/tetris", 900, "1.2.0", true)
sent, err := syncActivity(st)
if err != nil || sent != 0 {
t.Fatalf("signed-out sync: sent=%d err=%v", sent, err)
}
if got := st.Pending(); len(got) != 1 {
t.Errorf("signing out ate the queue: %v", got)
}
}
func TestSyncMessageOnlySpeaksWhenThereIsSomethingToSay(t *testing.T) {
if got := syncMessage(0, nil); got != "" {
t.Errorf("a quiet sync said %q", got)
}
if got := syncMessage(0, registry.ErrUnreachable); got != "" {
t.Errorf("being offline said %q; the arcade works offline", got)
}
if got := syncMessage(1, nil); got != "1 run synced to your account" {
t.Errorf("one run: %q", got)
}
if got := syncMessage(4, nil); got != "4 runs synced to your account" {
t.Errorf("several runs: %q", got)
}
// An expired session is the one failure somebody can do something about.
if got := syncMessage(0, registry.ErrLoginRequired); !strings.Contains(got, "expired") {
t.Errorf("expired session: %q", got)
}
}