summaryrefslogtreecommitdiff
path: root/internal/repl/completer_adapter_test.go
blob: 72d3c9dd95d4b4b5be2801156af91f4331764a73 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package repl

import (
	"strings"
	"testing"
)

func TestNewAutoCompleter(t *testing.T) {
	adapter := NewAutoCompleter()
	if adapter == nil {
		t.Fatal("NewAutoCompleter returned nil")
	}
	if adapter.commands == nil {
		t.Fatal("AutoCompleteAdapter.commands is nil")
	}
	expectedCommands := Commands()
	if len(adapter.commands) != len(expectedCommands) {
		t.Errorf("commands count = %d, want %d", len(adapter.commands), len(expectedCommands))
	}
}

func TestAutoCompleteAdapterDo(t *testing.T) {
	adapter := NewAutoCompleter()
	commands := Commands()

	tests := []struct {
		name        string
		line        []rune
		pos         int
		wantLen     int
		wantMinLen  int
		description string
	}{
		// Empty / whitespace input returns all commands
		{
			name:       "empty input returns all commands",
			line:       []rune(""),
			pos:        0,
			wantLen:    len(commands),
			wantMinLen: 0,
		},
		{
			name:       "whitespace only returns all commands",
			line:       []rune("   "),
			pos:        3,
			wantLen:    len(commands),
			wantMinLen: 0,
		},
		// Exact matches — no completion offered (readline would append the word)
		{
			name:       "exact match help",
			line:       []rune("help"),
			pos:        4,
			wantLen:    0,
			wantMinLen: 0,
		},
		{
			name:       "exact match clear",
			line:       []rune("clear"),
			pos:        5,
			wantLen:    0,
			wantMinLen: 0,
		},
		// Partial matches (single match, commonLen = shared prefix length)
		{
			name:       "partial match he",
			line:       []rune("he"),
			pos:        2,
			wantLen:    1,
			wantMinLen: 2, // "he" shared, suffix "lp"
		},
		{
			name:       "partial match cl",
			line:       []rune("cl"),
			pos:        2,
			wantLen:    1,
			wantMinLen: 2, // "cl" shared, suffix "ear"
		},
		{
			name:       "partial match q",
			line:       []rune("q"),
			pos:        1,
			wantLen:    1,
			wantMinLen: 1, // "q" shared, suffix "uit"
		},
		{
			name:       "partial match rp",
			line:       []rune("rp"),
			pos:        2,
			wantLen:    1,
			wantMinLen: 2, // "rp" shared, suffix "n"
		},
		// Multiple matches
		{
			name:       "partial match c matches calc and clear",
			line:       []rune("c"),
			pos:        1,
			wantLen:    2,
			wantMinLen: 1, // "c" shared, suffixes "alc" and "lear"
		},
		// No matches
		{
			name:       "no match xyz",
			line:       []rune("xyz"),
			pos:        3,
			wantLen:    0,
			wantMinLen: 0,
		},
		{
			name:       "no match numbers",
			line:       []rune("123"),
			pos:        3,
			wantLen:    0,
			wantMinLen: 0,
		},
		{
			name:       "no match symbols",
			line:       []rune("!@#"),
			pos:        3,
			wantLen:    0,
			wantMinLen: 0,
		},
		{
			name:       "no match too long prefix",
			line:       []rune("heloooooo"),
			pos:        9,
			wantLen:    0,
			wantMinLen: 0,
		},
		// Case insensitive — case mismatch means no shared prefix
		{
			name:       "uppercase HELP",
			line:       []rune("HELP"),
			pos:        4,
			wantLen:    1,
			wantMinLen: 0, // H vs h: no shared prefix
		},
		{
			name:       "uppercase CLEAR",
			line:       []rune("CLEAR"),
			pos:        5,
			wantLen:    1,
			wantMinLen: 0, // C vs c: no shared prefix
		},
		{
			name:       "mixed case HeLp",
			line:       []rune("HeLp"),
			pos:        4,
			wantLen:    1,
			wantMinLen: 0, // H vs h: no shared prefix
		},
		{
			name:       "uppercase Q matches quit",
			line:       []rune("Q"),
			pos:        1,
			wantLen:    1,
			wantMinLen: 0, // Q vs q: no shared prefix
		},
		{
			name:       "uppercase C matches calc and clear",
			line:       []rune("C"),
			pos:        1,
			wantLen:    2,
			wantMinLen: 0, // C vs c: no shared prefix
		},
		// Multi-word input completes last word (exact match returns nothing)
		{
			name:       "multi word input completes last word",
			line:       []rune("rpn help"),
			pos:        8,
			wantLen:    0,
			wantMinLen: 0,
		},
		// Cursor position matters
		{
			name:       "cursor in middle of word",
			line:       []rune("help"),
			pos:        2,
			wantLen:    1,
			wantMinLen: 2, // "he" shared, suffix "lp"
		},
		{
			name:       "single char h",
			line:       []rune("h"),
			pos:        1,
			wantLen:    1,
			wantMinLen: 1, // "h" shared, suffix "elp"
		},
		{
			name:       "single char e matches exit",
			line:       []rune("e"),
			pos:        1,
			wantLen:    1,
			wantMinLen: 1, // "e" shared, suffix "xit"
		},
		{
			name:       "single char r matches rpn and rat",
			line:       []rune("r"),
			pos:        1,
			wantLen:    2,
			wantMinLen: 1, // "r" shared, suffixes "pn" and "at"
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			matches, commonLen := adapter.Do(tt.line, tt.pos)

			if len(matches) != tt.wantLen {
				t.Errorf("got %d matches, want %d. matches: %v",
					len(matches), tt.wantLen, runeSliceToStringSlice(matches))
			}

			if commonLen != tt.wantMinLen {
				t.Errorf("common prefix len = %d, want %d", commonLen, tt.wantMinLen)
			}

			// Verify all matches are actual commands (prefix + suffix)
			prefixWords := strings.Fields(string(tt.line[:tt.pos]))
			var prefix string
			if len(prefixWords) > 0 {
				prefix = prefixWords[len(prefixWords)-1]
			}
			for _, m := range matches {
				matchStr := prefix + string(m)
				// For case-insensitive matches, also check case-folded
				found := false
				for _, cmd := range commands {
					if cmd == matchStr || strings.EqualFold(cmd, matchStr) {
						found = true
						break
					}
					// Case-insensitive: the suffix itself (lowercased) should be a valid command
					if commonLen == 0 && strings.EqualFold(string(m), cmd) {
						found = true
						break
					}
				}
				if !found {
					t.Errorf("match %q is not a valid command", matchStr)
				}
			}
		})
	}
}

func TestAutoCompleteAdapterDoPreserveCommandOrder(t *testing.T) {
	adapter := NewAutoCompleter()
	// 'c' matches calc and clear; they should appear in Commands() order
	matches, _ := adapter.Do([]rune("c"), 1)
	if len(matches) != 2 {
		t.Fatalf("expected 2 matches, got %d", len(matches))
	}

	cmds := Commands()
	calcIdx, clearIdx := -1, -1
	for i, cmd := range cmds {
		if cmd == "calc" {
			calcIdx = i
		}
		if cmd == "clear" {
			clearIdx = i
		}
	}

	// Suffixes: prefix is "c", so matches are "alc" and "lear"
	match0 := "c" + string(matches[0])
	match1 := "c" + string(matches[1])

	if calcIdx < clearIdx {
		if match0 != "calc" || match1 != "clear" {
			t.Errorf("expected [calc, clear], got [%s, %s]", match0, match1)
		}
	} else {
		if match0 != "clear" || match1 != "calc" {
			t.Errorf("expected [clear, calc], got [%s, %s]", match0, match1)
		}
	}
}

func TestAutoCompleteAdapterDoMultilineInput(t *testing.T) {
	adapter := NewAutoCompleter()

	// Tab-separated words — exact match "help" returns no completions
	matches, _ := adapter.Do([]rune("rpn\thelp"), 8)
	if len(matches) != 0 {
		t.Errorf("tab-separated 'rpn\\thelp' (exact match) should return 0, got %d: %v",
			len(matches), runeSliceToStringSlice(matches))
	}
}

// Helper function to convert [][]rune to []string for error messages
func runeSliceToStringSlice(runes [][]rune) []string {
	result := make([]string, len(runes))
	for i, r := range runes {
		result[i] = string(r)
	}
	return result
}