testutil

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package testutil provides deterministic, dependency-free test doubles for building examples and tests against manglekit without external services or API keys:

  • MockLLM: a scripted core.TextGenerator
  • DeterministicEmbedder / NewLengthEmbedder / NewKeywordEmbedder: deterministic core.Embedder implementations
  • InMemoryStateProvider: a core.StateProvider backed by a map
  • WorkflowSessionStore: an in-memory ports.SessionStateStore

Everything here is deterministic and safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeterministicEmbedder

type DeterministicEmbedder struct {
	// Dim is the vector dimension reported by Dimension().
	Dim int
	// VecFor maps text to its vector. It must be pure and deterministic.
	VecFor func(text string) []float32
}

DeterministicEmbedder is a core.Embedder whose vectors are a pure function of the input text. Construct it with NewLengthEmbedder or NewKeywordEmbedder, or directly with a custom VecFor function.

func NewKeywordEmbedder

func NewKeywordEmbedder(keywords map[string][]float32, fallback []float32) *DeterministicEmbedder

NewKeywordEmbedder returns an embedder that returns a fixed vector when the text contains any of the given keyword substrings, and fallback otherwise. keywords maps substring (matched via strings.Contains) to vector; all vectors must have the same dimension as fallback.

func NewLengthEmbedder

func NewLengthEmbedder() *DeterministicEmbedder

NewLengthEmbedder returns a 2-dimensional embedder that derives vectors from text length — cheap, deterministic, and distinct for different lengths.

func (*DeterministicEmbedder) Dimension

func (e *DeterministicEmbedder) Dimension() int

Dimension implements core.Embedder.

func (*DeterministicEmbedder) Embed

func (e *DeterministicEmbedder) Embed(_ context.Context, text string) ([]float32, error)

Embed implements core.Embedder.

func (*DeterministicEmbedder) EmbedBatch

func (e *DeterministicEmbedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

EmbedBatch implements core.Embedder.

type InMemoryStateProvider

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

InMemoryStateProvider implements core.StateProvider using a thread-safe map. Get marshals the stored state to JSON bytes (nil, nil when absent), Set accepts anything JSON-marshalable (plus *core.SessionState / []byte shortcuts), and Close resets the store. Suitable for tests and demos; use a Badger/Redis-backed provider in production.

func NewInMemoryStateProvider

func NewInMemoryStateProvider() *InMemoryStateProvider

NewInMemoryStateProvider creates an empty in-memory state provider.

func (*InMemoryStateProvider) Close

Close implements core.StateProvider: it resets the store.

func (*InMemoryStateProvider) Delete

func (p *InMemoryStateProvider) Delete(_ context.Context, sessionID string) error

Delete implements core.StateProvider.

func (*InMemoryStateProvider) Get

func (p *InMemoryStateProvider) Get(_ context.Context, sessionID string) (any, error)

Get implements core.StateProvider: it returns the JSON encoding of the session state, or (nil, nil) when the session does not exist.

func (*InMemoryStateProvider) Set

func (p *InMemoryStateProvider) Set(_ context.Context, sessionID string, state any) error

Set implements core.StateProvider.

func (*InMemoryStateProvider) Size

func (p *InMemoryStateProvider) Size() int

Size returns the number of stored sessions (test helper).

type MockLLM

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

MockLLM is a deterministic, scripted implementation of core.TextGenerator. Responses are consumed in order (FIFO); once the script is exhausted the last response repeats, so a single-response MockLLM never runs dry.

llm := testutil.NewMockLLM("first answer", "second answer")
client.SetLLM(llm)

It also records every prompt it was given (see Prompts) so tests can assert on what the supervised action actually sent.

func NewMockLLM

func NewMockLLM(responses ...string) *MockLLM

NewMockLLM builds a MockLLM that answers with the given responses in call order. With no responses it answers with the empty string.

func (*MockLLM) Calls

func (m *MockLLM) Calls() int

Calls returns the number of generation calls made so far.

func (*MockLLM) Complete

func (m *MockLLM) Complete(_ context.Context, prompt string) (string, error)

Complete implements core.TextGenerator.

func (*MockLLM) Generate

func (m *MockLLM) Generate(_ context.Context, prompt string, _ ...core.GenerateOption) (*core.LLMResponse, error)

Generate implements core.TextGenerator.

func (*MockLLM) Prompts

func (m *MockLLM) Prompts() []string

Prompts returns every prompt the mock has been given, in call order.

func (*MockLLM) Stream

func (m *MockLLM) Stream(ctx context.Context, prompt string) (<-chan core.StreamChunk, error)

Stream implements core.TextGenerator: it emits the scripted response as a single chunk and closes the channel.

type WorkflowSessionStore

type WorkflowSessionStore = ports.InMemorySessionStore

WorkflowSessionStore is the thread-safe in-memory ports.SessionStateStore used by multiagent.HydratedWorkflowExecutor for workflow-instance checkpointing and resume. It re-exports ports.InMemorySessionStore so examples and tests need one import for all their test doubles.

func NewWorkflowSessionStore

func NewWorkflowSessionStore() *WorkflowSessionStore

NewWorkflowSessionStore creates an empty in-memory workflow session store.

Jump to

Keyboard shortcuts

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