/ go / test / context_test.go
context_test.go
  1  package test
  2  
  3  import (
  4  	"os"
  5  	"path/filepath"
  6  	"testing"
  7  
  8  	"github.com/TransformerOS/kamaji-go/internal/context"
  9  )
 10  
 11  func TestProjectContextDetection(t *testing.T) {
 12  	// Test Go project detection
 13  	tempDir := t.TempDir()
 14  	goModPath := filepath.Join(tempDir, "go.mod")
 15  	
 16  	goModContent := `module test-project
 17  
 18  go 1.21
 19  
 20  require (
 21  	github.com/spf13/cobra v1.8.0
 22  	github.com/test/dep v1.0.0
 23  )`
 24  	
 25  	err := os.WriteFile(goModPath, []byte(goModContent), 0644)
 26  	if err != nil {
 27  		t.Fatalf("Failed to create go.mod: %v", err)
 28  	}
 29  	
 30  	// Create a Go file
 31  	goFile := filepath.Join(tempDir, "main.go")
 32  	err = os.WriteFile(goFile, []byte("package main\n\nfunc main() {}"), 0644)
 33  	if err != nil {
 34  		t.Fatalf("Failed to create main.go: %v", err)
 35  	}
 36  	
 37  	ctx := context.DetectProjectContext(tempDir)
 38  	
 39  	// Verify detection
 40  	if ctx.Type != context.TypeGo {
 41  		t.Errorf("Expected Go project, got %s", ctx.Type)
 42  	}
 43  	
 44  	if ctx.Language != "Go" {
 45  		t.Errorf("Expected Go language, got %s", ctx.Language)
 46  	}
 47  	
 48  	if len(ctx.Dependencies) == 0 {
 49  		t.Error("Expected dependencies to be parsed")
 50  	}
 51  	
 52  	if len(ctx.Files) == 0 {
 53  		t.Error("Expected files to be found")
 54  	}
 55  	
 56  	// Check if dependencies were parsed correctly
 57  	found := false
 58  	for _, dep := range ctx.Dependencies {
 59  		if dep == "github.com/spf13/cobra" {
 60  			found = true
 61  			break
 62  		}
 63  	}
 64  	if !found {
 65  		t.Error("Expected to find github.com/spf13/cobra in dependencies")
 66  	}
 67  }
 68  
 69  func TestPythonProjectDetection(t *testing.T) {
 70  	tempDir := t.TempDir()
 71  	reqPath := filepath.Join(tempDir, "requirements.txt")
 72  	
 73  	reqContent := `requests==2.28.0
 74  numpy>=1.21.0
 75  # comment line
 76  flask==2.0.1`
 77  	
 78  	err := os.WriteFile(reqPath, []byte(reqContent), 0644)
 79  	if err != nil {
 80  		t.Fatalf("Failed to create requirements.txt: %v", err)
 81  	}
 82  	
 83  	ctx := context.DetectProjectContext(tempDir)
 84  	
 85  	if ctx.Type != context.TypePython {
 86  		t.Errorf("Expected Python project, got %s", ctx.Type)
 87  	}
 88  	
 89  	if ctx.Language != "Python" {
 90  		t.Errorf("Expected Python language, got %s", ctx.Language)
 91  	}
 92  	
 93  	expectedDeps := []string{"requests", "numpy", "flask"}
 94  	for _, expected := range expectedDeps {
 95  		found := false
 96  		for _, dep := range ctx.Dependencies {
 97  			if dep == expected {
 98  				found = true
 99  				break
100  			}
101  		}
102  		if !found {
103  			t.Errorf("Expected to find %s in dependencies", expected)
104  		}
105  	}
106  }
107  
108  func TestGenericProjectDetection(t *testing.T) {
109  	tempDir := t.TempDir()
110  	
111  	// Create some generic files
112  	readmePath := filepath.Join(tempDir, "README.md")
113  	err := os.WriteFile(readmePath, []byte("# Test Project"), 0644)
114  	if err != nil {
115  		t.Fatalf("Failed to create README.md: %v", err)
116  	}
117  	
118  	ctx := context.DetectProjectContext(tempDir)
119  	
120  	if ctx.Type != context.TypeGeneric {
121  		t.Errorf("Expected Generic project, got %s", ctx.Type)
122  	}
123  	
124  	if len(ctx.Files) == 0 {
125  		t.Error("Expected files to be found")
126  	}
127  }