aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/room_rename_content_test.go
blob: 179b9f0a1c0ad25d9e7a33a4bfe6a3a1a6760e3c (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
package admin

// Regression: renaming a room must rewrite structural room references in
// non-room content files (courses, modules, triggers, items, objects, mobs),
// and undo must restore those files too. Before this, only data/rooms was
// scanned, so a course obstacle or transport destination silently kept the
// old ID.

import (
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func writeContent(t *testing.T, s *AdminServer, sub, name, body string) string {
	t.Helper()
	dir := filepath.Join(s.dataDir, sub)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatal(err)
	}
	path := filepath.Join(dir, name)
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	return path
}

func readFile(t *testing.T, path string) string {
	t.Helper()
	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	return string(data)
}

func TestRoomRenameRewritesNonRoomContent(t *testing.T) {
	s := newTestAdminServer(t, map[int]string{
		5: "id: 5\nname: Lab\nexits:\n  north:\n    room: 6\n",
		6: "id: 6\nname: Hall\nexits:\n  south:\n    room: 5\n",
	})
	s.rewriteRoomIDs = func(map[int]int) {}

	modulePath := writeContent(t, s, "modules", "transport_home.yaml",
		"name: Home\ncategory: transport\ndestination: 5\n")
	coursePath := writeContent(t, s, "courses", "agility.yaml",
		"name: Agility\nstart_room: 6\nobstacles:\n  - room_id: 5\n    verb: climb\n")
	// An author value equal to a room ID must survive untouched.
	itemPath := writeContent(t, s, "items", "gem.yaml",
		"name: gem\nvalue: 5\n")

	if rec := renameRoom(t, s, 5, 7); rec.Code != http.StatusOK {
		t.Fatalf("rename failed: %d %s", rec.Code, rec.Body.String())
	}

	if got := readFile(t, modulePath); !strings.Contains(got, "destination: 7") {
		t.Errorf("module destination not rewritten: %s", got)
	}
	course := readFile(t, coursePath)
	if !strings.Contains(course, "room_id: 7") {
		t.Errorf("course obstacle room_id not rewritten: %s", course)
	}
	// start_room: 6 is not the renamed ID, so it stays.
	if !strings.Contains(course, "start_room: 6") {
		t.Errorf("unrelated course start_room changed: %s", course)
	}
	if got := readFile(t, itemPath); !strings.Contains(got, "value: 5") {
		t.Errorf("author value equal to the renamed ID was rewritten: %s", got)
	}

	// Undo restores the content files alongside the room.
	if s.undoStack.Undo() == nil {
		t.Fatal("undo failed")
	}
	if got := readFile(t, modulePath); !strings.Contains(got, "destination: 5") {
		t.Errorf("undo did not restore module destination: %s", got)
	}
	if got := readFile(t, coursePath); !strings.Contains(got, "room_id: 5") {
		t.Errorf("undo did not restore course obstacle: %s", got)
	}
}