|
| 1 | +// Copyright 2022-2026 Salesforce, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package shell |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + tea "github.com/charmbracelet/bubbletea" |
| 21 | + "github.com/charmbracelet/x/ansi" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestInputModel(t *testing.T) { |
| 26 | + tests := map[string]struct { |
| 27 | + setup func() inputModel |
| 28 | + actions func(inputModel) inputModel |
| 29 | + assertFn func(t *testing.T, m inputModel) |
| 30 | + }{ |
| 31 | + "enter submits text": { |
| 32 | + setup: func() inputModel { |
| 33 | + return newInputModel(nil, "") |
| 34 | + }, |
| 35 | + actions: func(m inputModel) inputModel { |
| 36 | + for _, r := range "deploy" { |
| 37 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}}) |
| 38 | + m = updated.(inputModel) |
| 39 | + } |
| 40 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) |
| 41 | + m = updated.(inputModel) |
| 42 | + return m |
| 43 | + }, |
| 44 | + assertFn: func(t *testing.T, m inputModel) { |
| 45 | + assert.True(t, m.done) |
| 46 | + assert.Equal(t, "deploy", m.value) |
| 47 | + }, |
| 48 | + }, |
| 49 | + "ctrl+c returns exit": { |
| 50 | + setup: func() inputModel { |
| 51 | + return newInputModel(nil, "") |
| 52 | + }, |
| 53 | + actions: func(m inputModel) inputModel { |
| 54 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) |
| 55 | + m = updated.(inputModel) |
| 56 | + return m |
| 57 | + }, |
| 58 | + assertFn: func(t *testing.T, m inputModel) { |
| 59 | + assert.True(t, m.done) |
| 60 | + assert.Equal(t, "exit", m.value) |
| 61 | + }, |
| 62 | + }, |
| 63 | + "up arrow recalls history": { |
| 64 | + setup: func() inputModel { |
| 65 | + return newInputModel([]string{"deploy", "run"}, "") |
| 66 | + }, |
| 67 | + actions: func(m inputModel) inputModel { |
| 68 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyUp}) |
| 69 | + m = updated.(inputModel) |
| 70 | + return m |
| 71 | + }, |
| 72 | + assertFn: func(t *testing.T, m inputModel) { |
| 73 | + assert.Equal(t, "run", m.textInput.Value()) |
| 74 | + assert.Equal(t, 1, m.histIndex) |
| 75 | + }, |
| 76 | + }, |
| 77 | + "up arrow twice recalls older history": { |
| 78 | + setup: func() inputModel { |
| 79 | + return newInputModel([]string{"deploy", "run"}, "") |
| 80 | + }, |
| 81 | + actions: func(m inputModel) inputModel { |
| 82 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyUp}) |
| 83 | + m = updated.(inputModel) |
| 84 | + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyUp}) |
| 85 | + m = updated.(inputModel) |
| 86 | + return m |
| 87 | + }, |
| 88 | + assertFn: func(t *testing.T, m inputModel) { |
| 89 | + assert.Equal(t, "deploy", m.textInput.Value()) |
| 90 | + assert.Equal(t, 0, m.histIndex) |
| 91 | + }, |
| 92 | + }, |
| 93 | + "down arrow restores saved text": { |
| 94 | + setup: func() inputModel { |
| 95 | + m := newInputModel([]string{"deploy", "run"}, "") |
| 96 | + for _, r := range "ver" { |
| 97 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}}) |
| 98 | + m = updated.(inputModel) |
| 99 | + } |
| 100 | + return m |
| 101 | + }, |
| 102 | + actions: func(m inputModel) inputModel { |
| 103 | + // Go up - saves "ver", shows "run" |
| 104 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyUp}) |
| 105 | + m = updated.(inputModel) |
| 106 | + // Go back down - restores "ver" |
| 107 | + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyDown}) |
| 108 | + m = updated.(inputModel) |
| 109 | + return m |
| 110 | + }, |
| 111 | + assertFn: func(t *testing.T, m inputModel) { |
| 112 | + assert.Equal(t, "ver", m.textInput.Value()) |
| 113 | + assert.Equal(t, 2, m.histIndex) |
| 114 | + }, |
| 115 | + }, |
| 116 | + "up at oldest entry does nothing": { |
| 117 | + setup: func() inputModel { |
| 118 | + return newInputModel([]string{"deploy"}, "") |
| 119 | + }, |
| 120 | + actions: func(m inputModel) inputModel { |
| 121 | + updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyUp}) |
| 122 | + m = updated.(inputModel) |
| 123 | + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyUp}) |
| 124 | + m = updated.(inputModel) |
| 125 | + return m |
| 126 | + }, |
| 127 | + assertFn: func(t *testing.T, m inputModel) { |
| 128 | + assert.Equal(t, "deploy", m.textInput.Value()) |
| 129 | + assert.Equal(t, 0, m.histIndex) |
| 130 | + }, |
| 131 | + }, |
| 132 | + "view renders border": { |
| 133 | + setup: func() inputModel { |
| 134 | + return newInputModel(nil, "") |
| 135 | + }, |
| 136 | + actions: func(m inputModel) inputModel { |
| 137 | + return m |
| 138 | + }, |
| 139 | + assertFn: func(t *testing.T, m inputModel) { |
| 140 | + view := ansi.Strip(m.View()) |
| 141 | + assert.Contains(t, view, "─") |
| 142 | + assert.Contains(t, view, "❯") |
| 143 | + }, |
| 144 | + }, |
| 145 | + "view renders banner when version set": { |
| 146 | + setup: func() inputModel { |
| 147 | + m := newInputModel(nil, "v1.0.0") |
| 148 | + m.width = 40 |
| 149 | + return m |
| 150 | + }, |
| 151 | + actions: func(m inputModel) inputModel { return m }, |
| 152 | + assertFn: func(t *testing.T, m inputModel) { |
| 153 | + view := ansi.Strip(m.View()) |
| 154 | + assert.Contains(t, view, "Slack CLI Shell") |
| 155 | + assert.Contains(t, view, "v1.0.0") |
| 156 | + assert.Contains(t, view, "❯") |
| 157 | + }, |
| 158 | + }, |
| 159 | + "view renders no banner when version empty": { |
| 160 | + setup: func() inputModel { |
| 161 | + m := newInputModel(nil, "") |
| 162 | + m.width = 40 |
| 163 | + return m |
| 164 | + }, |
| 165 | + actions: func(m inputModel) inputModel { return m }, |
| 166 | + assertFn: func(t *testing.T, m inputModel) { |
| 167 | + view := ansi.Strip(m.View()) |
| 168 | + assert.NotContains(t, view, "Slack CLI Shell") |
| 169 | + assert.Contains(t, view, "❯") |
| 170 | + }, |
| 171 | + }, |
| 172 | + "view is empty when done": { |
| 173 | + setup: func() inputModel { |
| 174 | + m := newInputModel(nil, "") |
| 175 | + m.done = true |
| 176 | + return m |
| 177 | + }, |
| 178 | + actions: func(m inputModel) inputModel { |
| 179 | + return m |
| 180 | + }, |
| 181 | + assertFn: func(t *testing.T, m inputModel) { |
| 182 | + assert.Equal(t, "", m.View()) |
| 183 | + }, |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + for name, tc := range tests { |
| 188 | + t.Run(name, func(t *testing.T) { |
| 189 | + m := tc.setup() |
| 190 | + m = tc.actions(m) |
| 191 | + tc.assertFn(t, m) |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
0 commit comments