Documentation
¶
Index ¶
- func AssertToolResultContains(t *testing.T, result *mcp.CallToolResult, expectedText string, ...)
- func AssertToolResultEqual(t *testing.T, expected, actual *mcp.CallToolResult)
- func CreateStandardTestContext(t *testing.T, timeoutSeconds int) (context.Context, context.CancelFunc)
- func CreateTestContext(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc)
- func GetStructuredContent(result *mcp.CallToolResult) map[string]any
- func GetTextContent(content []mcp.Content) string
- func RunConcurrentTest(t *testing.T, numGoroutines int, testFunc func(int) error)
- func ValidateToolCall(t *testing.T, client *mcp.ClientSession, ctx context.Context, toolName string, ...) *mcp.CallToolResult
- type MockComment
- type MockCommentUser
- type MockGiteaServer
- func (m *MockGiteaServer) AddComments(owner, repo string, comments []MockComment)
- func (m *MockGiteaServer) AddFile(owner, repo, ref, filepath string, content []byte)
- func (m *MockGiteaServer) AddIssue(owner, repo string, issue MockIssue)
- func (m *MockGiteaServer) AddIssues(owner, repo string, issues []MockIssue)
- func (m *MockGiteaServer) AddPullRequests(owner, repo string, pullRequests []MockPullRequest)
- func (m *MockGiteaServer) SetForbiddenCommentEdit(commentID int)
- func (m *MockGiteaServer) SetNotFoundRepo(owner, repo string)
- func (m *MockGiteaServer) SetServerErrorCommentEdit(commentID int)
- func (m *MockGiteaServer) URL() string
- type MockIssue
- type MockPullRequest
- type TestServer
- func (ts *TestServer) CallToolWithValidation(ctx context.Context, toolName string, arguments map[string]any) (*mcp.CallToolResult, error)
- func (ts *TestServer) Client() *mcp.ClientSession
- func (ts *TestServer) Initialize() error
- func (ts *TestServer) IsRunning() bool
- func (ts *TestServer) Start() error
- func (ts *TestServer) ValidateErrorResult(result *mcp.CallToolResult, expectedErrorText string, t *testing.T) bool
- func (ts *TestServer) ValidateSuccessResult(result *mcp.CallToolResult, expectedSuccessText string, t *testing.T) bool
- func (ts *TestServer) ValidateToolResult(expected, actual *mcp.CallToolResult, t *testing.T) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertToolResultContains ¶
func AssertToolResultContains(t *testing.T, result *mcp.CallToolResult, expectedText string, expectError bool)
AssertToolResultContains validates that a tool result contains expected text
Parameters:
- t: *testing.T for test context and error reporting
- result: *mcp.CallToolResult the result to validate
- expectedText: string the expected text to contain
- expectError: bool whether to expect an error result
Example usage:
AssertToolResultContains(t, result, "Comment created successfully", false)
func AssertToolResultEqual ¶
func AssertToolResultEqual(t *testing.T, expected, actual *mcp.CallToolResult)
AssertToolResultEqual compares two tool results for equality with detailed error reporting
Parameters:
- t: *testing.T for test context and error reporting
- expected: *mcp.CallToolResult the expected result
- actual: *mcp.CallToolResult the actual result
Example usage:
AssertToolResultEqual(t, tc.expect, result)
func CreateStandardTestContext ¶
func CreateStandardTestContext(t *testing.T, timeoutSeconds int) (context.Context, context.CancelFunc)
CreateStandardTestContext creates a standardized test context with proper timeout and cleanup
Parameters:
- t: *testing.T for test context
- timeoutSeconds: int timeout in seconds (defaults to 5 if 0)
Returns:
- context.Context: the created context
- context.CancelFunc: the cancel function for cleanup
Example usage:
ctx, cancel := CreateStandardTestContext(t, 10) defer cancel()
func CreateTestContext ¶
CreateTestContext creates a standardized test context with timeout
Parameters:
- t: *testing.T for test context
- timeout: time.Duration for the context timeout (defaults to 5 seconds if 0)
Returns:
- context.Context: the created context
- context.CancelFunc: the cancel function for cleanup
Example usage:
ctx, cancel := CreateTestContext(t, 10*time.Second) defer cancel()
func GetStructuredContent ¶
func GetStructuredContent(result *mcp.CallToolResult) map[string]any
GetStructuredContent extracts structured content from MCP result
Parameters:
- result: *mcp.CallToolResult the result to extract structured content from
Returns:
- map[string]any: the structured content, or nil if not found
Example usage:
structured := GetStructuredContent(result)
if comment, ok := structured["comment"].(map[string]any); ok {
t.Logf("Comment ID: %v", comment["id"])
}
func GetTextContent ¶
GetTextContent extracts text content from MCP content slice
Parameters:
- content: []mcp.Content the content slice to extract from
Returns:
- string: the extracted text content, or empty string if not found
Example usage:
text := GetTextContent(result.Content)
if strings.Contains(text, "success") {
t.Log("Operation succeeded")
}
func RunConcurrentTest ¶
RunConcurrentTest executes a function concurrently with proper synchronization and error handling
Parameters:
- t: *testing.T for test context and error reporting
- numGoroutines: int number of goroutines to run
- testFunc: func(int) error function to execute in each goroutine
Example usage:
RunConcurrentTest(t, 3, func(id int) error {
_, err := ts.Client().CallTool(ctx, &mcp.CallToolParams{
Name: "tool_name",
Arguments: map[string]any{
"id": id,
},
})
return err
})
func ValidateToolCall ¶
func ValidateToolCall(t *testing.T, client *mcp.ClientSession, ctx context.Context, toolName string, arguments map[string]any, expectedError string) *mcp.CallToolResult
ValidateToolCall executes a tool call with standardized validation and error handling
Parameters:
- t: *testing.T for test context and error reporting
- client: *mcp.ClientSession the MCP client session
- ctx: context.Context for the tool call
- toolName: string name of the tool to call
- arguments: map[string]any arguments for the tool call
- expectedError: string expected error text (empty for success case)
Returns:
- *mcp.CallToolResult: the result of the tool call (nil on error)
Example usage:
result := ValidateToolCall(t, client, ctx, "issue_list", map[string]any{
"repository": "testuser/testrepo",
}, "")
if result != nil {
t.Log("Tool call succeeded")
}
Types ¶
type MockComment ¶
type MockComment struct {
ID int `json:"id"`
Content string `json:"body"`
Author string `json:"user"`
Created string `json:"created_at"`
Updated string `json:"updated_at"`
}
MockComment represents a mock comment for testing
type MockCommentUser ¶
MockCommentUser represents the user who created the comment
type MockGiteaServer ¶
type MockGiteaServer struct {
// contains filtered or unexported fields
}
MockGiteaServer represents a mock Gitea API server for testing
func NewMockGiteaServer ¶
func NewMockGiteaServer(t *testing.T) *MockGiteaServer
NewMockGiteaServer creates a new mock Gitea server
func (*MockGiteaServer) AddComments ¶
func (m *MockGiteaServer) AddComments(owner, repo string, comments []MockComment)
AddComments adds mock comments for a repository
func (*MockGiteaServer) AddFile ¶
func (m *MockGiteaServer) AddFile(owner, repo, ref, filepath string, content []byte)
AddFile adds mock file content for a repository
func (*MockGiteaServer) AddIssue ¶
func (m *MockGiteaServer) AddIssue(owner, repo string, issue MockIssue)
AddIssue adds a single mock issue for a repository
func (*MockGiteaServer) AddIssues ¶
func (m *MockGiteaServer) AddIssues(owner, repo string, issues []MockIssue)
AddIssues adds mock issues for a repository
func (*MockGiteaServer) AddPullRequests ¶
func (m *MockGiteaServer) AddPullRequests(owner, repo string, pullRequests []MockPullRequest)
AddPullRequests adds mock pull requests for a repository
func (*MockGiteaServer) SetForbiddenCommentEdit ¶
func (m *MockGiteaServer) SetForbiddenCommentEdit(commentID int)
SetForbiddenCommentEdit marks a comment ID as forbidden (will return 403)
func (*MockGiteaServer) SetNotFoundRepo ¶
func (m *MockGiteaServer) SetNotFoundRepo(owner, repo string)
SetNotFoundRepo marks a repository as not found (will return 404)
func (*MockGiteaServer) SetServerErrorCommentEdit ¶
func (m *MockGiteaServer) SetServerErrorCommentEdit(commentID int)
SetServerErrorCommentEdit marks a comment ID as server error (will return 500)
func (*MockGiteaServer) URL ¶
func (m *MockGiteaServer) URL() string
URL returns the mock server URL
type MockIssue ¶
type MockIssue struct {
Index int `json:"index"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Updated string `json:"updated_at"`
Created string `json:"created_at"`
}
MockIssue represents a mock issue for testing
type MockPullRequest ¶
type MockPullRequest struct {
ID int `json:"id"`
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
BaseRef string `json:"base_ref"`
UpdatedAt string `json:"updated_at"`
}
MockPullRequest represents a mock pull request for testing
type TestServer ¶
type TestServer struct {
// contains filtered or unexported fields
}
TestServer represents a test harness for running the MCP server
func NewTestServer ¶
NewTestServer creates a new TestServer instance with standardized setup
Parameters:
- t: testing.T for test context and cleanup
- ctx: context.Context for the test execution (uses t.Context() if nil)
- env: map[string]string for environment variables (FORGEJO_REMOTE_URL, FORGEJO_AUTH_TOKEN)
Returns:
- *TestServer: configured test server instance ready for use
Example usage:
mock := NewMockGiteaServer(t)
ts := NewTestServer(t, ctx, map[string]string{
"FORGEJO_REMOTE_URL": mock.URL(),
"FORGEJO_AUTH_TOKEN": "mock-token",
})
if err := ts.Initialize(); err != nil {
t.Fatal(err)
}
client := ts.Client()
func NewTestServerWithDebug ¶
func NewTestServerWithDebug(t *testing.T, ctx context.Context, env map[string]string, debug bool) *TestServer
NewTestServerWithDebug creates a new test server with optional debug mode
func (*TestServer) CallToolWithValidation ¶
func (ts *TestServer) CallToolWithValidation(ctx context.Context, toolName string, arguments map[string]any) (*mcp.CallToolResult, error)
CallToolWithValidation calls a tool with standardized error handling and validation
Parameters:
- ctx: context.Context for the tool call
- toolName: string name of the tool to call
- arguments: map[string]any arguments for the tool call
Returns:
- *mcp.CallToolResult: the result of the tool call
- error: any error that occurred during the call
Example usage:
result, err := ts.CallToolWithValidation(ctx, "issue_list", map[string]any{
"repository": "testuser/testrepo",
"limit": 10,
})
func (*TestServer) Client ¶
func (ts *TestServer) Client() *mcp.ClientSession
Client returns the MCP client session for tool calls
func (*TestServer) Initialize ¶
func (ts *TestServer) Initialize() error
Initialize initializes the MCP client for communication with the server
func (*TestServer) IsRunning ¶
func (ts *TestServer) IsRunning() bool
IsRunning checks if the server process is running
func (*TestServer) Start ¶
func (ts *TestServer) Start() error
Start starts the server process with error handling
func (*TestServer) ValidateErrorResult ¶
func (ts *TestServer) ValidateErrorResult(result *mcp.CallToolResult, expectedErrorText string, t *testing.T) bool
ValidateErrorResult validates that a tool result contains an expected error
Parameters:
- result: *mcp.CallToolResult the result to validate
- expectedErrorText: string the expected error text (partial match allowed)
- t: *testing.T for reporting errors
Returns:
- bool: true if result contains expected error, false otherwise
Example usage:
if !ts.ValidateErrorResult(result, "Invalid request", t) {
t.Errorf("Expected error result not found")
}
func (*TestServer) ValidateSuccessResult ¶
func (ts *TestServer) ValidateSuccessResult(result *mcp.CallToolResult, expectedSuccessText string, t *testing.T) bool
func (*TestServer) ValidateToolResult ¶
func (ts *TestServer) ValidateToolResult(expected, actual *mcp.CallToolResult, t *testing.T) bool
ValidateToolResult compares an actual tool result with expected result using deep equality
Parameters:
- expected: *mcp.CallToolResult the expected result
- actual: *mcp.CallToolResult the actual result
- t: *testing.T for reporting errors
Returns:
- bool: true if results match, false otherwise
Example usage:
if !ts.ValidateToolResult(tc.expect, result, t) {
t.Errorf("Tool result validation failed")
}