testutil

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package testutil provides shared testing utilities, mocks, and fixtures for use across the ai-provider-kit test suite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContains

func AssertContains(t *testing.T, s, substr string, msgAndArgs ...interface{})

AssertContains checks if a string contains a substring.

func AssertEmpty

func AssertEmpty(t *testing.T, object interface{}, msgAndArgs ...interface{})

AssertEmpty is a convenience wrapper around assert.Empty with helper marking.

func AssertEqual

func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

AssertEqual is a convenience wrapper around assert.Equal with helper marking.

func AssertError

func AssertError(t *testing.T, err error, msgAndArgs ...interface{})

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

func AssertErrorContains(t *testing.T, err error, substr string, msgAndArgs ...interface{})

AssertErrorContains checks that an error contains a specific substring.

func AssertFalse

func AssertFalse(t *testing.T, value bool, msgAndArgs ...interface{})

AssertFalse is a convenience wrapper around assert.False with helper marking.

func AssertGreaterThan

func AssertGreaterThan(t *testing.T, value, min int, msgAndArgs ...interface{})

AssertGreaterThan checks that a value is greater than the minimum.

func AssertInRange

func AssertInRange(t *testing.T, value, min, max int, msgAndArgs ...interface{})

AssertInRange checks that a value is within the specified range (inclusive).

func AssertJSONEqual

func AssertJSONEqual(t *testing.T, expected, actual string, msgAndArgs ...interface{})

AssertJSONEqual compares two JSON strings for equality, ignoring formatting differences.

func AssertLen

func AssertLen(t *testing.T, object interface{}, length int, msgAndArgs ...interface{})

AssertLen checks that the length of an object is equal to the expected length.

func AssertLessThan

func AssertLessThan(t *testing.T, value, max int, msgAndArgs ...interface{})

AssertLessThan checks that a value is less than the maximum.

func AssertNil

func AssertNil(t *testing.T, object interface{}, msgAndArgs ...interface{})

AssertNil is a convenience wrapper around assert.Nil with helper marking.

func AssertNoError

func AssertNoError(t *testing.T, err error, msgAndArgs ...interface{})

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

func AssertNotContains(t *testing.T, s, substr string, msgAndArgs ...interface{})

AssertNotContains checks if a string does not contain a substring.

func AssertNotEmpty

func AssertNotEmpty(t *testing.T, object interface{}, msgAndArgs ...interface{})

AssertNotEmpty is a convenience wrapper around assert.NotEmpty with helper marking.

func AssertNotEqual

func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

AssertNotEqual is a convenience wrapper around assert.NotEqual with helper marking.

func AssertNotNil

func AssertNotNil(t *testing.T, object interface{}, msgAndArgs ...interface{})

AssertNotNil is a convenience wrapper around assert.NotNil with helper marking.

func AssertNotPanics

func AssertNotPanics(t *testing.T, f func(), msgAndArgs ...interface{})

AssertNotPanics checks that a function does not panic.

func AssertPanics

func AssertPanics(t *testing.T, f func(), msgAndArgs ...interface{})

AssertPanics checks that a function panics.

func AssertStatusCode

func AssertStatusCode(t *testing.T, expected, actual int, msgAndArgs ...interface{})

AssertStatusCode checks that the HTTP status code matches the expected value.

func AssertStatusOK

func AssertStatusOK(t *testing.T, statusCode int, msgAndArgs ...interface{})

AssertStatusOK checks that the HTTP status code is 200 OK.

func AssertTrue

func AssertTrue(t *testing.T, value bool, msgAndArgs ...interface{})

AssertTrue is a convenience wrapper around assert.True with helper marking.

func BackgroundContext

func BackgroundContext(t *testing.T) context.Context

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

func ContextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc)

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

func ContextWithValue(t *testing.T, key, value interface{}) context.Context

ContextWithValue creates a test context with a key-value pair attached.

func LongTestContext

func LongTestContext(t *testing.T) (context.Context, context.CancelFunc)

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

func RequireEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

RequireEqual is like AssertEqual but stops test execution on failure.

func RequireError

func RequireError(t *testing.T, err error, msgAndArgs ...interface{})

RequireError is like AssertError but stops test execution if no error is present.

func RequireFalse

func RequireFalse(t *testing.T, value bool, msgAndArgs ...interface{})

RequireFalse is like AssertFalse but stops test execution on failure.

func RequireNoError

func RequireNoError(t *testing.T, err error, msgAndArgs ...interface{})

RequireNoError is like AssertNoError but stops test execution on failure.

func RequireNotEmpty

func RequireNotEmpty(t *testing.T, object interface{}, msgAndArgs ...interface{})

RequireNotEmpty is like AssertNotEmpty but stops test execution on failure.

func RequireNotNil

func RequireNotNil(t *testing.T, object interface{}, msgAndArgs ...interface{})

RequireNotNil is like AssertNotNil but stops test execution if object is nil.

func RequireTrue

func RequireTrue(t *testing.T, value bool, msgAndArgs ...interface{})

RequireTrue is like AssertTrue but stops test execution on failure.

func ShortTestContext

func ShortTestContext(t *testing.T) (context.Context, context.CancelFunc)

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

func TestContext(t *testing.T) (context.Context, context.CancelFunc)

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

func TestContextWithCancel(t *testing.T) (context.Context, context.CancelFunc)

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 (*ConfigurableMockProvider) CreateStream

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 (*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 (*ConfigurableMockProvider) GetModels

func (m *ConfigurableMockProvider) GetModels(ctx context.Context) ([]types.Model, error)

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 (*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

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

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.

Jump to

Keyboard shortcuts

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