aboutsummaryrefslogtreecommitdiff
path: root/internal/game/render_help_test.go
blob: dcfa619e326b945d086df7707a15611df07b9741 (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
110
111
112
package game

import (
	"strings"
	"testing"
)

func loadHelpOrSkip(t *testing.T) []HelpDef {
	t.Helper()
	helps, err := LoadHelp("../../data")
	if err != nil {
		t.Fatal(err)
	}
	if len(helps) == 0 {
		t.Fatal("no help topics loaded")
	}
	return helps
}

func TestLoadHelpAllFilesParse(t *testing.T) {
	helps := loadHelpOrSkip(t)
	seen := map[string]bool{}
	for _, h := range helps {
		if strings.TrimSpace(h.Name) == "" {
			t.Errorf("help entry with empty name: %q", h.Content)
		}
		if seen[h.Name] {
			t.Errorf("duplicate help topic %q", h.Name)
		}
		seen[h.Name] = true
	}
}

func TestNewPlayerHelpExists(t *testing.T) {
	helps := loadHelpOrSkip(t)
	found := false
	for _, h := range helps {
		if h.Name == "newplayer" {
			found = true
		}
	}
	if !found {
		t.Fatal("help topic 'newplayer' missing (referenced by intro room)")
	}
}

func TestHelpAliasesResolveWithoutCollision(t *testing.T) {
	helps := loadHelpOrSkip(t)
	owner := map[string]string{}
	for _, h := range helps {
		for _, a := range h.Aliases {
			a = strings.ToLower(strings.TrimSpace(a))
			if a == "" {
				continue
			}
			if prev, dup := owner[a]; dup {
				t.Errorf("alias %q claimed by both %q and %q", a, prev, h.Name)
			}
			owner[a] = h.Name
		}
	}
	for topic, want := range map[string]string{
		"mine":       "mining",
		"chop":       "woodcutting",
		"fish":       "fishing",
		"fletch":     "fletch",
		"newplayer":  "newplayer",
		"new player": "newplayer",
	} {
		resolved := ""
		for _, h := range helps {
			if matchesHelpTopic(h, topic) {
				resolved = h.Name
				break
			}
		}
		if resolved != want {
			t.Errorf("help %q resolved to %q, want %q", topic, resolved, want)
		}
	}
}

// Every verb advertised in the help table must actually be a command. "farm"
// was listed for a long time while not existing, so typing it returned
// "Unknown command." A help entry is the first thing a new player trusts.
func TestHelpCommandListMatchesRegistry(t *testing.T) {
	for _, entry := range commandList {
		for _, verb := range strings.Split(entry.Name, "/") {
			verb = strings.TrimSpace(verb)
			if verb == "" {
				continue
			}
			if _, ok := commandRegistry[verb]; ok {
				continue
			}
			if _, ok := verbAliases[verb]; ok {
				continue
			}
			t.Errorf("help lists %q (from %q) but it is not a registered command", verb, entry.Name)
		}
	}
}

// alwaysAvailable is a second hand-maintained verb list; it must not drift
// from the registry either.
func TestAlwaysAvailableCommandsAreRegistered(t *testing.T) {
	for _, verb := range alwaysAvailable {
		if _, ok := commandRegistry[verb]; !ok {
			t.Errorf("alwaysAvailable lists %q but it is not registered", verb)
		}
	}
}