Documentation
¶
Overview ¶
Package testing provides test utilities for the Iris SDK.
This package contains mock implementations and test helpers that enable unit testing of code that uses the Iris SDK without making real API calls.
Mock Provider ¶
MockProvider implements core.Provider and allows you to define canned responses:
provider := testing.NewMockProvider(
core.ChatResponse{Output: "Hello!"},
core.ChatResponse{Output: "How can I help?"},
)
client := core.NewClient(provider)
// First call returns "Hello!"
resp, _ := client.Chat("gpt-4o").User("Hi").GetResponse(ctx)
// Second call returns "How can I help?"
resp, _ = client.Chat("gpt-4o").User("What's up?").GetResponse(ctx)
Error Simulation ¶
Queue errors to test error handling:
provider := testing.NewMockProvider().
WithError(core.ErrRateLimited).
WithResponse(core.ChatResponse{Output: "Success after retry"})
Call Recording ¶
Verify that expected calls were made:
provider := testing.NewMockProvider(core.ChatResponse{Output: "test"})
client := core.NewClient(provider)
client.Chat("gpt-4o").User("Hello").GetResponse(ctx)
calls := provider.Calls()
if len(calls) != 1 {
t.Errorf("expected 1 call, got %d", len(calls))
}
if calls[0].Request.Messages[0].Content != "Hello" {
t.Error("unexpected message content")
}
Streaming Support ¶
Mock streaming responses:
provider := testing.NewMockProvider().
WithStreamingResponse([]string{"Hello", " ", "world", "!"}, nil)
Recording Provider ¶
Wrap a real provider to record calls for replay testing:
realProvider := openai.New(apiKey) recorder := testing.NewRecordingProvider(realProvider) client := core.NewClient(recorder) // Make calls... // Save recordings for later replay recordings := recorder.Recordings()
Index ¶
- type MockCall
- type MockProvider
- func (m *MockProvider) CallCount() int
- func (m *MockProvider) Calls() []MockCall
- func (m *MockProvider) Chat(ctx context.Context, req *core.ChatRequest) (*core.ChatResponse, error)
- func (m *MockProvider) ID() string
- func (m *MockProvider) LastCall() *MockCall
- func (m *MockProvider) Models() []core.ModelInfo
- func (m *MockProvider) Reset()
- func (m *MockProvider) ResetAll()
- func (m *MockProvider) StreamChat(ctx context.Context, req *core.ChatRequest) (*core.ChatStream, error)
- func (m *MockProvider) Supports(f core.Feature) bool
- func (m *MockProvider) WithDefaultResponse(resp core.ChatResponse) *MockProvider
- func (m *MockProvider) WithError(err error) *MockProvider
- func (m *MockProvider) WithErrors(errs ...error) *MockProvider
- func (m *MockProvider) WithFeatures(features ...core.Feature) *MockProvider
- func (m *MockProvider) WithID(id string) *MockProvider
- func (m *MockProvider) WithModels(models ...core.ModelInfo) *MockProvider
- func (m *MockProvider) WithResponse(resp core.ChatResponse) *MockProvider
- func (m *MockProvider) WithResponses(responses ...core.ChatResponse) *MockProvider
- func (m *MockProvider) WithStreamingError(chunks []string, err error) *MockProvider
- func (m *MockProvider) WithStreamingResponse(chunks []string, final *core.ChatResponse) *MockProvider
- type MockStreamConfig
- type RecordedCall
- type RecordingProvider
- func (r *RecordingProvider) Chat(ctx context.Context, req *core.ChatRequest) (*core.ChatResponse, error)
- func (r *RecordingProvider) Clear()
- func (r *RecordingProvider) ID() string
- func (r *RecordingProvider) LastRecording() *RecordedCall
- func (r *RecordingProvider) Models() []core.ModelInfo
- func (r *RecordingProvider) RecordingCount() int
- func (r *RecordingProvider) Recordings() []RecordedCall
- func (r *RecordingProvider) StreamChat(ctx context.Context, req *core.ChatRequest) (*core.ChatStream, error)
- func (r *RecordingProvider) Supports(f core.Feature) bool
- func (r *RecordingProvider) Underlying() core.Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MockCall ¶
type MockCall struct {
Method string // "Chat" or "StreamChat"
Request *core.ChatRequest // The request that was made
}
MockCall records a call made to the mock provider.
type MockProvider ¶
type MockProvider struct {
// contains filtered or unexported fields
}
MockProvider is a test double for core.Provider. It allows you to define canned responses, queue errors, and verify calls. MockProvider is safe for concurrent use.
func NewMockProvider ¶
func NewMockProvider(responses ...core.ChatResponse) *MockProvider
NewMockProvider creates a mock provider with optional canned responses. Responses are returned in order; after exhaustion, a default response is used.
func (*MockProvider) CallCount ¶
func (m *MockProvider) CallCount() int
CallCount returns the number of calls made.
func (*MockProvider) Calls ¶
func (m *MockProvider) Calls() []MockCall
Calls returns all recorded calls. The returned slice is a copy and safe to modify.
func (*MockProvider) Chat ¶
func (m *MockProvider) Chat(ctx context.Context, req *core.ChatRequest) (*core.ChatResponse, error)
Chat performs a non-streaming chat request.
func (*MockProvider) LastCall ¶
func (m *MockProvider) LastCall() *MockCall
LastCall returns the most recent call, or nil if no calls have been made.
func (*MockProvider) Models ¶
func (m *MockProvider) Models() []core.ModelInfo
Models returns the list of available models.
func (*MockProvider) Reset ¶
func (m *MockProvider) Reset()
Reset clears all recorded calls and resets response indices. Does not clear the queued responses or errors.
func (*MockProvider) ResetAll ¶
func (m *MockProvider) ResetAll()
ResetAll clears everything including queued responses and errors.
func (*MockProvider) StreamChat ¶
func (m *MockProvider) StreamChat(ctx context.Context, req *core.ChatRequest) (*core.ChatStream, error)
StreamChat performs a streaming chat request.
func (*MockProvider) Supports ¶
func (m *MockProvider) Supports(f core.Feature) bool
Supports reports whether the provider supports the given feature.
func (*MockProvider) WithDefaultResponse ¶
func (m *MockProvider) WithDefaultResponse(resp core.ChatResponse) *MockProvider
WithDefaultResponse sets the default response used when the queue is exhausted.
func (*MockProvider) WithError ¶
func (m *MockProvider) WithError(err error) *MockProvider
WithError queues an error to be returned by the next Chat call. Errors are consumed before responses.
func (*MockProvider) WithErrors ¶
func (m *MockProvider) WithErrors(errs ...error) *MockProvider
WithErrors queues multiple errors.
func (*MockProvider) WithFeatures ¶
func (m *MockProvider) WithFeatures(features ...core.Feature) *MockProvider
WithFeatures sets the supported features.
func (*MockProvider) WithID ¶
func (m *MockProvider) WithID(id string) *MockProvider
WithID sets the provider ID.
func (*MockProvider) WithModels ¶
func (m *MockProvider) WithModels(models ...core.ModelInfo) *MockProvider
WithModels sets the available models.
func (*MockProvider) WithResponse ¶
func (m *MockProvider) WithResponse(resp core.ChatResponse) *MockProvider
WithResponse adds a response to the queue.
func (*MockProvider) WithResponses ¶
func (m *MockProvider) WithResponses(responses ...core.ChatResponse) *MockProvider
WithResponses adds multiple responses to the queue.
func (*MockProvider) WithStreamingError ¶
func (m *MockProvider) WithStreamingError(chunks []string, err error) *MockProvider
WithStreamingError adds a streaming response that emits an error after chunks.
func (*MockProvider) WithStreamingResponse ¶
func (m *MockProvider) WithStreamingResponse(chunks []string, final *core.ChatResponse) *MockProvider
WithStreamingResponse adds a streaming response configuration.
type MockStreamConfig ¶
type MockStreamConfig struct {
Chunks []string // Text chunks to emit
Final *core.ChatResponse // Final response (optional, auto-generated if nil)
Error error // Error to emit (if set, sent after chunks)
}
MockStreamConfig configures a streaming response.
type RecordedCall ¶
type RecordedCall struct {
Method string // "Chat" or "StreamChat"
Request *core.ChatRequest // The request that was made
Response *core.ChatResponse // The response received (nil for streaming until drained)
Error error // Any error that occurred
StartTime time.Time // When the call started
EndTime time.Time // When the call completed
Duration time.Duration // How long the call took
}
RecordedCall contains details about a call made to the underlying provider.
type RecordingProvider ¶
type RecordingProvider struct {
// contains filtered or unexported fields
}
RecordingProvider wraps a real provider and records all calls for inspection. This is useful for debugging, testing, and creating replay fixtures. RecordingProvider is safe for concurrent use.
func NewRecordingProvider ¶
func NewRecordingProvider(underlying core.Provider) *RecordingProvider
NewRecordingProvider creates a recording wrapper around an existing provider.
func (*RecordingProvider) Chat ¶
func (r *RecordingProvider) Chat(ctx context.Context, req *core.ChatRequest) (*core.ChatResponse, error)
Chat performs a chat request and records the call.
func (*RecordingProvider) Clear ¶
func (r *RecordingProvider) Clear()
Clear removes all recorded calls.
func (*RecordingProvider) ID ¶
func (r *RecordingProvider) ID() string
ID returns the underlying provider's ID.
func (*RecordingProvider) LastRecording ¶
func (r *RecordingProvider) LastRecording() *RecordedCall
LastRecording returns the most recent recorded call, or nil if none.
func (*RecordingProvider) Models ¶
func (r *RecordingProvider) Models() []core.ModelInfo
Models returns the underlying provider's models.
func (*RecordingProvider) RecordingCount ¶
func (r *RecordingProvider) RecordingCount() int
RecordingCount returns the number of recorded calls.
func (*RecordingProvider) Recordings ¶
func (r *RecordingProvider) Recordings() []RecordedCall
Recordings returns all recorded calls. The returned slice is a copy and safe to modify.
func (*RecordingProvider) StreamChat ¶
func (r *RecordingProvider) StreamChat(ctx context.Context, req *core.ChatRequest) (*core.ChatStream, error)
StreamChat performs a streaming chat request and records the call. Note: The recorded response will be nil; use the stream to get the response.
func (*RecordingProvider) Supports ¶
func (r *RecordingProvider) Supports(f core.Feature) bool
Supports returns whether the underlying provider supports a feature.
func (*RecordingProvider) Underlying ¶
func (r *RecordingProvider) Underlying() core.Provider
Underlying returns the wrapped provider.