|
1 | 1 | package completion |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "reflect" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + tea "charm.land/bubbletea/v2" |
6 | 8 | "github.com/stretchr/testify/assert" |
7 | 9 | ) |
8 | 10 |
|
@@ -335,3 +337,79 @@ func TestCompletionManagerPinnedItems(t *testing.T) { |
335 | 337 | assert.Equal(t, "main.go", m.filteredItems[1].Label, "matching item should be second") |
336 | 338 | }) |
337 | 339 | } |
| 340 | + |
| 341 | +// extractSequenceCmds extracts the slice of commands from a tea.SequenceMsg using reflection, |
| 342 | +// since tea.sequenceMsg is unexported. |
| 343 | +func extractSequenceCmds(c tea.Cmd) []tea.Cmd { |
| 344 | + if c == nil { |
| 345 | + return nil |
| 346 | + } |
| 347 | + seqMsg := c() |
| 348 | + v := reflect.ValueOf(seqMsg) |
| 349 | + var cmds []tea.Cmd |
| 350 | + if v.Kind() == reflect.Slice { |
| 351 | + for i := range v.Len() { |
| 352 | + cmd, ok := v.Index(i).Interface().(tea.Cmd) |
| 353 | + if ok { |
| 354 | + cmds = append(cmds, cmd) |
| 355 | + } |
| 356 | + } |
| 357 | + } |
| 358 | + return cmds |
| 359 | +} |
| 360 | + |
| 361 | +func TestCompletionManagerAutoSubmit(t *testing.T) { |
| 362 | + t.Parallel() |
| 363 | + |
| 364 | + t.Run("enter triggers auto submit", func(t *testing.T) { |
| 365 | + t.Parallel() |
| 366 | + |
| 367 | + m := New().(*manager) |
| 368 | + |
| 369 | + m.Update(OpenMsg{ |
| 370 | + Items: []Item{ |
| 371 | + {Label: "option", Value: "/option"}, |
| 372 | + }, |
| 373 | + }) |
| 374 | + |
| 375 | + _, c := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) |
| 376 | + |
| 377 | + cmds := extractSequenceCmds(c) |
| 378 | + |
| 379 | + assert.False(t, m.visible, "completion view should close") |
| 380 | + assert.Len(t, cmds, 2, "should return a sequence of 2 commands") |
| 381 | + |
| 382 | + if len(cmds) > 0 { |
| 383 | + msg0 := cmds[0]() |
| 384 | + selectedMsg, ok := msg0.(SelectedMsg) |
| 385 | + assert.True(t, ok, "first message should be SelectedMsg") |
| 386 | + assert.True(t, selectedMsg.AutoSubmit, "should have auto submit true") |
| 387 | + } |
| 388 | + }) |
| 389 | + |
| 390 | + t.Run("tab disables auto submit", func(t *testing.T) { |
| 391 | + t.Parallel() |
| 392 | + |
| 393 | + m := New().(*manager) |
| 394 | + |
| 395 | + m.Update(OpenMsg{ |
| 396 | + Items: []Item{ |
| 397 | + {Label: "option", Value: "/option"}, |
| 398 | + }, |
| 399 | + }) |
| 400 | + |
| 401 | + _, c := m.Update(tea.KeyPressMsg{Code: tea.KeyTab}) |
| 402 | + |
| 403 | + cmds := extractSequenceCmds(c) |
| 404 | + |
| 405 | + assert.False(t, m.visible, "completion view should close") |
| 406 | + assert.Len(t, cmds, 2, "should return a sequence of 2 commands") |
| 407 | + |
| 408 | + if len(cmds) > 0 { |
| 409 | + msg0 := cmds[0]() |
| 410 | + selectedMsg, ok := msg0.(SelectedMsg) |
| 411 | + assert.True(t, ok, "first message should be SelectedMsg") |
| 412 | + assert.False(t, selectedMsg.AutoSubmit, "should have auto submit false") |
| 413 | + } |
| 414 | + }) |
| 415 | +} |
0 commit comments