Documentation
¶
Overview ¶
Package testutil provides shared testing utilities, mocks, and fixtures for use across the ai-provider-kit test suite.
Index ¶
- func AssertContains(t *testing.T, s, substr string, msgAndArgs ...interface{})
- func AssertEmpty(t *testing.T, object interface{}, msgAndArgs ...interface{})
- func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})
- func AssertError(t *testing.T, err error, msgAndArgs ...interface{})
- func AssertErrorContains(t *testing.T, err error, substr string, msgAndArgs ...interface{})
- func AssertFalse(t *testing.T, value bool, msgAndArgs ...interface{})
- func AssertGreaterThan(t *testing.T, value, min int, msgAndArgs ...interface{})
- func AssertInRange(t *testing.T, value, min, max int, msgAndArgs ...interface{})
- func AssertJSONEqual(t *testing.T, expected, actual string, msgAndArgs ...interface{})
- func AssertLen(t *testing.T, object interface{}, length int, msgAndArgs ...interface{})
- func AssertLessThan(t *testing.T, value, max int, msgAndArgs ...interface{})
- func AssertNil(t *testing.T, object interface{}, msgAndArgs ...interface{})
- func AssertNoError(t *testing.T, err error, msgAndArgs ...interface{})
- func AssertNotContains(t *testing.T, s, substr string, msgAndArgs ...interface{})
- func AssertNotEmpty(t *testing.T, object interface{}, msgAndArgs ...interface{})
- func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})
- func AssertNotNil(t *testing.T, object interface{}, msgAndArgs ...interface{})
- func AssertNotPanics(t *testing.T, f func(), msgAndArgs ...interface{})
- func AssertPanics(t *testing.T, f func(), msgAndArgs ...interface{})
- func AssertStatusCode(t *testing.T, expected, actual int, msgAndArgs ...interface{})
- func AssertStatusOK(t *testing.T, statusCode int, msgAndArgs ...interface{})
- func AssertTrue(t *testing.T, value bool, msgAndArgs ...interface{})
- func BackgroundContext(t *testing.T) context.Context
- func ContextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc)
- func ContextWithValue(t *testing.T, key, value interface{}) context.Context
- func LongTestContext(t *testing.T) (context.Context, context.CancelFunc)
- func RequireEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})
- func RequireError(t *testing.T, err error, msgAndArgs ...interface{})
- func RequireFalse(t *testing.T, value bool, msgAndArgs ...interface{})
- func RequireNoError(t *testing.T, err error, msgAndArgs ...interface{})
- func RequireNotEmpty(t *testing.T, object interface{}, msgAndArgs ...interface{})
- func RequireNotNil(t *testing.T, object interface{}, msgAndArgs ...interface{})
- func RequireTrue(t *testing.T, value bool, msgAndArgs ...interface{})
- func ShortTestContext(t *testing.T) (context.Context, context.CancelFunc)
- func TestContext(t *testing.T) (context.Context, context.CancelFunc)
- func TestContextWithCancel(t *testing.T) (context.Context, context.CancelFunc)
- func TestContextWithDeadline(t *testing.T, deadline time.Time) (context.Context, context.CancelFunc)
- func TestContextWithTimeout(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc)
- type ConfigurableMockProvider
- func (m *ConfigurableMockProvider) Authenticate(ctx context.Context, config types.AuthConfig) error
- func (m *ConfigurableMockProvider) Configure(config types.ProviderConfig) error
- func (m *ConfigurableMockProvider) CreateResponse(ctx context.Context, request types.StandardRequest) (*types.StandardResponse, error)
- func (m *ConfigurableMockProvider) CreateStream(ctx context.Context, request types.StandardRequest) (types.StandardStream, error)
- func (m *ConfigurableMockProvider) Description() string
- func (m *ConfigurableMockProvider) GenerateChatCompletion(ctx context.Context, options types.GenerateOptions) (types.ChatCompletionStream, error)
- func (m *ConfigurableMockProvider) GetAuthenticateCallCount() int
- func (m *ConfigurableMockProvider) GetConfig() types.ProviderConfig
- func (m *ConfigurableMockProvider) GetDefaultModel() string
- func (m *ConfigurableMockProvider) GetGenerateChatCallCount() int
- func (m *ConfigurableMockProvider) GetHealthCheckCallCount() int
- func (m *ConfigurableMockProvider) GetMetrics() types.ProviderMetrics
- func (m *ConfigurableMockProvider) GetModels(ctx context.Context) ([]types.Model, error)
- func (m *ConfigurableMockProvider) GetToolFormat() string
- func (m *ConfigurableMockProvider) HealthCheck(ctx context.Context) error
- func (m *ConfigurableMockProvider) InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)
- func (m *ConfigurableMockProvider) IsAuthenticated() bool
- func (m *ConfigurableMockProvider) Logout(ctx context.Context) error
- func (m *ConfigurableMockProvider) Name() string
- func (m *ConfigurableMockProvider) SetAuthenticateError(err error)
- func (m *ConfigurableMockProvider) SetChatCompletionStream(stream types.ChatCompletionStream)
- func (m *ConfigurableMockProvider) SetGenerateError(err error)
- func (m *ConfigurableMockProvider) SetGetModelsError(err error)
- func (m *ConfigurableMockProvider) SetHealthCheckError(err error)
- func (m *ConfigurableMockProvider) SetModels(models []types.Model)
- func (m *ConfigurableMockProvider) SupportsResponsesAPI() bool
- func (m *ConfigurableMockProvider) SupportsStreaming() bool
- func (m *ConfigurableMockProvider) SupportsToolCalling() bool
- func (m *ConfigurableMockProvider) Type() types.ProviderType
- type ConfigurableMockStream
- type TestFixtures
- func (f *TestFixtures) NewGenerateOptions(messages []types.ChatMessage, model string) types.GenerateOptions
- func (f *TestFixtures) NewMessage(role, content string) types.ChatMessage
- func (f *TestFixtures) NewProviderConfig(providerType types.ProviderType, apiKey string) types.ProviderConfig
- func (f *TestFixtures) NewToolCall(id, name, arguments string) types.ToolCall
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertContains ¶
AssertContains checks if a string contains a substring.
func AssertEmpty ¶
AssertEmpty is a convenience wrapper around assert.Empty with helper marking.
func AssertEqual ¶
AssertEqual is a convenience wrapper around assert.Equal with helper marking.
func AssertError ¶
AssertError is a convenience wrapper that fails the test if err is nil. It provides a cleaner test output with optional custom messages.
func AssertErrorContains ¶
AssertErrorContains checks that an error contains a specific substring.
func AssertFalse ¶
AssertFalse is a convenience wrapper around assert.False with helper marking.
func AssertGreaterThan ¶
AssertGreaterThan checks that a value is greater than the minimum.
func AssertInRange ¶
AssertInRange checks that a value is within the specified range (inclusive).
func AssertJSONEqual ¶
AssertJSONEqual compares two JSON strings for equality, ignoring formatting differences.
func AssertLessThan ¶
AssertLessThan checks that a value is less than the maximum.
func AssertNoError ¶
AssertNoError is a convenience wrapper that fails the test if err is not nil. It provides a cleaner test output with optional custom messages.
func AssertNotContains ¶
AssertNotContains checks if a string does not contain a substring.
func AssertNotEmpty ¶
AssertNotEmpty is a convenience wrapper around assert.NotEmpty with helper marking.
func AssertNotEqual ¶
AssertNotEqual is a convenience wrapper around assert.NotEqual with helper marking.
func AssertNotNil ¶
AssertNotNil is a convenience wrapper around assert.NotNil with helper marking.
func AssertNotPanics ¶
AssertNotPanics checks that a function does not panic.
func AssertPanics ¶
AssertPanics checks that a function panics.
func AssertStatusCode ¶
AssertStatusCode checks that the HTTP status code matches the expected value.
func AssertStatusOK ¶
AssertStatusOK checks that the HTTP status code is 200 OK.
func AssertTrue ¶
AssertTrue is a convenience wrapper around assert.True with helper marking.
func BackgroundContext ¶
BackgroundContext returns a background context for tests that don't need timeouts. This is useful for tests that use manual cancellation or don't need timeout protection.
func ContextWithTimeout ¶
ContextWithTimeout is a non-test-specific helper that creates a context with timeout. Unlike TestContext, this doesn't require a *testing.T parameter and can be used in helper functions or test utilities.
func ContextWithValue ¶
ContextWithValue creates a test context with a key-value pair attached.
func LongTestContext ¶
LongTestContext creates a context with a long timeout (2 minutes) for slow tests. Returns a context and a cancel function that should be deferred.
func RequireEqual ¶
RequireEqual is like AssertEqual but stops test execution on failure.
func RequireError ¶
RequireError is like AssertError but stops test execution if no error is present.
func RequireFalse ¶
RequireFalse is like AssertFalse but stops test execution on failure.
func RequireNoError ¶
RequireNoError is like AssertNoError but stops test execution on failure.
func RequireNotEmpty ¶
RequireNotEmpty is like AssertNotEmpty but stops test execution on failure.
func RequireNotNil ¶
RequireNotNil is like AssertNotNil but stops test execution if object is nil.
func RequireTrue ¶
RequireTrue is like AssertTrue but stops test execution on failure.
func ShortTestContext ¶
ShortTestContext creates a context with a short timeout (5 seconds) for quick tests. Returns a context and a cancel function that should be deferred.
func TestContext ¶
TestContext creates a context with a reasonable timeout for tests. The default timeout is 30 seconds, which should be sufficient for most tests. Returns a context and a cancel function that should be deferred.
func TestContextWithCancel ¶
TestContextWithCancel creates a cancellable context for tests. Returns a context and a cancel function that should be deferred.
func TestContextWithDeadline ¶
func TestContextWithDeadline(t *testing.T, deadline time.Time) (context.Context, context.CancelFunc)
TestContextWithDeadline creates a context with a specific deadline for tests. Returns a context and a cancel function that should be deferred.
func TestContextWithTimeout ¶
func TestContextWithTimeout(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc)
TestContextWithTimeout creates a context with a custom timeout for tests. Returns a context and a cancel function that should be deferred.
Types ¶
type ConfigurableMockProvider ¶
type ConfigurableMockProvider struct {
// contains filtered or unexported fields
}
ConfigurableMockProvider is a mock Provider implementation with configurable behavior. It allows tests to simulate various provider responses and scenarios.
func NewConfigurableMockProvider ¶
func NewConfigurableMockProvider(name string, providerType types.ProviderType) *ConfigurableMockProvider
NewConfigurableMockProvider creates a new mock provider with default settings.
func (*ConfigurableMockProvider) Authenticate ¶
func (m *ConfigurableMockProvider) Authenticate(ctx context.Context, config types.AuthConfig) error
func (*ConfigurableMockProvider) Configure ¶
func (m *ConfigurableMockProvider) Configure(config types.ProviderConfig) error
func (*ConfigurableMockProvider) CreateResponse ¶
func (m *ConfigurableMockProvider) CreateResponse(ctx context.Context, request types.StandardRequest) (*types.StandardResponse, error)
func (*ConfigurableMockProvider) CreateStream ¶
func (m *ConfigurableMockProvider) CreateStream(ctx context.Context, request types.StandardRequest) (types.StandardStream, error)
func (*ConfigurableMockProvider) Description ¶
func (m *ConfigurableMockProvider) Description() string
func (*ConfigurableMockProvider) GenerateChatCompletion ¶
func (m *ConfigurableMockProvider) GenerateChatCompletion(ctx context.Context, options types.GenerateOptions) (types.ChatCompletionStream, error)
func (*ConfigurableMockProvider) GetAuthenticateCallCount ¶
func (m *ConfigurableMockProvider) GetAuthenticateCallCount() int
GetAuthenticateCallCount returns the number of times Authenticate was called
func (*ConfigurableMockProvider) GetConfig ¶
func (m *ConfigurableMockProvider) GetConfig() types.ProviderConfig
func (*ConfigurableMockProvider) GetDefaultModel ¶
func (m *ConfigurableMockProvider) GetDefaultModel() string
func (*ConfigurableMockProvider) GetGenerateChatCallCount ¶
func (m *ConfigurableMockProvider) GetGenerateChatCallCount() int
GetGenerateChatCallCount returns the number of times GenerateChatCompletion was called
func (*ConfigurableMockProvider) GetHealthCheckCallCount ¶
func (m *ConfigurableMockProvider) GetHealthCheckCallCount() int
GetHealthCheckCallCount returns the number of times HealthCheck was called
func (*ConfigurableMockProvider) GetMetrics ¶
func (m *ConfigurableMockProvider) GetMetrics() types.ProviderMetrics
func (*ConfigurableMockProvider) GetToolFormat ¶
func (m *ConfigurableMockProvider) GetToolFormat() string
func (*ConfigurableMockProvider) HealthCheck ¶
func (m *ConfigurableMockProvider) HealthCheck(ctx context.Context) error
func (*ConfigurableMockProvider) InvokeServerTool ¶
func (m *ConfigurableMockProvider) InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)
func (*ConfigurableMockProvider) IsAuthenticated ¶
func (m *ConfigurableMockProvider) IsAuthenticated() bool
func (*ConfigurableMockProvider) Logout ¶
func (m *ConfigurableMockProvider) Logout(ctx context.Context) error
func (*ConfigurableMockProvider) Name ¶
func (m *ConfigurableMockProvider) Name() string
func (*ConfigurableMockProvider) SetAuthenticateError ¶
func (m *ConfigurableMockProvider) SetAuthenticateError(err error)
SetAuthenticateError configures the provider to return an error on Authenticate
func (*ConfigurableMockProvider) SetChatCompletionStream ¶
func (m *ConfigurableMockProvider) SetChatCompletionStream(stream types.ChatCompletionStream)
SetChatCompletionStream configures the stream returned by GenerateChatCompletion
func (*ConfigurableMockProvider) SetGenerateError ¶
func (m *ConfigurableMockProvider) SetGenerateError(err error)
SetGenerateError configures the provider to return an error on GenerateChatCompletion
func (*ConfigurableMockProvider) SetGetModelsError ¶
func (m *ConfigurableMockProvider) SetGetModelsError(err error)
SetGetModelsError configures the provider to return an error on GetModels
func (*ConfigurableMockProvider) SetHealthCheckError ¶
func (m *ConfigurableMockProvider) SetHealthCheckError(err error)
SetHealthCheckError configures the provider to return an error on HealthCheck
func (*ConfigurableMockProvider) SetModels ¶
func (m *ConfigurableMockProvider) SetModels(models []types.Model)
SetModels configures the list of models returned by GetModels
func (*ConfigurableMockProvider) SupportsResponsesAPI ¶
func (m *ConfigurableMockProvider) SupportsResponsesAPI() bool
func (*ConfigurableMockProvider) SupportsStreaming ¶
func (m *ConfigurableMockProvider) SupportsStreaming() bool
func (*ConfigurableMockProvider) SupportsToolCalling ¶
func (m *ConfigurableMockProvider) SupportsToolCalling() bool
func (*ConfigurableMockProvider) Type ¶
func (m *ConfigurableMockProvider) Type() types.ProviderType
type ConfigurableMockStream ¶
type ConfigurableMockStream struct {
// contains filtered or unexported fields
}
ConfigurableMockStream is a mock ChatCompletionStream implementation with configurable behavior.
func NewConfigurableMockStream ¶
func NewConfigurableMockStream(chunks []types.ChatCompletionChunk) *ConfigurableMockStream
NewConfigurableMockStream creates a new mock stream with the given chunks.
func (*ConfigurableMockStream) Close ¶
func (s *ConfigurableMockStream) Close() error
Close resets the stream to the beginning.
func (*ConfigurableMockStream) Next ¶
func (s *ConfigurableMockStream) Next() (types.ChatCompletionChunk, error)
Next returns the next chunk from the stream.
func (*ConfigurableMockStream) Reset ¶
func (s *ConfigurableMockStream) Reset()
Reset resets the stream to the beginning without locking (useful in tests).
func (*ConfigurableMockStream) SetError ¶
func (s *ConfigurableMockStream) SetError(err error)
SetError configures the stream to return an error on the next Next() call.
type TestFixtures ¶
type TestFixtures struct {
// Provider configurations
OpenAIConfig types.ProviderConfig
AnthropicConfig types.ProviderConfig
GeminiConfig types.ProviderConfig
QwenConfig types.ProviderConfig
CerebrasConfig types.ProviderConfig
// Auth configurations
APIKeyAuth types.AuthConfig
OAuthAuth types.AuthConfig
// Messages
SimpleMessage types.ChatMessage
SystemMessage types.ChatMessage
ToolCallMessage types.ChatMessage
ToolResponseMessage types.ChatMessage
MultiTurnMessages []types.ChatMessage
// Tools
WeatherTool types.Tool
CalculatorTool types.Tool
SearchTool types.Tool
AllTools []types.Tool
// Tool calls
WeatherToolCall types.ToolCall
CalculatorToolCall types.ToolCall
// Models
OpenAIModels []types.Model
AnthropicModels []types.Model
GeminiModels []types.Model
// Responses
StandardResponse types.StandardResponse
StreamChunks []types.ChatCompletionChunk
StandardStreamChunks []types.StandardStreamChunk
// Generate options
BasicGenerateOptions types.GenerateOptions
StreamingGenerateOptions types.GenerateOptions
ToolCallingGenerateOptions types.GenerateOptions
// Standard requests
BasicRequest types.StandardRequest
StreamingRequest types.StandardRequest
ToolCallingRequest types.StandardRequest
}
TestFixtures provides common test data and fixtures for use across tests.
func NewTestFixtures ¶
func NewTestFixtures() *TestFixtures
NewTestFixtures creates a new TestFixtures instance with all standard test data.
func (*TestFixtures) NewGenerateOptions ¶
func (f *TestFixtures) NewGenerateOptions(messages []types.ChatMessage, model string) types.GenerateOptions
NewGenerateOptions creates new generate options with sensible defaults.
func (*TestFixtures) NewMessage ¶
func (f *TestFixtures) NewMessage(role, content string) types.ChatMessage
NewMessage creates a new chat message with the given role and content.
func (*TestFixtures) NewProviderConfig ¶
func (f *TestFixtures) NewProviderConfig(providerType types.ProviderType, apiKey string) types.ProviderConfig
NewProviderConfig creates a custom provider configuration based on a template.
func (*TestFixtures) NewToolCall ¶
func (f *TestFixtures) NewToolCall(id, name, arguments string) types.ToolCall
NewToolCall creates a new tool call with the given parameters.