aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/blackjack_test.go
blob: a6735c0ff84bc5ab438bb37965695fe3ac343367 (plain)
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
package casino

import (
	"math/rand"
	"strings"
	"testing"
)

func TestBlackjackHandTreatsAceAsElevenWhenPossible(t *testing.T) {
	hand := &blackjackHand{cards: []card{{rank: "A"}, {rank: "6"}}}
	total, soft := hand.total()
	if total != 17 || !soft {
		t.Fatalf("total=%d soft=%v, want soft 17", total, soft)
	}
	hand.cards = append(hand.cards, card{rank: "K"})
	total, soft = hand.total()
	if total != 17 || soft {
		t.Fatalf("total=%d soft=%v, want hard 17", total, soft)
	}
}

func TestBlackjackHandScoreShowsBlackjackAs21(t *testing.T) {
	if got := handScore(&blackjackHand{cards: []card{{rank: "A"}, {rank: "K"}}}); got != "21" {
		t.Fatalf("blackjack score=%q, want 21", got)
	}
	if got := handScore(&blackjackHand{cards: []card{{rank: "A"}, {rank: "6"}}}); got != "7 or 17" {
		t.Fatalf("soft 17 score=%q, want 7 or 17", got)
	}
	if got := handScore(&blackjackHand{cards: []card{{rank: "10"}, {rank: "8"}}}); got != "18" {
		t.Fatalf("hard 18 score=%q, want 18", got)
	}
}

func TestBlackjackSettlementRules(t *testing.T) {
	rules := (BlackjackConfig{}).Normalize()
	natural := &blackjackHand{bet: 10, cards: []card{{rank: "A"}, {rank: "K"}}}
	if result, payout := settleHand(natural, 20, false, rules); result != "blackjack" || payout != 25 {
		t.Fatalf("natural result=%s payout=%d", result, payout)
	}
	dealerNatural := &blackjackHand{bet: 10, cards: []card{{rank: "A"}, {rank: "Q"}}}
	if result, payout := settleHand(dealerNatural, 21, true, rules); result != "push" || payout != 10 {
		t.Fatalf("dealer natural result=%s payout=%d", result, payout)
	}
	if result, payout := settleHand(&blackjackHand{bet: 10, cards: []card{{rank: "10"}, {rank: "8"}}}, 21, true, rules); result != "dealer blackjack" || payout != 0 {
		t.Fatalf("dealer blackjack result=%s payout=%d", result, payout)
	}
	if result, payout := settleHand(&blackjackHand{bet: 10, cards: []card{{rank: "10"}, {rank: "8"}}}, 22, false, rules); result != "win" || payout != 20 {
		t.Fatalf("dealer bust result=%s payout=%d", result, payout)
	}
}

func TestBlackjackTableStartsAfterAllPlayersBet(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack, MaxPlayers: 2}, newTestRand())
	table.join("alice")
	table.join("bob")
	wa, wb := &testWallet{credits: 100}, &testWallet{credits: 100}
	if events := table.bet("alice", "", 10, wa); hasMessage(events, "round begins") {
		t.Fatal("round started before all players bet")
	}
	events := table.bet("bob", "", 10, wb)
	if !hasSnapshot(events) {
		t.Fatalf("round did not start after all players bet: %+v", events)
	}
}

func TestSoloBlackjackTurnDoesNotTimeout(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack, Blackjack: BlackjackConfig{ActionTicks: 1}}, newTestRand())
	table.participants["alice"] = &blackjackPlayer{
		Participant: Participant{Name: "alice", HasBet: true, Connected: true},
		hands:       []*blackjackHand{{bet: 10, cards: []card{{rank: "10"}, {rank: "6"}}}},
	}
	table.order = []string{"alice"}
	table.phase = blackjackPlaying
	if events := table.tick(); len(events) != 0 {
		t.Fatalf("solo action timed out: %+v", events)
	}
	if table.phase != blackjackPlaying {
		t.Fatalf("solo table phase changed to %s", table.phase)
	}
}

func TestBlackjackRebetKeepsLastWager(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack, MinBet: 5, MaxBet: 100}, newTestRand())
	table.participants["alice"] = &blackjackPlayer{Participant: Participant{Name: "alice"}, lastBet: 25}
	if spot, got := table.rebet("alice"); got != 25 || spot != "" {
		t.Fatalf("rebet=(%q, %d), want (\"\", 25)", spot, got)
	}
}

func TestBlackjackPromptNamesActiveHandAndOnlyOffersAvailableActions(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack}, newTestRand())
	table.participants["alice"] = &blackjackPlayer{Participant: Participant{Name: "alice", HasBet: true, Connected: true}, hands: []*blackjackHand{{bet: 10, cards: []card{{rank: "K"}, {rank: "6"}}}}}
	table.order = []string{"alice"}
	table.activePlayer = 0
	table.activeHand = 0
	table.dealer.cards = []card{{rank: "10"}, {rank: "7"}}
	event := table.prompt()[0]
	if event.Message != "Your turn (K,6).\nActions: hit, stand, double" {
		t.Fatalf("prompt=%q", event.Message)
	}
	if event.Menu == nil || event.Menu.Kind != MenuBlackjackTurn || event.Menu.Title != "Your turn (K,6)" ||
		len(event.Menu.Actions) != 3 || event.Menu.Actions[0] != "hit" || event.Menu.Actions[1] != "stand" || event.Menu.Actions[2] != "double" {
		t.Fatalf("prompt menu=%+v", event.Menu)
	}
}

func TestBlackjackMenuEventCarriesStructuredMenu(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack}, newTestRand())
	event := table.menuEvent("alice")
	if event.Menu == nil || event.Menu.Kind != MenuBlackjackBet {
		t.Fatalf("menu event=%+v", event.Menu)
	}
	if len(event.Menu.Commands) != 3 || event.Menu.Commands[0].Command != "bet" || event.Menu.Commands[2].Command != "stop" {
		t.Fatalf("bet menu commands=%+v", event.Menu.Commands)
	}
}

func TestBlackjackActionCommandsDescribeEachAction(t *testing.T) {
	commands := blackjackActionCommands([]string{"hit", "stand", "double", "split", "surrender"})
	if len(commands) != 5 {
		t.Fatalf("commands=%+v", commands)
	}
	for _, c := range commands {
		if c.Desc == "" {
			t.Fatalf("action %q missing a description", c.Command)
		}
	}
}

func TestBlackjackInsurancePaysOnDealerBlackjack(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack}, newTestRand())
	w := &testWallet{credits: 100}
	table.participants["alice"] = &blackjackPlayer{
		Participant: Participant{Name: "alice", HasBet: true, Connected: true, Wallet: w, Wager: Wager{Total: 10}},
		hands:       []*blackjackHand{{bet: 10, cards: []card{{rank: "10"}, {rank: "8"}}}},
	}
	table.order = []string{"alice"}
	table.dealer.cards = []card{{rank: "K"}, {rank: "A"}}
	table.phase = blackjackInsurance
	events := table.action("alice", "insurance")
	if w.credits != 95 {
		t.Fatalf("insurance stake credits=%d, want 95", w.credits)
	}
	if w.paid != 15 {
		t.Fatalf("insurance payout=%d, want 15 (stake + 2x)", w.paid)
	}
	if !hasMessage(events, "Insurance pays 15 credits.") {
		t.Fatalf("missing insurance payout message: %+v", events)
	}
	if table.phase != blackjackWaiting {
		t.Fatalf("phase=%s, want waiting after dealer blackjack settles", table.phase)
	}
}

func TestBlackjackInsuranceDeclineContinuesRound(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack}, newTestRand())
	w := &testWallet{credits: 100}
	table.participants["alice"] = &blackjackPlayer{
		Participant: Participant{Name: "alice", HasBet: true, Connected: true, Wallet: w, Wager: Wager{Total: 10}},
		hands:       []*blackjackHand{{bet: 10, cards: []card{{rank: "10"}, {rank: "8"}}}},
	}
	table.order = []string{"alice"}
	table.dealer.cards = []card{{rank: "9"}, {rank: "A"}}
	table.phase = blackjackInsurance
	events := table.action("alice", "stand")
	if !hasMessage(events, "You decline insurance.") {
		t.Fatalf("missing decline message: %+v", events)
	}
	if table.phase != blackjackPlaying {
		t.Fatalf("phase=%s, want playing after insurance declined", table.phase)
	}
	if !hasMessage(events, "Your turn (10,8).") {
		t.Fatalf("round did not continue after insurance: %+v", events)
	}
}

func TestBlackjackDoubleSetsBust(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack}, newTestRand())
	// decks 0 disables the shuffle threshold so the forced card is drawn.
	table.shoe = &shoe{cards: []card{{rank: "K"}}}
	w := &testWallet{credits: 100}
	table.participants["alice"] = &blackjackPlayer{
		Participant: Participant{Name: "alice", HasBet: true, Connected: true, Wallet: w, Wager: Wager{Total: 10}},
		hands:       []*blackjackHand{{bet: 10, cards: []card{{rank: "10"}, {rank: "6"}}}},
	}
	table.order = []string{"alice"}
	table.dealer.cards = []card{{rank: "10"}, {rank: "7"}}
	table.phase = blackjackPlaying
	hand := table.participants["alice"].hands[0]
	events := table.action("alice", "double")
	if !hand.bust {
		t.Fatal("doubling into 26 did not set the bust flag")
	}
	if !hasMessage(events, "loses") {
		t.Fatalf("busted double did not settle as a loss: %+v", events)
	}
}

func TestBlackjackSplitAcesOnlyAllowResplit(t *testing.T) {
	resplit := true
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack, Blackjack: BlackjackConfig{ResplitAces: &resplit}}, newTestRand())
	w := &testWallet{credits: 100}
	table.participants["alice"] = &blackjackPlayer{
		Participant: Participant{Name: "alice", HasBet: true, Connected: true, Wallet: w, Wager: Wager{Total: 10}},
		hands:       []*blackjackHand{{bet: 10, fromSplit: true, cards: []card{{rank: "A"}, {rank: "A"}}}},
	}
	table.order = []string{"alice"}
	table.dealer.cards = []card{{rank: "10"}, {rank: "7"}}
	table.phase = blackjackPlaying
	p := table.participants["alice"]
	actions := table.availableActions(p, p.hands[0])
	if len(actions) != 2 || actions[0] != "stand" || actions[1] != "split" {
		t.Fatalf("split ace actions=%v, want [stand split]", actions)
	}
	if events := table.action("alice", "hit"); !hasMessage(events, "Split aces cannot be hit.") {
		t.Fatalf("hit on split aces not rejected: %+v", events)
	}
}

func TestBlackjackBettingIgnoresDisconnected(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack, MaxPlayers: 2}, newTestRand())
	table.join("alice")
	table.join("bob")
	table.markDisconnected("bob")
	w := &testWallet{credits: 100}
	events := table.bet("alice", "", 10, w)
	if !hasSnapshot(events) {
		t.Fatalf("disconnected player stalled the round: %+v", events)
	}
}

func TestBlackjackReconnectShowsTableSnapshot(t *testing.T) {
	table := newBlackjackTable(1, TableConfig{ID: "blackjack", Game: GameBlackjack}, newTestRand())
	table.participants["alice"] = &blackjackPlayer{
		Participant: Participant{Name: "alice", HasBet: true, Connected: true},
		hands:       []*blackjackHand{{bet: 10, cards: []card{{rank: "10"}, {rank: "8"}}}},
	}
	table.order = []string{"alice"}
	table.dealer.cards = []card{{rank: "10"}, {rank: "7"}}
	table.phase = blackjackPlaying
	events := table.join("alice")
	if len(events) != 1 || events[0].Blackjack == nil {
		t.Fatalf("reconnect during play lacked a table snapshot: %+v", events)
	}
}

func hasMessage(events []Event, fragment string) bool {
	for _, event := range events {
		if contains(event.Message, fragment) || contains(event.PrivateMessage, fragment) {
			return true
		}
	}
	return false
}

func hasSnapshot(events []Event) bool {
	for _, event := range events {
		if event.Blackjack != nil {
			return true
		}
	}
	return false
}

func contains(value, fragment string) bool {
	return strings.Contains(value, fragment)
}

func newTestRand() *rand.Rand { return rand.New(rand.NewSource(7)) }