/ go / test / integration_test.go
integration_test.go
  1  package test
  2  
  3  import (
  4  	"context"
  5  	"os"
  6  	"path/filepath"
  7  	"strings"
  8  	"testing"
  9  	"time"
 10  
 11  	projectcontext "github.com/TransformerOS/kamaji-go/internal/context"
 12  	"github.com/TransformerOS/kamaji-go/internal/providers"
 13  	"github.com/TransformerOS/kamaji-go/internal/streaming"
 14  	"github.com/TransformerOS/kamaji-go/internal/tools"
 15  )
 16  
 17  // Mock provider for integration testing
 18  type integrationMockProvider struct {
 19  	name     string
 20  	response string
 21  }
 22  
 23  func (m *integrationMockProvider) Call(ctx context.Context, prompt string) (string, error) {
 24  	return m.response, nil
 25  }
 26  
 27  func TestFullSystemIntegration(t *testing.T) {
 28  	// 1. Test project context detection
 29  	tempDir := t.TempDir()
 30  	goModPath := filepath.Join(tempDir, "go.mod")
 31  	
 32  	goModContent := `module integration-test
 33  
 34  go 1.21
 35  
 36  require (
 37  	github.com/spf13/cobra v1.8.0
 38  )`
 39  	
 40  	err := os.WriteFile(goModPath, []byte(goModContent), 0644)
 41  	if err != nil {
 42  		t.Fatalf("Failed to create go.mod: %v", err)
 43  	}
 44  	
 45  	projectCtx := projectcontext.DetectProjectContext(tempDir)
 46  	if projectCtx.Type != projectcontext.TypeGo {
 47  		t.Errorf("Expected Go project, got %s", projectCtx.Type)
 48  	}
 49  	
 50  	// 2. Test provider pool setup
 51  	pool := providers.NewProviderPool(providers.Failover)
 52  	
 53  	// Add mock provider
 54  	mockProvider := &integrationMockProvider{
 55  		name:     "integration-test",
 56  		response: "Integration test response",
 57  	}
 58  	pool.AddProvider(mockProvider)
 59  	
 60  	// Test provider pool call
 61  	ctx := context.Background()
 62  	response, err := pool.Call(ctx, "test prompt")
 63  	if err != nil {
 64  		t.Errorf("Provider pool call failed: %v", err)
 65  	}
 66  	if response != "Integration test response" {
 67  		t.Errorf("Expected 'Integration test response', got '%s'", response)
 68  	}
 69  	
 70  	// 3. Test streaming system
 71  	stream := streaming.NewAgentStream(ctx)
 72  	defer stream.Close()
 73  	
 74  	// Send events
 75  	go func() {
 76  		stream.Thinking("Processing integration test...")
 77  		stream.Final("Integration test completed")
 78  	}()
 79  	
 80  	// Collect events
 81  	var events []streaming.AgentEvent
 82  	timeout := time.After(1 * time.Second)
 83  	
 84  	for len(events) < 2 {
 85  		select {
 86  		case event := <-stream.Events:
 87  			events = append(events, event)
 88  		case <-timeout:
 89  			t.Fatal("Timeout waiting for stream events")
 90  		}
 91  	}
 92  	
 93  	if len(events) != 2 {
 94  		t.Errorf("Expected 2 events, got %d", len(events))
 95  	}
 96  	
 97  	// 4. Test tools availability
 98  	allTools := tools.GetAll()
 99  	if len(allTools) == 0 {
100  		t.Error("Expected tools to be available")
101  	}
102  	
103  	// Find a basic tool and test it
104  	var testTool tools.Tool
105  	for _, tool := range allTools {
106  		if strings.Contains(tool.Name(), "read") || strings.Contains(tool.Name(), "file") {
107  			testTool = tool
108  			break
109  		}
110  	}
111  	
112  	if testTool != nil {
113  		// Test tool call (should not crash)
114  		_, err := testTool.Call(ctx, "test input")
115  		// We don't check for success since it might fail due to missing files
116  		// but it should not panic
117  		if err != nil {
118  			t.Logf("Tool call failed (expected): %v", err)
119  		}
120  	}
121  	
122  	t.Logf("Integration test completed successfully:")
123  	t.Logf("- Project context: %s (%s)", projectCtx.Type, projectCtx.Language)
124  	t.Logf("- Provider pool: %d providers", 1)
125  	t.Logf("- Streaming: %d events processed", len(events))
126  	t.Logf("- Tools: %d available", len(allTools))
127  }
128  
129  func TestUnifiedBinaryComponents(t *testing.T) {
130  	// Test that all major components can be initialized without errors
131  	
132  	// 1. Context detection
133  	ctx := projectcontext.DetectProjectContext(".")
134  	if ctx.Root == "" {
135  		t.Error("Context detection failed - no root path")
136  	}
137  	
138  	// 2. Provider pool
139  	pool := providers.NewProviderPool(providers.Failover)
140  	if pool == nil {
141  		t.Error("Provider pool creation failed")
142  	}
143  	
144  	// 3. Streaming
145  	stream := streaming.NewAgentStream(context.Background())
146  	if stream == nil {
147  		t.Error("Streaming system creation failed")
148  	}
149  	stream.Close()
150  	
151  	// 4. Tools
152  	allTools := tools.GetAll()
153  	if len(allTools) == 0 {
154  		t.Error("No tools available")
155  	}
156  	
157  	t.Logf("All components initialized successfully:")
158  	t.Logf("- Context: %s project", ctx.Type)
159  	t.Logf("- Provider pool: Ready")
160  	t.Logf("- Streaming: Ready") 
161  	t.Logf("- Tools: %d loaded", len(allTools))
162  }