aboutsummaryrefslogtreecommitdiff
path: root/internal/player/store_test.go
blob: 2987d0990222a158a2fc267d7ebc641aa4cd74fe (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
package player

import (
	"os"
	"path/filepath"
	"reflect"
	"sync"
	"testing"
)

func testAccountStore(t *testing.T) *AccountStore {
	t.Helper()
	dir := t.TempDir()
	for _, kind := range []string{"accounts", "characters"} {
		if err := os.MkdirAll(filepath.Join(dir, "players", kind), 0755); err != nil {
			t.Fatal(err)
		}
	}
	return NewAccountStore(dir)
}

func TestSaveGodPlayerDoesNotMutateLiveState(t *testing.T) {
	s := testAccountStore(t)
	p := New("Alice")
	p.GodBackup = p.Skills
	p.Skills = map[SkillName]int{Hitpoints: 1000000}
	p.GodMode = true
	p.HP = 999
	expected := *p
	expected.Skills = p.GodBackup
	wantHP := expected.MaxHP()

	// Concurrent saves of an otherwise immutable player used to race while
	// temporarily swapping Skills/HP. Saving must be observationally read-only.
	var wg sync.WaitGroup
	for i := 0; i < 2; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			if err := s.SaveCharacter(p); err != nil {
				t.Error(err)
			}
		}()
	}
	wg.Wait()
	if p.HP != 999 || p.Skills[Hitpoints] != 1000000 {
		t.Fatal("save changed live god-mode state")
	}
	saved, err := s.LoadCharacter(p.Name)
	if err != nil {
		t.Fatal(err)
	}
	if saved.HP != wantHP || !reflect.DeepEqual(saved.Skills, p.GodBackup) {
		t.Fatalf("saved god-mode stats instead of underlying character: %+v", saved)
	}
}

func TestAccountStoreIgnoresNonYAMLFiles(t *testing.T) {
	s := testAccountStore(t)
	for _, path := range []string{s.AccountPath("Alice"), s.CharPath("Alice")} {
		for _, name := range []string{"Alice.yaml", ".save-stale", "Backup.yaml.bak"} {
			if err := os.WriteFile(filepath.Join(filepath.Dir(path), name), nil, 0600); err != nil {
				t.Fatal(err)
			}
		}
	}
	for _, list := range []func() ([]string, error){s.ListAccountNames, s.ListCharacterNames} {
		got, err := list()
		if err != nil || !reflect.DeepEqual(got, []string{"Alice"}) {
			t.Fatalf("list = %v, %v", got, err)
		}
	}
	for _, find := range []func(string) (string, error){s.FindAccount, s.FindCharacter} {
		if got, err := find("alice"); err != nil || got != "Alice" {
			t.Fatalf("case-insensitive lookup = %q, %v", got, err)
		}
		if _, err := find(".save-stale"); err == nil {
			t.Fatal("temporary file treated as an account/character")
		}
	}
}

func TestLoadCharacterReconcilesNameToStem(t *testing.T) {
	s := testAccountStore(t)
	if err := os.WriteFile(s.CharPath("alice"), []byte("name: WRONG\nroom_id: 1\n"), 0600); err != nil {
		t.Fatal(err)
	}
	p, err := s.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if p.Name != "alice" {
		t.Fatalf("LoadCharacter reconciled name to %q, want %q", p.Name, "alice")
	}
}

func TestLoadAccountReconcilesNameToStem(t *testing.T) {
	s := testAccountStore(t)
	if err := os.WriteFile(s.AccountPath("alice"), []byte("name: WRONG\n"), 0600); err != nil {
		t.Fatal(err)
	}
	acc, err := s.LoadAccount("alice")
	if err != nil {
		t.Fatal(err)
	}
	if acc.Name != "alice" {
		t.Fatalf("LoadAccount reconciled name to %q, want %q", acc.Name, "alice")
	}
}