testutil

package
v1.6.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2025 License: MIT Imports: 15 Imported by: 0

README

Test Utilities

This package provides shared testing utilities for the MCP server tests in both twprojects and twdesk packages.

Overview

The testutil package centralizes common test infrastructure to avoid code duplication and provide consistent testing patterns across all MCP tool implementations.

Usage

For Teamwork Projects Tests
import "github.com/teamwork/mcp/internal/testutil"

func TestSomething(t *testing.T) {
    mcpServer := testutil.ProjectsMCPServerMock(t, http.StatusOK, []byte(`{"id": 123}`))

    // Use testutil.ExecuteToolRequest for simple cases
    testutil.ExecuteToolRequest(t, mcpServer, "twprojects-get_comment", map[string]any{
        "id": float64(123),
    })
}
For Teamwork Desk Tests
import "github.com/teamwork/mcp/internal/testutil"

func TestSomething(t *testing.T) {
    mcpServer, cleanup := testutil.DeskMCPServerMock(t, http.StatusOK, []byte(`{"ticket_priority": {"id": 123}}`))
    defer cleanup()

    // Use testutil.ExecuteToolRequest for simple cases
    testutil.ExecuteToolRequest(t, mcpServer, "twdesk-get_priority", map[string]any{
        "id": 123,
    })
}

Components

  • ProjectsMCPServerMock: Creates a mock MCP server for testing twprojects tools
  • DeskMCPServerMock: Creates a mock MCP server for testing twdesk tools (with cleanup function)
  • CheckMessage: Validates that a tool execution was successful
  • ExecuteToolRequest: Helper to execute a tool request and validate the response
  • ToolRequest: Type alias for tool request structures

Migration Guide

To migrate existing tests to use the shared infrastructure:

  1. Replace your local mcpServerMock function calls with testutil.ProjectsMCPServerMock or testutil.DeskMCPServerMock
  2. Replace your local checkMessage function with testutil.CheckMessage
  3. Replace your local toolRequest type with testutil.ToolRequest
  4. Update imports to include "github.com/teamwork/mcp/internal/testutil"

This approach ensures consistency across all test suites and makes it easier to add new tool test suites in the future.

Documentation

Overview

Package testutil provides shared testing utilities for MCP server tests.

Package testutil provides schema validation helpers for testing MCP tools

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckMessage

func CheckMessage(t *testing.T, result mcp.Result)

CheckMessage validates that a message represents a successful tool execution

func DeskClientMock

func DeskClientMock(status int, response []byte) (*deskclient.Client, *httptest.Server)

DeskClientMock creates a mock desk client with a test server

func DeskMCPServerMock

func DeskMCPServerMock(t *testing.T, status int, response []byte) (*mcp.Server, func())

DeskMCPServerMock creates a mock MCP server for twdesk testing

func ExecuteToolRequest

func ExecuteToolRequest(
	t *testing.T,
	mcpServer *mcp.Server,
	toolName string,
	args map[string]any,
	optFuncs ...ExecuteToolRequestOption,
)

ExecuteToolRequest executes a tool request and validates the response

func GetInvalidTestData added in v1.5.8

func GetInvalidTestData() map[string]map[string]map[string]any

GetInvalidTestData returns invalid test data for all tools

func GetValidTestData added in v1.5.8

func GetValidTestData() map[string]map[string]map[string]any

GetValidTestData returns valid test data for all tools

func ProjectsEngineMock

func ProjectsEngineMock(status int, response []byte) *twapi.Engine

ProjectsEngineMock creates a mock twapi.Engine with the given HTTP response

func ProjectsMCPServerMock

func ProjectsMCPServerMock(t *testing.T, status int, response []byte) *mcp.Server

ProjectsMCPServerMock creates a mock MCP server for twprojects testing

Types

type ExecuteToolRequestOption added in v1.6.3

type ExecuteToolRequestOption func(*ExecuteToolRequestOptions)

ExecuteToolRequestOption is a function that modifies ExecuteToolRequestOptions.

func ExecuteToolRequestWithCheckMessage added in v1.6.3

func ExecuteToolRequestWithCheckMessage(f func(t *testing.T, result mcp.Result)) ExecuteToolRequestOption

ExecuteToolRequestWithCheckMessage executes a tool request and validates the response with a custom check function. Any nil function will be ignored.

type ExecuteToolRequestOptions added in v1.6.3

type ExecuteToolRequestOptions struct {
	// contains filtered or unexported fields
}

ExecuteToolRequestOptions represents options for ExecuteToolRequest.

type ProjectsSessionMock

type ProjectsSessionMock struct{}

ProjectsSessionMock implements a mock session for twprojects testing

func (ProjectsSessionMock) Authenticate

Authenticate implements the Authenticate method for ProjectsSessionMock

func (ProjectsSessionMock) Server

func (s ProjectsSessionMock) Server() string

Server implements the Server method for ProjectsSessionMock

type SchemaValidationTestSuite added in v1.5.8

type SchemaValidationTestSuite struct {
	// contains filtered or unexported fields
}

SchemaValidationTestSuite provides a comprehensive test suite for validating MCP tool JSON schemas

func NewSchemaValidationTestSuite added in v1.5.8

func NewSchemaValidationTestSuite() *SchemaValidationTestSuite

NewSchemaValidationTestSuite creates a new test suite with all twdesk tools

func (*SchemaValidationTestSuite) GetTool added in v1.5.8

func (s *SchemaValidationTestSuite) GetTool(toolName string) (toolsets.ToolWrapper, bool)

GetTool returns a tool by name if it exists

func (*SchemaValidationTestSuite) RunAllSchemaValidationTests added in v1.5.8

func (s *SchemaValidationTestSuite) RunAllSchemaValidationTests(t *testing.T)

RunAllSchemaValidationTests runs comprehensive schema validation tests for all tools

func (*SchemaValidationTestSuite) RunToolSchemaValidation added in v1.5.8

func (s *SchemaValidationTestSuite) RunToolSchemaValidation(t *testing.T, toolName string, tool toolsets.ToolWrapper)

RunToolSchemaValidation runs schema validation tests for a single tool (exported version)

type ToolRequest

type ToolRequest struct {
	mcp.CallToolRequest

	JSONRPC string `json:"jsonrpc"`
	ID      int64  `json:"id"`
}

ToolRequest represents a tool request for testing

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL