summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/agentutil_test.go
blob: 8bf2e64f6a757dce34472f8c3683145d6cf7afed (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
package tmuxedit

import (
	"regexp"
	"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)
			}
		})
	}
}

// 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
}