v0.6.0 7d47882

backdrop_test.go RawHistoryFolder

1package selection
2
3import (
4	"context"
5	"image"
6	"testing"
7
8	"github.com/stretchr/testify/assert"
9	"github.com/stretchr/testify/require"
10
11	"github.com/bnema/nefercap/internal/ports"
12)
13
14func TestBackdropImageIsOpaqueRGBA(t *testing.T) {
15	// One XRGB8888 pixel is B, G, R, X in memory; the stride has padding.
16	f := ports.Frame{Pixels: []byte{
17		0x10, 0x20, 0x30, 0x00, 0x40, 0x50, 0x60, 0x00, 0xee, 0xee, 0xee, 0xee,
18		0x70, 0x80, 0x90, 0x00, 0xa0, 0xb0, 0xc0, 0x00,
19	}, Width: 2, Height: 2, Stride: 12, Format: ports.XRGB8888}
20	require.NoError(t, f.Validate())
21	img := backdropImage(f)
22	assert.Equal(t, image.Rect(0, 0, 2, 2), img.Bounds())
23	assert.Equal(t, []byte{0x30, 0x20, 0x10, 0xff, 0x60, 0x50, 0x40, 0xff, 0x90, 0x80, 0x70, 0xff, 0xc0, 0xb0, 0xa0, 0xff}, img.Pix)
24}
25
26func TestWithBackdrops(t *testing.T) {
27	small := ports.Frame{Pixels: make([]byte, 4*4*4), Width: 4, Height: 4, Stride: 16, Format: ports.XRGB8888}
28	s, err := New(ports.Screenshot, ports.VideoSettings{}, nil, false).WithBackdrops(map[uint32]ports.Frame{7: small})
29	require.NoError(t, err)
30	assert.Contains(t, s.backdrops, uint32(7))
31
32	huge := ports.Frame{Width: maxBackdrop + 1, Height: 1, Stride: (maxBackdrop + 1) * 4, Format: ports.XRGB8888}
33	_, err = New(ports.Screenshot, ports.VideoSettings{}, nil, false).WithBackdrops(map[uint32]ports.Frame{9: huge})
34	assert.ErrorContains(t, err, "at most 4096", "the caller freezes only outputs CanBackdrop accepts")
35}
36
37func TestCanBackdrop(t *testing.T) {
38	assert.True(t, CanBackdrop(ports.Output{Width: 3840, Height: 2160}))
39	assert.True(t, CanBackdrop(ports.Output{Width: 2160, Height: 4096}))
40	assert.False(t, CanBackdrop(ports.Output{Width: 5120, Height: 2880}))
41	assert.False(t, CanBackdrop(ports.Output{}), "an unknown mode stays live")
42}
43
44// The frozen screen is drawn under a screenshot selection only: a recording
45// selects on the live screen.
46func TestBackdropDrawnOnlyForScreenshots(t *testing.T) {
47	m := newTestModel(t)
48	m.backdrop = image.NewNRGBA(image.Rect(0, 0, 1920, 1080))
49	assert.True(t, drawsBackdrop(m))
50	m.mode = ports.Record
51	assert.False(t, drawsBackdrop(m))
52	m.mode, m.backdrop = ports.Screenshot, nil
53	assert.False(t, drawsBackdrop(m))
54}
55
56func frozenModel(t *testing.T, index int, sess *session) *model {
57	t.Helper()
58	m := newModel(ports.Screenshot, testOutputs, index, sess, false)
59	m.backdrop = image.NewNRGBA(image.Rect(0, 0, 1, 1))
60	m.wake = make(chan struct{}, 1)
61	m.resize(1920, 1080, 1)
62	return m
63}
64
65// F switches every overlay between the frozen and the live screen, the
66// footer says what F does next, and the accepted pick records the state.
67func TestFreezeKeyTogglesEveryOverlay(t *testing.T) {
68	sess, _ := testSession()
69	sess.canFreeze = true
70	a, b := frozenModel(t, 0, sess), frozenModel(t, 1, sess)
71	sess.wakes = []chan struct{}{a.wake, b.wake}
72	require.True(t, drawsBackdrop(a))
73	assert.Contains(t, a.footerText, "G grid · F live · Esc cancel")
74
75	assert.True(t, a.input(key("f")), "F redraws")
76	assert.False(t, drawsBackdrop(a))
77	assert.Equal(t, toastLive, a.toast.text)
78	assert.Contains(t, a.footerText, "F freeze")
79	require.Len(t, b.wake, 1, "the other overlay is woken")
80	b.input(motion(1, 1))
81	assert.False(t, drawsBackdrop(b), "the other overlay follows")
82
83	b.input(key("F"))
84	assert.True(t, drawsBackdrop(b))
85	a.input(motion(2, 2))
86	assert.True(t, drawsBackdrop(a))
87
88	a.input(key("f"))
89	a.input(key("m"))
90	a.input(key("Return"))
91	_, ok, err := sess.outcome(context.Background())
92	require.NoError(t, err)
93	require.True(t, ok)
94	assert.False(t, sess.acceptedFrozen(), "a pick on the live screen is captured live")
95}
96
97// The live state survives a round trip through record mode, where F is
98// ignored.
99func TestFreezeKeyWithTab(t *testing.T) {
100	sess, _ := testSession()
101	sess.canFreeze = true
102	m := frozenModel(t, 0, sess)
103	m.toggle = true
104	m.input(key("f"))
105	m.input(key("Tab"))
106	require.Equal(t, ports.Record, m.mode)
107	assert.False(t, m.input(key("f")), "F is ignored in record mode")
108	m.input(key("Tab"))
109	assert.False(t, drawsBackdrop(m), "still live after the round trip")
110	assert.Contains(t, m.footerText, "F freeze")
111}
112
113// Like Tab, F allocates only the rebuilt footer text.
114func TestFreezeKeyAllocations(t *testing.T) {
115	if raceEnabled {
116		t.Skip("the race detector adds allocations")
117	}
118	sess, _ := testSession()
119	sess.canFreeze = true
120	m := frozenModel(t, 0, sess)
121	n := testing.AllocsPerRun(100, func() {
122		m.input(key("f"))
123		_ = m.scene()
124	})
125	assert.LessOrEqual(t, n, 1.0, "an F press allocates only the rebuilt legend text")
126}
127
128// Without a frozen screen, or in record mode, F does nothing and is not shown.
129func TestFreezeKeyHiddenWithoutBackdrop(t *testing.T) {
130	m := newTestModel(t)
131	assert.NotContains(t, m.footerText, "F ")
132	assert.False(t, m.input(key("f")))
133
134	sess, _ := testSession()
135	sess.canFreeze = true
136	m = frozenModel(t, 0, sess)
137	m.mode = ports.Record
138	m.refreshFooter()
139	assert.NotContains(t, m.footerText, "F ")
140	assert.False(t, m.input(key("f")))
141	m.input(key("m"))
142	m.input(key("Return"))
143	assert.True(t, sess.acceptedFrozen(), "untouched, the frozen state is kept for the app to thaw")
144}

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 2

Tags 9

↑↓ moveenter openesc close