1package config
2
3import (
4 "fmt"
5 "math"
6 "os"
7 "path/filepath"
8 "reflect"
9 "strings"
10 "testing"
11 "time"
12
13 "github.com/bnema/neferwl/internal/ports"
14)
15
16func parseString(t *testing.T, s string) (ports.Config, []Warning) {
17 t.Helper()
18 c, w, err := Parse(strings.NewReader(s))
19 if err != nil {
20 t.Fatal(err)
21 }
22 return c, w
23}
24
25func TestDefaultsAndLoad(t *testing.T) {
26 t.Setenv("TERMINAL", "")
27 d := Defaults()
28 if d.Keyboard.RepeatRate != 25 || d.Keyboard.CmdKey != "super" || !d.Render.DirectScanout || len(d.Binds) != 77 || d.Layout.MaxColumns != 2 || d.Floating.Dim != 0.3 {
29 t.Fatalf("defaults: %+v", d)
30 }
31 if d.Binds["Cmd+s"] != "toggle-stash-visible" || d.Binds["Cmd+Shift+s"] != "toggle-window-stash" || d.Binds["Cmd+o"] != "toggle-overview" || d.Binds["Cmd+f"] != "maximize-column" || d.Binds["Cmd+Shift+f"] != "toggle-fullscreen" || d.Binds["Cmd+Shift+h"] != "move-column-left" || d.Binds["Cmd+j"] != "focus-window-down" || d.Binds["Cmd+Shift+code:2"] != "move-column-to-workspace 1" || d.Focus.FollowMove {
32 t.Fatal(d.Binds)
33 }
34 if d.Binds["Cmd+Shift+Up"] != "move-window-up" || d.Binds["Cmd+Shift+j"] != "move-window-down" || d.Binds["Alt+Cmd+Left"] != "set-column-width -10%" || d.Binds["Alt+Cmd+l"] != "set-column-width +10%" || d.Binds["Alt+Cmd+k"] != "set-window-height -10%" || d.Binds["Cmd+Ctrl+Shift+Down"] != "move-workspace-down" || d.Binds["Cmd+Ctrl+Shift+k"] != "move-workspace-up" || d.Binds["Cmd+Shift+space"] != "toggle-floating" {
35 t.Fatal(d.Binds)
36 }
37 if d.Binds["Cmd+Ctrl+space"] != "spawn fuzzel" || d.Binds["Alt+Ctrl+BackSpace"] != "quit" {
38 t.Fatal(d.Binds)
39 }
40 if _, _, err := Load(filepath.Join(t.TempDir(), "missing")); !os.IsNotExist(err) {
41 t.Fatalf("explicit missing: %v", err)
42 }
43 t.Setenv("XDG_CONFIG_HOME", t.TempDir())
44 if DefaultPath() != filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "neferwl", "config") {
45 t.Fatal(DefaultPath())
46 }
47 got, w, err := LoadDefault()
48 if err != nil || len(w) != 0 || !reflect.DeepEqual(got, d) {
49 t.Fatalf("missing: %v %v %+v", err, w, got)
50 }
51 example, w, err := Load("../../../examples/config")
52 if err != nil || len(w) != 0 || !reflect.DeepEqual(example, d) {
53 t.Fatalf("example: %v %v\n%+v\n%+v", err, w, example, d)
54 }
55}
56
57func TestRealtime(t *testing.T) {
58 if !Defaults().Performance.Realtime {
59 t.Fatal("realtime default disabled")
60 }
61 c, w := parseString(t, "performance.realtime = off\n")
62 if c.Performance.Realtime || len(w) != 0 {
63 t.Fatal(c.Performance, w)
64 }
65 c, w = parseString(t, "performance.realtime = invalid\n")
66 if !c.Performance.Realtime || len(w) != 1 {
67 t.Fatal(c.Performance, w)
68 }
69}
70
71func TestFollowMove(t *testing.T) {
72 c, w := parseString(t, "focus.follow-move = on\n")
73 if !c.Focus.FollowMove || len(w) != 0 {
74 t.Fatal(c.Focus, w)
75 }
76 c, w = parseString(t, "focus.follow-move = yes\n")
77 if c.Focus.FollowMove || len(w) != 1 {
78 t.Fatal(c.Focus, w)
79 }
80}
81
82func TestAnimations(t *testing.T) {
83 d := Defaults().Animations
84 if d.On || d.Slowdown != normalSlowdown {
85 t.Fatal("animations defaults", d)
86 }
87 c, w := parseString(t, "animations = on\nanimations.speed = 0.5\n")
88 if !c.Animations.On || c.Animations.Slowdown != 2*normalSlowdown || len(w) != 0 {
89 t.Fatal(c.Animations, w)
90 }
91 for v, slowdown := range map[string]float64{"slow": 1, "normal": 0.7, "fast": 0.5, "10": 0.07, "0.1": 7} {
92 c, w = parseString(t, "animations.speed = "+v+"\n")
93 if math.Abs(c.Animations.Slowdown-slowdown) > 1e-9 || len(w) != 0 {
94 t.Fatal(v, c.Animations, w)
95 }
96 }
97 c, w = parseString(t, "animations = off\n")
98 if c.Animations.On || len(w) != 0 {
99 t.Fatal(c.Animations, w)
100 }
101 for _, line := range []string{"animations = maybe", "animations.speed = 0", "animations.speed = 11", "animations.speed = turbo", "animations.slowdown = 2"} {
102 c, w = parseString(t, line+"\n")
103 if c.Animations != d || len(w) != 1 {
104 t.Fatal(line, c.Animations, w)
105 }
106 if strings.Contains(line, "speed") && !strings.Contains(w[0].Msg, "must be slow, normal, fast or a number between 0.1 and 10") {
107 t.Fatal(line, w)
108 }
109 }
110}
111
112func TestFocusIndicator(t *testing.T) {
113 d := Defaults().Focus
114 if d.Animation != ports.FocusAnimationPulse || d.Effect != ports.FocusEffectScreen || d.Strength != 0.04 {
115 t.Fatal("focus indicator defaults", d)
116 }
117 c, w := parseString(t, "focus.animation = off\nfocus.effect = screen\nfocus.strength = 0.08\n")
118 if c.Focus.Animation != ports.FocusAnimationOff || c.Focus.Effect != ports.FocusEffectScreen || c.Focus.Strength != 0.08 || len(w) != 0 {
119 t.Fatal(c.Focus, w)
120 }
121 // Each rejected value warns and keeps its default.
122 for _, line := range []string{"focus.animation = blink", "focus.effect = glow", "focus.strength = 0", "focus.strength = 0.5", "focus.strength = x"} {
123 c, w = parseString(t, line+"\n")
124 if c.Focus != d || len(w) != 1 {
125 t.Fatal(line, c.Focus, w)
126 }
127 }
128}
129
130func TestTouchpadTap(t *testing.T) {
131 if !Defaults().Touchpad.Tap {
132 t.Fatal("tap to click off by default")
133 }
134 c, w := parseString(t, "touchpad.tap = off\n")
135 if c.Touchpad.Tap || len(w) != 0 {
136 t.Fatal(c.Touchpad, w)
137 }
138 c, w = parseString(t, "touchpad.tap = sometimes\n")
139 if !c.Touchpad.Tap || len(w) != 1 {
140 t.Fatal(c.Touchpad, w)
141 }
142}
143
144func TestTouchpadSpeed(t *testing.T) {
145 d := Defaults().Touchpad
146 if d.AccelSpeed != 0 || d.AccelProfile != ports.AccelAdaptive || d.ScrollFactor != 1 {
147 t.Fatal(d)
148 }
149 c, w := parseString(t, "touchpad.scroll-factor = 0.5\n")
150 if c.Touchpad.ScrollFactor != 0.5 || len(w) != 0 {
151 t.Fatal(c.Touchpad, w)
152 }
153 for _, bad := range []string{
154 "touchpad.scroll-factor = 0",
155 "touchpad.scroll-factor = 11",
156 } {
157 c, w := parseString(t, bad+"\n")
158 if c.Touchpad != d || len(w) != 1 {
159 t.Errorf("%s: %+v %v", bad, c.Touchpad, w)
160 }
161 }
162}
163
164func TestPointerKeys(t *testing.T) {
165 d := Defaults()
166 if d.Mouse != (ports.PointerConfig{AccelProfile: ports.AccelAdaptive}) {
167 t.Fatal(d.Mouse)
168 }
169 if d.Touchpad.LeftHanded || d.Touchpad.NaturalScroll {
170 t.Fatal(d.Touchpad)
171 }
172 touchpad := func(c ports.Config) ports.PointerConfig { return c.Touchpad.PointerConfig }
173 mouse := func(c ports.Config) ports.PointerConfig { return c.Mouse }
174 for _, tc := range []struct {
175 prefix string
176 pointer, other func(ports.Config) ports.PointerConfig
177 }{
178 {"touchpad", touchpad, mouse},
179 {"mouse", mouse, touchpad},
180 } {
181 t.Run(tc.prefix, func(t *testing.T) {
182 prefix := tc.prefix
183 c, w := parseString(t, prefix+".natural-scroll = on\n"+prefix+".left-handed = on\n"+prefix+".accel-speed = -0.5\n"+prefix+".accel-profile = flat\n")
184 want := ports.PointerConfig{NaturalScroll: true, LeftHanded: true, AccelSpeed: -0.5, AccelProfile: ports.AccelFlat}
185 if tc.pointer(c) != want || len(w) != 0 {
186 t.Errorf("%+v %v", tc.pointer(c), w)
187 }
188 // The other device keeps its defaults.
189 if other := tc.other(c); other != (ports.PointerConfig{AccelProfile: ports.AccelAdaptive}) {
190 t.Errorf("leaked into the other device: %+v", other)
191 }
192 for _, bad := range []string{
193 ".natural-scroll = maybe",
194 ".left-handed = 1",
195 ".accel-speed = 2",
196 ".accel-speed = -1.5",
197 ".accel-speed = NaN",
198 ".accel-speed = fast",
199 ".accel-profile = fast",
200 ".accel-profile = ",
201 ".unknown = on",
202 } {
203 c, w := parseString(t, prefix+bad+"\n")
204 if c.Mouse != d.Mouse || c.Touchpad != d.Touchpad || len(w) != 1 {
205 t.Errorf("%s%s: %+v %v", prefix, bad, tc.pointer(c), w)
206 }
207 }
208 })
209 }
210 // Touchpad-only keys do not exist for mice.
211 for _, bad := range []string{"mouse.tap = on", "mouse.scroll-factor = 2"} {
212 if c, w := parseString(t, bad+"\n"); c.Mouse != d.Mouse || c.Touchpad != d.Touchpad || len(w) != 1 {
213 t.Errorf("%s: %+v %v", bad, c.Mouse, w)
214 }
215 }
216}
217
218func TestParse(t *testing.T) {
219 c, w := parseString(t, `
220# comment
221keyboard.layout = fr
222keyboard.repeat-rate = 40 # inline comment
223terminal = foot --server
224background = #000000
225border.inactive =
226layout.presets = 1/3, 1/2 ,1
227log.debug = core, input
228output.DP-2.scale = 4/3
229output.DP-2 = 5120x2160@165.058
230output.HDMI-A-1 = off
231output.DP-1 = preferred
232output.DP-1.scale = 1.5
233output.DP-1.primary = on
234bind.cmd+code:30 = quit
235render.direct-scanout = off
236render.tearing = off
237render.vrr = off
238startup = wl-paste --watch cliphist store
239startup = wl-paste --primary --watch cliphist store
240`)
241 if len(w) != 0 {
242 t.Fatal(w)
243 }
244 if !reflect.DeepEqual(c.Startup, [][]string{{"wl-paste", "--watch", "cliphist", "store"}, {"wl-paste", "--primary", "--watch", "cliphist", "store"}}) {
245 t.Fatalf("startup: %q", c.Startup)
246 }
247 if c.Keyboard.Layout != "fr" || c.Keyboard.RepeatRate != 40 || !reflect.DeepEqual(c.Terminal.Command, []string{"foot", "--server"}) || c.Background.Color != "#000000" || c.Render.DirectScanout || c.Render.Tearing || c.Render.VRR {
248 t.Fatalf("%+v", c)
249 }
250 if !reflect.DeepEqual(c.Layout.Presets, []string{"1/3", "1/2", "1"}) || !reflect.DeepEqual(c.Log.Debug, []string{"core", "input"}) {
251 t.Fatalf("%+v", c)
252 }
253 // Scale and mode lines combine in either order.
254 if len(c.Outputs) != 3 || c.Outputs[0].Mode != "5120x2160@165.058" || c.Outputs[0].Scale != 4.0/3 || !c.Outputs[1].Off || c.Outputs[2].Mode != "" || c.Outputs[2].Off || c.Outputs[2].Scale != 1.5 || !c.Outputs[2].Primary || c.Outputs[0].Primary {
255 t.Fatalf("%+v", c.Outputs)
256 }
257 if c.Binds["Cmd+code:30"] != "quit" {
258 t.Fatal(c.Binds)
259 }
260}
261
262func TestWarningsKeepDefaults(t *testing.T) {
263 cases := []struct{ line, want string }{
264 {"keyboard.repeat-rate = 0", "keyboard.repeat-rate"},
265 {"keyboard.cmd = meta", "keyboard.cmd"},
266 {"terminal =", "terminal"},
267 {"xwayland = a b", "xwayland"},
268 {"background = red", "background"},
269 {"layout.gaps = 201", "layout.gaps"},
270 {"layout.max-columns = 0", "layout.max-columns"},
271 {"bind.cmd+x = focus-workspace 0", "focus-workspace"},
272 {"layout.presets = 0px", "layout.presets"},
273 {"log.level = trace", "log.level"},
274 {"log.debug = nope", "log.debug"},
275 {"output.DP-2 = big", "output.DP-2"},
276 {"output.DP-2 = 1920x1080@0", "output.DP-2"},
277 {"output.DP-2.scale = 0.5", "between 1 and 4"},
278 {"output.DP-2.primary = yes", "output.DP-2.primary"},
279 {"output.DP-2.scale = 3/0", "between 1 and 4"},
280 {"bind.cmd+code:x = quit", "evdev key code"},
281 {"nope = 1", "nope"},
282 {"just text", "expected key = value"},
283 {"bind.cmd+ = quit", "missing key"},
284 {"bind.hyper+x = quit", "unknown modifier"},
285 {"bind.cmd+x = bogus", "unknown action"},
286 {"bind.cmd+x = set-column-width 10%", "set-column-width needs"},
287 {"bind.cmd+x = set-column-width +0%", "set-column-width needs"},
288 {"bind.cmd+x = set-window-height -101%", "set-window-height needs"},
289 {"bind.cmd+x = set-window-height +5", "set-window-height needs"},
290 {"bind.cmd+x = spawn ", "unknown action"},
291 }
292 for _, tc := range cases {
293 t.Run(tc.line, func(t *testing.T) {
294 c, w := parseString(t, tc.line)
295 if len(w) != 1 || w[0].Line != 1 || !strings.Contains(w[0].Msg, tc.want) {
296 t.Fatalf("warnings %v, want %q", w, tc.want)
297 }
298 if !reflect.DeepEqual(c, Defaults()) {
299 t.Fatalf("config changed: %+v", c)
300 }
301 })
302 }
303}
304
305func TestXwayland(t *testing.T) {
306 if Defaults().Xwayland != "xwayland-satellite" {
307 t.Fatal(Defaults().Xwayland)
308 }
309 if c, w := parseString(t, "xwayland = off"); c.Xwayland != "" || len(w) != 0 {
310 t.Fatal(c.Xwayland, w)
311 }
312 if c, w := parseString(t, "xwayland = /opt/xwls"); c.Xwayland != "/opt/xwls" || len(w) != 0 {
313 t.Fatal(c.Xwayland, w)
314 }
315}
316
317func TestDuplicateKeyLastWins(t *testing.T) {
318 c, w := parseString(t, "background = #000000\nbackground = #111111\n")
319 if c.Background.Color != "#111111" || len(w) != 1 || w[0].Line != 2 {
320 t.Fatal(c.Background, w)
321 }
322}
323
324func TestBinds(t *testing.T) {
325 c, w := parseString(t, `
326bind.CTRL+Cmd+E = spawn foo --bar
327bind.cmd+; = close-window
328bind.cmd+é = quit
329bind.cmd+semicolon = quit
330bind.cmd+equal = spawn-terminal
331bind.cmd+# = none
332bind.cmd++ = quit
333bind.alt+f5 = quit
334bind.cmd+return = none
335bind.cmd+w = focus-workspace 12
336`)
337 want := map[string]string{
338 "Cmd+Ctrl+e": "spawn foo --bar",
339 "Cmd+semicolon": "quit",
340 "Cmd+eacute": "quit",
341 "Cmd+equal": "spawn-terminal",
342 "Cmd+plus": "quit",
343 "Alt+F5": "quit",
344 }
345 for combo, action := range want {
346 if c.Binds[combo] != action {
347 t.Errorf("%s = %q, want %q", combo, c.Binds[combo], action)
348 }
349 }
350 if _, ok := c.Binds["Cmd+Return"]; ok {
351 t.Error("none did not remove the default")
352 }
353 if _, ok := c.Binds["Cmd+numbersign"]; ok {
354 t.Error("none kept")
355 }
356 // cmd+; then cmd+semicolon is the same key: warn and keep the last.
357 if len(w) != 1 || w[0].Line != 5 || !strings.Contains(w[0].Msg, "overrides line 3") {
358 t.Fatal(w)
359 }
360}
361
362func TestBindReservedCharacter(t *testing.T) {
363 _, w := parseString(t, "bind.cmd+= = quit\n")
364 if len(w) != 1 || !strings.Contains(w[0].Msg, "cmd+equal") {
365 t.Fatal(w)
366 }
367}
368
369func TestBindResolvedCollision(t *testing.T) {
370 // The later line wins, like any duplicate key.
371 c, w := parseString(t, "keyboard.cmd = ctrl\nbind.ctrl+x = quit\nbind.cmd+x = close-window\nbind.cmd+ctrl+y = quit\n")
372 if c.Binds["Cmd+x"] != "close-window" {
373 t.Fatal(c.Binds)
374 }
375 if _, ok := c.Binds["Ctrl+x"]; ok {
376 t.Fatal(c.Binds)
377 }
378 if _, ok := c.Binds["Cmd+Ctrl+y"]; ok {
379 t.Fatal(c.Binds)
380 }
381 if len(w) != 2 || w[0].Line != 4 || w[1].Line != 2 || !strings.Contains(w[1].Msg, "overridden by bind.Cmd+x") {
382 t.Fatal(w)
383 }
384 // A user bind replaces a default with the same resolved keys, without a warning.
385 c, w = parseString(t, "bind.super+q = quit\n")
386 if c.Binds["Super+q"] != "quit" || c.Binds["Cmd+q"] != "" || len(w) != 0 {
387 t.Fatal(c.Binds, w)
388 }
389}
390
391func TestInvalidDuplicateKeepsEarlierValue(t *testing.T) {
392 c, w := parseString(t, "background = #000000\nbackground = red\nbackground = #111111\n")
393 if c.Background.Color != "#111111" || len(w) != 2 || w[0].Line != 2 || w[1].Line != 3 || !strings.Contains(w[1].Msg, "overrides line 1") {
394 t.Fatal(c.Background, w)
395 }
396}
397
398func TestBindCharacters(t *testing.T) {
399 c, w := parseString(t, "bind.cmd+É = quit\nbind.cmd+ж = quit\n")
400 if c.Binds["Cmd+eacute"] != "quit" {
401 t.Fatal(c.Binds)
402 }
403 if len(w) != 1 || w[0].Line != 2 || !strings.Contains(w[0].Msg, "keysym name") {
404 t.Fatal(w)
405 }
406}
407
408func TestParseMode(t *testing.T) {
409 w, h, hz, err := ParseMode("3440x1440")
410 if err != nil || w != 3440 || h != 1440 || hz != 0 {
411 t.Fatalf("%d %d %v %v", w, h, hz, err)
412 }
413 if _, _, hz, err := ParseMode("1920x1080@59.94"); err != nil || hz != 59.94 {
414 t.Fatal(hz, err)
415 }
416}
417
418func TestDigitBindReplacesPhysicalDefault(t *testing.T) {
419 c, w := parseString(t, "bind.cmd+1 = none\nbind.cmd+shift+2 = quit\n")
420 if len(w) != 0 {
421 t.Fatal(w)
422 }
423 if _, ok := c.Binds["Cmd+code:2"]; ok {
424 t.Fatal("default cmd+code:2 kept")
425 }
426 if _, ok := c.Binds["Cmd+Shift+code:3"]; ok || c.Binds["Cmd+Shift+2"] != "quit" {
427 t.Fatal(c.Binds)
428 }
429 if c.Binds["Cmd+code:4"] != "focus-workspace 3" {
430 t.Fatal("other defaults must stay")
431 }
432}
433
434func TestWorkspaces(t *testing.T) {
435 c, w := parseString(t, `layout.overflow = fixed
436workspace.dev.hidden = on
437workspace.dev.max-columns = 3
438workspace.web.overflow = scroll
439workspace.dev.monitor = DP-2
440workspace.bad name.hidden = on
441workspace.dev.color = red
442workspace.web.overflow = spiral
443bind.cmd+d = workspace dev
444bind.cmd+w = workspace
445bind.cmd+x = workspace typo
446workspace.lost.monitor = DP-1
447`)
448 want := []ports.WorkspaceConfig{
449 {Name: "dev", Monitor: "DP-2", MaxColumns: 3},
450 {Name: "web", Overflow: "scroll"},
451 {Name: "lost", Monitor: "DP-1"},
452 }
453 if c.Layout.Overflow != "fixed" || !reflect.DeepEqual(c.Workspaces, want) {
454 t.Fatalf("%q %+v", c.Layout.Overflow, c.Workspaces)
455 }
456 if c.Binds["Cmd+d"] != "workspace dev" {
457 t.Fatal(c.Binds["Cmd+d"])
458 }
459 lines := []int{}
460 for _, x := range w {
461 lines = append(lines, x.Line)
462 }
463 if !reflect.DeepEqual(lines, []int{2, 4, 6, 7, 8, 10, 11, 12}) {
464 t.Fatal(w)
465 }
466}
467
468func TestLayoutPerOutput(t *testing.T) {
469 c, w := parseString(t, `layout.max-columns = 2
470layout.HDMI-A-1.max-columns = 3
471layout.HDMI-A-1.overflow = fixed
472layout.LG Electronics 27GR95UM 123.overflow = scroll
473layout.DP-2.overflow = spiral
474layout.DP-2.color = red
475`)
476 want := []ports.OutputLayout{
477 {Output: "HDMI-A-1", MaxColumns: 3, Overflow: "fixed"},
478 {Output: "LG Electronics 27GR95UM 123", Overflow: "scroll"},
479 }
480 if c.Layout.MaxColumns != 2 || !reflect.DeepEqual(c.Layout.Outputs, want) || len(w) != 2 {
481 t.Fatalf("%+v %v", c.Layout.Outputs, w)
482 }
483}
484
485func TestFixedOutputWarnsSlotWidths(t *testing.T) {
486 _, w := parseString(t, `layout.DP-2.overflow = fixed
487workspace.dev.monitor = DP-2
488workspace.dev.column.1 = 50%, foot
489workspace.web.column.1 = 50%, foot
490`)
491 if len(w) != 3 || w[1].Line != 3 {
492 t.Fatal(w)
493 }
494}
495
496func TestWorkspaceSlots(t *testing.T) {
497 c, w := parseString(t, `workspace.dev.monitor = DP-2
498workspace.dev.column.2 = 33%, foot --title x
499workspace.dev.column.1 = 67%, code --new-window
500workspace.dev.column.2 = 1/3, kitty
501workspace.dev.column.0 = 50%, foot
502workspace.dev.column.3 = 50%
503workspace.dev.column.4 = huge, foot
504bind.cmd+d = workspace dev
505`)
506 want := []ports.SlotConfig{
507 {Index: 1, Width: "67%", Argv: []string{"code", "--new-window"}},
508 {Index: 2, Width: "1/3", Argv: []string{"kitty"}},
509 }
510 if len(c.Workspaces) != 1 || !reflect.DeepEqual(c.Workspaces[0].Slots, want) {
511 t.Fatalf("%+v", c.Workspaces)
512 }
513 lines := []int{}
514 for _, x := range w {
515 lines = append(lines, x.Line)
516 }
517 if !reflect.DeepEqual(lines, []int{4, 5, 6, 7}) {
518 t.Fatal(w)
519 }
520}
521
522func TestSlotWidthsIgnoredWithFixedOverflow(t *testing.T) {
523 _, w := parseString(t, `workspace.dev.overflow = fixed
524workspace.dev.column.1 = 67%, foot
525`)
526 if len(w) != 2 || w[1].Line != 2 || !strings.Contains(w[1].Msg, "ignored") {
527 t.Fatal(w)
528 }
529}
530
531func TestTerminalResolutionAndAutoOpen(t *testing.T) {
532 t.Setenv("TERMINAL", "ghostty --new-window")
533 c, w := parseString(t, "terminal.auto-open = all\n")
534 if len(w) != 0 || !reflect.DeepEqual(c.Terminal.Command, []string{"ghostty", "--new-window"}) || c.Terminal.AutoOpen != "all" {
535 t.Fatal(c.Terminal, w)
536 }
537 c, w = parseString(t, "terminal = foot --server\nterminal.auto-open = off\n")
538 if len(w) != 0 || !reflect.DeepEqual(c.Terminal.Command, []string{"foot", "--server"}) || c.Terminal.AutoOpen != "off" {
539 t.Fatal(c.Terminal, w)
540 }
541 c, w = parseString(t, "terminal.auto-open = invalid\n")
542 if len(w) != 1 || c.Terminal.AutoOpen != "first" {
543 t.Fatal(c.Terminal, w)
544 }
545 t.Setenv("TERMINAL", " ")
546 if got := Defaults().Terminal.Command; !reflect.DeepEqual(got, []string{"foot"}) {
547 t.Fatal(got)
548 }
549}
550
551func TestStash(t *testing.T) {
552 if d := Defaults(); d.Stash.Width != 80 || d.Stash.Gap != 2 || d.Stash.Dim != 0.5 {
553 t.Fatalf("defaults %+v", d.Stash)
554 }
555 for _, tc := range []struct {
556 line string
557 width int
558 gap int
559 dim float64
560 warning bool
561 }{
562 {"stash.width = 10", 10, 2, 0.5, false}, {"stash.width = 70%", 70, 2, 0.5, false}, {"stash.width = 90", 90, 2, 0.5, false},
563 {"stash.width = 9", 80, 2, 0.5, true}, {"stash.width = 91", 80, 2, 0.5, true}, {"stash.width = 0", 80, 2, 0.5, true}, {"stash.width = 1/2", 80, 2, 0.5, true},
564 {"stash.gap = 0", 80, 0, 0.5, false}, {"stash.gap = 10", 80, 10, 0.5, false},
565 {"stash.gap = 11", 80, 2, 0.5, true}, {"stash.gap = -1", 80, 2, 0.5, true}, {"stash.gap = 2.5", 80, 2, 0.5, true},
566 {"stash.dim = 0.8", 80, 2, 0.8, false}, {"stash.dim = 1.5", 80, 2, 0.5, true}, {"stash.dim = NaN", 80, 2, 0.5, true},
567 } {
568 t.Run(tc.line, func(t *testing.T) {
569 c, warnings := parseString(t, tc.line)
570 if c.Stash.Width != tc.width || c.Stash.Gap != tc.gap || c.Stash.Dim != tc.dim || (len(warnings) != 0) != tc.warning {
571 t.Fatalf("stash %+v, warnings %v", c.Stash, warnings)
572 }
573 })
574 }
575}
576
577func TestStashCapture(t *testing.T) {
578 if c, _ := parseString(t, ""); !c.Stash.Capture {
579 t.Fatal("stash.capture is on by default")
580 }
581 if c, w := parseString(t, "stash.capture = off"); c.Stash.Capture || len(w) != 0 {
582 t.Fatalf("off: %v %v", c.Stash.Capture, w)
583 }
584 if c, w := parseString(t, "stash.capture = yes"); !c.Stash.Capture || len(w) == 0 {
585 t.Fatalf("invalid: %v %v", c.Stash.Capture, w)
586 }
587}
588
589func TestVRRFlipGap(t *testing.T) {
590 for _, tc := range []struct {
591 value string
592 want time.Duration
593 warning bool
594 }{
595 {"0", 0, false}, {"0ms", 0, false}, {"500us", 500 * time.Microsecond, false}, {"2ms", 2 * time.Millisecond, false}, {"10ms", 10 * time.Millisecond, false},
596 {"11ms", time.Millisecond, true}, {"-1ms", time.Millisecond, true}, {"1", time.Millisecond, true}, {"fast", time.Millisecond, true},
597 } {
598 t.Run(tc.value, func(t *testing.T) {
599 c, warnings := parseString(t, "render.vrr-flip-gap = "+tc.value)
600 if c.Render.VRRFlipGap != tc.want || (len(warnings) != 0) != tc.warning {
601 t.Fatalf("gap %v, warnings %v", c.Render.VRRFlipGap, warnings)
602 }
603 })
604 }
605}
606
607func TestCursorHideAfter(t *testing.T) {
608 if d := Defaults().Cursor.HideAfter; d != 5*time.Second {
609 t.Fatalf("default %v", d)
610 }
611 for _, tc := range []struct {
612 value string
613 want time.Duration
614 warning bool
615 }{
616 {"off", 0, false}, {"100ms", 100 * time.Millisecond, false}, {"2s", 2 * time.Second, false}, {"1h", time.Hour, false},
617 {"0", 5 * time.Second, true}, {"50ms", 5 * time.Second, true}, {"2h", 5 * time.Second, true}, {"on", 5 * time.Second, true},
618 } {
619 t.Run(tc.value, func(t *testing.T) {
620 c, warnings := parseString(t, "cursor.hide-after = "+tc.value)
621 if c.Cursor.HideAfter != tc.want || (len(warnings) != 0) != tc.warning {
622 t.Fatalf("hide-after %v, warnings %v", c.Cursor.HideAfter, warnings)
623 }
624 })
625 }
626}
627
628func TestFloatingDim(t *testing.T) {
629 for _, tc := range []struct {
630 value string
631 want float64
632 warning bool
633 }{
634 {"0", 0, false}, {"0.75", 0.75, false}, {"1", 1, false},
635 {"-0.1", 0.3, true}, {"1.1", 0.3, true}, {"NaN", 0.3, true}, {"invalid", 0.3, true},
636 } {
637 t.Run(tc.value, func(t *testing.T) {
638 c, warnings := parseString(t, "floating.dim = "+tc.value)
639 if c.Floating.Dim != tc.want || (len(warnings) != 0) != tc.warning {
640 t.Fatalf("dim %v, warnings %v", c.Floating.Dim, warnings)
641 }
642 })
643 }
644}
645
646func TestOutputRelations(t *testing.T) {
647 c, w := parseString(t, `
648output.DP-2.right-of = DP-1
649output.DP-2.offset = 180
650output.DP-3.left-of = DP-1
651output.DP-3.offset = -40
652output.DP-4.above = DP-1
653output.DP-5.below = DP-1
654output.DP-5.offset = 7
655`)
656 if len(w) != 0 {
657 t.Fatal(w)
658 }
659 want := map[string]ports.OutputAnchor{
660 "DP-2": {Relation: ports.RelationRightOf, To: "DP-1", Offset: 180},
661 "DP-3": {Relation: ports.RelationLeftOf, To: "DP-1", Offset: -40},
662 "DP-4": {Relation: ports.RelationAbove, To: "DP-1"},
663 "DP-5": {Relation: ports.RelationBelow, To: "DP-1", Offset: 7},
664 }
665 if len(c.Outputs) != len(want) {
666 t.Fatalf("%+v", c.Outputs)
667 }
668 for _, o := range c.Outputs {
669 if o.Anchor != want[o.Name] || !o.ScaleOnly {
670 t.Fatalf("%s: %+v", o.Name, o)
671 }
672 }
673}
674
675func TestOutputTransform(t *testing.T) {
676 names := []string{"normal", "90", "180", "270", "flipped", "flipped-90", "flipped-180", "flipped-270"}
677 var text strings.Builder
678 for i, n := range names {
679 fmt.Fprintf(&text, "output.O%d.transform = %s\n", i, n)
680 }
681 c, w := parseString(t, text.String())
682 if len(w) != 0 || len(c.Outputs) != len(names) {
683 t.Fatalf("%v %+v", w, c.Outputs)
684 }
685 for i, o := range c.Outputs {
686 if o.Transform != ports.BufferTransform(i) || !o.ScaleOnly {
687 t.Fatalf("%s: %+v", names[i], o)
688 }
689 }
690
691 c, w = parseString(t, "output.DP-1.transform = 45\n")
692 if len(w) != 1 || !strings.Contains(w[0].Msg, "invalid transform") {
693 t.Fatalf("warnings %v", w)
694 }
695 if len(c.Outputs) != 0 {
696 t.Fatalf("%+v", c.Outputs)
697 }
698
699 // A later mode line keeps the transform.
700 c, w = parseString(t, "output.DP-1.transform = 90\noutput.DP-1 = 1920x1080\n")
701 if len(w) != 0 || len(c.Outputs) != 1 || c.Outputs[0].Transform != 1 || c.Outputs[0].ScaleOnly {
702 t.Fatalf("%v %+v", w, c.Outputs)
703 }
704}
705
706func TestOutputRelationOverride(t *testing.T) {
707 c, w := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.below = DP-3\n")
708 if len(w) != 1 || w[0].Line != 2 || !strings.Contains(w[0].Msg, "overrides line 1") {
709 t.Fatalf("warnings %v", w)
710 }
711 if got := c.Outputs[0].Anchor; got != (ports.OutputAnchor{Relation: ports.RelationBelow, To: "DP-3"}) {
712 t.Fatalf("%+v", got)
713 }
714}
715
716func TestOutputRelationWarnings(t *testing.T) {
717 const rangeMsg = "must be an integer between -65536 and 65536"
718 rel := "output.DP-2.right-of = DP-1\n"
719 cases := []struct {
720 name, text string
721 want []string // expected warning messages (substring), in order
722 lines []int // expected warning lines, in order
723 anchored []string // outputs that keep a relation
724 }{
725 {"self", "output.DP-2.right-of = DP-2", []string{"relative to itself"}, []int{1}, nil},
726 {"empty", "output.DP-2.above =", []string{"expected an output name"}, []int{1}, nil},
727 {"bad offset", rel + "output.DP-2.offset = abc", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
728 {"offset too large", rel + "output.DP-2.offset = 65537", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
729 {"offset too small", rel + "output.DP-2.offset = -65537", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
730 {"offset overflow", rel + "output.DP-2.offset = 99999999999999999999", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
731 {"offset alone", "output.DP-2.offset = 10", []string{"without a relation"}, []int{1}, nil},
732 {"two cycle", "output.A.right-of = B\noutput.B.below = A", []string{"relation cycle", "relation cycle"}, []int{1, 2}, nil},
733 {"three cycle", "output.A.right-of = B\noutput.B.right-of = C\noutput.C.above = A", []string{"relation cycle", "relation cycle", "relation cycle"}, []int{1, 2, 3}, nil},
734 {"tail into cycle", "output.A.right-of = B\noutput.B.right-of = A\noutput.C.left-of = A", []string{"relation cycle", "relation cycle"}, []int{1, 2}, []string{"C"}},
735 {"relation overridden twice", "output.DP-2.right-of = A\noutput.DP-2.below = B\noutput.DP-2.right-of = C", []string{"overrides line 1", "overrides line 2"}, []int{2, 3}, []string{"DP-2"}},
736 {"relation overridden three times", "output.DP-2.right-of = A\noutput.DP-2.below = B\noutput.DP-2.right-of = C\noutput.DP-2.below = D", []string{"overrides line 1", "overrides line 2", "overrides line 3"}, []int{2, 3, 4}, []string{"DP-2"}},
737 {"same relation on two outputs", "output.DP-2.right-of = DP-1\noutput.DP-3.right-of = DP-1", []string{"output.DP-3: same relation as output.DP-2; both get the same position"}, []int{2}, []string{"DP-2", "DP-3"}},
738 {"same relation and offset on three outputs", "output.A.below = R\noutput.A.offset = 5\noutput.B.below = R\noutput.B.offset = 5\noutput.C.below = R\noutput.C.offset = 5", []string{"output.B: same relation as output.A", "output.C: same relation as output.A"}, []int{3, 5}, []string{"A", "B", "C"}},
739 {"same reference, other offset", "output.DP-2.right-of = DP-1\noutput.DP-3.right-of = DP-1\noutput.DP-3.offset = 10", nil, nil, []string{"DP-2", "DP-3"}},
740 {"same reference, other relation", "output.DP-2.right-of = DP-1\noutput.DP-3.left-of = DP-1", nil, nil, []string{"DP-2", "DP-3"}},
741 {"same relation twice", "output.DP-2.below = A\noutput.DP-2.below = B", []string{"overrides line 1"}, []int{2}, []string{"DP-2"}},
742 }
743 for _, tc := range cases {
744 t.Run(tc.name, func(t *testing.T) {
745 c, w := parseString(t, tc.text)
746 if len(w) != len(tc.want) {
747 t.Fatalf("warnings %v, want %q", w, tc.want)
748 }
749 for i, x := range w {
750 if !strings.Contains(x.Msg, tc.want[i]) || x.Line != tc.lines[i] {
751 t.Fatalf("warning %d = %+v, want %q at line %d (all: %v)", i, x, tc.want[i], tc.lines[i], w)
752 }
753 }
754 var got []string
755 for _, o := range c.Outputs {
756 if o.Anchor.Relation != ports.RelationNone {
757 got = append(got, o.Name)
758 }
759 }
760 if !reflect.DeepEqual(got, tc.anchored) {
761 t.Fatalf("anchored %v, want %v: %+v", got, tc.anchored, c.Outputs)
762 }
763 })
764 }
765}
766
767func TestDefaultMonitorBinds(t *testing.T) {
768 d := Defaults()
769 for combo, action := range map[string]string{
770 "Cmd+Ctrl+Up": "focus-monitor-up", "Cmd+Ctrl+k": "focus-monitor-up",
771 "Cmd+Ctrl+Down": "focus-monitor-down", "Cmd+Ctrl+j": "focus-monitor-down",
772 "Cmd+Ctrl+Left": "focus-monitor-left", "Cmd+Ctrl+l": "focus-monitor-right",
773 } {
774 if d.Binds[combo] != action {
775 t.Errorf("%s = %q, want %q", combo, d.Binds[combo], action)
776 }
777 }
778 // The up/down move-workspace-to-monitor actions have no default bind but
779 // load from a config file.
780 c, w := parseString(t, "bind.cmd+ctrl+alt+up = move-workspace-to-monitor-up\nbind.cmd+ctrl+alt+down = move-workspace-to-monitor-down\nbind.cmd+alt+u = focus-monitor-up\n")
781 if len(w) != 0 || c.Binds["Alt+Cmd+Ctrl+Up"] != "move-workspace-to-monitor-up" || c.Binds["Alt+Cmd+Ctrl+Down"] != "move-workspace-to-monitor-down" {
782 t.Fatalf("%v %v", w, c.Binds)
783 }
784}
785
786func TestOutputOffsetKeepsEarlierValueWhenInvalid(t *testing.T) {
787 c, _ := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.offset = 40\noutput.DP-2.offset = 65537\n")
788 if got := c.Outputs[0].Anchor.Offset; got != 40 {
789 t.Fatalf("offset %d, want 40", got)
790 }
791 c, w := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.offset = 65536\noutput.DP-2.offset = -65536\n")
792 if len(w) != 1 || c.Outputs[0].Anchor.Offset != -65536 {
793 t.Fatalf("%v %+v", w, c.Outputs)
794 }
795}
796
797func TestOutputModeLineKeepsAnchor(t *testing.T) {
798 c, w := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.offset = 50\noutput.DP-2 = 1920x1080\n")
799 if len(w) != 0 {
800 t.Fatal(w)
801 }
802 o := c.Outputs[0]
803 if o.Mode != "1920x1080" || o.Anchor != (ports.OutputAnchor{Relation: ports.RelationRightOf, To: "DP-1", Offset: 50}) {
804 t.Fatalf("%+v", o)
805 }
806}