paste_collapse_test.go RawHistoryFolder
1package tui
2
3import (
4 "strings"
5 "testing"
6)
7
8// TestPasteCollapseInsertsPlaceholder verifies that a multi-line
9// paste gets collapsed to the "[paste #N +L lines]" placeholder
10// in the editor buffer, leaving the full body behind the scenes.
11func TestPasteCollapseInsertsPlaceholder(t *testing.T) {
12 e := NewEditor("▌ ")
13 body := "line1\nline2\nline3\nline4"
14 e.HandleKey(Key{Kind: KeyPaste, Paste: body})
15
16 got := e.Value()
17 want := "[pasted text #1 +4 lines]"
18 if got != want {
19 t.Fatalf("editor Value: want %q, got %q", want, got)
20 }
21 if e.SubmitValue() != body {
22 t.Fatalf("SubmitValue didn't expand placeholder: got %q", e.SubmitValue())
23 }
24}
25
26// TestPasteCollapseSingleLineFallthrough ensures short pastes are
27// NOT collapsed — single-line drag-drop file paths and short
28// two-line snippets should appear inline so the user can edit
29// them in place.
30func TestPasteCollapseSingleLineFallthrough(t *testing.T) {
31 e := NewEditor("▌ ")
32 e.HandleKey(Key{Kind: KeyPaste, Paste: "hello world"})
33 if strings.Contains(e.Value(), "[pasted text #") {
34 t.Errorf("single-line paste should not collapse, got %q", e.Value())
35 }
36
37 e2 := NewEditor("▌ ")
38 e2.HandleKey(Key{Kind: KeyPaste, Paste: "line1\nline2"})
39 if strings.Contains(e2.Value(), "[pasted text #") {
40 t.Errorf("two-line paste should not collapse, got %q", e2.Value())
41 }
42}
43
44// TestPasteCollapseSequentialIDs makes sure two separate pastes get
45// distinct ids and both expand correctly in SubmitValue.
46func TestPasteCollapseSequentialIDs(t *testing.T) {
47 e := NewEditor("▌ ")
48 a := "aaa\nbbb\nccc"
49 b := "xxx\nyyy\nzzz"
50 e.HandleKey(Key{Kind: KeyPaste, Paste: a})
51 e.Insert(" ")
52 e.HandleKey(Key{Kind: KeyPaste, Paste: b})
53
54 visible := e.Value()
55 if !strings.Contains(visible, "[pasted text #1 +3 lines]") ||
56 !strings.Contains(visible, "[pasted text #2 +3 lines]") {
57 t.Fatalf("expected two placeholders in %q", visible)
58 }
59
60 full := e.SubmitValue()
61 if !strings.Contains(full, a) || !strings.Contains(full, b) {
62 t.Fatalf("SubmitValue missing bodies: %q", full)
63 }
64}
65
66// TestPasteCollapseClearResetsMap verifies that Clear drops the
67// stored pastes so stale ids can't leak into a follow-up turn
68// (same placeholder number could otherwise be reused to expand to
69// the wrong body).
70func TestPasteCollapseClearResetsMap(t *testing.T) {
71 e := NewEditor("▌ ")
72 e.HandleKey(Key{Kind: KeyPaste, Paste: "a\nb\nc"})
73 e.Clear()
74
75 // A fresh paste must start at id #1 again.
76 e.HandleKey(Key{Kind: KeyPaste, Paste: "d\ne\nf"})
77 if !strings.Contains(e.Value(), "[pasted text #1 ") {
78 t.Errorf("Clear didn't reset pasteSeq: %q", e.Value())
79 }
80}