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-next" || d.Binds["Cmd+Ctrl+Shift+k"] != "move-workspace-prev" || 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 = focus-workspace-up", "unknown action"},
287 {"bind.cmd+x = move-workspace-down", "unknown action"},
288 {"bind.cmd+x = move-column-to-workspace-up", "unknown action"},
289 {"bind.cmd+x = set-column-width 10%", "set-column-width needs"},
290 {"bind.cmd+x = set-column-width +0%", "set-column-width needs"},
291 {"bind.cmd+x = set-window-height -101%", "set-window-height needs"},
292 {"bind.cmd+x = set-window-height +5", "set-window-height needs"},
293 {"bind.cmd+x = spawn ", "unknown action"},
294 }
295 for _, tc := range cases {
296 t.Run(tc.line, func(t *testing.T) {
297 c, w := parseString(t, tc.line)
298 if len(w) != 1 || w[0].Line != 1 || !strings.Contains(w[0].Msg, tc.want) {
299 t.Fatalf("warnings %v, want %q", w, tc.want)
300 }
301 if !reflect.DeepEqual(c, Defaults()) {
302 t.Fatalf("config changed: %+v", c)
303 }
304 })
305 }
306}
307
308func TestWorkspacePrevNextAction(t *testing.T) {
309 c, w := parseString(t, "bind.cmd+x = focus-workspace-next")
310 if len(w) != 0 || c.Binds["Cmd+x"] != "focus-workspace-next" {
311 t.Fatal(c.Binds["Cmd+x"], w)
312 }
313}
314
315func TestXwayland(t *testing.T) {
316 if Defaults().Xwayland != "xwayland-satellite" {
317 t.Fatal(Defaults().Xwayland)
318 }
319 if c, w := parseString(t, "xwayland = off"); c.Xwayland != "" || len(w) != 0 {
320 t.Fatal(c.Xwayland, w)
321 }
322 if c, w := parseString(t, "xwayland = /opt/xwls"); c.Xwayland != "/opt/xwls" || len(w) != 0 {
323 t.Fatal(c.Xwayland, w)
324 }
325}
326
327func TestDuplicateKeyLastWins(t *testing.T) {
328 c, w := parseString(t, "background = #000000\nbackground = #111111\n")
329 if c.Background.Color != "#111111" || len(w) != 1 || w[0].Line != 2 {
330 t.Fatal(c.Background, w)
331 }
332}
333
334func TestBinds(t *testing.T) {
335 c, w := parseString(t, `
336bind.CTRL+Cmd+E = spawn foo --bar
337bind.cmd+; = close-window
338bind.cmd+é = quit
339bind.cmd+semicolon = quit
340bind.cmd+equal = spawn-terminal
341bind.cmd+# = none
342bind.cmd++ = quit
343bind.alt+f5 = quit
344bind.cmd+return = none
345bind.cmd+w = focus-workspace 12
346`)
347 want := map[string]string{
348 "Cmd+Ctrl+e": "spawn foo --bar",
349 "Cmd+semicolon": "quit",
350 "Cmd+eacute": "quit",
351 "Cmd+equal": "spawn-terminal",
352 "Cmd+plus": "quit",
353 "Alt+F5": "quit",
354 }
355 for combo, action := range want {
356 if c.Binds[combo] != action {
357 t.Errorf("%s = %q, want %q", combo, c.Binds[combo], action)
358 }
359 }
360 if _, ok := c.Binds["Cmd+Return"]; ok {
361 t.Error("none did not remove the default")
362 }
363 if _, ok := c.Binds["Cmd+numbersign"]; ok {
364 t.Error("none kept")
365 }
366 // cmd+; then cmd+semicolon is the same key: warn and keep the last.
367 if len(w) != 1 || w[0].Line != 5 || !strings.Contains(w[0].Msg, "overrides line 3") {
368 t.Fatal(w)
369 }
370}
371
372func TestBindReservedCharacter(t *testing.T) {
373 _, w := parseString(t, "bind.cmd+= = quit\n")
374 if len(w) != 1 || !strings.Contains(w[0].Msg, "cmd+equal") {
375 t.Fatal(w)
376 }
377}
378
379func TestBindResolvedCollision(t *testing.T) {
380 // The later line wins, like any duplicate key.
381 c, w := parseString(t, "keyboard.cmd = ctrl\nbind.ctrl+x = quit\nbind.cmd+x = close-window\nbind.cmd+ctrl+y = quit\n")
382 if c.Binds["Cmd+x"] != "close-window" {
383 t.Fatal(c.Binds)
384 }
385 if _, ok := c.Binds["Ctrl+x"]; ok {
386 t.Fatal(c.Binds)
387 }
388 if _, ok := c.Binds["Cmd+Ctrl+y"]; ok {
389 t.Fatal(c.Binds)
390 }
391 if len(w) != 2 || w[0].Line != 4 || w[1].Line != 2 || !strings.Contains(w[1].Msg, "overridden by bind.Cmd+x") {
392 t.Fatal(w)
393 }
394 // A user bind replaces a default with the same resolved keys, without a warning.
395 c, w = parseString(t, "bind.super+q = quit\n")
396 if c.Binds["Super+q"] != "quit" || c.Binds["Cmd+q"] != "" || len(w) != 0 {
397 t.Fatal(c.Binds, w)
398 }
399}
400
401func TestInvalidDuplicateKeepsEarlierValue(t *testing.T) {
402 c, w := parseString(t, "background = #000000\nbackground = red\nbackground = #111111\n")
403 if c.Background.Color != "#111111" || len(w) != 2 || w[0].Line != 2 || w[1].Line != 3 || !strings.Contains(w[1].Msg, "overrides line 1") {
404 t.Fatal(c.Background, w)
405 }
406}
407
408func TestBindCharacters(t *testing.T) {
409 c, w := parseString(t, "bind.cmd+É = quit\nbind.cmd+ж = quit\n")
410 if c.Binds["Cmd+eacute"] != "quit" {
411 t.Fatal(c.Binds)
412 }
413 if len(w) != 1 || w[0].Line != 2 || !strings.Contains(w[0].Msg, "keysym name") {
414 t.Fatal(w)
415 }
416}
417
418func TestParseMode(t *testing.T) {
419 w, h, hz, err := ParseMode("3440x1440")
420 if err != nil || w != 3440 || h != 1440 || hz != 0 {
421 t.Fatalf("%d %d %v %v", w, h, hz, err)
422 }
423 if _, _, hz, err := ParseMode("1920x1080@59.94"); err != nil || hz != 59.94 {
424 t.Fatal(hz, err)
425 }
426}
427
428func TestDigitBindReplacesPhysicalDefault(t *testing.T) {
429 c, w := parseString(t, "bind.cmd+1 = none\nbind.cmd+shift+2 = quit\n")
430 if len(w) != 0 {
431 t.Fatal(w)
432 }
433 if _, ok := c.Binds["Cmd+code:2"]; ok {
434 t.Fatal("default cmd+code:2 kept")
435 }
436 if _, ok := c.Binds["Cmd+Shift+code:3"]; ok || c.Binds["Cmd+Shift+2"] != "quit" {
437 t.Fatal(c.Binds)
438 }
439 if c.Binds["Cmd+code:4"] != "focus-workspace 3" {
440 t.Fatal("other defaults must stay")
441 }
442}
443
444func TestWorkspaces(t *testing.T) {
445 c, w := parseString(t, `layout.overflow = fixed
446workspace.dev.hidden = on
447workspace.dev.max-columns = 3
448workspace.web.overflow = scroll
449workspace.dev.monitor = DP-2
450workspace.bad name.hidden = on
451workspace.dev.color = red
452workspace.web.overflow = spiral
453bind.cmd+d = workspace dev
454bind.cmd+w = workspace
455bind.cmd+x = workspace typo
456workspace.lost.monitor = DP-1
457`)
458 want := []ports.WorkspaceConfig{
459 {Name: "dev", Monitor: "DP-2", MaxColumns: 3},
460 {Name: "web", Overflow: "scroll"},
461 {Name: "lost", Monitor: "DP-1"},
462 }
463 if c.Layout.Overflow != "fixed" || !reflect.DeepEqual(c.Workspaces, want) {
464 t.Fatalf("%q %+v", c.Layout.Overflow, c.Workspaces)
465 }
466 if c.Binds["Cmd+d"] != "workspace dev" {
467 t.Fatal(c.Binds["Cmd+d"])
468 }
469 lines := []int{}
470 for _, x := range w {
471 lines = append(lines, x.Line)
472 }
473 if !reflect.DeepEqual(lines, []int{2, 4, 6, 7, 8, 10, 11, 12}) {
474 t.Fatal(w)
475 }
476}
477
478func TestLayoutPerOutput(t *testing.T) {
479 c, w := parseString(t, `layout.max-columns = 2
480layout.HDMI-A-1.max-columns = 3
481layout.HDMI-A-1.overflow = fixed
482layout.LG Electronics 27GR95UM 123.overflow = scroll
483layout.DP-2.overflow = spiral
484layout.DP-2.color = red
485`)
486 want := []ports.OutputLayout{
487 {Output: "HDMI-A-1", MaxColumns: 3, Overflow: "fixed"},
488 {Output: "LG Electronics 27GR95UM 123", Overflow: "scroll"},
489 }
490 if c.Layout.MaxColumns != 2 || !reflect.DeepEqual(c.Layout.Outputs, want) || len(w) != 2 {
491 t.Fatalf("%+v %v", c.Layout.Outputs, w)
492 }
493}
494
495func TestFixedOutputWarnsSlotWidths(t *testing.T) {
496 _, w := parseString(t, `layout.DP-2.overflow = fixed
497workspace.dev.monitor = DP-2
498workspace.dev.column.1 = 50%, foot
499workspace.web.column.1 = 50%, foot
500`)
501 if len(w) != 3 || w[1].Line != 3 {
502 t.Fatal(w)
503 }
504}
505
506func TestWorkspaceSlots(t *testing.T) {
507 c, w := parseString(t, `workspace.dev.monitor = DP-2
508workspace.dev.column.2 = 33%, foot --title x
509workspace.dev.column.1 = 67%, code --new-window
510workspace.dev.column.2 = 1/3, kitty
511workspace.dev.column.0 = 50%, foot
512workspace.dev.column.3 = 50%
513workspace.dev.column.4 = huge, foot
514bind.cmd+d = workspace dev
515`)
516 want := []ports.SlotConfig{
517 {Index: 1, Width: "67%", Argv: []string{"code", "--new-window"}},
518 {Index: 2, Width: "1/3", Argv: []string{"kitty"}},
519 }
520 if len(c.Workspaces) != 1 || !reflect.DeepEqual(c.Workspaces[0].Slots, want) {
521 t.Fatalf("%+v", c.Workspaces)
522 }
523 lines := []int{}
524 for _, x := range w {
525 lines = append(lines, x.Line)
526 }
527 if !reflect.DeepEqual(lines, []int{4, 5, 6, 7}) {
528 t.Fatal(w)
529 }
530}
531
532func TestSlotWidthsIgnoredWithFixedOverflow(t *testing.T) {
533 _, w := parseString(t, `workspace.dev.overflow = fixed
534workspace.dev.column.1 = 67%, foot
535`)
536 if len(w) != 2 || w[1].Line != 2 || !strings.Contains(w[1].Msg, "ignored") {
537 t.Fatal(w)
538 }
539}
540
541func TestTerminalResolutionAndAutoOpen(t *testing.T) {
542 t.Setenv("TERMINAL", "ghostty --new-window")
543 c, w := parseString(t, "terminal.auto-open = all\n")
544 if len(w) != 0 || !reflect.DeepEqual(c.Terminal.Command, []string{"ghostty", "--new-window"}) || c.Terminal.AutoOpen != "all" {
545 t.Fatal(c.Terminal, w)
546 }
547 c, w = parseString(t, "terminal = foot --server\nterminal.auto-open = off\n")
548 if len(w) != 0 || !reflect.DeepEqual(c.Terminal.Command, []string{"foot", "--server"}) || c.Terminal.AutoOpen != "off" {
549 t.Fatal(c.Terminal, w)
550 }
551 c, w = parseString(t, "terminal.auto-open = invalid\n")
552 if len(w) != 1 || c.Terminal.AutoOpen != "first" {
553 t.Fatal(c.Terminal, w)
554 }
555 t.Setenv("TERMINAL", " ")
556 if got := Defaults().Terminal.Command; !reflect.DeepEqual(got, []string{"foot"}) {
557 t.Fatal(got)
558 }
559}
560
561func TestStash(t *testing.T) {
562 if d := Defaults(); d.Stash.Width != 80 || d.Stash.Gap != 2 || d.Stash.Dim != 0.5 {
563 t.Fatalf("defaults %+v", d.Stash)
564 }
565 for _, tc := range []struct {
566 line string
567 width int
568 gap int
569 dim float64
570 warning bool
571 }{
572 {"stash.width = 10", 10, 2, 0.5, false}, {"stash.width = 70%", 70, 2, 0.5, false}, {"stash.width = 90", 90, 2, 0.5, false},
573 {"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},
574 {"stash.gap = 0", 80, 0, 0.5, false}, {"stash.gap = 10", 80, 10, 0.5, false},
575 {"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},
576 {"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},
577 } {
578 t.Run(tc.line, func(t *testing.T) {
579 c, warnings := parseString(t, tc.line)
580 if c.Stash.Width != tc.width || c.Stash.Gap != tc.gap || c.Stash.Dim != tc.dim || (len(warnings) != 0) != tc.warning {
581 t.Fatalf("stash %+v, warnings %v", c.Stash, warnings)
582 }
583 })
584 }
585}
586
587func TestStashCapture(t *testing.T) {
588 if c, _ := parseString(t, ""); !c.Stash.Capture {
589 t.Fatal("stash.capture is on by default")
590 }
591 if c, w := parseString(t, "stash.capture = off"); c.Stash.Capture || len(w) != 0 {
592 t.Fatalf("off: %v %v", c.Stash.Capture, w)
593 }
594 if c, w := parseString(t, "stash.capture = yes"); !c.Stash.Capture || len(w) == 0 {
595 t.Fatalf("invalid: %v %v", c.Stash.Capture, w)
596 }
597}
598
599func TestVRRFlipGap(t *testing.T) {
600 for _, tc := range []struct {
601 value string
602 want time.Duration
603 warning bool
604 }{
605 {"0", 0, false}, {"0ms", 0, false}, {"500us", 500 * time.Microsecond, false}, {"2ms", 2 * time.Millisecond, false}, {"10ms", 10 * time.Millisecond, false},
606 {"11ms", time.Millisecond, true}, {"-1ms", time.Millisecond, true}, {"1", time.Millisecond, true}, {"fast", time.Millisecond, true},
607 } {
608 t.Run(tc.value, func(t *testing.T) {
609 c, warnings := parseString(t, "render.vrr-flip-gap = "+tc.value)
610 if c.Render.VRRFlipGap != tc.want || (len(warnings) != 0) != tc.warning {
611 t.Fatalf("gap %v, warnings %v", c.Render.VRRFlipGap, warnings)
612 }
613 })
614 }
615}
616
617func TestCursorHideAfter(t *testing.T) {
618 if d := Defaults().Cursor.HideAfter; d != 5*time.Second {
619 t.Fatalf("default %v", d)
620 }
621 for _, tc := range []struct {
622 value string
623 want time.Duration
624 warning bool
625 }{
626 {"off", 0, false}, {"100ms", 100 * time.Millisecond, false}, {"2s", 2 * time.Second, false}, {"1h", time.Hour, false},
627 {"0", 5 * time.Second, true}, {"50ms", 5 * time.Second, true}, {"2h", 5 * time.Second, true}, {"on", 5 * time.Second, true},
628 } {
629 t.Run(tc.value, func(t *testing.T) {
630 c, warnings := parseString(t, "cursor.hide-after = "+tc.value)
631 if c.Cursor.HideAfter != tc.want || (len(warnings) != 0) != tc.warning {
632 t.Fatalf("hide-after %v, warnings %v", c.Cursor.HideAfter, warnings)
633 }
634 })
635 }
636}
637
638func TestFloatingDim(t *testing.T) {
639 for _, tc := range []struct {
640 value string
641 want float64
642 warning bool
643 }{
644 {"0", 0, false}, {"0.75", 0.75, false}, {"1", 1, false},
645 {"-0.1", 0.3, true}, {"1.1", 0.3, true}, {"NaN", 0.3, true}, {"invalid", 0.3, true},
646 } {
647 t.Run(tc.value, func(t *testing.T) {
648 c, warnings := parseString(t, "floating.dim = "+tc.value)
649 if c.Floating.Dim != tc.want || (len(warnings) != 0) != tc.warning {
650 t.Fatalf("dim %v, warnings %v", c.Floating.Dim, warnings)
651 }
652 })
653 }
654}
655
656func TestOutputRelations(t *testing.T) {
657 c, w := parseString(t, `
658output.DP-2.right-of = DP-1
659output.DP-2.offset = 180
660output.DP-3.left-of = DP-1
661output.DP-3.offset = -40
662output.DP-4.above = DP-1
663output.DP-5.below = DP-1
664output.DP-5.offset = 7
665`)
666 if len(w) != 0 {
667 t.Fatal(w)
668 }
669 want := map[string]ports.OutputAnchor{
670 "DP-2": {Relation: ports.RelationRightOf, To: "DP-1", Offset: 180},
671 "DP-3": {Relation: ports.RelationLeftOf, To: "DP-1", Offset: -40},
672 "DP-4": {Relation: ports.RelationAbove, To: "DP-1"},
673 "DP-5": {Relation: ports.RelationBelow, To: "DP-1", Offset: 7},
674 }
675 if len(c.Outputs) != len(want) {
676 t.Fatalf("%+v", c.Outputs)
677 }
678 for _, o := range c.Outputs {
679 if o.Anchor != want[o.Name] || !o.ScaleOnly {
680 t.Fatalf("%s: %+v", o.Name, o)
681 }
682 }
683}
684
685func TestOutputTransform(t *testing.T) {
686 names := []string{"normal", "90", "180", "270", "flipped", "flipped-90", "flipped-180", "flipped-270"}
687 var text strings.Builder
688 for i, n := range names {
689 fmt.Fprintf(&text, "output.O%d.transform = %s\n", i, n)
690 }
691 c, w := parseString(t, text.String())
692 if len(w) != 0 || len(c.Outputs) != len(names) {
693 t.Fatalf("%v %+v", w, c.Outputs)
694 }
695 for i, o := range c.Outputs {
696 if o.Transform != ports.BufferTransform(i) || !o.ScaleOnly {
697 t.Fatalf("%s: %+v", names[i], o)
698 }
699 }
700
701 c, w = parseString(t, "output.DP-1.transform = 45\n")
702 if len(w) != 1 || !strings.Contains(w[0].Msg, "invalid transform") {
703 t.Fatalf("warnings %v", w)
704 }
705 if len(c.Outputs) != 0 {
706 t.Fatalf("%+v", c.Outputs)
707 }
708
709 // A later mode line keeps the transform.
710 c, w = parseString(t, "output.DP-1.transform = 90\noutput.DP-1 = 1920x1080\n")
711 if len(w) != 0 || len(c.Outputs) != 1 || c.Outputs[0].Transform != 1 || c.Outputs[0].ScaleOnly {
712 t.Fatalf("%v %+v", w, c.Outputs)
713 }
714}
715
716func TestOutputRelationOverride(t *testing.T) {
717 c, w := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.below = DP-3\n")
718 if len(w) != 1 || w[0].Line != 2 || !strings.Contains(w[0].Msg, "overrides line 1") {
719 t.Fatalf("warnings %v", w)
720 }
721 if got := c.Outputs[0].Anchor; got != (ports.OutputAnchor{Relation: ports.RelationBelow, To: "DP-3"}) {
722 t.Fatalf("%+v", got)
723 }
724}
725
726func TestOutputRelationWarnings(t *testing.T) {
727 const rangeMsg = "must be an integer between -65536 and 65536"
728 rel := "output.DP-2.right-of = DP-1\n"
729 cases := []struct {
730 name, text string
731 want []string // expected warning messages (substring), in order
732 lines []int // expected warning lines, in order
733 anchored []string // outputs that keep a relation
734 }{
735 {"self", "output.DP-2.right-of = DP-2", []string{"relative to itself"}, []int{1}, nil},
736 {"empty", "output.DP-2.above =", []string{"expected an output name"}, []int{1}, nil},
737 {"bad offset", rel + "output.DP-2.offset = abc", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
738 {"offset too large", rel + "output.DP-2.offset = 65537", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
739 {"offset too small", rel + "output.DP-2.offset = -65537", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
740 {"offset overflow", rel + "output.DP-2.offset = 99999999999999999999", []string{rangeMsg}, []int{2}, []string{"DP-2"}},
741 {"offset alone", "output.DP-2.offset = 10", []string{"without a relation"}, []int{1}, nil},
742 {"two cycle", "output.A.right-of = B\noutput.B.below = A", []string{"relation cycle", "relation cycle"}, []int{1, 2}, nil},
743 {"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},
744 {"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"}},
745 {"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"}},
746 {"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"}},
747 {"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"}},
748 {"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"}},
749 {"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"}},
750 {"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"}},
751 {"same relation twice", "output.DP-2.below = A\noutput.DP-2.below = B", []string{"overrides line 1"}, []int{2}, []string{"DP-2"}},
752 }
753 for _, tc := range cases {
754 t.Run(tc.name, func(t *testing.T) {
755 c, w := parseString(t, tc.text)
756 if len(w) != len(tc.want) {
757 t.Fatalf("warnings %v, want %q", w, tc.want)
758 }
759 for i, x := range w {
760 if !strings.Contains(x.Msg, tc.want[i]) || x.Line != tc.lines[i] {
761 t.Fatalf("warning %d = %+v, want %q at line %d (all: %v)", i, x, tc.want[i], tc.lines[i], w)
762 }
763 }
764 var got []string
765 for _, o := range c.Outputs {
766 if o.Anchor.Relation != ports.RelationNone {
767 got = append(got, o.Name)
768 }
769 }
770 if !reflect.DeepEqual(got, tc.anchored) {
771 t.Fatalf("anchored %v, want %v: %+v", got, tc.anchored, c.Outputs)
772 }
773 })
774 }
775}
776
777func TestDefaultMonitorBinds(t *testing.T) {
778 d := Defaults()
779 for combo, action := range map[string]string{
780 "Cmd+Ctrl+Up": "focus-monitor-up", "Cmd+Ctrl+k": "focus-monitor-up",
781 "Cmd+Ctrl+Down": "focus-monitor-down", "Cmd+Ctrl+j": "focus-monitor-down",
782 "Cmd+Ctrl+Left": "focus-monitor-left", "Cmd+Ctrl+l": "focus-monitor-right",
783 } {
784 if d.Binds[combo] != action {
785 t.Errorf("%s = %q, want %q", combo, d.Binds[combo], action)
786 }
787 }
788 // The up/down move-workspace-to-monitor actions have no default bind but
789 // load from a config file.
790 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")
791 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" {
792 t.Fatalf("%v %v", w, c.Binds)
793 }
794}
795
796func TestOutputOffsetKeepsEarlierValueWhenInvalid(t *testing.T) {
797 c, _ := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.offset = 40\noutput.DP-2.offset = 65537\n")
798 if got := c.Outputs[0].Anchor.Offset; got != 40 {
799 t.Fatalf("offset %d, want 40", got)
800 }
801 c, w := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.offset = 65536\noutput.DP-2.offset = -65536\n")
802 if len(w) != 1 || c.Outputs[0].Anchor.Offset != -65536 {
803 t.Fatalf("%v %+v", w, c.Outputs)
804 }
805}
806
807func TestOutputModeLineKeepsAnchor(t *testing.T) {
808 c, w := parseString(t, "output.DP-2.right-of = DP-1\noutput.DP-2.offset = 50\noutput.DP-2 = 1920x1080\n")
809 if len(w) != 0 {
810 t.Fatal(w)
811 }
812 o := c.Outputs[0]
813 if o.Mode != "1920x1080" || o.Anchor != (ports.OutputAnchor{Relation: ports.RelationRightOf, To: "DP-1", Offset: 50}) {
814 t.Fatalf("%+v", o)
815 }
816}