|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + tea "charm.land/bubbletea/v2" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/docker/docker-agent/pkg/tui/dialog" |
| 11 | +) |
| 12 | + |
| 13 | +// applyOpenDialogMsgs feeds every dialog.OpenDialogMsg in cmd back into the |
| 14 | +// model, so the dialog manager actually receives them. This mirrors how the |
| 15 | +// real bubbletea event loop drains commands. |
| 16 | +func applyOpenDialogMsgs(t *testing.T, m *appModel, cmd tea.Cmd) { |
| 17 | + t.Helper() |
| 18 | + for _, msg := range collectMsgs(cmd) { |
| 19 | + if open, ok := msg.(dialog.OpenDialogMsg); ok { |
| 20 | + _, _ = m.Update(open) |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestCtrlC_NoDialog_OpensExitConfirmation(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + m, _ := newTestModel() |
| 29 | + require.False(t, m.dialogMgr.Open(), "no dialog should be open initially") |
| 30 | + |
| 31 | + _, cmd := m.Update(tea.KeyPressMsg{Code: 'c', Mod: tea.ModCtrl}) |
| 32 | + applyOpenDialogMsgs(t, m, cmd) |
| 33 | + |
| 34 | + assert.True(t, m.dialogMgr.Open(), "ctrl+c should open a dialog") |
| 35 | + assert.True(t, m.dialogMgr.TopIsExitConfirmation(), |
| 36 | + "ctrl+c with no dialog should open the exit confirmation dialog") |
| 37 | +} |
| 38 | + |
| 39 | +func TestCtrlC_OnOtherDialog_StacksExitConfirmation(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + |
| 42 | + m, _ := newTestModel() |
| 43 | + |
| 44 | + // Open an arbitrary, non-exit dialog first. |
| 45 | + _, cmd := m.Update(dialog.OpenDialogMsg{Model: dialog.NewHelpDialog(nil)}) |
| 46 | + applyOpenDialogMsgs(t, m, cmd) |
| 47 | + require.True(t, m.dialogMgr.Open(), "help dialog should be open") |
| 48 | + require.False(t, m.dialogMgr.TopIsExitConfirmation(), |
| 49 | + "help dialog should be on top, not exit confirmation") |
| 50 | + |
| 51 | + // Press ctrl+c — must stack the exit confirmation, not exit immediately. |
| 52 | + _, cmd = m.Update(tea.KeyPressMsg{Code: 'c', Mod: tea.ModCtrl}) |
| 53 | + applyOpenDialogMsgs(t, m, cmd) |
| 54 | + |
| 55 | + msgs := collectMsgs(cmd) |
| 56 | + assert.False(t, hasMsg[tea.QuitMsg](msgs), |
| 57 | + "first ctrl+c on a non-exit dialog must NOT quit the program") |
| 58 | + assert.True(t, m.dialogMgr.TopIsExitConfirmation(), |
| 59 | + "ctrl+c on a non-exit dialog should put the exit confirmation on top") |
| 60 | +} |
| 61 | + |
| 62 | +func TestCtrlC_OnExitConfirmation_ForwardsAndExits(t *testing.T) { |
| 63 | + neutralizeExitFunc(t) |
| 64 | + |
| 65 | + m, _ := newTestModel() |
| 66 | + |
| 67 | + // Put the exit confirmation dialog on top first (via a regular ctrl+c). |
| 68 | + _, cmd := m.Update(tea.KeyPressMsg{Code: 'c', Mod: tea.ModCtrl}) |
| 69 | + applyOpenDialogMsgs(t, m, cmd) |
| 70 | + require.True(t, m.dialogMgr.TopIsExitConfirmation(), |
| 71 | + "exit confirmation should be the topmost dialog") |
| 72 | + |
| 73 | + // A second ctrl+c is forwarded to the exit confirmation, which signals |
| 74 | + // ExitConfirmedMsg. Feed every produced message back through the model |
| 75 | + // so we end up at tea.Quit. |
| 76 | + _, cmd = m.Update(tea.KeyPressMsg{Code: 'c', Mod: tea.ModCtrl}) |
| 77 | + require.NotNil(t, cmd, "second ctrl+c should produce a command") |
| 78 | + |
| 79 | + sawQuit := false |
| 80 | + for _, msg := range collectMsgs(cmd) { |
| 81 | + if _, ok := msg.(dialog.ExitConfirmedMsg); ok { |
| 82 | + _, exitCmd := m.Update(msg) |
| 83 | + if hasMsg[tea.QuitMsg](collectMsgs(exitCmd)) { |
| 84 | + sawQuit = true |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + assert.True(t, sawQuit, |
| 89 | + "two ctrl+c presses must result in tea.Quit (exit the program)") |
| 90 | +} |
0 commit comments