aboutsummaryrefslogtreecommitdiff
path: root/internal/game/swapid_content_test.go
blob: f1eb97887d2963e8850e9bacc0552d0d4621880e (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
package game

// Regression: `swapid` rewrites structural room references in non-room content
// files (modules, triggers, items, ...) as well as in data/rooms. Before this,
// only room YAML was scanned, so a transport destination or trigger teleport
// silently kept pointing at the pre-swap ID.

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

func TestSwapIDRewritesNonRoomContent(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("admin")
	h.seedCharacter("admin", "admin")
	acc, err := h.g.AccountStore.LoadAccount("admin")
	if err != nil {
		t.Fatal(err)
	}
	acc.Admin = true
	if err := h.g.AccountStore.SaveAccount(acc); err != nil {
		t.Fatal(err)
	}
	sess, _ := h.connect("admin")

	// A transport module destination and an author value that equals a room ID.
	modulesDir := filepath.Join(h.dir, "modules")
	if err := os.MkdirAll(modulesDir, 0o755); err != nil {
		t.Fatal(err)
	}
	modulePath := filepath.Join(modulesDir, "transport_home.yaml")
	if err := os.WriteFile(modulePath, []byte("name: Home\ncategory: transport\ndestination: 1\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	itemsDir := filepath.Join(h.dir, "items")
	if err := os.MkdirAll(itemsDir, 0o755); err != nil {
		t.Fatal(err)
	}
	itemPath := filepath.Join(itemsDir, "gem.yaml")
	if err := os.WriteFile(itemPath, []byte("name: gem\nvalue: 1\n"), 0o644); err != nil {
		t.Fatal(err)
	}

	h.g.RunOnOwner(func() { h.g.executeSwapID(sess, []string{"1", "2"}, "swapid 1 2") })

	module, err := os.ReadFile(modulePath)
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(string(module), "destination: 2") {
		t.Errorf("module destination not swapped: %s", module)
	}
	// value: 1 is an author value, not a structural room reference.
	item, err := os.ReadFile(itemPath)
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(string(item), "value: 1") {
		t.Errorf("author value equal to a swapped ID was rewritten: %s", item)
	}
}