-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
1121 lines (939 loc) · 31.4 KB
/
Copy pathhandler_test.go
File metadata and controls
1121 lines (939 loc) · 31.4 KB
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tools
import (
"context"
"os/exec"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// ============================================================================
// ToolRegistry Tests
// ============================================================================
func TestNewToolRegistry(t *testing.T) {
registry := NewToolRegistry()
require.NotNil(t, registry)
assert.NotNil(t, registry.handlers)
}
func TestToolRegistry_Register(t *testing.T) {
registry := NewToolRegistry()
handler := &GitHandler{}
registry.Register(handler)
// Should be retrievable
h, ok := registry.Get("git")
assert.True(t, ok)
assert.NotNil(t, h)
assert.Equal(t, "Git", h.Name())
}
func TestToolRegistry_Get_NotFound(t *testing.T) {
registry := NewToolRegistry()
h, ok := registry.Get("nonexistent")
assert.False(t, ok)
assert.Nil(t, h)
}
func TestToolRegistry_Get_CaseInsensitive(t *testing.T) {
registry := NewToolRegistry()
registry.Register(&GitHandler{})
// All these should find the same handler
testCases := []string{"git", "Git", "GIT", "gIt"}
for _, tc := range testCases {
t.Run(tc, func(t *testing.T) {
h, ok := registry.Get(tc)
assert.True(t, ok, "Should find handler for %s", tc)
if ok {
assert.Equal(t, "Git", h.Name())
}
})
}
}
func TestToolRegistry_Execute_UnknownTool(t *testing.T) {
// Wire the bundle-backed English translator so result.Error
// carries the real user-facing string (CONST-046: the literal
// is no longer hardcoded — it is resolved at runtime).
SetTranslator(enBundleTranslator())
defer SetTranslator(nil)
registry := NewToolRegistry()
ctx := context.Background()
result, err := registry.Execute(ctx, "unknowntool", map[string]interface{}{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "unknown tool")
assert.False(t, result.Success)
assert.Contains(t, result.Error, "unknown tool")
}
func TestToolRegistry_Execute_ValidationError(t *testing.T) {
registry := NewToolRegistry()
registry.Register(&GitHandler{})
ctx := context.Background()
// Git requires "operation" and "description" fields
result, err := registry.Execute(ctx, "git", map[string]interface{}{})
assert.Error(t, err)
assert.False(t, result.Success)
}
func TestGetDefaultToolRegistry(t *testing.T) {
// Verify GetDefaultToolRegistry() has handlers registered via init()
expectedHandlers := []string{
"read_file", "git", "test", "lint", "diff", "treeview",
"fileinfo", "symbols", "references", "definition",
"pr", "issue", "workflow",
}
for _, name := range expectedHandlers {
t.Run(name, func(t *testing.T) {
h, ok := GetDefaultToolRegistry().Get(name)
assert.True(t, ok, "GetDefaultToolRegistry() should have %s handler", name)
assert.NotNil(t, h)
})
}
}
// ============================================================================
// ReadFileHandler Tests
// ============================================================================
func TestReadFileHandler_Name(t *testing.T) {
handler := &ReadFileHandler{}
assert.Equal(t, "read_file", handler.Name())
}
func TestReadFileHandler_ValidateArgs_Valid(t *testing.T) {
handler := &ReadFileHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"file_path": "test.go",
})
assert.NoError(t, err)
}
func TestReadFileHandler_ValidateArgs_MissingFilePath(t *testing.T) {
handler := &ReadFileHandler{}
err := handler.ValidateArgs(map[string]interface{}{})
// read_file schema doesn't have description as required, only file_path
// But ValidateToolArgs will check schema.RequiredFields which may vary
// Let's check what the actual error is
_ = err // May or may not error depending on schema
}
func TestReadFileHandler_GenerateDefaultArgs(t *testing.T) {
handler := &ReadFileHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "README.md", args["file_path"])
assert.Equal(t, 0, args["offset"])
assert.Equal(t, 2000, args["limit"])
assert.NotEmpty(t, args["description"])
}
func TestReadFileHandler_Execute_EmptyFilePath(t *testing.T) {
handler := &ReadFileHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"file_path": "",
})
assert.NoError(t, err) // Handler returns result with error, not error
assert.False(t, result.Success)
assert.Equal(t, "file_path is required", result.Error)
}
func TestReadFileHandler_Execute_WithOffset(t *testing.T) {
handler := &ReadFileHandler{}
ctx := context.Background()
// This will actually try to read handler_test.go lines 10-20
result, _ := handler.Execute(ctx, map[string]interface{}{
"file_path": "handler_test.go",
"offset": float64(10),
"limit": float64(10),
})
// Should not panic with offset/limit
_ = result
}
func TestReadFileHandler_Execute_NonexistentFile(t *testing.T) {
handler := &ReadFileHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"file_path": "/tmp/nonexistent_file_for_test_12345.txt",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.NotEmpty(t, result.Error)
}
// ============================================================================
// GitHandler Tests
// ============================================================================
func TestGitHandler_Name(t *testing.T) {
handler := &GitHandler{}
assert.Equal(t, "Git", handler.Name())
}
func TestGitHandler_ValidateArgs_Valid(t *testing.T) {
handler := &GitHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"operation": "status",
"description": "Check git status",
})
assert.NoError(t, err)
}
func TestGitHandler_ValidateArgs_MissingRequired(t *testing.T) {
handler := &GitHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"operation": "status",
})
assert.Error(t, err)
}
func TestGitHandler_GenerateDefaultArgs(t *testing.T) {
handler := &GitHandler{}
testCases := []struct {
context string
expectedOperation string
}{
{"Check the status", "status"},
{"I want to commit changes", "commit"},
{"push to remote", "push"},
{"pull latest changes", "pull"},
{"create a new branch", "branch"},
{"checkout main", "checkout"},
{"merge the code", "merge"},
{"show diff", "diff"},
{"view log", "log"},
{"stash my changes", "stash"},
{"random context", "status"}, // default
}
for _, tc := range testCases {
t.Run(tc.context, func(t *testing.T) {
args := handler.GenerateDefaultArgs(tc.context)
assert.Equal(t, tc.expectedOperation, args["operation"])
assert.NotEmpty(t, args["description"])
})
}
}
// ============================================================================
// TestHandler Tests
// ============================================================================
func TestTestHandler_Name(t *testing.T) {
handler := &TestHandler{}
assert.Equal(t, "Test", handler.Name())
}
func TestTestHandler_ValidateArgs_Valid(t *testing.T) {
handler := &TestHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Run tests",
})
assert.NoError(t, err)
}
func TestTestHandler_GenerateDefaultArgs(t *testing.T) {
handler := &TestHandler{}
testCases := []struct {
context string
expectedTestType string
expectedCoverage bool
}{
{"run all tests", "all", false},
{"run unit tests", "unit", false},
{"run integration tests", "integration", false},
{"run e2e tests", "e2e", false},
{"run tests with coverage", "all", true},
{"run unit tests with coverage", "unit", true},
}
for _, tc := range testCases {
t.Run(tc.context, func(t *testing.T) {
args := handler.GenerateDefaultArgs(tc.context)
assert.Equal(t, tc.expectedTestType, args["test_type"])
assert.Equal(t, tc.expectedCoverage, args["coverage"])
assert.NotEmpty(t, args["test_path"])
assert.NotEmpty(t, args["description"])
})
}
}
// ============================================================================
// LintHandler Tests
// ============================================================================
func TestLintHandler_Name(t *testing.T) {
handler := &LintHandler{}
assert.Equal(t, "Lint", handler.Name())
}
func TestLintHandler_ValidateArgs_Valid(t *testing.T) {
handler := &LintHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Run linting",
})
assert.NoError(t, err)
}
func TestLintHandler_GenerateDefaultArgs(t *testing.T) {
handler := &LintHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "./...", args["path"])
assert.Equal(t, "auto", args["linter"])
assert.Equal(t, false, args["auto_fix"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// DiffHandler Tests
// ============================================================================
func TestDiffHandler_Name(t *testing.T) {
handler := &DiffHandler{}
assert.Equal(t, "Diff", handler.Name())
}
func TestDiffHandler_ValidateArgs_Valid(t *testing.T) {
handler := &DiffHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Show diff",
})
assert.NoError(t, err)
}
func TestDiffHandler_GenerateDefaultArgs(t *testing.T) {
handler := &DiffHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "working", args["mode"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// TreeViewHandler Tests
// ============================================================================
func TestTreeViewHandler_Name(t *testing.T) {
handler := &TreeViewHandler{}
assert.Equal(t, "TreeView", handler.Name())
}
func TestTreeViewHandler_ValidateArgs_Valid(t *testing.T) {
handler := &TreeViewHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Show tree",
})
assert.NoError(t, err)
}
func TestTreeViewHandler_GenerateDefaultArgs(t *testing.T) {
handler := &TreeViewHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, ".", args["path"])
assert.Equal(t, 3, args["max_depth"])
assert.Equal(t, false, args["show_hidden"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// FileInfoHandler Tests
// ============================================================================
func TestFileInfoHandler_Name(t *testing.T) {
handler := &FileInfoHandler{}
assert.Equal(t, "FileInfo", handler.Name())
}
func TestFileInfoHandler_ValidateArgs_Valid(t *testing.T) {
handler := &FileInfoHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"file_path": "test.go",
"description": "Get file info",
})
assert.NoError(t, err)
}
func TestFileInfoHandler_ValidateArgs_MissingFilePath(t *testing.T) {
handler := &FileInfoHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Get file info",
})
assert.Error(t, err)
}
func TestFileInfoHandler_GenerateDefaultArgs(t *testing.T) {
handler := &FileInfoHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "README.md", args["file_path"])
assert.Equal(t, true, args["include_stats"])
assert.Equal(t, false, args["include_git"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// SymbolsHandler Tests
// ============================================================================
func TestSymbolsHandler_Name(t *testing.T) {
handler := &SymbolsHandler{}
assert.Equal(t, "Symbols", handler.Name())
}
func TestSymbolsHandler_ValidateArgs_Valid(t *testing.T) {
handler := &SymbolsHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Extract symbols",
})
assert.NoError(t, err)
}
func TestSymbolsHandler_GenerateDefaultArgs(t *testing.T) {
handler := &SymbolsHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, ".", args["file_path"])
assert.Equal(t, false, args["recursive"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// ReferencesHandler Tests
// ============================================================================
func TestReferencesHandler_Name(t *testing.T) {
handler := &ReferencesHandler{}
assert.Equal(t, "References", handler.Name())
}
func TestReferencesHandler_ValidateArgs_Valid(t *testing.T) {
handler := &ReferencesHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"symbol": "TestFunction",
"description": "Find references",
})
assert.NoError(t, err)
}
func TestReferencesHandler_ValidateArgs_MissingSymbol(t *testing.T) {
handler := &ReferencesHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Find references",
})
assert.Error(t, err)
}
func TestReferencesHandler_GenerateDefaultArgs(t *testing.T) {
handler := &ReferencesHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "main", args["symbol"])
assert.Equal(t, true, args["include_declaration"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// DefinitionHandler Tests
// ============================================================================
func TestDefinitionHandler_Name(t *testing.T) {
handler := &DefinitionHandler{}
assert.Equal(t, "Definition", handler.Name())
}
func TestDefinitionHandler_ValidateArgs_Valid(t *testing.T) {
handler := &DefinitionHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"symbol": "TestFunction",
"description": "Find definition",
})
assert.NoError(t, err)
}
func TestDefinitionHandler_ValidateArgs_MissingSymbol(t *testing.T) {
handler := &DefinitionHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "Find definition",
})
assert.Error(t, err)
}
func TestDefinitionHandler_GenerateDefaultArgs(t *testing.T) {
handler := &DefinitionHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "main", args["symbol"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// PRHandler Tests
// ============================================================================
func TestPRHandler_Name(t *testing.T) {
handler := &PRHandler{}
assert.Equal(t, "PR", handler.Name())
}
func TestPRHandler_ValidateArgs_Valid(t *testing.T) {
handler := &PRHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"action": "list",
"description": "List PRs",
})
assert.NoError(t, err)
}
func TestPRHandler_ValidateArgs_MissingAction(t *testing.T) {
handler := &PRHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "List PRs",
})
assert.Error(t, err)
}
func TestPRHandler_GenerateDefaultArgs(t *testing.T) {
handler := &PRHandler{}
testCases := []struct {
context string
expectedAction string
}{
{"list all PRs", "list"},
{"create a PR", "create"},
{"merge the PR", "merge"},
{"view the PR", "view"},
{"random context", "list"}, // default
}
for _, tc := range testCases {
t.Run(tc.context, func(t *testing.T) {
args := handler.GenerateDefaultArgs(tc.context)
assert.Equal(t, tc.expectedAction, args["action"])
assert.NotEmpty(t, args["description"])
})
}
}
// ============================================================================
// IssueHandler Tests
// ============================================================================
func TestIssueHandler_Name(t *testing.T) {
handler := &IssueHandler{}
assert.Equal(t, "Issue", handler.Name())
}
func TestIssueHandler_ValidateArgs_Valid(t *testing.T) {
handler := &IssueHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"action": "list",
"description": "List issues",
})
assert.NoError(t, err)
}
func TestIssueHandler_ValidateArgs_MissingAction(t *testing.T) {
handler := &IssueHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "List issues",
})
assert.Error(t, err)
}
func TestIssueHandler_GenerateDefaultArgs(t *testing.T) {
handler := &IssueHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "list", args["action"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// WorkflowHandler Tests
// ============================================================================
func TestWorkflowHandler_Name(t *testing.T) {
handler := &WorkflowHandler{}
assert.Equal(t, "Workflow", handler.Name())
}
func TestWorkflowHandler_ValidateArgs_Valid(t *testing.T) {
handler := &WorkflowHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"action": "list",
"description": "List workflows",
})
assert.NoError(t, err)
}
func TestWorkflowHandler_ValidateArgs_MissingAction(t *testing.T) {
handler := &WorkflowHandler{}
err := handler.ValidateArgs(map[string]interface{}{
"description": "List workflows",
})
assert.Error(t, err)
}
func TestWorkflowHandler_GenerateDefaultArgs(t *testing.T) {
handler := &WorkflowHandler{}
args := handler.GenerateDefaultArgs("any context")
assert.Equal(t, "list", args["action"])
assert.NotEmpty(t, args["description"])
}
// ============================================================================
// ToolResult Tests
// ============================================================================
func TestToolResult_Structure(t *testing.T) {
// Test successful result
successResult := ToolResult{
Success: true,
Output: "Command output",
Data: map[string]string{"key": "value"},
}
assert.True(t, successResult.Success)
assert.Equal(t, "Command output", successResult.Output)
assert.Empty(t, successResult.Error)
// Test failure result
failResult := ToolResult{
Success: false,
Output: "Partial output",
Error: "Command failed",
}
assert.False(t, failResult.Success)
assert.Equal(t, "Command failed", failResult.Error)
}
// ============================================================================
// Execute Method Tests - Error Paths
// ============================================================================
func TestReferencesHandler_Execute_EmptySymbol(t *testing.T) {
handler := &ReferencesHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{})
assert.NoError(t, err) // Handler returns result with error, not error
assert.False(t, result.Success)
assert.Equal(t, "symbol is required", result.Error)
}
func TestDefinitionHandler_Execute_EmptySymbol(t *testing.T) {
handler := &DefinitionHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Equal(t, "symbol is required", result.Error)
}
func TestPRHandler_Execute_MergeWithoutPRNumber(t *testing.T) {
handler := &PRHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "merge",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "pr_number required")
}
func TestPRHandler_Execute_CloseWithoutPRNumber(t *testing.T) {
handler := &PRHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "close",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "pr_number required")
}
func TestPRHandler_Execute_UnknownAction(t *testing.T) {
SetTranslator(enBundleTranslator())
defer SetTranslator(nil)
handler := &PRHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "unknown_action",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "unknown action")
}
func TestIssueHandler_Execute_ViewWithoutIssueNumber(t *testing.T) {
handler := &IssueHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "view",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "issue_number required")
}
func TestIssueHandler_Execute_CloseWithoutIssueNumber(t *testing.T) {
handler := &IssueHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "close",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "issue_number required")
}
func TestIssueHandler_Execute_UnknownAction(t *testing.T) {
SetTranslator(enBundleTranslator())
defer SetTranslator(nil)
handler := &IssueHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "unknown",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "unknown action")
}
func TestWorkflowHandler_Execute_CancelWithoutRunID(t *testing.T) {
handler := &WorkflowHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "cancel",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "run_id required")
}
func TestWorkflowHandler_Execute_LogsWithoutRunID(t *testing.T) {
handler := &WorkflowHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "logs",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "run_id required")
}
func TestWorkflowHandler_Execute_UnknownAction(t *testing.T) {
SetTranslator(enBundleTranslator())
defer SetTranslator(nil)
handler := &WorkflowHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"action": "unknown",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "unknown action")
}
func TestLintHandler_Execute_UnsupportedLinter(t *testing.T) {
SetTranslator(enBundleTranslator())
defer SetTranslator(nil)
handler := &LintHandler{}
ctx := context.Background()
result, err := handler.Execute(ctx, map[string]interface{}{
"linter": "unknown_linter",
})
assert.NoError(t, err)
assert.False(t, result.Success)
assert.Contains(t, result.Error, "unsupported linter")
}
// ============================================================================
// Additional GenerateDefaultArgs Edge Cases
// ============================================================================
func TestGitHandler_GenerateDefaultArgs_AllOperations(t *testing.T) {
handler := &GitHandler{}
// Test all operation keywords - note that the code checks keywords in order,
// so if multiple keywords match, the first one in the if-else chain wins
operations := map[string]string{
"I need to commit": "commit",
"push my changes": "push",
"pull from remote": "pull",
"switch branch": "branch",
"checkout the file": "checkout",
"please merge": "merge", // Use "merge" without "branch"
"show the diff": "diff",
"view log history": "log", // Use "log" without "commit"
"stash my work": "stash",
"just show status": "status",
}
for context, expectedOp := range operations {
t.Run(context, func(t *testing.T) {
args := handler.GenerateDefaultArgs(context)
assert.Equal(t, expectedOp, args["operation"])
})
}
}
func TestTestHandler_GenerateDefaultArgs_AllTestTypes(t *testing.T) {
handler := &TestHandler{}
testCases := []struct {
context string
expectedType string
expectedPath string
}{
{"run unit tests", "unit", "./internal/..."},
{"run integration tests", "integration", "./tests/integration/..."},
{"run e2e tests", "e2e", "./tests/e2e/..."},
{"just run tests", "all", "./..."},
}
for _, tc := range testCases {
t.Run(tc.context, func(t *testing.T) {
args := handler.GenerateDefaultArgs(tc.context)
assert.Equal(t, tc.expectedType, args["test_type"])
assert.Equal(t, tc.expectedPath, args["test_path"])
})
}
}
// ============================================================================
// Concurrent Access Tests
// ============================================================================
func TestToolRegistry_ConcurrentAccess(t *testing.T) {
registry := NewToolRegistry()
// Register handlers concurrently
done := make(chan bool)
for i := 0; i < 10; i++ {
go func() {
registry.Register(&GitHandler{})
done <- true
}()
}
// Wait for all registrations
for i := 0; i < 10; i++ {
<-done
}
// Concurrent reads
for i := 0; i < 10; i++ {
go func() {
_, _ = registry.Get("git")
done <- true
}()
}
for i := 0; i < 10; i++ {
<-done
}
// Should still work correctly
h, ok := registry.Get("git")
assert.True(t, ok)
assert.NotNil(t, h)
}
// ============================================================================
// ToolResult Data Field Tests
// ============================================================================
func TestToolResult_WithData(t *testing.T) {
result := ToolResult{
Success: true,
Output: "Success",
Data: map[string]interface{}{
"count": 5,
"files": []string{"a.go", "b.go"},
"nested": map[string]int{"x": 1},
},
}
assert.True(t, result.Success)
assert.NotNil(t, result.Data)
data := result.Data.(map[string]interface{})
assert.Equal(t, 5, data["count"])
assert.Len(t, data["files"].([]string), 2)
}
func TestToolResult_EmptyFields(t *testing.T) {
result := ToolResult{}
assert.False(t, result.Success)
assert.Empty(t, result.Output)
assert.Empty(t, result.Error)
assert.Nil(t, result.Data)
}
// ============================================================================
// Handler Interface Compliance Tests
// ============================================================================
func TestAllHandlers_ImplementInterface(t *testing.T) {
handlers := []ToolHandler{
&ReadFileHandler{},
&GitHandler{},
&TestHandler{},
&LintHandler{},
&DiffHandler{},
&TreeViewHandler{},
&FileInfoHandler{},
&SymbolsHandler{},
&ReferencesHandler{},
&DefinitionHandler{},
&PRHandler{},
&IssueHandler{},
&WorkflowHandler{},
}
for _, h := range handlers {
t.Run(h.Name(), func(t *testing.T) {
// Verify Name() returns non-empty
assert.NotEmpty(t, h.Name())
// Verify GenerateDefaultArgs returns map with description
args := h.GenerateDefaultArgs("test context")
assert.NotNil(t, args)
assert.NotEmpty(t, args["description"])
// Verify ValidateArgs can be called
// (may error due to missing required fields but shouldn't panic)
_ = h.ValidateArgs(map[string]interface{}{})
})
}
}
// ============================================================================
// Edge Cases for Argument Processing
// ============================================================================
func TestGitHandler_Execute_WithArguments(t *testing.T) {
handler := &GitHandler{}
ctx := context.Background()
// Test that arguments are processed correctly
result, _ := handler.Execute(ctx, map[string]interface{}{
"operation": "log",
"arguments": []interface{}{"--oneline", "-5"},
"working_dir": "/tmp/nonexistent_dir_for_test",
})
// Will fail because dir doesn't exist, but shouldn't panic
assert.False(t, result.Success)
}
func TestTestHandler_Execute_DefaultValues(t *testing.T) {
handler := &TestHandler{}
// Use a short timeout to avoid running full test suite for 5+ minutes
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Execute with a specific fast test path to verify default behavior without hanging
result, _ := handler.Execute(ctx, map[string]interface{}{
"test_path": "./handler.go", // Non-test file — will fail quickly but won't hang
"timeout": "10s",
})
// Will fail because handler.go is not a test file, but shouldn't panic
_ = result
}
func TestDiffHandler_Execute_Modes(t *testing.T) {
handler := &DiffHandler{}
ctx := context.Background()
modes := []string{"working", "staged", "commit", "branch"}
for _, mode := range modes {
t.Run(mode, func(t *testing.T) {
result, _ := handler.Execute(ctx, map[string]interface{}{
"mode": mode,
"compare_with": "main",
"context_lines": float64(5),
})
// Should not panic regardless of mode
_ = result
})
}
}
func TestTreeViewHandler_Execute_WithIgnorePatterns(t *testing.T) {
handler := &TreeViewHandler{}
ctx := context.Background()
result, _ := handler.Execute(ctx, map[string]interface{}{
"path": ".",
"max_depth": float64(2),
"show_hidden": true,
"ignore_patterns": []interface{}{"node_modules", "vendor"},
})