aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_colors_test.go
blob: cb63714a096d4c222bc7c4ae26fe730b30747393 (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
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\"\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))
	}
	preview, _ := out["preview"].([]any)
	if len(preview) < 6 {
		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 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"
	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"])
	}
}

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" {
			continue
		}
		if !seen[cat.Name] {
			t.Errorf("preview missing category: %s", cat.Name)
		}
	}
	_ = io.Discard
}