|
| 1 | +package submitview |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + tea "github.com/charmbracelet/bubbletea" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// runeMsg builds the multi-rune key message Bubble Tea emits for a leaked |
| 13 | +// fragment of an escape sequence. |
| 14 | +func runeMsg(s string, alt bool) tea.KeyMsg { |
| 15 | + return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(s), Alt: alt} |
| 16 | +} |
| 17 | + |
| 18 | +// splitSGRBurst reproduces the exact key messages Bubble Tea's input parser |
| 19 | +// emits when it splits the SGR mouse sequence seq ("\x1b[<Cb;Cx;Cy(M|m)") at |
| 20 | +// byte offset cut (2..len-1): an Alt+"[" for the consumed "\x1b[", an optional |
| 21 | +// middle run for the bytes before the cut, then the tail run after it. |
| 22 | +func splitSGRBurst(seq string, cut int) []tea.KeyMsg { |
| 23 | + burst := []tea.KeyMsg{runeMsg("[", true)} |
| 24 | + if cut > 2 { |
| 25 | + burst = append(burst, runeMsg(seq[2:cut], false)) |
| 26 | + } |
| 27 | + burst = append(burst, runeMsg(seq[cut:], false)) |
| 28 | + return burst |
| 29 | +} |
| 30 | + |
| 31 | +func TestConsumeLeakedMouseKey_SwallowsSplitSequences(t *testing.T) { |
| 32 | + for _, seq := range []string{"\x1b[<65;54;51M", "\x1b[<64;10;20m", "\x1b[<0;1;1M"} { |
| 33 | + for cut := 2; cut < len(seq); cut++ { |
| 34 | + t.Run(fmt.Sprintf("%q@%d", seq, cut), func(t *testing.T) { |
| 35 | + m := testModel(t, newNodes()) |
| 36 | + require.Equal(t, fieldTitle, m.focusedField) |
| 37 | + before := m.titleInput.Value() |
| 38 | + |
| 39 | + for _, msg := range splitSGRBurst(seq, cut) { |
| 40 | + updated, _ := m.Update(msg) |
| 41 | + m = updated.(Model) |
| 42 | + } |
| 43 | + |
| 44 | + assert.Equal(t, before, m.titleInput.Value(), "no fragment should reach the title field") |
| 45 | + assert.False(t, m.mouseLeakActive, "the sequence resets once its terminator is consumed") |
| 46 | + |
| 47 | + // A normal keystroke afterwards must still register. |
| 48 | + m = sendKey(t, m, runeKey('x')) |
| 49 | + assert.Equal(t, before+"x", m.titleInput.Value(), "typing works after a swallowed sequence") |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestConsumeLeakedMouseKey_SwallowsSingleRunTail(t *testing.T) { |
| 56 | + // When the parser consumes "\x1b" as a lone Escape, the rest of the body can |
| 57 | + // arrive as one run. |
| 58 | + m := testModel(t, newNodes()) |
| 59 | + before := m.titleInput.Value() |
| 60 | + m = sendKey(t, m, runeMsg("[<65;54;51M", false)) |
| 61 | + assert.Equal(t, before, m.titleInput.Value(), "a whole leaked body in one run is dropped") |
| 62 | +} |
| 63 | + |
| 64 | +func TestConsumeLeakedMouseKey_PreservesRealTyping(t *testing.T) { |
| 65 | + m := testModel(t, newNodes()) |
| 66 | + before := m.titleInput.Value() |
| 67 | + |
| 68 | + // Characters from the mouse alphabet typed normally must pass through. Each |
| 69 | + // real keystroke is a single-rune message, never a complete SGR body. |
| 70 | + for _, r := range []rune{'<', '3', ';', '5', 'M', 'm'} { |
| 71 | + m = sendKey(t, m, runeKey(r)) |
| 72 | + } |
| 73 | + assert.Equal(t, before+"<3;5Mm", m.titleInput.Value(), "ordinary characters are never filtered") |
| 74 | +} |
| 75 | + |
| 76 | +func TestConsumeLeakedMouseKey_StrayAltBracketDoesNotEatNextKey(t *testing.T) { |
| 77 | + m := testModel(t, newNodes()) |
| 78 | + before := m.titleInput.Value() |
| 79 | + |
| 80 | + // A lone Alt+"[" opens a sequence, but the following key is not a mouse body, |
| 81 | + // so it must still be handled (here: typed into the field). |
| 82 | + m = sendKey(t, m, runeMsg("[", true)) |
| 83 | + require.True(t, m.mouseLeakActive) |
| 84 | + m = sendKey(t, m, runeKey('z')) |
| 85 | + assert.False(t, m.mouseLeakActive, "a non-body rune ends the sequence") |
| 86 | + assert.Equal(t, before+"z", m.titleInput.Value(), "the following real key is not eaten") |
| 87 | +} |
| 88 | + |
| 89 | +func TestConsumeLeakedMouseKey_BracketedPasteIsNotFiltered(t *testing.T) { |
| 90 | + m := testModel(t, newNodes()) |
| 91 | + before := m.titleInput.Value() |
| 92 | + |
| 93 | + // A genuine paste that happens to look like a mouse body is preserved. |
| 94 | + paste := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("<65;54;51M"), Paste: true} |
| 95 | + updated, _ := m.Update(paste) |
| 96 | + m = updated.(Model) |
| 97 | + assert.Equal(t, before+"<65;54;51M", m.titleInput.Value(), "pasted content is never filtered") |
| 98 | +} |
| 99 | + |
| 100 | +func TestConsumeLeakedMouseKey_DescriptionField(t *testing.T) { |
| 101 | + m := testModel(t, newNodes()) |
| 102 | + m = sendKey(t, m, tea.KeyMsg{Type: tea.KeyTab}) // focus description |
| 103 | + require.Equal(t, fieldDescription, m.focusedField) |
| 104 | + before := m.descArea.Value() |
| 105 | + |
| 106 | + for _, msg := range splitSGRBurst("\x1b[<65;54;51M", 10) { |
| 107 | + updated, _ := m.Update(msg) |
| 108 | + m = updated.(Model) |
| 109 | + } |
| 110 | + assert.Equal(t, before, m.descArea.Value(), "no fragment should reach the description field") |
| 111 | +} |
0 commit comments