summaryrefslogtreecommitdiff
path: root/internal/repl/help_test.go
blob: b537c61600139aa33c510a0795682e21bb88a1a8 (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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package repl

import (
	"strings"
	"testing"
)

func TestGetHelpEmptyReturnsOverview(t *testing.T) {
	output := GetHelp("")
	if !strings.Contains(output, "gt") {
		t.Error("General help should mention 'gt'")
	}
	if !strings.Contains(output, "help categories") {
		t.Error("General help should mention 'help categories'")
	}
	if !strings.Contains(output, "Arithmetic") {
		t.Error("General help should list Arithmetic section")
	}
}

func TestGetHelpCategories(t *testing.T) {
	output := GetHelp("categories")
	if !strings.Contains(output, "REPL") {
		t.Error("Categories should list REPL")
	}
	if !strings.Contains(output, "Arithmetic") {
		t.Error("Categories should list Arithmetic")
	}
	if !strings.Contains(output, "Hyper") {
		t.Error("Categories should list Hyper")
	}
}

func TestGetHelpClearReturnsREPL(t *testing.T) {
	// "clear" exists in both REPL (screen clear) and Variables (clear vars)
	// helpByTopic should prefer the REPL entry
	output := GetHelp("clear")
	if !strings.Contains(output, "screen") && !strings.Contains(output, "terminal") {
		t.Errorf("'help clear' should show REPL screen clear, got: %s", output[:80])
	}
}

func TestGetHelpKnownOperator(t *testing.T) {
	output := GetHelp("+")
	if !strings.Contains(output, "Add") {
		t.Errorf("'help +' should describe Add, got: %s", output[:80])
	}
	if !strings.Contains(output, "Examples:") {
		t.Error("'help +' should have examples")
	}
	if !strings.Contains(output, "3 4 +") {
		t.Error("'help +' should have example '3 4 +'")
	}
}

func TestGetHelpOperatorWithAliases(t *testing.T) {
	// Test by alias
	output := GetHelp("gt")
	if !strings.Contains(output, ">") {
		t.Errorf("'help gt' should show > operator, got: %s", output[:80])
	}
	if !strings.Contains(output, "Aliases:") {
		t.Error("'help gt' should show aliases")
	}
}

func TestGetHelpHyperOperator(t *testing.T) {
	output := GetHelp("[+]")
	if !strings.Contains(output, "Add all stack values") {
		t.Errorf("'help [+]' should describe hyper add, got: %s", output[:80])
	}
	if !strings.Contains(output, "Hyper") {
		t.Error("'help [+]' should be in Hyper category")
	}
}

func TestGetHelpVariableOperator(t *testing.T) {
	output := GetHelp(":=")
	if !strings.Contains(output, "Assign") {
		t.Errorf("'help :=' should describe assignment, got: %s", output[:80])
	}
	if !strings.Contains(output, "Variables") {
		t.Error("'help :=' should be in Variables category")
	}
}

func TestGetHelpUnknownTopic(t *testing.T) {
	output := GetHelp("nonexistent")
	if !strings.Contains(output, "No help for") {
		t.Errorf("'help nonexistent' should say no help, got: %s", output)
	}
	if !strings.Contains(output, "help categories") {
		t.Error("'help nonexistent' should suggest 'help categories'")
	}
}

func TestGetHelpCaseInsensitive(t *testing.T) {
	output := GetHelp("lg")
	outputUpper := GetHelp("LG")
	if output != outputUpper {
		t.Error("Help should handle case consistently for lg/LG")
	}
}

func TestGetAllTopics(t *testing.T) {
	topics := GetAllTopics()
	if len(topics) == 0 {
		t.Error("GetAllTopics() should return topics")
	}

	// Check known topics are present
	expectedTopics := []string{"+", "-", "*", "/", "dup", "swap", "help", "rat", "[+]"}
	for _, expected := range expectedTopics {
		found := false
		for _, t := range topics {
			if t == expected {
				found = true
				break
			}
		}
		if !found {
			t.Errorf("GetAllTopics() missing topic %q", expected)
		}
	}
}

func TestGetCompletionTopicsIncludesAliases(t *testing.T) {
	topics := GetCompletionTopics()

	// Should include aliases
	expectedAliases := []string{"exit", "calc", "gt", "lt", "categories"}
	for _, expected := range expectedAliases {
		found := false
		for _, topic := range topics {
			if topic == expected {
				found = true
				break
			}
		}
		if !found {
			t.Errorf("GetCompletionTopics() missing alias/topic %q", expected)
		}
	}

	// "help" itself should NOT be in completion topics
	for _, topic := range topics {
		if topic == "help" {
			t.Error("GetCompletionTopics() should not include 'help' itself")
		}
	}
}

func TestGetCompletionTopicsIsSorted(t *testing.T) {
	topics := GetCompletionTopics()
	for i := 1; i < len(topics); i++ {
		if topics[i] < topics[i-1] {
			t.Errorf("Topics not sorted: %q > %q at index %d", topics[i-1], topics[i], i)
		}
	}
}

func TestFormatTopic(t *testing.T) {
	topic := helpByTopic["+"]
	if topic == nil {
		t.Fatal("helpByTopic[\"+\"] is nil")
	}

	output := formatTopic(topic)
	if !strings.Contains(output, "Topic:") {
		t.Error("formatTopic should contain 'Topic:'")
	}
	if !strings.Contains(output, "Usage:") {
		t.Error("formatTopic should contain 'Usage:'")
	}
	if !strings.Contains(output, "Desc:") {
		t.Error("formatTopic should contain 'Desc:'")
	}
	if !strings.Contains(output, "Examples:") {
		t.Error("formatTopic should contain 'Examples:'")
	}
}

func TestHelpTopicsNoDuplicates(t *testing.T) {
	// Operators can appear in multiple categories (e.g. "clear" in REPL and Variables)
	// Check for true duplicates: same operator within the same category
	seen := make(map[string]map[string]bool) // category -> operators
	for _, topic := range helpTopics {
		if seen[topic.Category] == nil {
			seen[topic.Category] = make(map[string]bool)
		}
		if seen[topic.Category][topic.Operator] {
			t.Errorf("Duplicate topic operator %q in category %q", topic.Operator, topic.Category)
		}
		seen[topic.Category][topic.Operator] = true
	}
}

func TestHelpByAliasResolution(t *testing.T) {
	// Test that aliases resolve to the correct topic
	output := GetHelp("exit")
	if !strings.Contains(output, "Exit") {
		t.Errorf("'help exit' should describe exit, got: %s", output[:80])
	}

	output = GetHelp("calc")
	if !strings.Contains(output, "RPN") {
		t.Errorf("'help calc' should describe RPN, got: %s", output[:80])
	}

	output = GetHelp("showstack")
	if !strings.Contains(output, "stack") {
		t.Errorf("'help showstack' should mention stack, got: %s", output[:80])
	}
}

func TestHelpCompleterIntegration(t *testing.T) {
	adapter := NewAutoCompleter()
	if adapter == nil {
		t.Fatal("NewAutoCompleter returned nil")
	}

	// "help " (with trailing space) should offer help topics, not "help help"
	matches, _ := adapter.Do([]rune("help "), 5)
	for _, m := range matches {
		if string(m) == "help" {
			t.Error("'help ' should not complete to 'help help'")
		}
	}
	// Should include a known topic
	foundPlus := false
	for _, m := range matches {
		if string(m) == "+" {
			foundPlus = true
		}
	}
	if !foundPlus {
		t.Errorf("'help ' should suggest '+', got: %v", matches)
	}

	// Test help topic completion
	matches, _ = adapter.Do([]rune("help +"), 6)
	if len(matches) != 1 {
		t.Errorf("'help +' should match +, got %d matches: %v", len(matches), matches)
	}

	// Test partial help topic completion
	matches, _ = adapter.Do([]rune("help du"), 7)
	if len(matches) != 1 {
		t.Errorf("'help du' should match dup, got %d matches: %v", len(matches), matches)
	}
}

func TestCmdHelpIntegration(t *testing.T) {
	// cmdHelp should delegate to GetHelp
	output := cmdHelp(nil)
	if !strings.Contains(output, "gt") {
		t.Error("cmdHelp(nil) should return general help")
	}

	output = cmdHelp([]string{"+"})
	if !strings.Contains(output, "Add") {
		t.Errorf("cmdHelp(['+']) should return + help, got: %s", output[:80])
	}

	output = cmdHelp([]string{"categories"})
	if !strings.Contains(output, "REPL") {
		t.Error("cmdHelp(['categories']) should list categories")
	}
}