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