testutil

package
v0.2.75 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 4 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

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

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

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

Jump to

Keyboard shortcuts

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