v0.1.0 d9218d6

app_test.go RawHistoryFolder

1package app
2
3import (
4	"context"
5	"image/color"
6	"image/png"
7	"io"
8	"os"
9	"os/exec"
10	"path/filepath"
11	"strings"
12	"testing"
13	"time"
14
15	"github.com/bnema/neferwl/internal/adapters/config"
16	"github.com/bnema/neferwl/internal/adapters/xkb"
17	"github.com/bnema/neferwl/internal/logging"
18	"github.com/bnema/neferwl/internal/ports"
19)
20
21func TestHeadlessConfigReload(t *testing.T) {
22	t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
23	dir := t.TempDir()
24	path := filepath.Join(dir, "config")
25	if err := os.WriteFile(path, []byte("background = #000000\n"), 0600); err != nil {
26		t.Fatal(err)
27	}
28	cfg, _, err := config.Load(path)
29	if err != nil {
30		t.Fatal(err)
31	}
32	shots := t.TempDir()
33	ctx, cancel := context.WithCancel(context.Background())
34	done := make(chan error, 1)
35	go func() {
36		done <- Run(ctx, Options{Backend: "headless", NoXwayland: true, Config: cfg, ConfigPath: path, NoTerminal: true, ScreenshotDir: shots})
37	}()
38	defer func() {
39		cancel()
40		select {
41		case err := <-done:
42			if err != nil {
43				t.Errorf("Run: %v", err)
44			}
45		case <-time.After(3 * time.Second):
46			t.Error("Run did not stop")
47		}
48	}()
49	latest := filepath.Join(shots, "latest.png")
50	waitColor := func(want color.RGBA, deadline time.Time) bool {
51		for time.Now().Before(deadline) {
52			f, err := os.Open(latest)
53			if err == nil {
54				img, decodeErr := png.Decode(f)
55				f.Close()
56				if decodeErr == nil {
57					r, g, b, _ := img.At(1900, 10).RGBA()
58					if uint8(r>>8) == want.R && uint8(g>>8) == want.G && uint8(b>>8) == want.B {
59						return true
60					}
61				}
62			}
63			select {
64			case err := <-done:
65				if err != nil && strings.Contains(strings.ToLower(err.Error()), "vulkan") {
66					t.Skipf("Vulkan unavailable: %v", err)
67				}
68				t.Fatalf("app exited early: %v", err)
69			default:
70			}
71			time.Sleep(20 * time.Millisecond)
72		}
73		return false
74	}
75	if !waitColor(color.RGBA{A: 255}, time.Now().Add(10*time.Second)) {
76		t.Fatal("first black frame missing")
77	}
78	if err := os.WriteFile(path, []byte("background = #ff0000\nkeyboard.layout = fr\n"), 0600); err != nil {
79		t.Fatal(err)
80	}
81	if !waitColor(color.RGBA{R: 255, A: 255}, time.Now().Add(10*time.Second)) {
82		t.Fatal("red frame missing after reload")
83	}
84}
85
86func TestQuitJoinsWorkers(t *testing.T) {
87	done := make(chan error, 1)
88	go func() {
89		done <- run(context.Background(), Options{Backend: "headless", NoXwayland: true, Config: config.Defaults(), NoTerminal: true}, func(input chan<- ports.InputEvent) {
90			input <- ports.KeyEvent{Keysym: "BackSpace", Mods: ports.ModCtrl | ports.ModAlt, Pressed: true}
91		})
92	}()
93	select {
94	case err := <-done:
95		if err != nil {
96			t.Fatal(err)
97		}
98	case <-time.After(2 * time.Second):
99		t.Fatal("quit did not join workers")
100	}
101}
102
103func TestHeadlessSpawnClose(t *testing.T) {
104	if _, err := exec.LookPath("weston-simple-shm"); err != nil {
105		t.Skip("weston-simple-shm not installed")
106	}
107	t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
108	t.Setenv("XDG_STATE_HOME", t.TempDir())
109	cfg := config.Defaults()
110	cfg.Terminal.Command = []string{"weston-simple-shm"}
111	scenes := make(chan []ports.Scene, 64)
112	ctx, cancel := context.WithCancel(context.Background())
113	defer cancel()
114	inputReady := make(chan chan<- ports.InputEvent, 1)
115	done := make(chan error, 1)
116	go func() {
117		done <- run(ctx, Options{Backend: "headless", NoXwayland: true, NoTerminal: true, Config: cfg, testScenes: scenes}, func(input chan<- ports.InputEvent) { inputReady <- input })
118	}()
119	var input chan<- ports.InputEvent
120	select {
121	case input = <-inputReady:
122	case <-time.After(3 * time.Second):
123		t.Fatal("app did not start")
124	}
125	defer func() {
126		cancel()
127		select {
128		case err := <-done:
129			if err != nil {
130				t.Errorf("Run: %v", err)
131			}
132		case <-time.After(3 * time.Second):
133			t.Error("Run did not stop")
134		}
135	}()
136	input <- ports.KeyEvent{Keysym: "Return", Mods: ports.ModSuper, Pressed: true}
137	waitScene := func(count int, timeout time.Duration) {
138		t.Helper()
139		timer := time.NewTimer(timeout)
140		defer timer.Stop()
141		for {
142			select {
143			case set := <-scenes:
144				s := set[0]
145				if len(s.Windows) == count {
146					// A lone column fills the 1920 output (default zero gaps).
147					if count == 1 && s.Windows[0].Rect.W != 1920 {
148						t.Errorf("width = %d, want 1920", s.Windows[0].Rect.W)
149					}
150					return
151				}
152			case <-timer.C:
153				t.Fatalf("no scene with %d windows", count)
154			case err := <-done:
155				t.Fatalf("app exited early: %v", err)
156			}
157		}
158	}
159	waitScene(1, 10*time.Second)
160	input <- ports.KeyEvent{Keysym: "q", Mods: ports.ModSuper, Pressed: true}
161	waitScene(0, 5*time.Second)
162}
163
164func TestHeadlessTyping(t *testing.T) {
165	if _, err := exec.LookPath("foot"); err != nil {
166		t.Skip("foot unavailable")
167	}
168	t.Setenv("HOME", t.TempDir())
169	t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
170	t.Setenv("XDG_STATE_HOME", t.TempDir())
171	cfg := config.Defaults()
172	cfg.Terminal.Command = []string{"foot", "-c", "/dev/null", "sh"}
173	dir := t.TempDir()
174	err := Run(context.Background(), Options{Backend: "headless", NoXwayland: true, Config: cfg, ScreenshotDir: dir, Script: io.NopCloser(strings.NewReader("sleep 1.5s\ntype echo neferwl-ok\nkey Return\nsleep 1s\n")), Timeout: 6 * time.Second})
175	if err != nil {
176		if strings.Contains(err.Error(), "Vulkan") || strings.Contains(err.Error(), "vulkan") {
177			t.Skipf("Vulkan unavailable: %v", err)
178		}
179		t.Fatal(err)
180	}
181	path := filepath.Join(dir, "latest.png")
182	f, err := os.Open(path)
183	if err != nil {
184		t.Fatal(err)
185	}
186	defer f.Close()
187	img, err := png.Decode(f)
188	if err != nil {
189		t.Fatal(err)
190	}
191	// The lone column fills the output; the typed text sits in its top-left area.
192	first := img.At(1500, 600)
193	varied := false
194	for y := 0; y < 400 && !varied; y += 2 {
195		for x := 0; x < 900; x += 2 {
196			if img.At(x, y) != first {
197				varied = true
198				break
199			}
200		}
201	}
202	if !varied {
203		t.Fatal("window image is uniform")
204	}
205	if os.Getenv("NEFERWL_KEEP_SHOTS") == "1" {
206		data, err := os.ReadFile(path)
207		if err != nil {
208			t.Fatal(err)
209		}
210		if err := os.WriteFile("/tmp/neferwl-typing.png", data, 0600); err != nil {
211			t.Fatal(err)
212		}
213	}
214}
215
216func TestHeadlessPointerClickFocus(t *testing.T) {
217	if _, err := exec.LookPath("foot"); err != nil {
218		t.Skip("foot unavailable")
219	}
220	t.Setenv("HOME", t.TempDir())
221	t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
222	t.Setenv("XDG_STATE_HOME", t.TempDir())
223	cfg := config.Defaults()
224	cfg.Terminal.Command = []string{"foot", "-c", "/dev/null", "sh"}
225	scenes := make(chan []ports.Scene, 128)
226	err := Run(context.Background(), Options{Backend: "headless", NoXwayland: true, Config: cfg, Script: io.NopCloser(strings.NewReader("sleep 1s\nkey Super+Return\nsleep 1s\nmove 600 300\nclick\nsleep 500ms\n")), Timeout: 5 * time.Second, testScenes: scenes})
227	if err != nil {
228		if strings.Contains(strings.ToLower(err.Error()), "vulkan") {
229			t.Skipf("Vulkan unavailable: %v", err)
230		}
231		t.Fatal(err)
232	}
233	two, focused := false, false
234	for len(scenes) > 0 {
235		s := (<-scenes)[0]
236		if len(s.Windows) == 2 {
237			two = true
238			for _, w := range s.Windows {
239				if w.ID == 1 && w.Focused {
240					focused = true
241				}
242			}
243		}
244	}
245	if !two || !focused {
246		t.Fatalf("two columns=%v first focused=%v", two, focused)
247	}
248}
249
250func TestBlockedScriptShutdown(t *testing.T) {
251	t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
252	r, w := io.Pipe()
253	defer w.Close()
254	done := make(chan error, 1)
255	go func() {
256		done <- Run(context.Background(), Options{Backend: "headless", NoXwayland: true, Config: config.Defaults(), NoTerminal: true, Script: r, Timeout: 300 * time.Millisecond})
257	}()
258	select {
259	case <-done:
260	case <-time.After(2 * time.Second):
261		t.Fatal("blocked script prevented shutdown")
262	}
263}
264
265func TestRelayConfigKeyboard(t *testing.T) {
266	ctx, cancel := context.WithCancel(context.Background())
267	defer cancel()
268	cur := config.Defaults()
269	in := make(chan ports.ConfigChanged, 1)
270	out := make(chan ports.ConfigChanged, 1)
271	keymaps := make(chan *xkb.Keymap, 1)
272	commands := make(chan ports.ClientCommand, 1)
273	go relayConfig(ctx, cur, in, out, keymaps, commands, logging.For(ctx, "config"))
274
275	next := config.Defaults()
276	next.Background.Color = "#000000"
277	in <- ports.ConfigChanged{Config: next}
278	if got := <-out; got.Config.Background.Color != "#000000" || len(keymaps) != 0 || len(commands) != 0 {
279		t.Fatalf("non-keyboard change rebuilt keymap: %+v", got)
280	}
281
282	next.Keyboard.Layout, next.Keyboard.RepeatRate = "fr", 40
283	in <- ports.ConfigChanged{Config: next}
284	<-out
285	km := <-keymaps
286	defer km.Close()
287	cmd := (<-commands).(ports.SetKeymap)
288	if cmd.RepeatRate != 40 || !strings.Contains(cmd.Keymap, "xkb_keymap") {
289		t.Fatalf("keymap command: rate=%d", cmd.RepeatRate)
290	}
291	if code, _, ok := km.KeycodeFor("a"); !ok || code != 16 {
292		t.Fatalf("fr keymap: a at %d", code)
293	}
294
295	// Repeat-only changes update clients without a new keymap.
296	next.Keyboard.RepeatDelay = 300
297	in <- ports.ConfigChanged{Config: next}
298	<-out
299	if cmd := (<-commands).(ports.SetKeymap); cmd.Keymap != "" || cmd.RepeatDelay != 300 || len(keymaps) != 0 {
300		t.Fatalf("repeat-only: keymap=%t delay=%d", cmd.Keymap != "", cmd.RepeatDelay)
301	}
302
303	// A bad layout keeps the previous one; its repeat change still applies.
304	bad := next
305	bad.Keyboard.Layout, bad.Keyboard.RepeatRate = "no-such-layout", 50
306	in <- ports.ConfigChanged{Config: bad}
307	if got := <-out; got.Config.Keyboard.Layout != "fr" || got.Config.Keyboard.RepeatRate != 50 || len(keymaps) != 0 {
308		t.Fatalf("bad layout applied: %+v", got.Config.Keyboard)
309	}
310	if cmd := (<-commands).(ports.SetKeymap); cmd.Keymap != "" || cmd.RepeatRate != 50 {
311		t.Fatalf("bad layout command: keymap=%t rate=%d", cmd.Keymap != "", cmd.RepeatRate)
312	}
313}
314
315func TestHeadlessTwoOutputs(t *testing.T) {
316	t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
317	scenes := make(chan []ports.Scene, 64)
318	shots := t.TempDir()
319	err := Run(context.Background(), Options{Backend: "headless", NoXwayland: true, Config: config.Defaults(), NoTerminal: true, ScreenshotDir: shots, Sizes: [][2]int{{640, 480}, {320, 240}}, Timeout: 2 * time.Second, testScenes: scenes})
320	if err != nil {
321		if strings.Contains(strings.ToLower(err.Error()), "vulkan") {
322			t.Skipf("Vulkan unavailable: %v", err)
323		}
324		t.Fatal(err)
325	}
326	var last []ports.Scene
327	for len(scenes) > 0 {
328		last = <-scenes
329	}
330	if len(last) != 2 || last[0].Output != "HEADLESS-1" || last[1].OutputWidth != 320 {
331		t.Fatalf("%+v", last)
332	}
333	for _, name := range []string{"HEADLESS-1", "HEADLESS-2"} {
334		if _, err := os.Stat(filepath.Join(shots, name, "latest.png")); err != nil {
335			t.Fatal(err)
336		}
337	}
338}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 26

Tags 10

↑↓ moveenter openesc close