|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | +) |
| 10 | + |
| 11 | +// Key handling for the form and the root model (#172). |
| 12 | + |
| 13 | +var ( |
| 14 | + keyTab = tea.KeyMsg{Type: tea.KeyTab} |
| 15 | + keyShiftTab = tea.KeyMsg{Type: tea.KeyShiftTab} |
| 16 | + keyDown = tea.KeyMsg{Type: tea.KeyDown} |
| 17 | + keyUp = tea.KeyMsg{Type: tea.KeyUp} |
| 18 | + keySpace = tea.KeyMsg{Type: tea.KeySpace, Runes: []rune{' '}} |
| 19 | +) |
| 20 | + |
| 21 | +func typeText(s string) tea.KeyMsg { return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(s)} } |
| 22 | + |
| 23 | +// focusedInputs counts the text inputs that have focus. |
| 24 | +func focusedInputs(m formModel) int { |
| 25 | + n := 0 |
| 26 | + for _, in := range m.inputs { |
| 27 | + if in.Focused() { |
| 28 | + n++ |
| 29 | + } |
| 30 | + } |
| 31 | + return n |
| 32 | +} |
| 33 | + |
| 34 | +func TestFormTabOrderWrapsAndSkipsGatedFields(t *testing.T) { |
| 35 | + m := newFormModel(savedConfig{}) |
| 36 | + var order []int |
| 37 | + for range fieldCount { |
| 38 | + var cmd tea.Cmd |
| 39 | + m, cmd = m.Update(keyTab) |
| 40 | + order = append(order, m.focus) |
| 41 | + if inputForField[m.focus] >= 0 { |
| 42 | + if cmd == nil || !m.inputs[inputForField[m.focus]].Focused() { |
| 43 | + t.Fatalf("tab onto text field %d: cmd=%v focused=%v", m.focus, cmd != nil, m.inputs[inputForField[m.focus]].Focused()) |
| 44 | + } |
| 45 | + } else if cmd != nil { |
| 46 | + t.Fatalf("tab onto toggle %d returned a command", m.focus) |
| 47 | + } |
| 48 | + if focusedInputs(m) > 1 { |
| 49 | + t.Fatalf("%d inputs focused after tab to %d", focusedInputs(m), m.focus) |
| 50 | + } |
| 51 | + } |
| 52 | + for _, f := range order { |
| 53 | + if f == fieldHitRate || f == fieldDepth { |
| 54 | + t.Fatalf("tab visited gated field %d with its toggle off: %v", f, order) |
| 55 | + } |
| 56 | + } |
| 57 | + // 18 fields less the 2 gated ones: the 16th tab is back on the domain. |
| 58 | + if order[fieldCount-3] != fieldDomain { |
| 59 | + t.Fatalf("tab order %v does not wrap to the domain after 16 steps", order) |
| 60 | + } |
| 61 | + |
| 62 | + // Backwards from the domain wraps to the last field. |
| 63 | + m = newFormModel(savedConfig{}) |
| 64 | + m, _ = m.Update(keyShiftTab) |
| 65 | + if m.focus != fieldNoAbort || m.inputs[0].Focused() { |
| 66 | + t.Fatalf("shift+tab from the domain: focus=%d domain focused=%v", m.focus, m.inputs[0].Focused()) |
| 67 | + } |
| 68 | + m, _ = m.Update(keyUp) |
| 69 | + if m.focus != fieldForce { |
| 70 | + t.Fatalf("up: focus=%d, want Force", m.focus) |
| 71 | + } |
| 72 | + m, _ = m.Update(keyDown) |
| 73 | + if m.focus != fieldNoAbort { |
| 74 | + t.Fatalf("down: focus=%d, want No Abort", m.focus) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestFormTabVisitsGatedFieldsWhenEnabled(t *testing.T) { |
| 79 | + m := newFormModel(savedConfig{Simulate: true, Recursive: true}) |
| 80 | + seen := map[int]bool{} |
| 81 | + for range fieldCount { |
| 82 | + m, _ = m.Update(keyTab) |
| 83 | + seen[m.focus] = true |
| 84 | + } |
| 85 | + if len(seen) != fieldCount || !seen[fieldHitRate] || !seen[fieldDepth] { |
| 86 | + t.Fatalf("visited %d fields, want all %d including Hit Rate and Depth", len(seen), fieldCount) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestFormSpaceTogglesFocusedToggle(t *testing.T) { |
| 91 | + for _, tc := range []struct { |
| 92 | + field, index int |
| 93 | + }{ |
| 94 | + {fieldSimulate, 0}, {fieldForce, 1}, {fieldRecursive, 2}, {fieldNoAbort, 3}, |
| 95 | + } { |
| 96 | + m := newFormModel(savedConfig{}) |
| 97 | + m.focus = tc.field |
| 98 | + m, _ = m.Update(keySpace) |
| 99 | + for i, on := range m.toggles { |
| 100 | + if on != (i == tc.index) { |
| 101 | + t.Fatalf("space on field %d: toggles = %v, want only %d on", tc.field, m.toggles, tc.index) |
| 102 | + } |
| 103 | + } |
| 104 | + m, _ = m.Update(keySpace) |
| 105 | + if m.toggles[tc.index] { |
| 106 | + t.Fatalf("second space on field %d did not toggle back", tc.field) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // On a text field, space is typed rather than toggling anything. |
| 111 | + m := newFormModel(savedConfig{}) |
| 112 | + m.focus = fieldExclude |
| 113 | + m.inputs[13].Focus() |
| 114 | + m, _ = m.Update(typeText("a")) |
| 115 | + m, _ = m.Update(keySpace) |
| 116 | + if m.inputs[13].Value() != "a " || m.toggles != [4]bool{} { |
| 117 | + t.Fatalf("space in a text field: value=%q toggles=%v", m.inputs[13].Value(), m.toggles) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestFormTypingAndResize(t *testing.T) { |
| 122 | + m := newFormModel(savedConfig{}) |
| 123 | + m, _ = m.Update(typeText("example.com")) |
| 124 | + if got := m.inputs[0].Value(); got != "example.com" { |
| 125 | + t.Fatalf("domain = %q", got) |
| 126 | + } |
| 127 | + m, _ = m.Update(tea.WindowSizeMsg{Width: 90, Height: 30}) |
| 128 | + if m.width != 90 { |
| 129 | + t.Fatalf("width = %d", m.width) |
| 130 | + } |
| 131 | + // Keys on a toggle are not forwarded to any input. |
| 132 | + m.focus = fieldForce |
| 133 | + m, _ = m.Update(typeText("x")) |
| 134 | + if m.inputs[0].Value() != "example.com" { |
| 135 | + t.Fatalf("typing on a toggle changed the domain: %q", m.inputs[0].Value()) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestFormViewShowsFocusHintsAndGates(t *testing.T) { |
| 140 | + m := newFormModel(savedConfig{}) |
| 141 | + v := m.View() |
| 142 | + for _, want := range []string{"Configure Scan", "▸ ", "(enable Simulate)", "(enable Recursive)", "ctrl+r run"} { |
| 143 | + if !strings.Contains(v, want) { |
| 144 | + t.Errorf("form view lacks %q", want) |
| 145 | + } |
| 146 | + } |
| 147 | + m.focus = fieldRecursive |
| 148 | + m.toggles = [4]bool{true, false, true, false} |
| 149 | + v = m.View() |
| 150 | + if !strings.Contains(v, "[space to toggle]") || strings.Contains(v, "(enable Simulate)") || strings.Contains(v, "(enable Recursive)") { |
| 151 | + t.Errorf("view with simulate and recursive on:\n%s", v) |
| 152 | + } |
| 153 | + m.err = "Domain is required" |
| 154 | + if !strings.Contains(m.View(), "✗ Domain is required") { |
| 155 | + t.Error("form view does not show the error") |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestModelFormKeys(t *testing.T) { |
| 160 | + m := Model{state: stateForm, form: newFormModel(savedConfig{})} |
| 161 | + if m.Init() == nil { |
| 162 | + t.Error("Init returned no blink command") |
| 163 | + } |
| 164 | + if !strings.Contains(m.View(), "Configure Scan") { |
| 165 | + t.Error("model view is not the form") |
| 166 | + } |
| 167 | + |
| 168 | + // ctrl+r with an invalid domain shows an error and starts nothing. |
| 169 | + m.form.inputs[0].SetValue("not a domain!") |
| 170 | + next, cmd := m.Update(tea.KeyMsg{Type: tea.KeyCtrlR}) |
| 171 | + nm := next.(Model) |
| 172 | + if cmd != nil || nm.form.err == "" || nm.form.loading || nm.state != stateForm { |
| 173 | + t.Fatalf("invalid domain: cmd=%v err=%q loading=%v state=%v", cmd != nil, nm.form.err, nm.form.loading, nm.state) |
| 174 | + } |
| 175 | + |
| 176 | + // A resize reaches both the model and the form. |
| 177 | + next, _ = nm.Update(tea.WindowSizeMsg{Width: 100, Height: 40}) |
| 178 | + nm = next.(Model) |
| 179 | + if nm.width != 100 || nm.height != 40 || nm.form.width != 100 { |
| 180 | + t.Fatalf("resize: model %dx%d form %d", nm.width, nm.height, nm.form.width) |
| 181 | + } |
| 182 | + |
| 183 | + // Keys reach the focused form field. |
| 184 | + next, _ = nm.Update(keyTab) |
| 185 | + if next.(Model).form.focus != fieldWordlist { |
| 186 | + t.Fatalf("tab through the model: focus=%d", next.(Model).form.focus) |
| 187 | + } |
| 188 | + |
| 189 | + _, cmd = nm.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) |
| 190 | + if cmd == nil { |
| 191 | + t.Fatal("ctrl+c on the form returned no command") |
| 192 | + } |
| 193 | + if _, ok := cmd().(tea.QuitMsg); !ok { |
| 194 | + t.Fatal("ctrl+c on the form did not quit") |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func TestModelScanKeys(t *testing.T) { |
| 199 | + ctx, cancel := context.WithCancel(context.Background()) |
| 200 | + m := Model{state: stateScan, scanView: newScanViewModel(80, 24, false), cancel: cancel} |
| 201 | + if m.View() == "" { |
| 202 | + t.Fatal("scan state renders nothing") |
| 203 | + } |
| 204 | + |
| 205 | + // q and r do nothing while the scan runs. |
| 206 | + for _, k := range []tea.KeyMsg{typeText("q"), typeText("r")} { |
| 207 | + next, cmd := m.Update(k) |
| 208 | + if next.(Model).state != stateScan { |
| 209 | + t.Fatalf("%q during a scan left the scan screen", k.String()) |
| 210 | + } |
| 211 | + if cmd != nil { |
| 212 | + if _, quit := cmd().(tea.QuitMsg); quit { |
| 213 | + t.Fatalf("%q during a scan quit", k.String()) |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + // ctrl+c during a scan cancels it and marks it aborted, without quitting. |
| 219 | + next, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) |
| 220 | + am := next.(Model) |
| 221 | + if ctx.Err() == nil || !am.scanView.aborted || am.state != stateScan { |
| 222 | + t.Fatalf("ctrl+c during a scan: cancelled=%v aborted=%v state=%v", ctx.Err() != nil, am.scanView.aborted, am.state) |
| 223 | + } |
| 224 | + |
| 225 | + // Once done, q quits and r returns to a fresh form. |
| 226 | + am.scanView.done = true |
| 227 | + if _, cmd := am.Update(typeText("q")); cmd == nil { |
| 228 | + t.Fatal("q after done returned no command") |
| 229 | + } else if _, ok := cmd().(tea.QuitMsg); !ok { |
| 230 | + t.Fatal("q after done did not quit") |
| 231 | + } |
| 232 | + redirectConfig(t) |
| 233 | + next, cmd := am.Update(typeText("r")) |
| 234 | + fm := next.(Model) |
| 235 | + if fm.state != stateForm || fm.events != nil || cmd == nil { |
| 236 | + t.Fatalf("r after done: state=%v events=%v cmd=%v", fm.state, fm.events != nil, cmd != nil) |
| 237 | + } |
| 238 | + if !strings.Contains(fm.View(), "Configure Scan") { |
| 239 | + t.Fatal("r after done does not show the form") |
| 240 | + } |
| 241 | +} |
0 commit comments