testutil

package
v0.2.90 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 15 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	SamplePNG  = []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01")
	SampleJPEG = []byte("\xff\xd8\xff\xe0\x00\x10JFIF\x00")
	SampleGIF  = []byte("GIF89a\x01\x00\x01\x00")
	SampleWebP = []byte("RIFF\x24\x00\x00\x00WEBPVP8 ")
	SamplePDF  = []byte("%PDF-1.4\n%sample\n")
	SampleBMP  = []byte("BM\x36\x00\x00\x00\x00\x00")
)

Sample file headers that http.DetectContentType recognizes. They are not complete images or documents: they exercise type detection only, so use them where the data is checked by its type and not decoded.

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

func (f *FakeConversationMemory) GetMemoryStatistics(ctx context.Context) (int, int, error)

GetMemoryStatistics implements interfaces.ConversationMemory.

type FakeImageEditSession added in v0.2.85

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

FakeImageEditSession is a scriptable interfaces.ImageEditSession that answers every SendMessage with a fixed response and error and records what it was sent. Build one with NewFakeImageEditSession.

func NewFakeImageEditSession added in v0.2.85

func NewFakeImageEditSession(resp *interfaces.ImageEditResponse, err error) *FakeImageEditSession

NewFakeImageEditSession returns a session that answers with resp and err. A nil resp is replaced by an empty response.

func (*FakeImageEditSession) Close added in v0.2.85

func (s *FakeImageEditSession) Close() error

Close implements interfaces.ImageEditSession.

func (*FakeImageEditSession) Closed added in v0.2.85

func (s *FakeImageEditSession) Closed() bool

Closed reports whether Close was called.

func (*FakeImageEditSession) GetHistory added in v0.2.85

func (s *FakeImageEditSession) GetHistory() []interfaces.ImageEditTurn

GetHistory implements interfaces.ImageEditSession.

func (*FakeImageEditSession) LastOptions added in v0.2.85

LastOptions returns the options of the last SendMessage call.

func (*FakeImageEditSession) Prompts added in v0.2.85

func (s *FakeImageEditSession) Prompts() []string

Prompts returns every message sent, in order.

func (*FakeImageEditSession) SendMessage added in v0.2.85

SendMessage implements interfaces.ImageEditSession.

func (*FakeImageEditSession) SendMessageWithImage added in v0.2.85

SendMessageWithImage implements interfaces.ImageEditSession. It is not scripted and always fails.

func (*FakeImageEditSession) SetErr added in v0.2.85

func (s *FakeImageEditSession) SetErr(err error)

SetErr changes the error returned by later SendMessage calls.

func (*FakeImageEditSession) SetHistory added in v0.2.85

func (s *FakeImageEditSession) SetHistory(history []interfaces.ImageEditTurn)

SetHistory sets the history returned by GetHistory.

type FakeImageEditor added in v0.2.85

type FakeImageEditor struct {
	NewSession  func() *FakeImageEditSession
	Err         error
	Unsupported bool
	// contains filtered or unexported fields
}

FakeImageEditor is a scriptable interfaces.MultiTurnImageEditor.

Each CreateImageEditSession call builds a session with NewSession, or an empty-response session when NewSession is nil, unless Err is set.

func (*FakeImageEditor) CreateImageEditSession added in v0.2.85

CreateImageEditSession implements interfaces.MultiTurnImageEditor.

func (*FakeImageEditor) LastOptions added in v0.2.85

LastOptions returns the options of the last CreateImageEditSession call.

func (*FakeImageEditor) Sessions added in v0.2.85

func (e *FakeImageEditor) Sessions() []*FakeImageEditSession

Sessions returns every session handed out, in order.

func (*FakeImageEditor) SupportsMultiTurnImageEditing added in v0.2.85

func (e *FakeImageEditor) SupportsMultiTurnImageEditing() bool

SupportsMultiTurnImageEditing implements interfaces.MultiTurnImageEditor.

type FakeImageStorage added in v0.2.85

type FakeImageStorage struct {
	Err error
	// StorageName is returned by Name; it defaults to "fake".
	StorageName string
	// contains filtered or unexported fields
}

FakeImageStorage is a storage.ImageStorage that returns "https://cdn.test/<mime type>" for every stored image, or Err when set.

func (*FakeImageStorage) Delete added in v0.2.85

Delete implements storage.ImageStorage.

func (*FakeImageStorage) Get added in v0.2.85

Get implements storage.ImageStorage.

func (*FakeImageStorage) Metadata added in v0.2.85

func (f *FakeImageStorage) Metadata() []storage.StorageMetadata

Metadata returns the metadata of every stored image, in order.

func (*FakeImageStorage) Name added in v0.2.85

func (f *FakeImageStorage) Name() string

Name implements storage.ImageStorage.

func (*FakeImageStorage) Store added in v0.2.85

Store implements storage.ImageStorage.

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) Calls

func (f *FakeLLM) Calls() int

Calls returns how many Generate* calls have been made.

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) Name

func (f *FakeLLM) Name() string

Name implements interfaces.LLM.

func (*FakeLLM) Prompts

func (f *FakeLLM) Prompts() []string

Prompts returns every prompt seen, in order.

func (*FakeLLM) SupportsStreaming

func (f *FakeLLM) SupportsStreaming() bool

SupportsStreaming implements interfaces.LLM.

func (*FakeLLM) ToolNamesAt

func (f *FakeLLM) ToolNamesAt(i int) []string

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

GetMessages implements interfaces.Memory.

type FakeSQL added in v0.2.85

type FakeSQL struct {
	// Respond answers every query and exec. Nil answers with an empty response.
	Respond func(query string) FakeSQLResponse

	PingErr     error
	BeginErr    error
	CommitErr   error
	RollbackErr error
	// contains filtered or unexported fields
}

FakeSQL is a minimal database/sql driver that records statements and replies with scripted responses, so tests can check the SQL a client builds without a real database server.

Set the fields before calling Open; they are read without locking.

func (*FakeSQL) Calls added in v0.2.85

func (f *FakeSQL) Calls() []FakeSQLCall

Calls returns every statement received, in order.

func (*FakeSQL) Committed added in v0.2.85

func (f *FakeSQL) Committed() int

Committed returns how many transactions reached the driver's Commit.

func (*FakeSQL) LastCall added in v0.2.85

func (f *FakeSQL) LastCall() FakeSQLCall

LastCall returns the most recent statement, or the zero value if none.

func (*FakeSQL) Open added in v0.2.85

func (f *FakeSQL) Open() *sql.DB

Open returns a *sql.DB backed by f.

func (*FakeSQL) RolledBack added in v0.2.85

func (f *FakeSQL) RolledBack() int

RolledBack returns how many transactions reached the driver's Rollback.

type FakeSQLCall added in v0.2.85

type FakeSQLCall struct {
	Query string
	Args  []driver.Value
}

FakeSQLCall records one statement sent to FakeSQL.

type FakeSQLResponse added in v0.2.85

type FakeSQLResponse struct {
	Columns      []string
	Rows         [][]driver.Value
	RowsAffected int64
	// Err is returned by the query or exec call itself.
	Err error
	// AffectedErr is returned by Result.RowsAffected.
	AffectedErr error
	// IterErr is returned by Rows.Next after the last row.
	IterErr error
}

FakeSQLResponse is what FakeSQL returns for one statement.

type FakeSpan

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

FakeSpan is an inert interfaces.Span that records what it was told.

func (*FakeSpan) AddEvent

func (s *FakeSpan) AddEvent(name string, _ map[string]interface{})

AddEvent implements interfaces.Span.

func (*FakeSpan) Attribute

func (s *FakeSpan) Attribute(key string) (interface{}, bool)

Attribute returns a recorded attribute.

func (*FakeSpan) End

func (s *FakeSpan) End()

End implements interfaces.Span.

func (*FakeSpan) Ended

func (s *FakeSpan) Ended() bool

Ended reports whether End was called.

func (*FakeSpan) Errors

func (s *FakeSpan) Errors() []error

Errors returns every error recorded on the span.

func (*FakeSpan) RecordError

func (s *FakeSpan) RecordError(err error)

RecordError implements interfaces.Span.

func (*FakeSpan) SetAttribute

func (s *FakeSpan) SetAttribute(key string, value interface{})

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) CallCount

func (f *FakeTool) CallCount() int

CallCount returns how many times the tool was invoked.

func (*FakeTool) Calls

func (f *FakeTool) Calls() []string

Calls returns the arguments of every invocation, in order.

func (*FakeTool) Description

func (f *FakeTool) Description() string

Description implements interfaces.Tool.

func (*FakeTool) DisplayName

func (f *FakeTool) DisplayName() string

DisplayName implements interfaces.ToolWithDisplayName.

func (*FakeTool) Execute

func (f *FakeTool) Execute(ctx context.Context, args string) (string, error)

Execute implements interfaces.Tool.

func (*FakeTool) Internal

func (f *FakeTool) Internal() bool

Internal implements interfaces.InternalTool.

func (*FakeTool) Name

func (f *FakeTool) Name() string

Name implements interfaces.Tool.

func (*FakeTool) Parameters

func (f *FakeTool) Parameters() map[string]interfaces.ParameterSpec

Parameters implements interfaces.Tool.

func (*FakeTool) Run

func (f *FakeTool) Run(ctx context.Context, input string) (string, error)

Run 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

Delete implements interfaces.VectorStore.

func (*FakeVectorStore) DeleteTenant

func (f *FakeVectorStore) DeleteTenant(context.Context, string) error

DeleteTenant implements interfaces.VectorStore.

func (*FakeVectorStore) Get

Get implements interfaces.VectorStore.

func (*FakeVectorStore) GlobalDelete

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

Search implements interfaces.VectorStore.

func (*FakeVectorStore) SearchByVector

SearchByVector implements interfaces.VectorStore.

func (*FakeVectorStore) Searches

func (f *FakeVectorStore) Searches() []string

Searches returns every query passed to Search, in order.

func (*FakeVectorStore) Store

Store implements interfaces.VectorStore.

func (*FakeVectorStore) Stored

func (f *FakeVectorStore) Stored() []interfaces.Document

Stored returns every document handed to Store, in order.

type FakeWeaviate added in v0.2.85

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

FakeWeaviate is an httptest server that speaks enough of the Weaviate REST and GraphQL API for a client to run against it. It records every request. Tests override replies with SetHandle and SetGraphQL.

func NewFakeWeaviate added in v0.2.85

func NewFakeWeaviate(t *testing.T) *FakeWeaviate

NewFakeWeaviate starts a FakeWeaviate that is closed when the test ends.

func (*FakeWeaviate) GraphQLQueries added in v0.2.85

func (fw *FakeWeaviate) GraphQLQueries() []string

GraphQLQueries returns the GraphQL query texts received so far, in order.

func (*FakeWeaviate) Host added in v0.2.85

func (fw *FakeWeaviate) Host() string

Host returns the server's host:port, as the Weaviate client config wants it.

func (*FakeWeaviate) RequestsTo added in v0.2.85

func (fw *FakeWeaviate) RequestsTo(method, path string) []WeaviateRequest

RequestsTo returns the recorded requests that match method and path.

func (*FakeWeaviate) Reset added in v0.2.85

func (fw *FakeWeaviate) Reset()

Reset forgets the recorded requests.

func (*FakeWeaviate) SetGraphQL added in v0.2.85

func (fw *FakeWeaviate) SetGraphQL(h func(query string) WeaviateReply)

SetGraphQL answers GraphQL queries that the handle does not take. The default returns no data.

func (*FakeWeaviate) SetHandle added in v0.2.85

func (fw *FakeWeaviate) SetHandle(h func(WeaviateRequest) (WeaviateReply, bool))

SetHandle installs a handler that answers before the defaults. It returns false to fall through to the defaults. Nil removes it.

type WeaviateReply added in v0.2.85

type WeaviateReply struct {
	Status int
	Body   string
}

WeaviateReply is the status and JSON body FakeWeaviate sends back.

type WeaviateRequest added in v0.2.85

type WeaviateRequest struct {
	Method string
	Path   string
	Query  url.Values
	Header http.Header
	Body   []byte
	// GraphQL is the query text of a POST /v1/graphql request.
	GraphQL string
}

WeaviateRequest is one HTTP request received by FakeWeaviate.

Jump to

Keyboard shortcuts

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