/ test / integration_test.go
integration_test.go
 1  package test
 2  
 3  import (
 4  	"context"
 5  	"encoding/json"
 6  	"net/http"
 7  	"net/http/httptest"
 8  	"testing"
 9  
10  	"github.com/Kocoro-lab/ShanClaw/internal/agent"
11  	"github.com/Kocoro-lab/ShanClaw/internal/client"
12  	"github.com/Kocoro-lab/ShanClaw/internal/tools"
13  )
14  
15  func TestEndToEnd_FileReadAndAnalyze(t *testing.T) {
16  	callCount := 0
17  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18  		callCount++
19  
20  		if callCount == 1 {
21  			// First call: LLM decides to read a file
22  			json.NewEncoder(w).Encode(client.CompletionResponse{
23  				Model:        "test-model",
24  				OutputText:   "",
25  				FinishReason: "tool_use",
26  				FunctionCall: &client.FunctionCall{
27  					Name:      "file_read",
28  					Arguments: json.RawMessage(`{"path": "go.mod"}`),
29  				},
30  				Usage:     client.Usage{InputTokens: 10, OutputTokens: 5, TotalTokens: 15},
31  				RequestID: "req-test",
32  			})
33  		} else {
34  			// Second call: LLM analyzes the file content
35  			json.NewEncoder(w).Encode(client.CompletionResponse{
36  				Model:        "test-model",
37  				OutputText:   "This is a Go module for shannon-cli.",
38  				FinishReason: "end_turn",
39  				Usage:        client.Usage{InputTokens: 20, OutputTokens: 10, TotalTokens: 30},
40  				RequestID:    "req-test",
41  			})
42  		}
43  	}))
44  	defer server.Close()
45  
46  	gw := client.NewGatewayClient(server.URL, "")
47  	reg := agent.NewToolRegistry()
48  	reg.Register(&tools.FileReadTool{})
49  
50  	loop := agent.NewAgentLoop(gw, reg, "medium", "", 25, 2000, 200, nil, nil, nil)
51  	result, usage, err := loop.Run(context.Background(), "read go.mod", nil, nil)
52  
53  	if err != nil {
54  		t.Fatalf("unexpected error: %v", err)
55  	}
56  	if result != "This is a Go module for shannon-cli." {
57  		t.Errorf("unexpected result: %q", result)
58  	}
59  	if callCount != 2 {
60  		t.Errorf("expected 2 LLM calls, got %d", callCount)
61  	}
62  	if usage.TotalTokens != 45 {
63  		t.Errorf("expected 45 total tokens, got %d", usage.TotalTokens)
64  	}
65  	if usage.LLMCalls != 2 {
66  		t.Errorf("expected 2 LLM calls in usage, got %d", usage.LLMCalls)
67  	}
68  }