testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package testutil provides test utilities for goai. Source: ai-sdk/packages/ai/src/test/mock-language-model-v3.ts

Index

Constants

This section is empty.

Variables

This section is empty.

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 streamed 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 for a response with both text and tool call.

func MockToolCallResponse

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

MockToolCallResponse creates events for a tool call response.

func MockUsage

func MockUsage(promptTokens, completionTokens int) stream.Usage

MockUsage creates a Usage struct with common defaults.

Types

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 MockLanguageModel

type MockLanguageModel struct {

	// DoStreamFunc is called for each Stream call. If set, takes precedence over StreamResponses.
	DoStreamFunc func(ctx context.Context, options *stream.CallOptions) (<-chan stream.Event, error)

	// StreamResponses is a list of responses to return in sequence.
	// Each call to Stream returns the next response.
	StreamResponses [][]stream.Event

	// DoStreamCalls records all calls to Stream for assertions.
	DoStreamCalls []*stream.CallOptions
	// contains filtered or unexported fields
}

MockLanguageModel is a mock implementation of LanguageModel for testing. It mirrors ai-sdk's MockLanguageModelV4.

func NewMockLanguageModel

func NewMockLanguageModel(opts MockLanguageModelOptions) *MockLanguageModel

NewMockLanguageModel creates a new mock language model.

func (*MockLanguageModel) ID

func (m *MockLanguageModel) ID() string

func (*MockLanguageModel) Provider

func (m *MockLanguageModel) Provider() string

func (*MockLanguageModel) Stream

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

type MockLanguageModelOptions

type MockLanguageModelOptions struct {
	// ID is the model identifier. Defaults to "mock-model-id".
	ID string

	// Provider is the provider identifier. Defaults to "mock-provider".
	Provider string

	// DoStreamFunc is called for each Stream call.
	// If set, takes precedence over StreamResponse/StreamResponses.
	DoStreamFunc func(ctx context.Context, options *stream.CallOptions) (<-chan stream.Event, error)

	// StreamResponse is a single response to return for all Stream calls.
	// Use StreamResponses for multiple sequential responses.
	StreamResponse []stream.Event

	// StreamResponses is a list of responses to return in sequence.
	// Takes precedence over StreamResponse.
	StreamResponses [][]stream.Event
}

MockLanguageModelOptions configures the mock language model.

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