aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_colors_test.go
blob: 1f5d3ebbadb366628929dad212c4c1acac452b4a (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
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
package admin

import (
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"testing"

	"thehouseoficarus/internal/config"
)

func newColorsTestServer(t *testing.T) (*AdminServer, string) {
	t.Helper()
	dir := t.TempDir()
	path := filepath.Join(dir, "config.yaml")
	if err := os.WriteFile(path, []byte("tick_length: 600\ndefault_colors:\n  room_name: \"74 bold\"\nconstant_colors:\n  hp_bar_low: \"A7\"\n"), 0644); err != nil {
		t.Fatal(err)
	}
	loaded, err := config.Load(path)
	if err != nil {
		t.Fatal(err)
	}
	return &AdminServer{cfg: loaded, configPath: path}, path
}

func TestColorsGet(t *testing.T) {
	s, _ := newColorsTestServer(t)
	req := httptest.NewRequest(http.MethodGet, "/api/colors", nil)
	w := httptest.NewRecorder()
	s.getColors(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("status %d", w.Code)
	}
	var out map[string]any
	if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
		t.Fatal(err)
	}
	cats, _ := out["categories"].([]any)
	if len(cats) != len(colorCategories) {
		t.Fatalf("categories count: got %d want %d", len(cats), len(colorCategories))
	}
	constCats, _ := out["constant_categories"].([]any)
	if len(constCats) != len(constantCategories) {
		t.Fatalf("constant_categories count: got %d want %d", len(constCats), len(constantCategories))
	}
	preview, _ := out["preview"].([]any)
	if len(preview) < 7 {
		t.Fatalf("preview sections too few: %d", len(preview))
	}
}

func TestColorsSaveAndPersist(t *testing.T) {
	s, path := newColorsTestServer(t)
	before := s.cfg.DefaultColors["room_name"]

	body := map[string]any{"colors": map[string]string{"room_name": "FF bold"}}
	b, _ := json.Marshal(body)
	req := httptest.NewRequest(http.MethodPost, "/api/colors", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	s.saveColors(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("save status %d: %s", w.Code, w.Body.String())
	}
	if s.cfg.DefaultColors["room_name"] == before {
		t.Fatalf("live map not updated")
	}
	if s.cfg.DefaultColors["room_name"] != "FF bold" {
		t.Fatalf("live value = %q", s.cfg.DefaultColors["room_name"])
	}
	raw, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Contains(raw, []byte("FF bold")) {
		t.Fatalf("config.yaml not persisted with new value; got:\n%s", string(raw))
	}
}

func TestColorsSaveRejectsBadSpec(t *testing.T) {
	s, _ := newColorsTestServer(t)
	body := map[string]any{"colors": map[string]string{"room_name": "zzzzz"}}
	b, _ := json.Marshal(body)
	req := httptest.NewRequest(http.MethodPost, "/api/colors", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	s.saveColors(w, req)
	if w.Code != http.StatusBadRequest {
		t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
	}
}

func TestColorsSaveRejectsUnknownKey(t *testing.T) {
	s, _ := newColorsTestServer(t)
	body := map[string]any{"colors": map[string]string{"nope": "FF"}}
	b, _ := json.Marshal(body)
	req := httptest.NewRequest(http.MethodPost, "/api/colors", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	s.saveColors(w, req)
	if w.Code != http.StatusBadRequest {
		t.Fatalf("expected 400, got %d", w.Code)
	}
}

func TestConstantsSaveAndPersist(t *testing.T) {
	s, path := newColorsTestServer(t)
	before, ok := s.cfg.ConstantColors["hp_bar_low"]
	if !ok || before == "" {
		t.Fatalf("constant not pre-populated: got %q", before)
	}
	body := map[string]any{"constant_colors": map[string]string{"hp_bar_low": "FF bold"}}
	b, _ := json.Marshal(body)
	req := httptest.NewRequest(http.MethodPost, "/api/colors", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	s.saveColors(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("save status %d: %s", w.Code, w.Body.String())
	}
	if s.cfg.ConstantColors["hp_bar_low"] == before {
		t.Fatalf("constant live map not updated")
	}
	if s.cfg.ConstantColors["hp_bar_low"] != "FF bold" {
		t.Fatalf("constant live value = %q", s.cfg.ConstantColors["hp_bar_low"])
	}
	if s.cfg.DefaultColors["room_name"] != "74 bold" {
		t.Fatalf("player color leaked from constant save: room_name=%q", s.cfg.DefaultColors["room_name"])
	}
	raw, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Contains(raw, []byte("FF bold")) {
		t.Fatalf("config.yaml not persisted with new constant value; got:\n%s", string(raw))
	}
}

func TestConstantsSaveRejectsPlayerKey(t *testing.T) {
	s, _ := newColorsTestServer(t)
	// Player categories like "room_name" are not legal constant keys.
	body := map[string]any{"constant_colors": map[string]string{"room_name": "FF"}}
	b, _ := json.Marshal(body)
	req := httptest.NewRequest(http.MethodPost, "/api/colors", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	s.saveColors(w, req)
	if w.Code != http.StatusBadRequest {
		t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
	}
}

func TestColorsOffAllowed(t *testing.T) {
	s, _ := newColorsTestServer(t)
	body := map[string]any{"colors": map[string]string{"mob": "off"}}
	b, _ := json.Marshal(body)
	req := httptest.NewRequest(http.MethodPost, "/api/colors", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	s.saveColors(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("expected 200 for 'off', got %d: %s", w.Code, w.Body.String())
	}
}

func TestColorsReset(t *testing.T) {
	s, _ := newColorsTestServer(t)
	s.cfg.DefaultColors["room_name"] = "FF"
	s.cfg.ConstantColors["hp_bar_low"] = "FF"
	req := httptest.NewRequest(http.MethodPost, "/api/colors/reset", nil)
	w := httptest.NewRecorder()
	s.handleColorsReset(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("status %d: %s", w.Code, w.Body.String())
	}
	if s.cfg.DefaultColors["room_name"] != "74 bold" {
		t.Fatalf("reset did not restore builtin; got %q", s.cfg.DefaultColors["room_name"])
	}
	if s.cfg.ConstantColors["hp_bar_low"] != "A7" {
		t.Fatalf("constant reset did not restore builtin; got %q", s.cfg.ConstantColors["hp_bar_low"])
	}
}

func TestColorsPreviewExhaustive(t *testing.T) {
	s, _ := newColorsTestServer(t)
	req := httptest.NewRequest(http.MethodGet, "/api/colors", nil)
	w := httptest.NewRecorder()
	s.getColors(w, req)
	var out map[string]any
	if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
		t.Fatal(err)
	}
	seen := map[string]bool{}
	for _, sec := range out["preview"].([]any) {
		for _, ln := range sec.(map[string]any)["lines"].([]any) {
			for _, seg := range ln.(map[string]any)["segments"].([]any) {
				c := seg.(map[string]any)["c"].(string)
				if c != "" {
					seen[c] = true
				}
			}
		}
	}
	for _, cat := range colorCategories {
		if cat.Name == "currency_pickup" ||
			cat.Name == "map_blocked" || cat.Name == "sequence" ||
			cat.Name == "science_mod" ||
			// `error` and `warning` used to be previewed as HP-bar proxies;
			// the bars now paint with the real hp_bar_* constants, so these
			// two combat-message categories have no natural example slot.
			cat.Name == "error" || cat.Name == "warning" {
			continue
		}
		if !seen[cat.Name] {
			t.Errorf("preview missing category: %s", cat.Name)
		}
	}
	for _, cat := range constantCategories {
		if !seen[cat.Name] {
			t.Errorf("preview missing constant category: %s", cat.Name)
		}
	}
	_ = io.Discard
}