Debian Codegraph @

app/keyhint_test.go

raw
1 package app
2
3 import (
4 "testing"
5
6 "git.sr.ht/~rjarry/aerc/config"
7 "go.rockorager.dev/vaxis"
8 )
9
10 func stroke(key rune) config.KeyStroke {
11 return config.KeyStroke{Key: key}
12 }
13
14 func binding(input, output []config.KeyStroke, annotation string) *config.Binding {
15 return &config.Binding{Input: input, Output: output, Annotation: annotation}
16 }
17
18 func TestNewKeyHintEmptyPrefix(t *testing.T) {
19 bindings := []*config.Binding{
20 binding([]config.KeyStroke{stroke('z'), stroke('c')}, nil, "fold"),
21 binding([]config.KeyStroke{stroke('z'), stroke('o')}, nil, "unfold"),
22 binding([]config.KeyStroke{stroke('g'), stroke('g')}, nil, "top"),
23 }
24 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('z')})
25 if len(kh.entries) != 2 {
26 t.Fatalf("expected 2 entries, got %d", len(kh.entries))
27 }
28 if kh.entries[0].key != "c" {
29 t.Errorf("first key: got %q, want %q", kh.entries[0].key, "c")
30 }
31 if kh.entries[0].desc != "fold" {
32 t.Errorf("first desc: got %q, want %q", kh.entries[0].desc, "fold")
33 }
34 if kh.entries[1].key != "o" {
35 t.Errorf("second key: got %q, want %q", kh.entries[1].key, "o")
36 }
37 }
38
39 func TestNewKeyHintDeduplication(t *testing.T) {
40 bindings := []*config.Binding{
41 binding([]config.KeyStroke{stroke('z'), stroke('c')}, nil, "fold"),
42 binding([]config.KeyStroke{stroke('z'), stroke('c')}, nil, "fold duplicate"),
43 }
44 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('z')})
45 if len(kh.entries) != 1 {
46 t.Fatalf("expected 1 entry after dedup, got %d", len(kh.entries))
47 }
48 if kh.entries[0].desc != "fold" {
49 t.Errorf("desc: got %q, want %q", kh.entries[0].desc, "fold")
50 }
51 }
52
53 func TestNewKeyHintAnnotationFallback(t *testing.T) {
54 output := []config.KeyStroke{
55 {Key: ':'},
56 {Key: 'n'},
57 {Key: 'e'},
58 {Key: 'x'},
59 {Key: 't'},
60 {Key: vaxis.KeyEnter},
61 }
62 bindings := []*config.Binding{
63 binding([]config.KeyStroke{stroke('g'), stroke('n')}, output, ""),
64 }
65 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('g')})
66 if len(kh.entries) != 1 {
67 t.Fatalf("expected 1 entry, got %d", len(kh.entries))
68 }
69 if kh.entries[0].desc != "next" {
70 t.Errorf("desc fallback: got %q, want %q", kh.entries[0].desc, "next")
71 }
72 }
73
74 func TestNewKeyHintSorting(t *testing.T) {
75 bindings := []*config.Binding{
76 binding([]config.KeyStroke{stroke('z'), stroke('b')}, nil, "bottom"),
77 binding([]config.KeyStroke{stroke('z'), stroke('a')}, nil, "toggle"),
78 binding([]config.KeyStroke{stroke('z'), stroke('c')}, nil, "fold"),
79 }
80 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('z')})
81 if len(kh.entries) != 3 {
82 t.Fatalf("expected 3 entries, got %d", len(kh.entries))
83 }
84 want := []string{"a", "b", "c"}
85 for i, e := range kh.entries {
86 if e.key != want[i] {
87 t.Errorf("entry %d: got key %q, want %q", i, e.key, want[i])
88 }
89 }
90 }
91
92 func TestNewKeyHintNoMatch(t *testing.T) {
93 bindings := []*config.Binding{
94 binding([]config.KeyStroke{stroke('g'), stroke('g')}, nil, "top"),
95 binding([]config.KeyStroke{stroke('z'), stroke('c')}, nil, "fold"),
96 }
97 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('r')})
98 if len(kh.entries) != 0 {
99 t.Fatalf("expected 0 entries for non-matching prefix, got %d", len(kh.entries))
100 }
101 }
102
103 func TestNewKeyHintMultiKeyPrefix(t *testing.T) {
104 bindings := []*config.Binding{
105 binding([]config.KeyStroke{stroke('r'), stroke('t'), stroke('q')}, nil, "quick ack"),
106 binding([]config.KeyStroke{stroke('r'), stroke('t'), stroke('f')}, nil, "follow up"),
107 binding([]config.KeyStroke{stroke('r'), stroke('r')}, nil, "reply all"),
108 }
109 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('r'), stroke('t')})
110 if len(kh.entries) != 2 {
111 t.Fatalf("expected 2 entries, got %d", len(kh.entries))
112 }
113 if kh.entries[0].key != "f" {
114 t.Errorf("first key: got %q, want %q", kh.entries[0].key, "f")
115 }
116 if kh.entries[1].key != "q" {
117 t.Errorf("second key: got %q, want %q", kh.entries[1].key, "q")
118 }
119 }
120
121 func TestNewKeyHintExcludesExactMatch(t *testing.T) {
122 bindings := []*config.Binding{
123 binding([]config.KeyStroke{stroke('g')}, nil, "top"),
124 binding([]config.KeyStroke{stroke('g'), stroke('g')}, nil, "gg"),
125 }
126 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('g')})
127 if len(kh.entries) != 1 {
128 t.Fatalf("expected 1 entry (exact match excluded), got %d", len(kh.entries))
129 }
130 if kh.entries[0].key != "g" {
131 t.Errorf("key: got %q, want %q", kh.entries[0].key, "g")
132 }
133 }
134
135 func TestNewKeyHintSizing(t *testing.T) {
136 bindings := []*config.Binding{
137 binding([]config.KeyStroke{stroke('z'), stroke('c')}, nil, "fold"),
138 binding([]config.KeyStroke{stroke('z'), stroke('o')}, nil, "unfold"),
139 }
140 kh := NewKeyHint(bindings, []config.KeyStroke{stroke('z')})
141 if kh.RequiredHeight() != 2 {
142 t.Errorf("height: got %d, want 2 (2 content rows)", kh.RequiredHeight())
143 }
144 wantW := 1 + 3 + 20 + 1
145 if kh.RequiredWidth() != wantW {
146 t.Errorf("width: got %d, want %d", kh.RequiredWidth(), wantW)
147 }
148 }
149
150 func TestNewKeyHintEmptyBindings(t *testing.T) {
151 kh := NewKeyHint(nil, []config.KeyStroke{stroke('z')})
152 if len(kh.entries) != 0 {
153 t.Fatalf("expected 0 entries, got %d", len(kh.entries))
154 }
155 if kh.RequiredHeight() != 0 {
156 t.Errorf("height: got %d, want 0 (no entries)", kh.RequiredHeight())
157 }
158 }