summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/agentutil_test.go
blob: 69111b51dacea9228aa1490db029aad4d402fd3a (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package tmuxedit

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

func TestScopeToLastSection(t *testing.T) {
	tests := []struct {
		name    string
		content string
		pattern string
		want    string
	}{
		{
			name:    "no pattern returns full content",
			content: "line1\nline2\nline3",
			pattern: "",
			want:    "line1\nline2\nline3",
		},
		{
			name:    "invalid regex returns full content",
			content: "line1\nline2",
			pattern: "[invalid",
			want:    "line1\nline2",
		},
		{
			name:    "fewer than two delimiters returns full content",
			content: "─────\nhello",
			pattern: `^─{5,}`,
			want:    "─────\nhello",
		},
		{
			name:    "extracts last section between two delimiters",
			content: "─────\nold message\n─────\n❯ prompt text\n─────",
			pattern: `^─{5,}`,
			want:    "❯ prompt text",
		},
		{
			name: "skips earlier sections",
			content: "─────\n❯ old msg1\n─────\n" +
				"─────\n❯ old msg2\n─────\n" +
				"─────\n❯ current prompt\n─────",
			pattern: `^─{5,}`,
			want:    "❯ current prompt",
		},
		{
			name: "claude multi-line prompt between rules",
			content: "previous output\n" +
				"─────────────\n" +
				"❯ first line\n" +
				"\n" +
				"❯ second line\n" +
				"\n" +
				"❯ third line\n" +
				"─────────────\n" +
				"  -- INSERT --",
			pattern: `^─{5,}`,
			want:    "❯ first line\n\n❯ second line\n\n❯ third line",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := scopeToLastSection(tt.content, tt.pattern)
			if got != tt.want {
				t.Errorf("scopeToLastSection() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestStripNoise(t *testing.T) {
	tests := []struct {
		name     string
		text     string
		patterns []string
		want     string
	}{
		{"no patterns", "hello world", nil, "hello world"},
		{"strip INSERT", "fix the bug INSERT", []string{"INSERT"}, "fix the bug"},
		{"strip multiple", "INSERT fix the bug Add a follow-up", []string{"INSERT", "Add a follow-up"}, "fix the bug"},
		{"strip to empty", "INSERT", []string{"INSERT"}, ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := stripNoise(tt.text, tt.patterns)
			if got != tt.want {
				t.Errorf("stripNoise() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestMatchPromptLines(t *testing.T) {
	tests := []struct {
		name    string
		pattern string
		content string
		want    int
	}{
		{"no matches", `❯\s*(.+)$`, "no prompt here", 0},
		{"single match", `❯\s*(.+)$`, "❯ hello", 1},
		{"multiple matches", `❯\s*(.+)$`, "❯ first\nother\n❯ second", 2},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			re := mustCompile(t, tt.pattern)
			got := matchPromptLines(re, tt.content)
			if len(got) != tt.want {
				t.Errorf("matchPromptLines() returned %d matches, want %d", len(got), tt.want)
			}
		})
	}
}

func TestJoinAllMatches(t *testing.T) {
	matches := []promptMatch{
		{lineNum: 0, text: "first"},
		{lineNum: 2, text: "INSERT"},
		{lineNum: 4, text: "third"},
	}
	got := joinAllMatches(matches, []string{"INSERT"})
	if got != "first\nthird" {
		t.Errorf("joinAllMatches() = %q, want %q", got, "first\nthird")
	}
}

func TestJoinLastContiguousBlock(t *testing.T) {
	tests := []struct {
		name    string
		matches []promptMatch
		strips  []string
		want    string
	}{
		{
			name: "single block",
			matches: []promptMatch{
				{lineNum: 5, text: "first"},
				{lineNum: 6, text: "second"},
			},
			want: "first\nsecond",
		},
		{
			name: "two blocks takes last",
			matches: []promptMatch{
				{lineNum: 1, text: "old"},
				{lineNum: 2, text: "old2"},
				{lineNum: 10, text: "new"},
				{lineNum: 11, text: "new2"},
			},
			want: "new\nnew2",
		},
		{
			name: "strips noise",
			matches: []promptMatch{
				{lineNum: 0, text: "fix INSERT"},
			},
			strips: []string{"INSERT"},
			want:   "fix",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := joinLastContiguousBlock(tt.matches, tt.strips)
			if got != tt.want {
				t.Errorf("joinLastContiguousBlock() = %q, want %q", got, tt.want)
			}
		})
	}
}

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 TestSendClearSequence_EscapeKey(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
	}

	// sendClearSequence with "Escape" should succeed and send the key.
	// The 150ms Escape delay is real but acceptable in tests.
	err := sendClearSequence("%1", "Escape C-k")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want := []string{"Escape", "C-k"}
	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 TestSendClearSequence_SingleKeyError(t *testing.T) {
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(string, ...string) error {
		return fmt.Errorf("send failed")
	}

	err := sendClearSequence("%1", "C-u")
	if err == nil {
		t.Fatal("expected error from sendKeys failure")
	}
	if !strings.Contains(err.Error(), "clear key") {
		t.Errorf("error should mention 'clear key', got: %v", err)
	}
}

func TestSendClearSequence_RepeatedKeyError(t *testing.T) {
	oldRepeat := sendRepeatedKey
	defer func() { sendRepeatedKey = oldRepeat }()
	sendRepeatedKey = func(string, string, int) error {
		return fmt.Errorf("repeat failed")
	}

	err := sendClearSequence("%1", "BSpace*200")
	if err == nil {
		t.Fatal("expected error from sendRepeatedKey failure")
	}
	if !strings.Contains(err.Error(), "clear key") {
		t.Errorf("error should mention 'clear key', got: %v", err)
	}
}

// mustCompile is a test helper that compiles a regex or fails the test.
func mustCompile(t *testing.T, pattern string) *regexp.Regexp {
	t.Helper()
	re, err := regexp.Compile(pattern)
	if err != nil {
		t.Fatalf("regexp.Compile(%q) failed: %v", pattern, err)
	}
	return re
}