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
|
package game
import (
"encoding/json"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"unicode/utf8"
"thehouseoficarus/internal/color"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/prediction"
)
func predictionTestServer(t *testing.T, g *Game, m prediction.Market) {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/markets":
json.NewEncoder(w).Encode([]prediction.Market{m})
case "/markets/1":
json.NewEncoder(w).Encode(m)
case "/prices-history":
w.Write([]byte(`{"history":[{"t":100,"p":0.3},{"t":200,"p":0.6}]}`))
default:
w.WriteHeader(404)
}
}))
t.Cleanup(server.Close)
g.PredictionClient = prediction.NewClient()
g.PredictionClient.Gamma = server.URL
g.PredictionClient.CLOB = server.URL
}
func TestPredictionModalCommandsAndEntryAliases(t *testing.T) {
for _, alias := range []string{"predictions", "market", "counter", "exchange", "prediction market counter"} {
t.Run(alias, func(t *testing.T) {
g, s, conn, m := predictionFixture(t)
predictionTestServer(t, g, m)
if _, ok := commandRegistry["predictions"]; ok {
t.Fatal("standalone predictions command survives")
}
if _, ok := commandRegistry["predict"]; ok {
t.Fatal("standalone predict alias survives")
}
if g.predictionInputOwned(s, "active") {
t.Fatal("counter verbs escaped session scope")
}
g.doUse(s, alias)
if !s.PredictionActive || s.PredictionScreen != "list" {
t.Fatalf("not entered: %s", conn.data)
}
g.handleGameCommand(s, "1")
if s.PredictionSelected != "1" || s.PredictionScreen != "view" {
t.Fatalf("numeric selection failed: %s", conn.data)
}
g.handleGameCommand(s, "buy 10 9")
if s.PredictionScreen != "view" || s.PredictionSelected != "1" || len(s.Player.PredictionContracts) != 0 {
t.Fatal("rejected order lost selection or changed ledger")
}
g.handleGameCommand(s, "buy 10 1")
if len(s.Player.PredictionContracts) != 1 || s.Player.Credits != 9570 {
t.Fatalf("context buy failed: %s", conn.data)
}
if s.PredictionScreen != "view" {
t.Fatal("purchase lost the selected market")
}
g.handleGameCommand(s, "active")
g.handleGameCommand(s, "1")
if s.PredictionScreen != "view" {
t.Fatal("portfolio number did not open holding")
}
g.handleGameCommand(s, "active")
g.handleGameCommand(s, "sell 1")
if s.Player.PredictionContracts[0].Status != "sold" {
t.Fatal("context sale failed")
}
g.handleGameCommand(s, "stop")
if s.PredictionActive || g.predictionInputOwned(s, "active") {
t.Fatal("stop did not leave mode")
}
})
}
}
func TestPredictionScreenGlyphsColorAndWidths(t *testing.T) {
for _, unicode := range []bool{true, false} {
t.Run(map[bool]string{true: "unicode", false: "ascii"}[unicode], func(t *testing.T) {
g, s, conn, m := predictionFixture(t)
s.Player.Options["unicode"] = unicode
s.Player.Options["color"] = "xterm256"
s.Player.Options["casino_hints"] = "on"
g.predictionList(s, []prediction.Market{m}, 1, "")
m.Prices = `["0.60","0.40"]`
g.predictionList(s, []prediction.Market{m}, 1, "")
g.predictionView(s, m, []prediction.Point{{T: 100, P: .3}, {T: 200, P: .6}})
text := string(conn.data)
if !strings.Contains(text, "\x1b[") {
t.Fatal("no color")
}
text = regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(text, "")
if strings.Contains(text, "predictions buy") || strings.Contains(text, "predictions board") {
t.Fatal("old command hints")
}
for _, want := range []string{"Available", "buy", "ODDS HISTORY", "Period move:", "+30.0", "Market", "Odds", "Outcome"} {
if !strings.Contains(text, want) {
t.Fatalf("missing %q", want)
}
}
if unicode {
for _, glyph := range []string{"╔", "║", "█", "░", "↑", "─"} {
if !strings.Contains(text, glyph) {
t.Fatalf("missing glyph %s", glyph)
}
}
} else {
for _, r := range text {
if r > 127 {
t.Fatalf("ASCII mode emitted %U", r)
}
}
if !strings.Contains(text, "#") || !strings.Contains(text, "^") {
t.Fatal("ASCII graphics missing")
}
}
for _, line := range strings.Split(text, "\n") {
if width := color.VisibleLen(strings.TrimSuffix(line, "\r")); width > 80 {
t.Fatalf("line exceeds terminal (%d): %s", width, line)
}
}
if !utf8.ValidString(text) {
t.Fatal("broken UTF-8")
}
})
}
}
func TestPredictionHintPreferencesAndWorldCommands(t *testing.T) {
g, s, conn, m := predictionFixture(t)
predictionTestServer(t, g, m)
s.Player.Options["casino_hints"] = "off"
g.doUse(s, "market")
if strings.Contains(string(conn.data), "view N") {
t.Fatal("ignored hints off")
}
conn.data = nil
g.handleGameCommand(s, "what")
if !strings.Contains(string(conn.data), "view N") {
t.Fatal("global what did not show the casino menu")
}
if g.predictionInputOwned(s, "what") || g.predictionInputOwned(s, "verbs") {
t.Fatal("global commands claimed by counter")
}
if strings.Contains(string(conn.data), "YOUR NEXT MOVE") || strings.Contains(string(conn.data), "what /") {
t.Fatal("custom hint footer survived")
}
if g.predictionInputOwned(s, "north") || g.predictionInputOwned(s, "inventory") {
t.Fatal("world command intercepted")
}
epoch := s.PredictionEpoch
g.handlePredictionInput(s, "use", []string{"market"})
if !s.PredictionActive || s.PredictionEpoch != epoch {
t.Fatal("reusing counter invalidates pending operation")
}
g.handlePredictionInput(s, "play", []string{"roulette"})
if s.PredictionActive {
t.Fatal("another casino game did not release mode")
}
}
func TestPredictionStopCancelsPendingTrade(t *testing.T) {
g, s, _, m := predictionFixture(t)
entered, canceled := make(chan struct{}), make(chan struct{})
var calls atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
close(entered)
<-r.Context().Done()
close(canceled)
}))
defer server.Close()
g.PredictionClient = prediction.NewClient()
g.PredictionClient.Gamma = server.URL
g.owner = newOwner()
defer g.owner.stop()
g.loggedInChars = map[string]*net.Session{"alice": s}
s.PredictionActive = true
s.PredictionSelected = m.ID
s.PredictionScreen = "view"
g.run(func() { g.executePredictions(s, []string{"buy", "10", "1"}, "") })
select {
case <-entered:
case <-time.After(time.Second):
t.Fatal("request not started")
}
g.run(func() { g.executeCommand(s, "stop", nil, "stop") })
select {
case <-canceled:
case <-time.After(time.Second):
t.Fatal("stop did not cancel network request")
}
g.run(func() {
if s.PredictionActive || s.PredictionBusy || s.Player.Credits != 10000 || len(s.Player.PredictionContracts) != 0 {
t.Fatal("stopped trade changed state")
}
})
if calls.Load() != 1 {
t.Fatal("unexpected requests")
}
}
func TestPredictionRoomEntryClearsModeAndCancelsRequest(t *testing.T) {
_, s, _, _ := predictionFixture(t)
canceled := false
s.PredictionActive = true
s.PredictionBusy = true
s.PredictionSelected = "1"
s.PredictionCancel = func() { canceled = true }
hub := net.NewHub()
hub.Add(s)
hub.EnterRoom(s, 2)
if s.PredictionActive || s.PredictionBusy || s.PredictionSelected != "" || !canceled {
t.Fatal("room change retained counter mode")
}
}
func TestPredictionBackKeepsSearchAndPage(t *testing.T) {
g, s, _, m := predictionFixture(t)
markets := make([]prediction.Market, 21)
for i := range markets {
markets[i] = m
markets[i].ID = strconv.Itoa(i + 1)
markets[i].Question = "Sports market " + markets[i].ID
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/markets":
json.NewEncoder(w).Encode(markets)
case "/prices-history":
w.Write([]byte(`{"history":[]}`))
default:
n, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/markets/"))
if n < 1 || n > len(markets) {
w.WriteHeader(404)
return
}
json.NewEncoder(w).Encode(markets[n-1])
}
}))
defer server.Close()
g.PredictionClient = prediction.NewClient()
g.PredictionClient.Gamma = server.URL
g.PredictionClient.CLOB = server.URL
g.doUse(s, "market")
g.handleGameCommand(s, "find sports")
g.handleGameCommand(s, "next")
if s.PredictionPage != 2 {
t.Fatal("next did not turn page")
}
g.handleGameCommand(s, "5")
if s.PredictionSelected != "5" {
t.Fatal("wrong selected market")
}
g.handleGameCommand(s, "back")
if s.PredictionScreen != "list" || s.PredictionPage != 2 || s.PredictionSearch != "sports" {
t.Fatal("back lost browsing position")
}
g.handleGameCommand(s, "markets")
if s.PredictionPage != 1 || s.PredictionSearch != "" {
t.Fatal("markets did not reset selection")
}
}
|