Documentation
¶
Overview ¶
Package testutil provides shared, scriptable test doubles for the SDK's own tests.
Before this package there were a dozen separate hand-rolled mock LLMs across pkg/agent, pkg/memory, pkg/llm and pkg/microservice, each implementing the same six interfaces.LLM methods slightly differently and none of them safe for concurrent use. Every new test either grew a thirteenth or borrowed one from an unrelated file.
All doubles here are safe to call from multiple goroutines, so a test that reports a race is reporting a race in the code under test.
It lives under internal/ deliberately: these are conveniences for this module's tests, not API this module supports for downstream users.
Index ¶
- type FakeConversationMemory
- type FakeLLM
- func (f *FakeLLM) Calls() int
- func (f *FakeLLM) Generate(ctx context.Context, prompt string, _ ...interfaces.GenerateOption) (string, error)
- func (f *FakeLLM) GenerateDetailed(ctx context.Context, prompt string, _ ...interfaces.GenerateOption) (*interfaces.LLMResponse, error)
- func (f *FakeLLM) GenerateStream(ctx context.Context, prompt string, _ ...interfaces.GenerateOption) (<-chan interfaces.StreamEvent, error)
- func (f *FakeLLM) GenerateWithTools(ctx context.Context, prompt string, toolSet []interfaces.Tool, ...) (string, error)
- func (f *FakeLLM) GenerateWithToolsDetailed(ctx context.Context, prompt string, toolSet []interfaces.Tool, ...) (*interfaces.LLMResponse, error)
- func (f *FakeLLM) GenerateWithToolsStream(ctx context.Context, prompt string, toolSet []interfaces.Tool, ...) (<-chan interfaces.StreamEvent, error)
- func (f *FakeLLM) Name() string
- func (f *FakeLLM) Prompts() []string
- func (f *FakeLLM) SupportsStreaming() bool
- func (f *FakeLLM) ToolNamesAt(i int) []string
- type FakeMemory
- type FakeSpan
- func (s *FakeSpan) AddEvent(name string, _ map[string]interface{})
- func (s *FakeSpan) Attribute(key string) (interface{}, bool)
- func (s *FakeSpan) End()
- func (s *FakeSpan) Ended() bool
- func (s *FakeSpan) Errors() []error
- func (s *FakeSpan) RecordError(err error)
- func (s *FakeSpan) SetAttribute(key string, value interface{})
- type FakeTool
- func (f *FakeTool) CallCount() int
- func (f *FakeTool) Calls() []string
- func (f *FakeTool) Description() string
- func (f *FakeTool) DisplayName() string
- func (f *FakeTool) Execute(ctx context.Context, args string) (string, error)
- func (f *FakeTool) Internal() bool
- func (f *FakeTool) Name() string
- func (f *FakeTool) Parameters() map[string]interfaces.ParameterSpec
- func (f *FakeTool) Run(ctx context.Context, input string) (string, error)
- type FakeTracer
- type FakeVectorStore
- func (f *FakeVectorStore) CreateTenant(context.Context, string) error
- func (f *FakeVectorStore) Delete(context.Context, []string, ...interfaces.DeleteOption) error
- func (f *FakeVectorStore) DeleteTenant(context.Context, string) error
- func (f *FakeVectorStore) Get(_ context.Context, id string, _ ...interfaces.StoreOption) (*interfaces.Document, error)
- func (f *FakeVectorStore) GlobalDelete(context.Context, []string, ...interfaces.DeleteOption) error
- func (f *FakeVectorStore) GlobalSearch(ctx context.Context, query string, limit int, opts ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
- func (f *FakeVectorStore) GlobalSearchByVector(ctx context.Context, vector []float32, limit int, ...) ([]interfaces.SearchResult, error)
- func (f *FakeVectorStore) GlobalStore(ctx context.Context, documents []interfaces.Document, ...) error
- func (f *FakeVectorStore) ListTenants(context.Context) ([]string, error)
- func (f *FakeVectorStore) Search(_ context.Context, query string, _ int, _ ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
- func (f *FakeVectorStore) SearchByVector(_ context.Context, _ []float32, _ int, _ ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
- func (f *FakeVectorStore) Searches() []string
- func (f *FakeVectorStore) Store(_ context.Context, documents []interfaces.Document, ...) error
- func (f *FakeVectorStore) Stored() []interfaces.Document
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakeConversationMemory ¶
type FakeConversationMemory struct {
FakeMemory
// Conversations is returned by GetAllConversations.
Conversations []string
}
FakeConversationMemory is a FakeMemory that also implements interfaces.ConversationMemory, for testing capability discovery through decorators.
func (*FakeConversationMemory) GetAllConversations ¶
func (f *FakeConversationMemory) GetAllConversations(context.Context) ([]string, error)
GetAllConversations implements interfaces.ConversationMemory.
func (*FakeConversationMemory) GetConversationMessages ¶
func (f *FakeConversationMemory) GetConversationMessages(ctx context.Context, _ string) ([]interfaces.Message, error)
GetConversationMessages implements interfaces.ConversationMemory.
func (*FakeConversationMemory) GetMemoryStatistics ¶
GetMemoryStatistics implements interfaces.ConversationMemory.
type FakeLLM ¶
type FakeLLM struct {
// ProviderName is returned by Name. Defaults to "fake".
ProviderName string
// ModelName is reported on LLMResponse.Model. Defaults to "fake-model".
ModelName string
// DefaultResponse is returned once Responses is exhausted.
DefaultResponse string
// Responses is consumed in order, one per Generate* call.
Responses []string
// Err, when set, is returned by every Generate* call.
Err error
// Usage, when set, is reported on every detailed response.
Usage *interfaces.TokenUsage
// GenerateFunc, when set, overrides all of the above.
GenerateFunc func(ctx context.Context, prompt string, tools []interfaces.Tool) (string, error)
// InvokeTools, when true, calls Execute on every supplied tool before
// answering. Useful for exercising tool plumbing without a real model.
InvokeTools bool
// Streaming reports whether SupportsStreaming returns true. Defaults false.
Streaming bool
// contains filtered or unexported fields
}
FakeLLM is a scriptable interfaces.LLM and interfaces.StreamingLLM.
The zero value is usable: it answers every prompt with DefaultResponse. Queue specific answers with Responses, or take full control with GenerateFunc.
func NewFakeLLM ¶
func NewFakeLLM() *FakeLLM
NewFakeLLM returns a FakeLLM that answers every prompt with "ok". It exists so migrated call sites read as intent rather than struct literals.
func (*FakeLLM) Generate ¶
func (f *FakeLLM) Generate(ctx context.Context, prompt string, _ ...interfaces.GenerateOption) (string, error)
Generate implements interfaces.LLM.
func (*FakeLLM) GenerateDetailed ¶
func (f *FakeLLM) GenerateDetailed(ctx context.Context, prompt string, _ ...interfaces.GenerateOption) (*interfaces.LLMResponse, error)
GenerateDetailed implements interfaces.LLM.
func (*FakeLLM) GenerateStream ¶
func (f *FakeLLM) GenerateStream(ctx context.Context, prompt string, _ ...interfaces.GenerateOption) (<-chan interfaces.StreamEvent, error)
GenerateStream implements interfaces.StreamingLLM, emitting the scripted answer as one content event followed by completion.
func (*FakeLLM) GenerateWithTools ¶
func (f *FakeLLM) GenerateWithTools(ctx context.Context, prompt string, toolSet []interfaces.Tool, _ ...interfaces.GenerateOption) (string, error)
GenerateWithTools implements interfaces.LLM.
func (*FakeLLM) GenerateWithToolsDetailed ¶
func (f *FakeLLM) GenerateWithToolsDetailed(ctx context.Context, prompt string, toolSet []interfaces.Tool, _ ...interfaces.GenerateOption) (*interfaces.LLMResponse, error)
GenerateWithToolsDetailed implements interfaces.LLM.
func (*FakeLLM) GenerateWithToolsStream ¶
func (f *FakeLLM) GenerateWithToolsStream(ctx context.Context, prompt string, toolSet []interfaces.Tool, _ ...interfaces.GenerateOption) (<-chan interfaces.StreamEvent, error)
GenerateWithToolsStream implements interfaces.StreamingLLM.
func (*FakeLLM) SupportsStreaming ¶
SupportsStreaming implements interfaces.LLM.
func (*FakeLLM) ToolNamesAt ¶
ToolNamesAt returns the tool names supplied to the i'th call.
type FakeMemory ¶
type FakeMemory struct {
// Conversational makes the fake report conversation-level capability.
Conversational bool
// contains filtered or unexported fields
}
FakeMemory is an in-process interfaces.Memory keyed by a single scope, for tests that do not care about org or conversation scoping.
Set Conversational to also satisfy interfaces.ConversationMemory.
func (*FakeMemory) AddMessage ¶
func (f *FakeMemory) AddMessage(_ context.Context, message interfaces.Message) error
AddMessage implements interfaces.Memory.
func (*FakeMemory) Clear ¶
func (f *FakeMemory) Clear(_ context.Context) error
Clear implements interfaces.Memory.
func (*FakeMemory) GetMessages ¶
func (f *FakeMemory) GetMessages(_ context.Context, _ ...interfaces.GetMessagesOption) ([]interfaces.Message, error)
GetMessages implements interfaces.Memory.
type FakeSpan ¶
type FakeSpan struct {
// contains filtered or unexported fields
}
FakeSpan is an inert interfaces.Span that records what it was told.
func (*FakeSpan) RecordError ¶
RecordError implements interfaces.Span.
func (*FakeSpan) SetAttribute ¶
SetAttribute implements interfaces.Span.
type FakeTool ¶
type FakeTool struct {
// ToolName is returned by Name. Defaults to "fake_tool".
ToolName string
// Desc is returned by Description.
Desc string
// Result is returned by Run and Execute when ExecuteFunc is nil.
Result string
// Err, when set, is returned by Run and Execute.
Err error
// Params is returned by Parameters. Defaults to an empty map.
Params map[string]interfaces.ParameterSpec
// ExecuteFunc, when set, overrides Result and Err.
ExecuteFunc func(ctx context.Context, args string) (string, error)
// Display, when non-empty, is returned by DisplayName.
Display string
// IsInternal is returned by Internal.
IsInternal bool
// contains filtered or unexported fields
}
FakeTool is a scriptable interfaces.Tool that records its invocations.
func (*FakeTool) Description ¶
Description implements interfaces.Tool.
func (*FakeTool) DisplayName ¶
DisplayName implements interfaces.ToolWithDisplayName.
func (*FakeTool) Parameters ¶
func (f *FakeTool) Parameters() map[string]interfaces.ParameterSpec
Parameters implements interfaces.Tool.
type FakeTracer ¶
type FakeTracer struct {
// contains filtered or unexported fields
}
FakeTracer is an inert interfaces.Tracer that hands out FakeSpans.
func (*FakeTracer) SpanNames ¶
func (t *FakeTracer) SpanNames() []string
SpanNames returns the names of every span started, in order.
func (*FakeTracer) Spans ¶
func (t *FakeTracer) Spans() []*FakeSpan
Spans returns every span handed out, in order.
func (*FakeTracer) StartSpan ¶
func (t *FakeTracer) StartSpan(ctx context.Context, name string) (context.Context, interfaces.Span)
StartSpan implements interfaces.Tracer.
func (*FakeTracer) StartTraceSession ¶
func (t *FakeTracer) StartTraceSession(ctx context.Context, name string) (context.Context, interfaces.Span)
StartTraceSession implements interfaces.Tracer.
type FakeVectorStore ¶
type FakeVectorStore struct {
// Results is returned by Search and SearchByVector.
Results []interfaces.SearchResult
// SearchErr, when set, is returned by every search.
SearchErr error
// StoreErr, when set, is returned by every store.
StoreErr error
// contains filtered or unexported fields
}
FakeVectorStore is an interfaces.VectorStore that records what it was asked to store and search, and returns canned results.
interfaces.VectorStore is a twelve-method interface, so hand-rolling one per test is exactly the duplication this package exists to prevent.
func (*FakeVectorStore) CreateTenant ¶
func (f *FakeVectorStore) CreateTenant(context.Context, string) error
CreateTenant implements interfaces.VectorStore.
func (*FakeVectorStore) Delete ¶
func (f *FakeVectorStore) Delete(context.Context, []string, ...interfaces.DeleteOption) error
Delete implements interfaces.VectorStore.
func (*FakeVectorStore) DeleteTenant ¶
func (f *FakeVectorStore) DeleteTenant(context.Context, string) error
DeleteTenant implements interfaces.VectorStore.
func (*FakeVectorStore) Get ¶
func (f *FakeVectorStore) Get(_ context.Context, id string, _ ...interfaces.StoreOption) (*interfaces.Document, error)
Get implements interfaces.VectorStore.
func (*FakeVectorStore) GlobalDelete ¶
func (f *FakeVectorStore) GlobalDelete(context.Context, []string, ...interfaces.DeleteOption) error
GlobalDelete implements interfaces.VectorStore.
func (*FakeVectorStore) GlobalSearch ¶
func (f *FakeVectorStore) GlobalSearch(ctx context.Context, query string, limit int, opts ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
GlobalSearch implements interfaces.VectorStore.
func (*FakeVectorStore) GlobalSearchByVector ¶
func (f *FakeVectorStore) GlobalSearchByVector(ctx context.Context, vector []float32, limit int, opts ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
GlobalSearchByVector implements interfaces.VectorStore.
func (*FakeVectorStore) GlobalStore ¶
func (f *FakeVectorStore) GlobalStore(ctx context.Context, documents []interfaces.Document, opts ...interfaces.StoreOption) error
GlobalStore implements interfaces.VectorStore.
func (*FakeVectorStore) ListTenants ¶
func (f *FakeVectorStore) ListTenants(context.Context) ([]string, error)
ListTenants implements interfaces.VectorStore.
func (*FakeVectorStore) Search ¶
func (f *FakeVectorStore) Search(_ context.Context, query string, _ int, _ ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
Search implements interfaces.VectorStore.
func (*FakeVectorStore) SearchByVector ¶
func (f *FakeVectorStore) SearchByVector(_ context.Context, _ []float32, _ int, _ ...interfaces.SearchOption) ([]interfaces.SearchResult, error)
SearchByVector implements interfaces.VectorStore.
func (*FakeVectorStore) Searches ¶
func (f *FakeVectorStore) Searches() []string
Searches returns every query passed to Search, in order.
func (*FakeVectorStore) Store ¶
func (f *FakeVectorStore) Store(_ context.Context, documents []interfaces.Document, _ ...interfaces.StoreOption) error
Store implements interfaces.VectorStore.
func (*FakeVectorStore) Stored ¶
func (f *FakeVectorStore) Stored() []interfaces.Document
Stored returns every document handed to Store, in order.