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
|
package game
import (
"io"
"testing"
"thehouseoficarus/internal/behavior"
"thehouseoficarus/internal/combat"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
)
type nopConn struct{}
func (nopConn) ReadMessage() (string, error) { return "", io.EOF }
func (nopConn) Write(p []byte) (int, error) { return len(p), nil }
func (nopConn) Close() error { return nil }
func (nopConn) SetEcho(bool) error { return nil }
func newTestGame() *Game {
return &Game{
GlobalFlags: NewGlobalFlagStore(),
Combat: combat.NewTracker(),
}
}
func newTestGameWithStore(t *testing.T) *Game {
g := newTestGame()
g.AccountStore = player.NewAccountStore(t.TempDir())
return g
}
func newTestSession(p *player.Player, acc *player.Account) *net.Session {
if p.Options == nil {
p.Options = make(map[string]any)
}
return &net.Session{Player: p, Conn: nopConn{}, State: net.StateGame, Account: acc}
}
func newTestPlayer(name string) *player.Player {
return &player.Player{
Name: name,
RoomID: 100,
Skills: map[player.SkillName]int{player.Hitpoints: player.XPForLevel(99)},
HP: 50,
Options: make(map[string]any),
}
}
func TestRunInteractionNoMessagesAppliesInstantly(t *testing.T) {
g := newTestGameWithStore(t)
p := newTestPlayer("test")
sess := newTestSession(p, nil)
step := &behavior.StepAction{
NodeAction: behavior.NodeAction{Heal: 10},
}
g.runInteraction(sess, p, step, 100, scopePlayer, nil)
if p.Action != nil {
t.Error("expected no pending action for message-less interaction")
}
if p.HP != 60 {
t.Errorf("expected HP 60 after heal, got %d", p.HP)
}
}
func TestRunInteractionWithMessagesStartsSequence(t *testing.T) {
g := newTestGame()
p := newTestPlayer("test")
p.Credits = 100
sess := newTestSession(p, nil)
step := &behavior.StepAction{
NodeAction: behavior.NodeAction{Credits: 50},
Messages: []behavior.DelayedMessage{
{Message: "You feel a strange energy.", Delay: 3},
},
}
g.runInteraction(sess, p, step, 100, scopePlayer, nil)
if p.Action == nil {
t.Fatal("expected pending action for message-bearing interaction")
}
if p.Action.Type != behavior.TypeInteractionSeq {
t.Errorf("expected TypeInteractionSeq, got %v", p.Action.Type)
}
if p.Action.WaitLeft != 3 {
t.Errorf("expected WaitLeft 3, got %d", p.Action.WaitLeft)
}
if p.Credits != 100 {
t.Errorf("expected credits unchanged (100), got %d", p.Credits)
}
}
func TestAdvanceInteractionSeqDrainsZeroDelay(t *testing.T) {
g := newTestGameWithStore(t)
p := newTestPlayer("test")
sess := newTestSession(p, nil)
step := &behavior.StepAction{
NodeAction: behavior.NodeAction{Heal: 10},
Messages: []behavior.DelayedMessage{
{Message: "First.", Delay: 0},
{Message: "Second.", Delay: 0},
{Message: "Third.", Delay: 0},
},
}
g.startInteractionSeq(sess, p, step, 100, scopePlayer, nil)
if p.Action == nil || p.Action.WaitLeft != 1 {
t.Fatalf("expected WaitLeft 1 (delay 0 floored), got Action:%v", p.Action)
}
g.advanceInteractionSeq(sess, p)
if p.Action != nil {
t.Error("expected action cancelled after draining all messages")
}
if p.HP != 60 {
t.Errorf("expected HP 60 after heal, got %d", p.HP)
}
}
func TestAdvanceInteractionSeqHonorsDelay(t *testing.T) {
g := newTestGameWithStore(t)
p := newTestPlayer("test")
sess := newTestSession(p, nil)
step := &behavior.StepAction{
NodeAction: behavior.NodeAction{Heal: 10},
Messages: []behavior.DelayedMessage{
{Message: "First.", Delay: 2},
{Message: "Second.", Delay: 3},
},
}
g.startInteractionSeq(sess, p, step, 100, scopePlayer, nil)
if p.Action == nil || p.Action.WaitLeft != 2 {
t.Fatalf("expected WaitLeft 2, got %v", p.Action)
}
p.Action.WaitLeft = 1
g.advanceInteractionSeq(sess, p)
if p.Action == nil {
t.Fatal("expected action still running (second message pending)")
}
if p.Action.WaitLeft != 3 {
t.Errorf("expected WaitLeft 3 (second message delay), got %d", p.Action.WaitLeft)
}
if p.HP != 50 {
t.Error("heal should not have fired yet")
}
g.advanceInteractionSeq(sess, p)
if p.Action != nil {
t.Error("expected action cancelled after last message + heal")
}
if p.HP != 60 {
t.Errorf("expected HP 60 after heal, got %d", p.HP)
}
}
func TestInteractionSeqCancelPreventsTrailingEffects(t *testing.T) {
g := newTestGame()
p := newTestPlayer("test")
sess := newTestSession(p, nil)
step := &behavior.StepAction{
NodeAction: behavior.NodeAction{Heal: 10},
Messages: []behavior.DelayedMessage{
{Message: "Hello.", Delay: 5},
},
}
g.startInteractionSeq(sess, p, step, 100, scopePlayer, nil)
if p.Action == nil {
t.Fatal("expected pending action")
}
g.cancelAction(p)
if p.Action != nil {
t.Error("expected action to be cancelled")
}
if p.HP != 50 {
t.Error("heal should not have fired after cancel")
}
}
func TestApplyStepActionEmitsAllMessages(t *testing.T) {
g := newTestGameWithStore(t)
p := newTestPlayer("test")
sess := newTestSession(p, nil)
step := &behavior.StepAction{
NodeAction: behavior.NodeAction{Heal: 10},
Messages: []behavior.DelayedMessage{
{Message: "A.", Delay: 99},
{Message: "B.", Delay: 99},
},
}
g.applyStepAction(sess, p, step, 100, scopePlayer, nil)
if p.HP != 60 {
t.Errorf("expected HP 60 after heal, got %d", p.HP)
}
}
|