testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package testutil provides reusable test helpers for the agent package. MockProvider replaces a real LLM backend in agent tests with scripted responses, request recording, and error injection — so the harness loop, cache behaviour, and tool dispatch can be verified without network calls.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockProvider

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

MockProvider is a provider.Provider whose Stream returns scripted responses, one Turn per call. It records every request it receives so tests can inspect what was sent to the model (cache surface, tool schemas, message ordering).

Usage:

mp := NewMock("test-model", Turn{Text: "Hello"}).Record()
agent := agent.New(mp, registry, session, opts, nil)
agent.Run(ctx, "hi")

for i, req := range mp.Requests() {
    fmt.Printf("turn %d: %d messages, %d tools\n", i+1,
        len(req.Messages), len(req.Tools))
}

func NewMock

func NewMock(name string, turns ...Turn) *MockProvider

NewMock creates a MockProvider. The turns argument is the script; each Stream call consumes one Turn. Extra calls after the script are exhausted return an error. Use Append or SetScript to add more turns later.

func (*MockProvider) Append

func (p *MockProvider) Append(turns ...Turn)

Append adds turns to the existing script.

func (*MockProvider) CallCount

func (p *MockProvider) CallCount() int

MessageCount is a shortcut for len(Requests()).

func (*MockProvider) LastRequest

func (p *MockProvider) LastRequest() *provider.Request

LastRequest returns the most recent request, or nil if none.

func (*MockProvider) Name

func (p *MockProvider) Name() string

Name returns the provider instance name.

func (*MockProvider) Requests

func (p *MockProvider) Requests() []provider.Request

Requests returns all recorded requests in call order. Safe to call from any goroutine after the run loop finishes.

func (*MockProvider) Reset

func (p *MockProvider) Reset()

Reset clears recorded requests and the call counter without changing the script.

func (*MockProvider) SetScript

func (p *MockProvider) SetScript(turns ...Turn)

SetScript replaces the script and resets the call counter.

func (*MockProvider) Stream

func (p *MockProvider) Stream(ctx context.Context, req provider.Request) (<-chan provider.Chunk, error)

Stream replays the next scripted turn. It records the request, then sends chunks in order (reasoning → text → tool calls → usage → done). If the Turn has StreamError set it is returned immediately.

type Turn

type Turn struct {
	Text      string
	Reasoning string
	ToolCalls []provider.ToolCall
	Usage     *provider.Usage
	// Chunks, when non-empty, is emitted exactly as provided. It is useful for
	// edge cases such as partial tool-call starts followed by an error.
	Chunks []provider.Chunk

	// StreamError, when set, causes Stream to return this error before any
	// chunks, simulating a network or auth failure for that turn.
	StreamError error
	// ChunkError, when set, is emitted after the scripted chunks, simulating a
	// mid-stream provider failure after partial output has reached the agent.
	ChunkError error
}

Turn describes one expected Stream call: the text, optional reasoning, optional tool calls, usage telemetry, and optionally an error to inject.

func ErrorTurn

func ErrorTurn(err error) Turn

ErrorTurn is a convenience: a Turn that immediately returns the given error.

func UsageTurn

func UsageTurn(hit, miss, completion int) Turn

UsageTurn is a convenience: a Turn whose text is empty but usage is set. Useful for simulating a tool-call round-trip where the final model response that round is tested later.

Jump to

Keyboard shortcuts

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