| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | tea "charm.land/bubbletea/v2" |
| 7 | ) |
| 8 | |
| 9 | func TestSubmittedInputRecallKeepsDraftAcrossCursorKeys(t *testing.T) { |
| 10 | m := newTestChatTUI() |
| 11 | m.rememberSubmittedInput("old") |
| 12 | m.input.SetValue("my draft") |
| 13 | |
| 14 | for _, key := range []tea.KeyPressMsg{ |
| 15 | {Code: tea.KeyUp}, |
| 16 | {Code: tea.KeyRight}, |
| 17 | {Code: tea.KeyHome}, |
| 18 | } { |
| 19 | model, _ := m.Update(key) |
| 20 | m = model.(chatTUI) |
| 21 | } |
| 22 | if got := m.input.Value(); got != "old" { |
| 23 | t.Fatalf("up should recall %q, got %q", "old", got) |
| 24 | } |
| 25 | |
| 26 | model, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyDown}) |
| 27 | m = model.(chatTUI) |
| 28 | if got := m.input.Value(); got != "my draft" { |
| 29 | t.Fatalf("down after cursor keys should restore the draft, got %q", got) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestSubmittedInputRecallEditedEntryBecomesDraft(t *testing.T) { |
| 34 | m := newTestChatTUI() |
| 35 | m.rememberSubmittedInput("old") |
| 36 | m.input.SetValue("my draft") |
| 37 | |
| 38 | model, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyUp}) |
| 39 | m = model.(chatTUI) |
| 40 | model, _ = m.Update(tea.KeyPressMsg{Code: 'x', Text: "x"}) |
| 41 | m = model.(chatTUI) |
| 42 | edited := m.input.Value() |
| 43 | if edited == "old" { |
| 44 | t.Fatalf("typing should edit the recalled entry") |
| 45 | } |
| 46 | |
| 47 | model, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyDown}) |
| 48 | m = model.(chatTUI) |
| 49 | if got := m.input.Value(); got != edited { |
| 50 | t.Fatalf("down on an edited entry should leave it alone, got %q want %q", got, edited) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestSubmittedInputRecallMovesWithinMultiLineEntry(t *testing.T) { |
| 55 | m := newTestChatTUI() |
| 56 | m.rememberSubmittedInput("line1\nline2\nline3") |
| 57 | m.input.SetValue("draft") |
| 58 | |
| 59 | for _, key := range []tea.KeyPressMsg{ |
| 60 | {Code: tea.KeyUp}, |
| 61 | {Code: tea.KeyLeft}, |
| 62 | {Code: tea.KeyUp}, |
| 63 | {Code: tea.KeyDown}, |
| 64 | } { |
| 65 | model, _ := m.Update(key) |
| 66 | m = model.(chatTUI) |
| 67 | } |
| 68 | if got := m.input.Value(); got != "line1\nline2\nline3" { |
| 69 | t.Fatalf("up/down inside a recalled entry should move the cursor, got %q", got) |
| 70 | } |
| 71 | if got := m.input.Line(); got != 2 { |
| 72 | t.Fatalf("down from line 2 should reach the last line, got line %d", got) |
| 73 | } |
| 74 | |
| 75 | model, _ := m.Update(tea.KeyPressMsg{Code: tea.KeyDown}) |
| 76 | m = model.(chatTUI) |
| 77 | if got := m.input.Value(); got != "draft" { |
| 78 | t.Fatalf("down on the last line should restore the draft, got %q", got) |
| 79 | } |
| 80 | } |
| 81 |