first commit
This commit is contained in:
128
app/mcp/server_mcpgo_test.go
Normal file
128
app/mcp/server_mcpgo_test.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
mcpgo "github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
// Test server creation
|
||||
func TestNewMCPGoServer(t *testing.T) {
|
||||
server := newMCPGoServer()
|
||||
if server == nil {
|
||||
t.Fatal("expected server to be created, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// Test withToolRunLog wrapper succeeds
|
||||
func TestWithToolRunLogWrapper(t *testing.T) {
|
||||
called := false
|
||||
handler := withToolRunLog("test_tool", func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
|
||||
called = true
|
||||
return mcpgo.NewToolResultText("test result"), nil
|
||||
})
|
||||
result, err := handler(context.Background(), mcpgo.CallToolRequest{
|
||||
Params: mcpgo.CallToolParams{
|
||||
Name: "test_tool",
|
||||
Arguments: map[string]any{},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
if !called {
|
||||
t.Error("expected handler to be called")
|
||||
}
|
||||
if result == nil {
|
||||
t.Error("expected result to be non-nil")
|
||||
}
|
||||
}
|
||||
|
||||
// Test withToolRunLog wrapper with error result
|
||||
func TestWithToolRunLogWrapperErrorResult(t *testing.T) {
|
||||
handler := withToolRunLog("error_tool", func(ctx context.Context, req mcpgo.CallToolRequest) (*mcpgo.CallToolResult, error) {
|
||||
return mcpgo.NewToolResultError("test error"), nil
|
||||
})
|
||||
result, err := handler(context.Background(), mcpgo.CallToolRequest{
|
||||
Params: mcpgo.CallToolParams{
|
||||
Name: "error_tool",
|
||||
Arguments: map[string]any{},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
if result == nil {
|
||||
t.Error("expected result to be non-nil")
|
||||
}
|
||||
if !result.IsError {
|
||||
t.Error("expected IsError flag to be set")
|
||||
}
|
||||
}
|
||||
|
||||
// Test extractToolResultText with nil result
|
||||
func TestExtractToolResultTextNil(t *testing.T) {
|
||||
result := extractToolResultText(nil)
|
||||
if result != "tool error" {
|
||||
t.Errorf("expected 'tool error', got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
// Test extractToolResultText with empty content
|
||||
func TestExtractToolResultTextEmpty(t *testing.T) {
|
||||
toolResult := &mcpgo.CallToolResult{
|
||||
Content: []mcpgo.Content{},
|
||||
}
|
||||
result := extractToolResultText(toolResult)
|
||||
if result != "tool error" {
|
||||
t.Errorf("expected 'tool error', got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
// Test getMCPGoHTTPHandler initializes once
|
||||
func TestGetMCPGoHTTPHandlerOnce(t *testing.T) {
|
||||
handler1 := getMCPGoHTTPHandler()
|
||||
handler2 := getMCPGoHTTPHandler()
|
||||
if handler1 == nil {
|
||||
t.Error("expected handler1 to be non-nil")
|
||||
}
|
||||
if handler2 == nil {
|
||||
t.Error("expected handler2 to be non-nil")
|
||||
}
|
||||
// Both should be the same instance (sync.Once ensures this)
|
||||
if handler1 != handler2 {
|
||||
t.Error("expected handlers to be the same instance")
|
||||
}
|
||||
}
|
||||
|
||||
// Test resolveBaseURLFromContext with env var
|
||||
func TestResolveBaseURLFromContextEnv(t *testing.T) {
|
||||
t.Setenv("GINIMAGE_API_BASE_URL", "http://api.example.com")
|
||||
url := resolveBaseURLFromContext(context.Background())
|
||||
expected := "http://api.example.com"
|
||||
if url != expected {
|
||||
t.Errorf("expected %q, got %q", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
// Test resolveBaseURLFromContext without env var
|
||||
func TestResolveBaseURLFromContextDefault(t *testing.T) {
|
||||
t.Setenv("GINIMAGE_API_BASE_URL", "")
|
||||
t.Setenv("PORT", "")
|
||||
url := resolveBaseURLFromContext(context.Background())
|
||||
if url != "http://127.0.0.1:8080" {
|
||||
t.Errorf("expected default URL, got %q", url)
|
||||
}
|
||||
}
|
||||
|
||||
// Test resolveBaseURLFromContext with custom port
|
||||
func TestResolveBaseURLFromContextCustomPort(t *testing.T) {
|
||||
t.Setenv("GINIMAGE_API_BASE_URL", "")
|
||||
t.Setenv("PORT", "9090")
|
||||
url := resolveBaseURLFromContext(context.Background())
|
||||
expected := "http://127.0.0.1:9090"
|
||||
if url != expected {
|
||||
t.Errorf("expected %q, got %q", expected, url)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user