/ go / test / track_b_test.go
track_b_test.go
  1  package test
  2  
  3  import (
  4  	"context"
  5  	"runtime"
  6  	"strings"
  7  	"testing"
  8  
  9  	"github.com/TransformerOS/kamaji-go/internal/tools"
 10  )
 11  
 12  // TestTrackB_ShellAndGitTools tests shell and git tools comprehensively
 13  func TestTrackB_ShellAndGitTools(t *testing.T) {
 14  	ctx := context.Background()
 15  
 16  	t.Run("ShellExecuteTool", func(t *testing.T) {
 17  		tool := tools.ShellExecuteTool{}
 18  		
 19  		// Test basic command
 20  		var testCmd string
 21  		if runtime.GOOS == "windows" {
 22  			testCmd = "echo hello"
 23  		} else {
 24  			testCmd = "echo hello"
 25  		}
 26  		
 27  		result, err := tool.Call(ctx, testCmd)
 28  		if err != nil {
 29  			t.Errorf("ShellExecuteTool failed: %v", err)
 30  		}
 31  		if !strings.Contains(result, "hello") {
 32  			t.Errorf("Expected result to contain 'hello', got: %s", result)
 33  		}
 34  
 35  		// Test command with output
 36  		if runtime.GOOS != "windows" {
 37  			result, err = tool.Call(ctx, "pwd")
 38  			if err != nil {
 39  				t.Errorf("ShellExecuteTool pwd failed: %v", err)
 40  			}
 41  			if result == "" {
 42  				t.Error("Expected non-empty result from pwd")
 43  			}
 44  		}
 45  
 46  		// Test empty command
 47  		_, err = tool.Call(ctx, "")
 48  		if err == nil {
 49  			t.Error("Expected error for empty command")
 50  		}
 51  
 52  		// Test invalid command
 53  		_, err = tool.Call(ctx, "nonexistent_command_12345")
 54  		if err == nil {
 55  			t.Error("Expected error for invalid command")
 56  		}
 57  	})
 58  
 59  	t.Run("GetCurrentDirectoryTool", func(t *testing.T) {
 60  		tool := tools.GetCurrentDirectoryTool{}
 61  		
 62  		result, err := tool.Call(ctx, "")
 63  		if err != nil {
 64  			t.Errorf("GetCurrentDirectoryTool failed: %v", err)
 65  		}
 66  		if result == "" {
 67  			t.Error("Expected non-empty current directory")
 68  		}
 69  		
 70  		// Should work with any input (input is ignored)
 71  		result2, err := tool.Call(ctx, "ignored_input")
 72  		if err != nil {
 73  			t.Errorf("GetCurrentDirectoryTool failed with input: %v", err)
 74  		}
 75  		if result != result2 {
 76  			t.Error("GetCurrentDirectoryTool should return same result regardless of input")
 77  		}
 78  	})
 79  
 80  	t.Run("GitTools_Availability", func(t *testing.T) {
 81  		// Test if git tools are available
 82  		allTools := tools.GetAll()
 83  		
 84  		gitToolNames := []string{
 85  			"git_status",
 86  			"git_commit", 
 87  			"git_add",
 88  		}
 89  		
 90  		toolNames := make(map[string]bool)
 91  		for _, tool := range allTools {
 92  			toolNames[tool.Name()] = true
 93  		}
 94  		
 95  		gitToolsFound := 0
 96  		for _, gitTool := range gitToolNames {
 97  			if toolNames[gitTool] {
 98  				gitToolsFound++
 99  				t.Logf("✅ Git tool found: %s", gitTool)
100  			} else {
101  				t.Logf("⏳ Git tool missing: %s", gitTool)
102  			}
103  		}
104  		
105  		if gitToolsFound == 0 {
106  			t.Log("⚠️  No git tools found - Track B incomplete")
107  		} else if gitToolsFound < len(gitToolNames) {
108  			t.Logf("⚠️  Partial git tools: %d/%d found", gitToolsFound, len(gitToolNames))
109  		} else {
110  			t.Log("✅ All git tools found")
111  		}
112  	})
113  
114  	t.Run("ShellTool_Registration", func(t *testing.T) {
115  		// Test that shell tools are registered
116  		allTools := tools.GetAll()
117  		
118  		expectedShellTools := []string{
119  			"shell_execute",
120  			"get_current_directory",
121  		}
122  		
123  		toolNames := make(map[string]bool)
124  		for _, tool := range allTools {
125  			toolNames[tool.Name()] = true
126  		}
127  		
128  		for _, expected := range expectedShellTools {
129  			if !toolNames[expected] {
130  				t.Errorf("Expected shell tool %s to be registered", expected)
131  			} else {
132  				t.Logf("✅ Shell tool registered: %s", expected)
133  			}
134  		}
135  	})
136  
137  	t.Run("ShellTool_Interface", func(t *testing.T) {
138  		// Test shell tools implement interface correctly
139  		shellTools := []tools.Tool{
140  			tools.ShellExecuteTool{},
141  			tools.GetCurrentDirectoryTool{},
142  		}
143  		
144  		for _, tool := range shellTools {
145  			name := tool.Name()
146  			if name == "" {
147  				t.Errorf("Shell tool %T returned empty name", tool)
148  			}
149  			
150  			desc := tool.Description()
151  			if desc == "" {
152  				t.Errorf("Shell tool %s returned empty description", name)
153  			}
154  			
155  			t.Logf("✅ Shell tool %s: %s", name, desc)
156  		}
157  	})
158  
159  	t.Run("GitRepository_Detection", func(t *testing.T) {
160  		// Test if we're in a git repository
161  		tool := tools.ShellExecuteTool{}
162  		
163  		result, err := tool.Call(ctx, "git status")
164  		if err != nil {
165  			t.Logf("⚠️  Not in a git repository or git not available: %v", err)
166  		} else {
167  			t.Logf("✅ Git repository detected: %s", strings.Split(result, "\n")[0])
168  		}
169  	})
170  
171  	t.Run("ShellTool_ErrorHandling", func(t *testing.T) {
172  		tool := tools.ShellExecuteTool{}
173  		
174  		// Test timeout behavior (if implemented)
175  		// This is a long-running command that should be handled gracefully
176  		if runtime.GOOS != "windows" {
177  			_, err := tool.Call(ctx, "sleep 0.1")
178  			if err != nil {
179  				t.Logf("⚠️  Sleep command failed (may indicate timeout issues): %v", err)
180  			} else {
181  				t.Log("✅ Short sleep command handled correctly")
182  			}
183  		}
184  	})
185  }