1package tui
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7)
8
9// makeLines returns a body with n lines of short text — useful for
10// exercising the line-count trigger without also tripping the
11// character-count one.
12func makeLines(n int) string {
13 parts := make([]string, n)
14 for i := range parts {
15 parts[i] = fmt.Sprintf("line %d", i+1)
16 }
17 return strings.Join(parts, "\n")
18}
19
20// TestPasteCollapseLineTrigger verifies that a paste with more than
21// ten lines gets collapsed to the "+L lines" placeholder shape.
22// The full body is preserved and expanded back by SubmitValue.
23func TestPasteCollapseLineTrigger(t *testing.T) {
24 e := NewEditor("▌ ")
25 body := makeLines(11)
26 e.HandleKey(Key{Kind: KeyPaste, Paste: body})
27
28 got := e.Value()
29 want := "[pasted text #1 +11 lines]"
30 if got != want {
31 t.Fatalf("editor Value: want %q, got %q", want, got)
32 }
33 if e.SubmitValue() != body {
34 t.Fatalf("SubmitValue didn't expand placeholder: got %q", e.SubmitValue())
35 }
36}
37
38// TestPasteCollapseCharTrigger verifies that a paste with more than
39// 1000 characters but few enough lines collapses to the "C chars"
40// placeholder shape (long single-line / near-single-line dumps).
41func TestPasteCollapseCharTrigger(t *testing.T) {
42 e := NewEditor("▌ ")
43 body := strings.Repeat("a", 1500) // 1 line, 1500 chars
44 e.HandleKey(Key{Kind: KeyPaste, Paste: body})
45
46 got := e.Value()
47 want := "[pasted text #1 1500 chars]"
48 if got != want {
49 t.Fatalf("editor Value: want %q, got %q", want, got)
50 }
51 if e.SubmitValue() != body {
52 t.Fatalf("SubmitValue didn't expand placeholder: got %q", e.SubmitValue())
53 }
54}
55
56// TestPasteCollapseLinePrecedence verifies that when a paste trips
57// both thresholds, the line-count marker wins. "12 lines, 4000
58// chars" should read as "+12 lines", not "4000 chars".
59func TestPasteCollapseLinePrecedence(t *testing.T) {
60 e := NewEditor("▌ ")
61 // 12 lines, each 400 chars = 12 lines, ~4800 chars.
62 parts := make([]string, 12)
63 for i := range parts {
64 parts[i] = strings.Repeat("x", 400)
65 }
66 body := strings.Join(parts, "\n")
67 e.HandleKey(Key{Kind: KeyPaste, Paste: body})
68
69 if !strings.HasPrefix(e.Value(), "[pasted text #1 +12 lines") {
70 t.Errorf("want line-trigger placeholder, got %q", e.Value())
71 }
72}
73
74// TestPasteCollapseFallthrough ensures short pastes (under both
75// thresholds) are NOT collapsed — 1-10 lines with < 1000 chars
76// should appear inline so the user can edit them in place.
77func TestPasteCollapseFallthrough(t *testing.T) {
78 cases := map[string]string{
79 "single line": "hello world",
80 "two lines": "line1\nline2",
81 "ten lines (at limit)": makeLines(10),
82 "exactly 1000 chars": strings.Repeat("a", 1000),
83 "multiline under caps": "aaa\nbbb\nccc\nddd\neee",
84 }
85 for name, body := range cases {
86 t.Run(name, func(t *testing.T) {
87 e := NewEditor("▌ ")
88 e.HandleKey(Key{Kind: KeyPaste, Paste: body})
89 if strings.Contains(e.Value(), "[pasted text #") {
90 t.Errorf("%s should NOT collapse, got %q", name, e.Value())
91 }
92 })
93 }
94}
95
96// TestPasteCollapseSequentialIDs makes sure two separate large
97// pastes get distinct ids and both expand correctly in
98// SubmitValue, even when they use different placeholder shapes.
99func TestPasteCollapseSequentialIDs(t *testing.T) {
100 e := NewEditor("▌ ")
101 a := makeLines(12) // line trigger
102 b := strings.Repeat("y", 1500) // char trigger, single line
103 e.HandleKey(Key{Kind: KeyPaste, Paste: a})
104 e.Insert(" ")
105 e.HandleKey(Key{Kind: KeyPaste, Paste: b})
106
107 visible := e.Value()
108 if !strings.Contains(visible, "[pasted text #1 +12 lines]") ||
109 !strings.Contains(visible, "[pasted text #2 1500 chars]") {
110 t.Fatalf("expected mixed-shape placeholders in %q", visible)
111 }
112
113 full := e.SubmitValue()
114 if !strings.Contains(full, a) || !strings.Contains(full, b) {
115 t.Fatalf("SubmitValue missing bodies: %q", full)
116 }
117}
118
119// TestPasteCollapseClearResetsMap verifies that Clear drops the
120// stored pastes so stale ids can't leak into a follow-up turn
121// (same placeholder number could otherwise be reused to expand to
122// the wrong body).
123func TestPasteCollapseClearResetsMap(t *testing.T) {
124 e := NewEditor("▌ ")
125 e.HandleKey(Key{Kind: KeyPaste, Paste: makeLines(11)})
126 e.Clear()
127
128 // A fresh paste must start at id #1 again.
129 e.HandleKey(Key{Kind: KeyPaste, Paste: makeLines(11)})
130 if !strings.Contains(e.Value(), "[pasted text #1 ") {
131 t.Errorf("Clear didn't reset pasteSeq: %q", e.Value())
132 }
133}