testutil

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package testutil provides deterministic offline models and event fixtures.

Index

Constants

This section is empty.

Variables

View Source
var ErrMockNoMatch = errors.New("mock model: no matching response")

ErrMockNoMatch means no rule matched and no fallback response was configured.

Functions

func AssertResultWarning

func AssertResultWarning(t *testing.T, warnings []stream.Warning, want stream.Warning)

AssertResultWarning is an alias for AssertWarning, named to match non-language-model result types (ImageResult.Warnings, etc.).

func AssertWarning

func AssertWarning(t *testing.T, warnings []stream.Warning, want stream.Warning)

AssertWarning fails the test if warnings does not contain want. Matches on Type + Feature + Message; Details is ignored.

func CollectStream

func CollectStream(t *testing.T, ch <-chan stream.Event, err error) []stream.Event

CollectStream drains a stream channel into a slice.

func FindFirstStartEvent

func FindFirstStartEvent(t *testing.T, events []stream.Event) *stream.StartEvent

FindFirstStartEvent returns the first StartEvent in events. Fails the test if no StartEvent is present.

func MockErrorResponse

func MockErrorResponse(err error) []stream.Event

MockErrorResponse creates events for an error response.

func MockReasoningResponse

func MockReasoningResponse(reasoningText, text string, usage stream.Usage) []stream.Event

MockReasoningResponse creates events for a response with reasoning.

func MockStreamedTextResponse

func MockStreamedTextResponse(chunks []string, usage stream.Usage) []stream.Event

MockStreamedTextResponse creates events for a text response with multiple chunks.

func MockTextResponse

func MockTextResponse(text string, usage stream.Usage) []stream.Event

MockTextResponse creates events for a simple text response.

func MockTextWithToolCallResponse

func MockTextWithToolCallResponse(text, toolCallID, toolName string, input any, usage stream.Usage) []stream.Event

MockTextWithToolCallResponse creates events containing text and a tool call.

func MockToolCallResponse

func MockToolCallResponse(toolCallID, toolName string, input any, usage stream.Usage) []stream.Event

MockToolCallResponse creates events for a tool call response. Input must be JSON-serializable.

func MockUsage

func MockUsage(promptTokens, completionTokens int) stream.Usage

MockUsage creates explicit input and output token usage.

Types

type MockConfig added in v0.3.0

type MockConfig struct {
	ID      string
	Default *MockResponse
	Rules   []MockRule
	// Responses is the fallback sequence when no rule matches. It takes
	// precedence over Default and repeats its last response when exhausted.
	Responses []MockResponse
	// Stream takes precedence over Rules, Responses, and Default. It receives
	// an isolated request and a context canceled by either the call context or
	// AbortSignal. Hooks own their channel and must honor context cancellation.
	Stream func(context.Context, *stream.CallOptions) (<-chan stream.Event, error)
}

MockConfig configures a deterministic in-process model. Rules are checked in order; Responses and Default provide fallback when none matches.

type MockEmbeddingModel

type MockEmbeddingModel struct {
	DoEmbedFunc   func(ctx context.Context, opts model.EmbedCallOptions) (*model.EmbedResult, error)
	DoEmbedCalls  []model.EmbedCallOptions
	EmbedResponse *model.EmbedResult
	// contains filtered or unexported fields
}

MockEmbeddingModel is a mock implementation of EmbeddingModel for testing.

func NewMockEmbeddingModel

func NewMockEmbeddingModel(opts MockEmbeddingModelOptions) *MockEmbeddingModel

NewMockEmbeddingModel creates a new mock embedding model.

func (*MockEmbeddingModel) Dimensions

func (m *MockEmbeddingModel) Dimensions() int

func (*MockEmbeddingModel) Embed

func (*MockEmbeddingModel) ID

func (m *MockEmbeddingModel) ID() string

func (*MockEmbeddingModel) MaxEmbeddingsPerCall

func (m *MockEmbeddingModel) MaxEmbeddingsPerCall() int

func (*MockEmbeddingModel) Provider

func (m *MockEmbeddingModel) Provider() string

type MockEmbeddingModelOptions

type MockEmbeddingModelOptions struct {
	ID                   string
	Provider             string
	Dimensions           int
	MaxEmbeddingsPerCall int
	DoEmbedFunc          func(ctx context.Context, opts model.EmbedCallOptions) (*model.EmbedResult, error)
	EmbedResponse        *model.EmbedResult
}

MockEmbeddingModelOptions configures the mock embedding model.

type MockImageModel

type MockImageModel struct {
	DoGenerateFunc   func(ctx context.Context, opts model.ImageCallOptions) (*model.ImageResult, error)
	DoGenerateCalls  []model.ImageCallOptions
	GenerateResponse *model.ImageResult
	// contains filtered or unexported fields
}

MockImageModel is a mock implementation of ImageModel for testing.

func NewMockImageModel

func NewMockImageModel(opts MockImageModelOptions) *MockImageModel

NewMockImageModel creates a new mock image model.

func (*MockImageModel) Generate

func (*MockImageModel) ID

func (m *MockImageModel) ID() string

func (*MockImageModel) MaxImagesPerCall

func (m *MockImageModel) MaxImagesPerCall() int

func (*MockImageModel) Provider

func (m *MockImageModel) Provider() string

type MockImageModelOptions

type MockImageModelOptions struct {
	ID               string
	Provider         string
	MaxImagesPerCall int
	DoGenerateFunc   func(ctx context.Context, opts model.ImageCallOptions) (*model.ImageResult, error)
	GenerateResponse *model.ImageResult
}

MockImageModelOptions configures the mock image model.

type MockModel added in v0.3.0

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

MockModel implements stream.Model without credentials or network access. Configure, Requests, and ResetRequests are safe to use concurrently with Stream.

func NewMockModel added in v0.3.0

func NewMockModel(config MockConfig) (*MockModel, error)

NewMockModel copies configuration and rejects rules without a matcher. ID must be explicit. Request/response data must be JSON-serializable.

func (*MockModel) Configure added in v0.3.0

func (m *MockModel) Configure(config MockConfig) error

Configure atomically replaces response behavior and restarts its sequence. ID must equal the model's immutable ID. History is preserved. In-flight streams retain their captured configuration; invalid changes have no effect.

func (*MockModel) ID added in v0.3.0

func (m *MockModel) ID() string

func (*MockModel) Provider added in v0.3.0

func (m *MockModel) Provider() string

func (*MockModel) Requests added in v0.3.0

func (m *MockModel) Requests() []stream.CallOptions

Requests returns independent snapshots in capture order. JSON values use encoding/json's decoded types (for example, numbers in any become float64). Contexts and tool Execute functions retain their identity.

func (*MockModel) ResetRequests added in v0.3.0

func (m *MockModel) ResetRequests()

ResetRequests clears captured requests without changing response rules.

func (*MockModel) Stream added in v0.3.0

func (m *MockModel) Stream(ctx context.Context, options *stream.CallOptions) (<-chan stream.Event, error)

Stream captures valid requests, including unmatched ones. Canceled requests are rejected before capture. Cancellation during emission closes the stream; consumers should check their context, as with other stream.Model providers.

type MockResponse added in v0.3.0

type MockResponse struct {
	Text      string
	ToolCalls []stream.ToolCall
	Usage     stream.Usage
	// Events, when non-nil, replaces generated text/tool/finish events entirely.
	// An empty non-nil slice produces an empty stream.
	Events []stream.Event
}

MockResponse emits text, followed by optional tool calls and a finish event. Usage is supplied explicitly; zero Usage leaves token counts unreported.

type MockRule added in v0.3.0

type MockRule struct {
	LastUserText *string
	Match        func(*stream.CallOptions) bool
	Response     MockResponse
}

MockRule matches the last user message's text and/or a predicate. When both are set, both must match. Predicates receive an isolated request snapshot.

type MockSpeechModel

type MockSpeechModel struct {
	DoGenerateFunc   func(ctx context.Context, opts model.SpeechCallOptions) (*model.SpeechResult, error)
	DoGenerateCalls  []model.SpeechCallOptions
	GenerateResponse *model.SpeechResult
	// contains filtered or unexported fields
}

MockSpeechModel is a mock implementation of SpeechModel for testing.

func NewMockSpeechModel

func NewMockSpeechModel(opts MockSpeechModelOptions) *MockSpeechModel

NewMockSpeechModel creates a new mock speech model.

func (*MockSpeechModel) Generate

func (*MockSpeechModel) ID

func (m *MockSpeechModel) ID() string

func (*MockSpeechModel) Provider

func (m *MockSpeechModel) Provider() string

type MockSpeechModelOptions

type MockSpeechModelOptions struct {
	ID               string
	Provider         string
	DoGenerateFunc   func(ctx context.Context, opts model.SpeechCallOptions) (*model.SpeechResult, error)
	GenerateResponse *model.SpeechResult
}

MockSpeechModelOptions configures the mock speech model.

type MockTranscriptionModel

type MockTranscriptionModel struct {
	DoTranscribeFunc   func(ctx context.Context, opts model.TranscribeCallOptions) (*model.TranscriptionResult, error)
	DoTranscribeCalls  []model.TranscribeCallOptions
	TranscribeResponse *model.TranscriptionResult
	// contains filtered or unexported fields
}

MockTranscriptionModel is a mock implementation of TranscriptionModel for testing.

func NewMockTranscriptionModel

func NewMockTranscriptionModel(opts MockTranscriptionModelOptions) *MockTranscriptionModel

NewMockTranscriptionModel creates a new mock transcription model.

func (*MockTranscriptionModel) ID

func (*MockTranscriptionModel) Provider

func (m *MockTranscriptionModel) Provider() string

func (*MockTranscriptionModel) Transcribe

type MockTranscriptionModelOptions

type MockTranscriptionModelOptions struct {
	ID                 string
	Provider           string
	DoTranscribeFunc   func(ctx context.Context, opts model.TranscribeCallOptions) (*model.TranscriptionResult, error)
	TranscribeResponse *model.TranscriptionResult
}

MockTranscriptionModelOptions configures the mock transcription model.

Jump to

Keyboard shortcuts

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