-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappearance_handlers_test.go
More file actions
412 lines (390 loc) · 13.1 KB
/
Copy pathappearance_handlers_test.go
File metadata and controls
412 lines (390 loc) · 13.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
)
// TestHandleAppearanceUpdate_PersistsFields — POST all three font
// fields, reload config from disk, confirm everything landed.
// Mirrors how the Settings modal's live-apply flow works on the
// frontend.
func TestHandleAppearanceUpdate_PersistsFields(t *testing.T) {
path := withTempConfig(t)
body := `{"ui_font_family":"Inter","ui_font_size":16,"terminal_font_family":"Fira Code","terminal_font_size":18}`
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d body=%s", w.Code, w.Body.String())
}
cfg := loadConfig()
if cfg.UIFontFamily != "Inter" {
t.Errorf("UIFontFamily: got %q want %q", cfg.UIFontFamily, "Inter")
}
if cfg.UIFontSize != 16 {
t.Errorf("UIFontSize: got %d want 16", cfg.UIFontSize)
}
if cfg.TerminalFontFamily != "Fira Code" {
t.Errorf("TerminalFontFamily: got %q want %q", cfg.TerminalFontFamily, "Fira Code")
}
if cfg.TerminalFontSize != 18 {
t.Errorf("TerminalFontSize: got %d want 18", cfg.TerminalFontSize)
}
// Confirm the on-disk file also has them (so a process restart
// would re-load them).
raw, err := readFile(path)
if err != nil {
t.Fatalf("read persisted config: %v", err)
}
if !strings.Contains(string(raw), `"ui_font_family"`) {
t.Errorf("ui_font_family not in persisted file: %s", raw)
}
if !strings.Contains(string(raw), `"ui_font_size"`) {
t.Errorf("ui_font_size not in persisted file: %s", raw)
}
if !strings.Contains(string(raw), `"terminal_font_family"`) {
t.Errorf("terminal_font_family not in persisted file: %s", raw)
}
if !strings.Contains(string(raw), `"terminal_font_size"`) {
t.Errorf("terminal_font_size not in persisted file: %s", raw)
}
}
// TestHandleAppearanceUpdate_PartialUpdate — only one field in the
// body. The others must be left untouched on disk.
func TestHandleAppearanceUpdate_PartialUpdate(t *testing.T) {
withTempConfig(t)
// Seed: all three set.
cfg := loadConfig()
cfg.UIFontFamily = "SeedFamily"
cfg.UIFontSize = 18
cfg.TerminalFontFamily = "SeedTerm"
saveConfig(cfg)
// POST only one field.
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(`{"ui_font_size":13}`))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d", w.Code)
}
cfg2 := loadConfig()
if cfg2.UIFontFamily != "SeedFamily" {
t.Errorf("UIFontFamily should be untouched: got %q", cfg2.UIFontFamily)
}
if cfg2.UIFontSize != 13 {
t.Errorf("UIFontSize should be updated: got %d", cfg2.UIFontSize)
}
if cfg2.TerminalFontFamily != "SeedTerm" {
t.Errorf("TerminalFontFamily should be untouched: got %q", cfg2.TerminalFontFamily)
}
}
// TestHandleAppearanceUpdate_ClampsFontSize — 4 must become 10, 99
// must become 24. Anything outside [10, 24] silently clamps; the
// client could overshoot via stale UI state.
func TestHandleAppearanceUpdate_ClampsFontSize(t *testing.T) {
cases := []struct {
in int
want int
}{
{4, 10}, // below min
{9, 10}, // just below min
{10, 10}, // at min — unchanged
{24, 24}, // at max — unchanged
{99, 24}, // above max
{16, 16}, // middle — unchanged
{0, 10}, // zero (sentinel for "unset") — clamp to min so client sees a real value
}
for _, tc := range cases {
t.Run("", func(t *testing.T) {
withTempConfig(t)
body := `{"ui_font_size":` + itoaSmall(tc.in) + `}`
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d body=%s", w.Code, w.Body.String())
}
cfg := loadConfig()
if cfg.UIFontSize != tc.want {
t.Errorf("size %d: got %d want %d", tc.in, cfg.UIFontSize, tc.want)
}
})
}
}
// TestHandleAppearanceUpdate_ClampsTerminalFontSize — terminal font
// size clamps to [8, 32], but 0 (the "unset/default" sentinel) must
// round-trip as 0 rather than clamping up to 8.
func TestHandleAppearanceUpdate_ClampsTerminalFontSize(t *testing.T) {
cases := []struct {
in int
want int
}{
{4, 8}, // below min
{7, 8}, // just below min
{8, 8}, // at min — unchanged
{32, 32}, // at max — unchanged
{99, 32}, // above max
{0, 0}, // zero (sentinel for "unset") — must NOT clamp up to 8
{16, 16}, // middle — unchanged
}
for _, tc := range cases {
t.Run("", func(t *testing.T) {
withTempConfig(t)
body := `{"terminal_font_size":` + itoaSmall(tc.in) + `}`
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d body=%s", w.Code, w.Body.String())
}
cfg := loadConfig()
if cfg.TerminalFontSize != tc.want {
t.Errorf("size %d: got %d want %d", tc.in, cfg.TerminalFontSize, tc.want)
}
})
}
}
func TestHandleAppearanceUpdate_HostnameOverride(t *testing.T) {
// Shared vectors with test-js/hostOverride.test.js: both sides must
// accept the same values.
cases := []struct {
in string
want string
}{
// hostname_override is a free-form DISPLAY LABEL (it never
// touches a socket URL), so anything printable and short is
// stored verbatim — including values that are not hostnames.
{`example.com:8080`, `example.com:8080`},
{`dusty_potato`, `dusty_potato`},
{`homelab`, `homelab`},
{`etraces fugaces`, `etraces fugaces`},
{``, ``}, // blank clears back to the OS hostname
}
for _, tc := range cases {
t.Run("", func(t *testing.T) {
withTempConfig(t)
body := `{"hostname_override":` + strconv.Quote(tc.in) + `}`
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d body=%s", w.Code, w.Body.String())
}
if cfg := loadConfig(); cfg.HostnameOverride != tc.want {
t.Errorf("in %q: got %q want %q", tc.in, cfg.HostnameOverride, tc.want)
}
})
}
}
func TestHandleAppearanceUpdate_HostnameOverrideRejects(t *testing.T) {
withTempConfig(t)
cfg := loadConfig()
cfg.HostnameOverride = "example.com:8080"
saveConfig(cfg)
// Only things that break the label's role as a DISPLAY string are
// rejected: too long, control characters. Hostname-shaped garbage
// is fine — the label never dials anything.
long := strings.Repeat("a", 65)
for _, bad := range []string{long, "line1\nline2", "tab\there"} {
body := `{"hostname_override":` + strconv.Quote(bad) + `}`
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("in %q: status %d", bad, w.Code)
}
// Rejected input leaves the stored value unchanged.
if got := loadConfig().HostnameOverride; got != "example.com:8080" {
t.Errorf("in %q: stored value changed to %q", bad, got)
}
}
}
func TestHandleAppearanceUpdate_ClampsMobileScrollback(t *testing.T) {
cases := []struct {
in int
want int
}{
{100, 500}, // below min
{499, 500}, // just below min
{500, 500}, // at min — unchanged
{2000, 2000}, // middle — unchanged
{10000, 10000}, // at max — unchanged
{99999, 10000}, // above max
{0, 0}, // zero (sentinel for full history) — must NOT clamp up
}
for _, tc := range cases {
t.Run("", func(t *testing.T) {
withTempConfig(t)
body := `{"mobile_scrollback_rows":` + itoaSmall(tc.in) + `}`
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d body=%s", w.Code, w.Body.String())
}
cfg := loadConfig()
if cfg.MobileScrollbackRows != tc.want {
t.Errorf("rows %d: got %d want %d", tc.in, cfg.MobileScrollbackRows, tc.want)
}
})
}
}
// TestHandleAppearanceUpdate_RequiresPost — GET/DELETE/etc. all 405.
func TestHandleAppearanceUpdate_RequiresPost(t *testing.T) {
withTempConfig(t)
for _, method := range []string{http.MethodGet, http.MethodDelete, http.MethodPut} {
req := httptest.NewRequest(method, "/api/config/appearance", nil)
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("%s: got %d want 405", method, w.Code)
}
}
}
// TestHandleAppearanceUpdate_RejectsGarbageBody — non-JSON body
// returns 400, leaving config untouched.
func TestHandleAppearanceUpdate_RejectsGarbageBody(t *testing.T) {
withTempConfig(t)
req := httptest.NewRequest(http.MethodPost, "/api/config/appearance",
strings.NewReader(`{not json`))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handleAppearanceUpdate(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("garbage body: got %d want 400", w.Code)
}
}
// TestHandleConfig_IncludesAppearanceFields — /api/config response
// must surface the three new fields so the Settings modal can read
// them on open.
func TestHandleConfig_IncludesAppearanceFields(t *testing.T) {
withTempConfig(t)
cfg := loadConfig()
cfg.UIFontFamily = "Inter"
cfg.UIFontSize = 15
cfg.TerminalFontFamily = "JetBrains Mono"
cfg.TerminalFontSize = 20
saveConfig(cfg)
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
w := httptest.NewRecorder()
handleConfig(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d", w.Code)
}
var got map[string]any
body, _ := io.ReadAll(w.Body)
if err := json.Unmarshal(body, &got); err != nil {
t.Fatalf("decode: %v body=%s", err, body)
}
if got["ui_font_family"] != "Inter" {
t.Errorf("ui_font_family: got %v want Inter", got["ui_font_family"])
}
// JSON numbers decode as float64.
if sz, ok := got["ui_font_size"].(float64); !ok || int(sz) != 15 {
t.Errorf("ui_font_size: got %v want 15", got["ui_font_size"])
}
if got["terminal_font_family"] != "JetBrains Mono" {
t.Errorf("terminal_font_family: got %v", got["terminal_font_family"])
}
if sz, ok := got["terminal_font_size"].(float64); !ok || int(sz) != 20 {
t.Errorf("terminal_font_size: got %v want 20", got["terminal_font_size"])
}
}
// itoaSmall is a minimal int->string for table-driven tests so we
// don't pull fmt into the test file just to format sizes.
func itoaSmall(n int) string {
if n == 0 {
return "0"
}
neg := n < 0
if neg {
n = -n
}
digits := "0123456789"
var buf [12]byte
pos := len(buf)
for n > 0 {
pos--
buf[pos] = digits[n%10]
n /= 10
}
if neg {
pos--
buf[pos] = '-'
}
return string(buf[pos:])
}
// readFile is a thin wrapper for test readability.
func readFile(path string) ([]byte, error) {
return os.ReadFile(path)
}
func TestReportedHostname(t *testing.T) {
// Pure function on the Config value — no disk touch, so no
// withTempConfig needed.
osHost, _ := os.Hostname()
if osHost == "" {
osHost = "localhost"
}
cases := []struct {
name string
override string
want string
}{
{"blank falls back to OS hostname", "", osHost},
{"override wins over OS hostname", "example.com", "example.com"},
{"label reported verbatim, no port stripping", "example.com:8080", "example.com:8080"},
{"free-form label reported verbatim", "dusty_potato", "dusty_potato"},
{"codename with colon reported verbatim", "homelab:gw", "homelab:gw"},
{"whitespace-only override falls back", " ", osHost},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := reportedHostname(Config{HostnameOverride: tc.override}); got != tc.want {
t.Errorf("override %q: got %q want %q", tc.override, got, tc.want)
}
})
}
}
func TestHandleConfig_ReportsOverrideHostname(t *testing.T) {
withTempConfig(t)
cfg := loadConfig()
cfg.HostnameOverride = "dusty_potato"
saveConfig(cfg)
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
w := httptest.NewRecorder()
handleConfig(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: %d", w.Code)
}
var body map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode: %v", err)
}
// Identity field reports the label verbatim, uppercased for the
// display style; the raw label rides separately for the settings
// input. The label is free-form — "dusty_potato" on a corp PC
// named CORP01 is the whole point.
if body["hostname"] != "DUSTY_POTATO" {
t.Errorf("hostname: got %v want DUSTY_POTATO", body["hostname"])
}
if body["hostname_override"] != "dusty_potato" {
t.Errorf("hostname_override: got %v", body["hostname_override"])
}
}