Documentation
¶
Overview ¶
Package testutil provides deterministic offline models and event fixtures.
Index ¶
- Variables
- func AssertResultWarning(t *testing.T, warnings []stream.Warning, want stream.Warning)
- func AssertWarning(t *testing.T, warnings []stream.Warning, want stream.Warning)
- func CollectStream(t *testing.T, ch <-chan stream.Event, err error) []stream.Event
- func FindFirstStartEvent(t *testing.T, events []stream.Event) *stream.StartEvent
- func MockErrorResponse(err error) []stream.Event
- func MockReasoningResponse(reasoningText, text string, usage stream.Usage) []stream.Event
- func MockStreamedTextResponse(chunks []string, usage stream.Usage) []stream.Event
- func MockTextResponse(text string, usage stream.Usage) []stream.Event
- func MockTextWithToolCallResponse(text, toolCallID, toolName string, input any, usage stream.Usage) []stream.Event
- func MockToolCallResponse(toolCallID, toolName string, input any, usage stream.Usage) []stream.Event
- func MockUsage(promptTokens, completionTokens int) stream.Usage
- type MockConfig
- type MockEmbeddingModel
- func (m *MockEmbeddingModel) Dimensions() int
- func (m *MockEmbeddingModel) Embed(ctx context.Context, opts model.EmbedCallOptions) (*model.EmbedResult, error)
- func (m *MockEmbeddingModel) ID() string
- func (m *MockEmbeddingModel) MaxEmbeddingsPerCall() int
- func (m *MockEmbeddingModel) Provider() string
- type MockEmbeddingModelOptions
- type MockImageModel
- type MockImageModelOptions
- type MockModel
- func (m *MockModel) Configure(config MockConfig) error
- func (m *MockModel) ID() string
- func (m *MockModel) Provider() string
- func (m *MockModel) Requests() []stream.CallOptions
- func (m *MockModel) ResetRequests()
- func (m *MockModel) Stream(ctx context.Context, options *stream.CallOptions) (<-chan stream.Event, error)
- type MockResponse
- type MockRule
- type MockSpeechModel
- type MockSpeechModelOptions
- type MockTranscriptionModel
- type MockTranscriptionModelOptions
Constants ¶
This section is empty.
Variables ¶
var ErrMockNoMatch = errors.New("mock model: no matching response")
ErrMockNoMatch means no rule matched and no fallback response was configured.
Functions ¶
func AssertResultWarning ¶
AssertResultWarning is an alias for AssertWarning, named to match non-language-model result types (ImageResult.Warnings, etc.).
func AssertWarning ¶
AssertWarning fails the test if warnings does not contain want. Matches on Type + Feature + Message; Details is ignored.
func CollectStream ¶
CollectStream drains a stream channel into a slice.
func FindFirstStartEvent ¶
FindFirstStartEvent returns the first StartEvent in events. Fails the test if no StartEvent is present.
func MockErrorResponse ¶
MockErrorResponse creates events for an error response.
func MockReasoningResponse ¶
MockReasoningResponse creates events for a response with reasoning.
func MockStreamedTextResponse ¶
MockStreamedTextResponse creates events for a text response with multiple chunks.
func MockTextResponse ¶
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.
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 (m *MockEmbeddingModel) Embed(ctx context.Context, opts model.EmbedCallOptions) (*model.EmbedResult, error)
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 (m *MockImageModel) Generate(ctx context.Context, opts model.ImageCallOptions) (*model.ImageResult, error)
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) 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 (m *MockSpeechModel) Generate(ctx context.Context, opts model.SpeechCallOptions) (*model.SpeechResult, error)
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 (m *MockTranscriptionModel) ID() string
func (*MockTranscriptionModel) Provider ¶
func (m *MockTranscriptionModel) Provider() string
func (*MockTranscriptionModel) Transcribe ¶
func (m *MockTranscriptionModel) Transcribe(ctx context.Context, opts model.TranscribeCallOptions) (*model.TranscriptionResult, error)
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.