summaryrefslogtreecommitdiff
path: root/internal/repl/repl_test.go
blob: f4b95eab5cf2c4d9a0398f809da3772a5b44a902 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package repl

import (
	"strings"
	"testing"

	"codeberg.org/snonux/perc/internal/rpn"

	"github.com/c-bata/go-prompt"
)

func TestExecutor(t *testing.T) {
	// Test that executor doesn't panic on empty input
	executor("")
}

func TestExecutorWithHelp(t *testing.T) {
	// Test executor with help command
	executor("help")
}

func TestExecutorWithClear(t *testing.T) {
	executor("clear")
}

func TestExecutorWithQuit(t *testing.T) {
	executor("quit")
}

func TestExecutorWithExit(t *testing.T) {
	executor("exit")
}

func TestExecutorWithPercentage(t *testing.T) {
	executor("20% of 150")
}

func TestExecutorWithRPN(t *testing.T) {
	executor("rpn 3 4 +")
}

func TestExecutorWithInvalid(t *testing.T) {
	executor("invalid input")
}

func TestExecutorWithVars(t *testing.T) {
	executor("rpn x 5 = vars")
}

func TestExecutorWithClearVariables(t *testing.T) {
	executor("rpn clear")
}

func TestIsBuiltinCommand(t *testing.T) {
	tests := []struct {
		input    string
		expected bool
	}{
		{"help", true},
		{"clear", true},
		{"quit", true},
		{"exit", true},
		{"rpn", true},
		{"calc", true},
		{"20% of 150", false},
		{"invalid", false},
	}

	for _, tt := range tests {
		t.Run(tt.input, func(t *testing.T) {
			_, ok := isBuiltinCommand(tt.input)
			if ok != tt.expected {
				t.Errorf("isBuiltinCommand(%q) = %v, want %v", tt.input, ok, tt.expected)
			}
		})
	}
}

func TestIsBuiltinCommandWithSubcommand(t *testing.T) {
	_, ok := isBuiltinCommand("help clear")
	if !ok {
		t.Error("isBuiltinCommand('help clear') should return true")
	}
}

func TestIsBuiltinCommandEdgeCases(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		{"empty string", "", false},
		{"single space", " ", false},
		{"case insensitive - HELP", "HELP", true},
		{"case insensitive - HeLp", "HeLp", true},
		{"command with extra spaces", "  help  ", true},
		{"partial match - hel", "hel", false},
		{"partial match - cal", "cal", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, ok := isBuiltinCommand(tt.input)
			if ok != tt.expected {
				t.Errorf("isBuiltinCommand(%q) = %v, want %v", tt.input, ok, tt.expected)
			}
		})
	}
}

func TestIsBuiltinCommandEdgeCasesWithAllCommands(t *testing.T) {
	allCommands := []string{"help", "clear", "quit", "exit", "rpn", "calc"}
	for _, cmd := range allCommands {
		t.Run(cmd, func(t *testing.T) {
			input, ok := isBuiltinCommand(cmd)
			if !ok {
				t.Errorf("isBuiltinCommand(%q) should return true for builtin command", cmd)
			}
			if input != cmd {
				t.Errorf("isBuiltinCommand(%q) returned %q, want %q", cmd, input, cmd)
			}
		})
	}
}

func TestIsBuiltinCommandWithMixedCase(t *testing.T) {
	tests := []struct {
		input    string
		expected bool
	}{
		{"HELP", true},
		{"Help", true},
		{"hElP", true},
		{"CLEAR", true},
		{"quit", true},
		{"QUIT", true},
		{"RPN", true},
		{"calc", true},
		{"CALC", true},
	}

	for _, tt := range tests {
		t.Run(tt.input, func(t *testing.T) {
			_, ok := isBuiltinCommand(tt.input)
			if ok != tt.expected {
				t.Errorf("isBuiltinCommand(%q) = %v, want %v", tt.input, ok, tt.expected)
			}
		})
	}
}

// TestRunRPN tests the runRPN helper function
func TestRunRPN(t *testing.T) {
	tests := []struct {
		name    string
		input   string
		wantErr bool
	}{
		{"simple addition", "3 4 +", false},
		{"simple subtraction", "10 3 -", false},
		{"simple multiplication", "2 3 *", false},
		{"simple division", "10 2 /", false},
		{"power operation", "2 3 ^", false},
		{"modulo operation", "10 3 %", false},
		{"with variables", "x 5 = x x +", false},
		{"empty input", "", true},
		{"invalid input", "invalid", true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := runRPN(tt.input)
			if (err != nil) != tt.wantErr {
				t.Errorf("runRPN(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
			}
		})
	}
}

func TestGetCommandDescription(t *testing.T) {
	tests := []struct {
		cmd        string
		wantPrefix string
	}{
		{"help", "Show help"},
		{"clear", "Clear"},
		{"quit", "Exit"},
		{"exit", "Exit"},
		{"rpn", "Evaluate an RPN"},
		{"calc", "Same as rpn"},
		{"unknown", ""},
	}

	for _, tt := range tests {
		t.Run(tt.cmd, func(t *testing.T) {
			desc := getCommandDescription(tt.cmd)
			if tt.wantPrefix != "" && !strings.Contains(desc, tt.wantPrefix) {
				t.Errorf("getCommandDescription(%q) = %q, should contain %q", tt.cmd, desc, tt.wantPrefix)
			}
		})
	}
}

func TestGetCommandDescriptionForUnknownCommand(t *testing.T) {
	desc := getCommandDescription("unknown")
	if desc != "" {
		t.Errorf("getCommandDescription(%q) = %q, should be empty", "unknown", desc)
	}
}

func TestExecutorWithSingleOperator(t *testing.T) {
	executor("+")
	executor("-")
	executor("*")
	executor("/")
	executor("^")
	executor("%")
	executor("dup")
	executor("swap")
	executor("pop")
	executor("show")
	executor("vars")
	executor("clear")
}

func TestExecutorWithPercentageExpression(t *testing.T) {
	executor("20% of 150")
	executor("30 is what %% of 150")
	executor("30 is 20%% of what")
}

func TestExecutorWithInvalidPercentage(t *testing.T) {
	executor("invalid percentage input")
}

func TestExecutorWithOperatorOnly(t *testing.T) {
	executor("1 2 +")
	executor("+")
}

func TestExecutorWithRPNPrefix(t *testing.T) {
	executor("rpn 3 4 +")
}

func TestExecutorWithCalcPrefix(t *testing.T) {
	executor("calc 5 6 +")
}

func TestExecutorWithEmptyInput(t *testing.T) {
	executor("")
}

func TestExecutorWithWhitespaceOnly(t *testing.T) {
	executor("   ")
}

func TestExecutorWithInvalidInput(t *testing.T) {
	tests := []string{"invalid input", "not a valid command", "xyz"}
	for _, input := range tests {
		t.Run(input, func(t *testing.T) {
			executor(input)
		})
	}
}

func TestExecutorWithInvalidRPN(t *testing.T) {
	executor("rpn 1 +")
}

func TestExecutorWithEmptyRPNPrefix(t *testing.T) {
	executor("rpn")
	executor("calc")
}

func TestExecutorWithAssignment(t *testing.T) {
	executor("rpn x 42 =")
	executor("rpn x")
}

func TestExecutorWithPercentageAndRPNFallback(t *testing.T) {
	executor("20% of 150")
	executor("3 4 +")
}

func TestGetHistoryPath(t *testing.T) {
	path := getHistoryPath()
	if path == "" {
		t.Error("getHistoryPath() returned empty string")
	}
}

func TestLoadHistory(t *testing.T) {
	history := loadHistory()
	_ = history
}

func TestSaveHistory(t *testing.T) {
	err := saveHistory([]string{"test1", "test2"})
	_ = err
}

func TestExecutorWithRPNExpressionOnly(t *testing.T) {
	executor("5 3 +")
}

func TestExecutorWithRPNThenOperator(t *testing.T) {
	executor("1 2 +")
	executor("+")
}

func TestExecutorWithRPNThenRPN(t *testing.T) {
	executor("rpn 1 2 +")
	executor("rpn 3 4 +")
}

func TestExecutorWithRPNShow(t *testing.T) {
	executor("rpn show")
}

func TestExecutorWithRPNDup(t *testing.T) {
	executor("rpn dup")
}

func TestExecutorWithRPNSwap(t *testing.T) {
	executor("rpn swap")
}

func TestExecutorWithRPNSingle(t *testing.T) {
	executor("rpn 42")
}

func TestExecutorWithRPNMulti(t *testing.T) {
	executor("rpn 1 2 3 4 5 +")
}

func TestExecutorWithStackOps(t *testing.T) {
	executor("dup")
	executor("swap")
	executor("pop")
	executor("show")
}

func TestExecutorWithRPNClear(t *testing.T) {
	executor("rpn clear")
}

func TestExecutorWithHistoryCommands(t *testing.T) {
	executor("vars")
	executor("clear")
}

func TestExecutorWithMixedInput(t *testing.T) {
	executor("25% of 200")
	executor("10 20 +")
}

func TestExecutorWithRPNCalcMixed(t *testing.T) {
	executor("rpn 1 2 +")
	executor("3 4 +")
	executor("calc 5 6 +")
}

func TestExecutorCommandsEdgeCases(t *testing.T) {
	executor("  clear  ")
	executor("HELP")
	executor("CLEAR")
}

func TestExecutorWithRPMPrefix(t *testing.T) {
	executor("rpn 1 2 +")
}

func TestExecutorWithCalcPrefixMixed(t *testing.T) {
	executor("calc 1 2 +")
}

func TestExecutorWithRatModeOn(t *testing.T) {
	executor("rat on")
	state := getRPNState()
	if state.rpnCalc.GetMode() != rpn.RationalMode {
		t.Errorf("Expected RationalMode after rat on, got %v", state.rpnCalc.GetMode())
	}
}

func TestExecutorWithRatModeOff(t *testing.T) {
	executor("rat off")
	state := getRPNState()
	if state.rpnCalc.GetMode() != rpn.FloatMode {
		t.Errorf("Expected FloatMode after rat off, got %v", state.rpnCalc.GetMode())
	}
}

func TestExecutorWithRatModeToggle(t *testing.T) {
	// First toggle - should enable rational mode if currently float
	executor("rat toggle")
	state := getRPNState()
	mode1 := state.rpnCalc.GetMode()

	// Second toggle - should toggle back
	executor("rat toggle")
	state = getRPNState()
	mode2 := state.rpnCalc.GetMode()

	// Modes should be different after toggle
	if mode1 == mode2 {
		t.Errorf("Modes should be different after toggle: %v -> %v", mode1, mode2)
	}
}

func TestExecutorWithRatModeInvalid(t *testing.T) {
	// Just verify it doesn't panic
	executor("rat invalid")
}

func TestExecutorWithRatModeNoArg(t *testing.T) {
	// Just verify it doesn't panic
	executor("rat")
}

func TestIsBuiltinCommandWithSubcommandHelp(t *testing.T) {
	_, ok := isBuiltinCommand("help")
	if !ok {
		t.Error("isBuiltinCommand('help') should return true")
	}
}

func TestRPNHandlerWithUnknownInput(t *testing.T) {
	// Test that unknown input falls through to next handler
	chain := NewCommandChain()

	// Create a minimal REPL
	r := &REPL{
		ttyChecker:    &TTYChecker{},
		historyMgr:    NewHistoryManager(".gt_history"),
		signalHandler: NewSignalHandler(),
		commandChain:  chain,
	}

	// Test unknown input - should not be handled by RPNHandler directly
	// but will be handled by Error handler after RPNHandler passes it through
	output, handled, err := chain.Handle(r, "unknowncommand")
	if handled {
		t.Errorf("Expected unknowncommand to be handled by error handler, got handled=%v, err=%v, output=%q", handled, err, output)
	}
}

func TestRPNHandlerWithPercentageExpression(t *testing.T) {
	// Test that percentage expressions are handled by PercentageHandler, not RPNHandler
	chain := NewCommandChain()
	r := &REPL{
		ttyChecker:    &TTYChecker{},
		historyMgr:    NewHistoryManager(".gt_history"),
		signalHandler: NewSignalHandler(),
		commandChain:  chain,
	}

	// Test percentage expression
	output, handled, err := chain.Handle(r, "20% of 150")
	if !handled {
		t.Errorf("Expected percentage expression to be handled, got handled=%v, err=%v, output=%q", handled, err, output)
	}
	if err != nil {
		t.Errorf("Expected no error for percentage expression, got %v", err)
	}
}

func TestRPNHandlerWithRPNExpression(t *testing.T) {
	// Test RPN expressions
	chain := NewCommandChain()
	vars := rpn.NewVariables()
	rpnState := &RPNState{
		vars:    vars,
		rpnCalc: rpn.NewRPN(vars),
	}
	r := &REPL{
		ttyChecker:    &TTYChecker{},
		historyMgr:    NewHistoryManager(".gt_history"),
		signalHandler: NewSignalHandler(),
		commandChain:  chain,
		rpnState:      rpnState,
	}

	// Test RPN expression
	output, handled, err := chain.Handle(r, "3 4 +")
	if !handled {
		t.Errorf("Expected RPN expression to be handled, got handled=%v, err=%v, output=%q", handled, err, output)
	}
	if err != nil {
		t.Errorf("Expected no error for RPN expression, got %v", err)
	}
}

func TestRPNHandlerWithSingleNumber(t *testing.T) {
	// Test single number input (RPN - pushes number onto stack)
	chain := NewCommandChain()
	vars := rpn.NewVariables()
	rpnState := &RPNState{
		vars:    vars,
		rpnCalc: rpn.NewRPN(vars),
	}
	r := &REPL{
		ttyChecker:    &TTYChecker{},
		historyMgr:    NewHistoryManager(".gt_history"),
		signalHandler: NewSignalHandler(),
		commandChain:  chain,
		rpnState:      rpnState,
	}

	// Test single number
	output, handled, err := chain.Handle(r, "42")
	if !handled {
		t.Errorf("Expected single number to be handled, got handled=%v, err=%v, output=%q", handled, err, output)
	}
	if err != nil {
		t.Errorf("Expected no error for single number, got %v", err)
	}
}

// TestNewREPL tests that NewREPL creates a valid REPL instance.
// Note: This test is skipped when not running in a TTY because the prompt
// library requires TTY access.
func TestNewREPL(t *testing.T) {
	// Skip this test if not running in a TTY
	ttyChecker := &TTYChecker{}
	if !ttyChecker.IsTTY() {
		t.Skip("Skipping test - not running in a TTY")
	}

	// Test that NewREPL creates a valid REPL instance without panicking
	repl := NewREPL(nil, nil)
	if repl == nil {
		t.Fatal("Expected REPL to be created, got nil")
	}
	if repl.prompt == nil {
		t.Error("Expected prompt to be set")
	}
	if repl.commandChain == nil {
		t.Error("Expected commandChain to be set")
	}
	if repl.ttyChecker == nil {
		t.Error("Expected ttyChecker to be set")
	}
	if repl.historyMgr == nil {
		t.Error("Expected historyMgr to be set")
	}
	if repl.signalHandler == nil {
		t.Error("Expected signalHandler to be set")
	}
}

func TestDefaultCompleter(t *testing.T) {
	// Test the default completer function directly
	// Note: This test has limited coverage because defaultCompleter uses
	// GetWordBeforeCursor() which requires proper cursor position.
	// The actual completer logic is tested in completer_test.go

	// Test with text that would match if cursor position was set correctly
	repl := &REPL{}
	doc := prompt.Document{Text: "h"}
	suggestions := defaultCompleter(repl, doc)

	// When cursor is at position 0 (default), GetWordBeforeCursor returns empty
	// But the test in completer_test.go verifies the actual behavior
	_ = suggestions

	// Test with clear prefix
	doc2 := prompt.Document{Text: "cl"}
	suggestions2 := defaultCompleter(repl, doc2)
	_ = suggestions2
}

func TestDefaultGetCommandDescription(t *testing.T) {
	// Create a REPL and test the defaultGetCommandDescription method
	repl := &REPL{}

	tests := []struct {
		cmd        string
		wantPrefix string
	}{
		{"help", "Show"},
		{"clear", "Clear"},
		{"quit", "Exit"},
		{"exit", "Exit"},
		{"rpn", "Evaluate"},
		{"calc", "Same"},
	}

	for _, tt := range tests {
		t.Run(tt.cmd, func(t *testing.T) {
			desc := repl.defaultGetCommandDescription(tt.cmd)
			if !strings.Contains(desc, tt.wantPrefix) {
				t.Errorf("defaultGetCommandDescription(%q) = %q, should contain %q", tt.cmd, desc, tt.wantPrefix)
			}
		})
	}
}

func TestExecutorWithUnknownCommand(t *testing.T) {
	// Test that unknown commands are handled by the error handler
	// This should exercise the "Not handled by any handler" path
	executor("completelyunknowncommand123")
}

func TestDefaultExecutorCodePaths(t *testing.T) {
	// Test all code paths in defaultExecutor
	// 1. Empty input (returns early at line 110)
	// 2. Handled=true with error (prints error, returns at line 124)
	// 3. Handled=true with output (prints output, returns at line 124)
	// 4. Handled=false with error (prints error at line 130)
	// 5. Handled=false without error (does nothing)

	// Path 1: Empty input
	executor("")

	// Path 2: Built-in command with error (clear should not error but let's verify)
	executor("clear")

	// Path 3: Built-in command with output (help returns help text)
	executor("help")

	// Path 4: Unknown command (error handler returns handled=false, err!=nil)
	executor("completelyunknowncommand123")

	// Path 5: Whitespace only (trimmed to empty, returns early)
	executor("   ")
}

func TestExecutorWithAssignmentRight(t *testing.T) {
	// Test := and =: operators
	executor("5 x :=")
	state := getRPNState()
	val, exists := state.vars.GetVariable("x")
	if !exists {
		t.Errorf("Variable x should exist after x :=")
	}
	if val != 5 {
		t.Errorf("Variable x = %v, want 5", val)
	}

	executor("y 3 =:")
	state = getRPNState()
	val, exists = state.vars.GetVariable("y")
	if !exists {
		t.Errorf("Variable y should exist after y =:")
	}
	if val != 3 {
		t.Errorf("Variable y = %v, want 3", val)
	}
}


func TestExecutorWithAssignmentAfterCalculation(t *testing.T) {
	// Test that assignment works after a calculation
	// Note: This test uses a fresh variable name to avoid conflicts with previous tests
	// that may have set x=5 from TestExecutorWithAssignmentRight
	executor("1 2 + z =:")
	state := getRPNState()
	val, exists := state.vars.GetVariable("z")
	if !exists {
		t.Errorf("Variable z should exist")
	}
	if val != 3 {
		t.Errorf("Variable z = %v, want 3", val)
	}
}

func TestExecutorWithIncrementalAssignment(t *testing.T) {
	// Test that assignment works after a calculation with separate commands
	// This should use the value from the stack for assignment
	executor("1 2 +")
	state := getRPNState()
	
	// Now use z =: to assign the top of stack (3) to variable z
	executor("z =:")
	state = getRPNState()
	val, exists := state.vars.GetVariable("z")
	if !exists {
		t.Errorf("Variable z should exist after z =:")
	}
	if val != 3 {
		t.Errorf("Variable z = %v, want 3", val)
	}
}

// TestExecutorWithSimpleIncrementalAssignment tests x =: after 2 in REPL
func TestExecutorWithSimpleIncrementalAssignment(t *testing.T) {
	// First execute 2 to put it on the stack
	executor("2")
	state := getRPNState()
	
	// Then use x =: to assign the top of stack to variable x
	executor("x =:")
	state = getRPNState()
	val, exists := state.vars.GetVariable("x")
	if !exists {
		t.Errorf("Variable x should exist after x =:")
	}
	if val != 2 {
		t.Errorf("Variable x = %v, want 2", val)
	}
}

// TestExecutorWithExactUserScenario tests the exact user scenario: 2 then x =:
func TestExecutorWithExactUserScenario(t *testing.T) {
	// This test replicates the exact user interaction:
	// > 2
	// > x =:
	// The variable should be assigned the value 2

	executor("2")
	state := getRPNState()
	
	// Verify stack has 2
	// (can't directly check stack without exposing it, but next command will fail if stack is empty)
	
	executor("x =:")
	state = getRPNState()
	val, exists := state.vars.GetVariable("x")
	if !exists {
		t.Errorf("Variable x should exist after x =:")
	}
	if val != 2 {
		t.Errorf("Variable x = %v, want 2", val)
	}
}