summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/send_test.go
blob: e45828259e9755179a89edd3b54deb9531c8866b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package tmuxedit

import (
	"fmt"
	"strings"
	"testing"
)

func TestDeduplicateText(t *testing.T) {
	tests := []struct {
		name     string
		original string
		edited   string
		want     string
	}{
		{"empty both", "", "", ""},
		{"empty original", "", "new text", "new text"},
		{"empty edited", "original", "", ""},
		{"unchanged", "hello world", "hello world", ""},
		{"appended", "hello", "hello world", "hello world"},
		{"rewritten", "hello world", "goodbye world", "goodbye world"},
		{"whitespace handling", "  hello  ", "  hello  world  ", "hello  world"},
		{"appended with newlines", "line1\nline2", "line1\nline2\nline3", "line1\nline2\nline3"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := deduplicateText(tt.original, tt.edited)
			if got != tt.want {
				t.Errorf("deduplicateText(%q, %q) = %q, want %q",
					tt.original, tt.edited, got, tt.want)
			}
		})
	}
}

// noSleep disables the post-clear sleep in tests and restores it on cleanup.
func noSleep(t *testing.T) {
	t.Helper()
	old := sleepAfterClear
	sleepAfterClear = func() {}
	t.Cleanup(func() { sleepAfterClear = old })
}

func TestSendTextToPane_SingleLine(t *testing.T) {
	noSleep(t)
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, fmt.Sprintf("send:%s:%s", paneID, strings.Join(keys, ",")))
		return nil
	}
	agent := AgentConfig{ClearFirst: true, ClearKeys: "C-u", NewlineKeys: "S-Enter"}
	err := sendTextToPane("%5", "hello", agent)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// Expect: clear, then single line (no newline after last line)
	if len(calls) != 2 {
		t.Fatalf("got %d calls, want 2: %v", len(calls), calls)
	}
	if calls[0] != "send:%5:C-u" {
		t.Errorf("call[0] = %q, want clear", calls[0])
	}
	if calls[1] != "send:%5:hello" {
		t.Errorf("call[1] = %q, want text", calls[1])
	}
}

func TestSendTextToPane_MultiLine(t *testing.T) {
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, strings.Join(keys, ","))
		return nil
	}
	agent := AgentConfig{NewlineKeys: "S-Enter"}
	err := sendTextToPane("%1", "line1\nline2\nline3", agent)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// Expect: line1, S-Enter, line2, S-Enter, line3 (no trailing newline)
	want := []string{"line1", "S-Enter", "line2", "S-Enter", "line3"}
	if len(calls) != len(want) {
		t.Fatalf("got %d calls, want %d: %v", len(calls), len(want), calls)
	}
	for i, w := range want {
		if calls[i] != w {
			t.Errorf("call[%d] = %q, want %q", i, calls[i], w)
		}
	}
}

func TestSendTextToPane_NoClear(t *testing.T) {
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, strings.Join(keys, ","))
		return nil
	}
	agent := AgentConfig{ClearFirst: false, ClearKeys: "C-u"}
	err := sendTextToPane("%1", "hello", agent)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// No clear call; just the text
	if len(calls) != 1 {
		t.Fatalf("got %d calls, want 1: %v", len(calls), calls)
	}
}

func TestSendTextToPane_Empty(t *testing.T) {
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(string, ...string) error {
		t.Fatal("sendKeys should not be called for empty text")
		return nil
	}
	err := sendTextToPane("%1", "", AgentConfig{})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestSendTextToPane_ClearError(t *testing.T) {
	noSleep(t)
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		return fmt.Errorf("tmux error")
	}
	agent := AgentConfig{ClearFirst: true, ClearKeys: "C-u"}
	err := sendTextToPane("%1", "hello", agent)
	if err == nil {
		t.Fatal("expected error on clear failure")
	}
}

func TestSendTextToPane_SendError(t *testing.T) {
	noSleep(t)
	callCount := 0
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		callCount++
		if callCount == 2 { // fail on second call (first line text)
			return fmt.Errorf("send failed")
		}
		return nil
	}
	agent := AgentConfig{ClearFirst: true, ClearKeys: "C-u"}
	err := sendTextToPane("%1", "hello", agent)
	if err == nil {
		t.Fatal("expected error on send failure")
	}
}

func TestSendTextToPane_BulkClear(t *testing.T) {
	noSleep(t)
	var calls []string
	oldSend := sendKeys
	oldRepeat := sendRepeatedKey
	defer func() {
		sendKeys = oldSend
		sendRepeatedKey = oldRepeat
	}()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, fmt.Sprintf("send:%s:%s", paneID, strings.Join(keys, ",")))
		return nil
	}
	sendRepeatedKey = func(paneID, key string, count int) error {
		calls = append(calls, fmt.Sprintf("repeat:%s:%s*%d", paneID, key, count))
		return nil
	}
	// "End BSpace*200" should send End normally, then BSpace 200 times via -N
	agent := AgentConfig{ClearFirst: true, ClearKeys: "End BSpace*200", NewlineKeys: "S-Enter"}
	err := sendTextToPane("%5", "new text", agent)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want := []string{
		"send:%5:End",
		"repeat:%5:BSpace*200",
		"send:%5:new text",
	}
	if len(calls) != len(want) {
		t.Fatalf("got %d calls, want %d: %v", len(calls), len(want), calls)
	}
	for i, w := range want {
		if calls[i] != w {
			t.Errorf("call[%d] = %q, want %q", i, calls[i], w)
		}
	}
}

func TestParseKeyRepeat(t *testing.T) {
	tests := []struct {
		token     string
		wantKey   string
		wantCount int
	}{
		{"BSpace*200", "BSpace", 200},
		{"End", "End", 1},
		{"C-u", "C-u", 1},
		{"BSpace*1", "BSpace", 1},
		{"BSpace*0", "BSpace*0", 1},     // invalid count
		{"BSpace*abc", "BSpace*abc", 1}, // non-numeric
		{"*200", "*200", 1},             // no key name
		{"x*3", "x", 3},
	}
	for _, tt := range tests {
		t.Run(tt.token, func(t *testing.T) {
			key, count := parseKeyRepeat(tt.token)
			if key != tt.wantKey || count != tt.wantCount {
				t.Errorf("parseKeyRepeat(%q) = (%q, %d), want (%q, %d)",
					tt.token, key, count, tt.wantKey, tt.wantCount)
			}
		})
	}
}

func TestSendTextToPane_FallbackNewline(t *testing.T) {
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, strings.Join(keys, ","))
		return nil
	}
	// Agent with empty NewlineKeys should fallback to "Enter"
	agent := AgentConfig{NewlineKeys: ""}
	err := sendTextToPane("%1", "a\nb", agent)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// Expect: a, Enter, b
	if len(calls) != 3 {
		t.Fatalf("got %d calls, want 3: %v", len(calls), calls)
	}
	if calls[1] != "Enter" {
		t.Errorf("newline key = %q, want Enter (fallback)", calls[1])
	}
}