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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
package game
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"thehouseoficarus/internal/combat"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
"thehouseoficarus/internal/world"
)
// removeTestConn is a minimal Conn that records writes for assertions.
type removeTestConn struct{ buf []byte }
func (c *removeTestConn) ReadMessage() (string, error) { return "", nil }
func (c *removeTestConn) Write(b []byte) (int, error) {
c.buf = append(c.buf, b...)
return len(b), nil
}
func (c *removeTestConn) Close() error { return nil }
func (c *removeTestConn) SetEcho(bool) error { return nil }
func (c *removeTestConn) output() string { return string(c.buf) }
// newRemoveGame builds a Game with the data stores roomRemove touches, a Hub
// (so relocate logic is exercised), and no telephony. Rooms are written into
// dir by the caller.
func newRemoveGame(t *testing.T, dir string) *Game {
t.Helper()
g := &Game{
Deps: Deps{
World: world.New(dir),
MobStore: world.NewMobStore(dir),
AccountStore: player.NewAccountStore(dir),
DataDir: dir,
},
Hub: net.NewHub(),
Combat: combat.NewTracker(),
safespot: NewSafespotManager(),
}
return g
}
func newRemoveSession(p *player.Player) *net.Session {
return &net.Session{Conn: &removeTestConn{}, Player: p, State: net.StateGame}
}
func writeRemoveRoomFile(t *testing.T, dir string, id int, body string) {
t.Helper()
roomsDir := filepath.Join(dir, "rooms")
if err := os.MkdirAll(roomsDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(roomsDir, strconv.Itoa(id)+".yaml"), []byte(body), 0644); err != nil {
t.Fatal(err)
}
}
func assertOutContains(t *testing.T, sess *net.Session, sub string) {
t.Helper()
out := sess.Conn.(*removeTestConn).output()
if !strings.Contains(out, sub) {
t.Errorf("expected session output to contain %q, got:\n%s", sub, out)
}
}
func assertOutNotContains(t *testing.T, sess *net.Session, sub string) {
t.Helper()
out := sess.Conn.(*removeTestConn).output()
if strings.Contains(out, sub) {
t.Errorf("expected session output to NOT contain %q, got:\n%s", sub, out)
}
}
// reloadAfterCmd re-reads a room from disk after a command has mutated it.
func reloadRoom(t *testing.T, g *Game, id int) *world.Room {
t.Helper()
r, err := g.World.LoadRoom(id)
if err != nil {
t.Fatalf("reload room %d: %v", id, err)
}
return r
}
func TestRoomRemoveSuccessPull(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n west: {room: 2}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "You remove #2 (B) and pull #3 (C) back to the east.")
// A's east exit now leads to C (3), not B (2).
a := reloadRoom(t, g, 1)
if a.Exits[world.East].Room != 3 {
t.Errorf("A.Exits[east] = %d, want 3", a.Exits[world.East].Room)
}
// C's west exit now leads to A (1), not B (2).
c := reloadRoom(t, g, 3)
if c.Exits[world.West].Room != 1 {
t.Errorf("C.Exits[west] = %d, want 1", c.Exits[world.West].Room)
}
// B's file is gone.
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); !os.IsNotExist(err) {
t.Errorf("expected 2.yaml to be deleted, got err=%v", err)
}
}
func TestRoomRemoveDiagonalPull(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n northeast: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n northeast: {room: 3}\n southwest: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n southwest: {room: 2}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"northeast"})
assertOutContains(t, sess, "You remove #2 (B) and pull #3 (C) back to the northeast.")
a := reloadRoom(t, g, 1)
if a.Exits[world.Northeast].Room != 3 {
t.Errorf("A.Exits[ne] = %d, want 3", a.Exits[world.Northeast].Room)
}
c := reloadRoom(t, g, 3)
if c.Exits[world.Southwest].Room != 1 {
t.Errorf("C.Exits[sw] = %d, want 1", c.Exits[world.Southwest].Room)
}
}
func TestRoomRemoveNoExit(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "There is no exit to the east from here.")
}
func TestRoomRemoveNotInsertChain(t *testing.T) {
dir := t.TempDir()
// B does not lead back to A via west.
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n west: {room: 2}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "does not lead back here via west")
// Nothing was deleted.
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); err != nil {
t.Errorf("2.yaml should still exist, got err=%v", err)
}
}
func TestRoomRemoveDeadEndFarRoom(t *testing.T) {
dir := t.TempDir()
// B leads back to A but has no forward east exit.
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n west: {room: 1}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "no room beyond to pull")
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); err != nil {
t.Errorf("2.yaml should still exist, got err=%v", err)
}
}
func TestRoomRemoveOtherExitsInB(t *testing.T) {
dir := t.TempDir()
// B has east (fwd), west (back), AND up (extra).
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n up: {room: 4}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n west: {room: 2}\n")
writeRemoveRoomFile(t, dir, 4, "name: D\nexits:\n down: {room: 2}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "has other exits besides west and east")
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); err != nil {
t.Errorf("2.yaml should still exist, got err=%v", err)
}
}
func TestRoomRemoveFarReciprocityBroken(t *testing.T) {
dir := t.TempDir()
// C's west does not point back to B.
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "does not lead back to #2")
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); err != nil {
t.Errorf("2.yaml should still exist, got err=%v", err)
}
}
func TestRoomRemoveExtraInboundEdge(t *testing.T) {
dir := t.TempDir()
// A clean chain A-east->B-east->C, but an unrelated room 5 also points to B.
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n west: {room: 2}\n")
writeRemoveRoomFile(t, dir, 5, "name: E\nexits:\n north: {room: 2}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "other exit(s) point into #2")
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); err != nil {
t.Errorf("2.yaml should still exist, got err=%v", err)
}
}
// TestRoomRemoveOverlap mirrors TestRemoveGridConflictsOverlap: removing B
// pulls room 6 onto room 5's cell, which must be rejected.
func TestRoomRemoveOverlap(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n south: {room: 4}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n south: {room: 6}\n west: {room: 2}\n")
writeRemoveRoomFile(t, dir, 4, "name: D\nexits:\n east: {room: 5}\n north: {room: 1}\n")
writeRemoveRoomFile(t, dir, 5, "name: E\nexits:\n west: {room: 4}\n")
writeRemoveRoomFile(t, dir, 6, "name: F\nexits:\n north: {room: 3}\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
assertOutContains(t, sess, "pulling the far end would cause grid collision")
if _, err := os.Stat(filepath.Join(dir, "rooms", "2.yaml")); err != nil {
t.Errorf("2.yaml should still exist, got err=%v", err)
}
}
func TestRoomRemoveInvalidDirection(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"sideways"})
assertOutContains(t, sess, "Invalid direction")
}
func TestRoomRemoveUsage(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n")
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, nil)
assertOutContains(t, sess, "Usage: room remove <direction>")
}
// TestRoomRemovePreservesExitProps confirms the near-side exit's flags travel
// onto the repointed A→dir→C edge (and the far side's onto C→oppositeDir→A).
func TestRoomRemovePreservesExitProps(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, `name: A
exits:
east:
room: 2
blocked_message: "A rock blocks the way."
hidden: true
`)
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, `name: C
exits:
west:
room: 2
blocked_message: "The far side is sealed."
`)
g := newRemoveGame(t, dir)
sess := newRemoveSession(&player.Player{Name: "tester", RoomID: 1})
g.roomRemove(sess, []string{"east"})
a := reloadRoom(t, g, 1)
if a.Exits[world.East].Room != 3 {
t.Errorf("A.Exits[east].Room = %d, want 3", a.Exits[world.East].Room)
}
if a.Exits[world.East].BlockedMessage != "A rock blocks the way." {
t.Errorf("A.Exits[east].BlockedMessage = %q, want preserved", a.Exits[world.East].BlockedMessage)
}
if !a.Exits[world.East].Hidden {
t.Error("A.Exits[east].Hidden should be preserved true")
}
c := reloadRoom(t, g, 3)
if c.Exits[world.West].Room != 1 {
t.Errorf("C.Exits[west].Room = %d, want 1", c.Exits[world.West].Room)
}
if c.Exits[world.West].BlockedMessage != "The far side is sealed." {
t.Errorf("C.Exits[west].BlockedMessage = %q, want preserved", c.Exits[world.West].BlockedMessage)
}
}
// TestRoomRemoveRelocatesPlayersInB verifies another session standing in B is
// moved to A and notified. The caller stays in A.
func TestRoomRemoveRelocatesPlayersInB(t *testing.T) {
dir := t.TempDir()
writeRemoveRoomFile(t, dir, 1, "name: A\nexits:\n east: {room: 2}\n")
writeRemoveRoomFile(t, dir, 2, "name: B\nexits:\n east: {room: 3}\n west: {room: 1}\n")
writeRemoveRoomFile(t, dir, 3, "name: C\nexits:\n west: {room: 2}\n")
g := newRemoveGame(t, dir)
caller := newRemoveSession(&player.Player{Name: "builder", RoomID: 1})
bystander := newRemoveSession(&player.Player{Name: "wanderer", RoomID: 2})
g.Hub.Add(caller)
g.Hub.Add(bystander)
g.Hub.EnterRoom(caller, 1)
g.Hub.EnterRoom(bystander, 2)
g.roomRemove(caller, []string{"east"})
if bystander.Player.RoomID != 1 {
t.Errorf("bystander RoomID = %d, want 1 (relocated to A)", bystander.Player.RoomID)
}
assertOutContains(t, bystander, "The room around you collapses")
}
|