test

package
v1.3.18 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Harness

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

Harness is an end-to-end test harness for the seed agent. It wires up mock Provider, ToolExecutor, UI, and EventBus so you can script full conversation flows and assert on every interface interaction.

Usage:

h := test.NewHarness()
h.Provider().AddTextResponse("Hello!")
agent := h.NewAgent()
result, err := agent.Run(ctx, "Hi")
h.AssertNoError(err)
h.AssertEquals(result, "Hello!")

func NewHarness

func NewHarness() *Harness

NewHarness creates a new test harness.

func NewHarnessWithT

func NewHarnessWithT(t interface {
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}) *Harness

NewHarnessWithT creates a harness that can call t.Fatalf directly.

func (*Harness) AssertContains

func (h *Harness) AssertContains(s, substr string)

AssertContains fails if s doesn't contain substr.

func (*Harness) AssertEquals

func (h *Harness) AssertEquals(got, want string)

AssertEquals fails if got != want.

func (*Harness) AssertError

func (h *Harness) AssertError(err error)

AssertError fails the test if err is nil.

func (*Harness) AssertErrorContains

func (h *Harness) AssertErrorContains(err error, substr string)

AssertErrorContains fails if err doesn't contain the substring.

func (*Harness) AssertEventPublished

func (h *Harness) AssertEventPublished(eventType string)

AssertEventPublished checks that an event of the given type was published.

func (*Harness) AssertExecutorCalledN

func (h *Harness) AssertExecutorCalledN(n int)

AssertExecutorCalledN times.

func (*Harness) AssertFirstMessageIsSystem

func (h *Harness) AssertFirstMessageIsSystem()

AssertFirstMessageIsSystem checks that the first message in the last request is a system message.

func (*Harness) AssertLastRequestHasNMessages

func (h *Harness) AssertLastRequestHasNMessages(n int)

AssertLastRequestHasNMessages checks the message count in the last provider request.

func (*Harness) AssertLastRequestHasTool

func (h *Harness) AssertLastRequestHasTool(name string)

AssertLastRequestHasTool checks that the last request included a tool with the given name.

func (*Harness) AssertNoError

func (h *Harness) AssertNoError(err error)

AssertNoError fails the test if err is not nil.

func (*Harness) AssertNoEventsPublished

func (h *Harness) AssertNoEventsPublished()

AssertNoEventsPublished checks that no events were published.

func (*Harness) AssertProviderCalledN

func (h *Harness) AssertProviderCalledN(n int)

AssertProviderCalledN times.

func (*Harness) AssertSessionID

func (h *Harness) AssertSessionID(agent *core.Agent, want string)

AssertSessionID checks the session ID.

func (*Harness) AssertSessionIDNotEmpty

func (h *Harness) AssertSessionIDNotEmpty(agent *core.Agent)

AssertSessionIDNotEmpty checks that the session ID is non-empty.

func (*Harness) AssertStateHasNMessages

func (h *Harness) AssertStateHasNMessages(agent *core.Agent, n int)

AssertStateHasNMessages checks the agent state message count.

func (*Harness) AssertStateHasTokens

func (h *Harness) AssertStateHasTokens(agent *core.Agent, want int)

AssertStateHasTokens checks the total token count in state.

func (*Harness) AssertSystemPromptEquals

func (h *Harness) AssertSystemPromptEquals(want string)

AssertSystemPromptEquals checks the system prompt content.

func (*Harness) EventBus

func (h *Harness) EventBus() *events.EventBus

EventBus returns the event bus.

func (*Harness) Executor

func (h *Harness) Executor() *MockExecutor

Executor returns the mock executor for configuration.

func (*Harness) FindEvents

func (h *Harness) FindEvents(eventType string) []events.UIEvent

FindEvents returns all collected events of the given type.

func (*Harness) NewAgent

func (h *Harness) NewAgent() *core.Agent

NewAgent creates a new agent wired to the harness mocks.

func (*Harness) NewAgentWithOptions

func (h *Harness) NewAgentWithOptions(opts core.Options) *core.Agent

NewAgentWithOptions creates an agent with custom options. Provider and Executor are set from the harness; other options are from opts.

func (*Harness) Provider

func (h *Harness) Provider() *MockProvider

Provider returns the mock provider for configuration.

func (*Harness) Reset

func (h *Harness) Reset()

Reset clears all mock state for reuse.

func (*Harness) Run

func (h *Harness) Run(query string) (string, error)

Run is a convenience method: creates an agent, runs a query, returns result.

func (*Harness) RunWithAgent

func (h *Harness) RunWithAgent(agent *core.Agent, query string) (string, error)

RunWithAgent runs a query on the given agent.

func (*Harness) UI

func (h *Harness) UI() *MockUI

UI returns the mock UI for configuration.

type MockExecutor

type MockExecutor struct {

	// Recorded calls for assertion
	Calls [][]core.ToolCall
	// contains filtered or unexported fields
}

MockExecutor is a recording mock implementation of core.ToolExecutor. Configure tool results via AddResult or SetResults.

func NewMockExecutor

func NewMockExecutor() *MockExecutor

NewMockExecutor creates an empty MockExecutor.

func (*MockExecutor) AddResult

func (m *MockExecutor) AddResult(msg core.Message) *MockExecutor

AddResult appends a tool result message to the sequence.

func (*MockExecutor) AddTool

func (m *MockExecutor) AddTool(tool core.Tool) *MockExecutor

AddTool adds a single tool.

func (*MockExecutor) AddToolResult

func (m *MockExecutor) AddToolResult(toolCallID, content string) *MockExecutor

AddToolResult adds a tool result for a specific call ID.

func (*MockExecutor) CallCount

func (m *MockExecutor) CallCount() int

CallCount returns the number of Execute calls made.

func (*MockExecutor) Execute

func (m *MockExecutor) Execute(_ context.Context, calls []core.ToolCall) []core.Message

Execute implements core.ToolExecutor.

func (*MockExecutor) GetTools

func (m *MockExecutor) GetTools() []core.Tool

GetTools implements core.ToolExecutor.

func (*MockExecutor) LastCalls

func (m *MockExecutor) LastCalls() []core.ToolCall

LastCalls returns the tool calls from the last Execute call.

func (*MockExecutor) Reset

func (m *MockExecutor) Reset()

Reset clears recorded calls and resets the result index.

func (*MockExecutor) SetResults

func (m *MockExecutor) SetResults(results []core.Message) *MockExecutor

SetResults replaces the entire result sequence.

func (*MockExecutor) WithTools

func (m *MockExecutor) WithTools(tools []core.Tool) *MockExecutor

WithTools sets the available tools.

type MockProvider

type MockProvider struct {

	// Recorded calls for assertion
	Calls []*core.ChatRequest
	// contains filtered or unexported fields
}

MockProvider is a scriptable mock implementation of core.Provider. Configure it with a sequence of responses via AddResponse or SetResponses. It records every Chat call for assertion.

func NewMockProvider

func NewMockProvider() *MockProvider

NewMockProvider creates a MockProvider with default info.

func (*MockProvider) AddError

func (m *MockProvider) AddError(err error) *MockProvider

AddError appends an error to the sequence. The next call to Chat will return this error.

func (*MockProvider) AddMalformedResponse

func (m *MockProvider) AddMalformedResponse(content string) *MockProvider

AddMalformedResponse adds a response where tool calls are embedded in the content string rather than in the structured ToolCalls field. This simulates a malformed LLM response that the fallback parser must recover from. The content should contain tool-call-like patterns (e.g., JSON with "tool_calls", XML <function=...>, etc.) so the fallback parser detects them.

func (*MockProvider) AddResponse

func (m *MockProvider) AddResponse(resp *core.ChatResponse) *MockProvider

AddResponse appends a response to the sequence. The next call to Chat will return this response.

func (*MockProvider) AddStreamChunks

func (m *MockProvider) AddStreamChunks(chunks ...string) *MockProvider

AddStreamChunks configures explicit chunk sequences for streaming. Each call to ChatStream will use the next chunk sequence in order.

func (*MockProvider) AddTextResponse

func (m *MockProvider) AddTextResponse(content string) *MockProvider

AddTextResponse adds a simple text response (no tool calls).

func (*MockProvider) AddTextResponseWithFinish

func (m *MockProvider) AddTextResponseWithFinish(content, finishReason string) *MockProvider

AddTextResponseWithFinish adds a simple text response with a custom finish reason. Useful for testing finish-reason-specific behavior (e.g., "length", "stop", "content_filter").

func (*MockProvider) AddToolCallResponse

func (m *MockProvider) AddToolCallResponse(content string, toolCalls ...core.ToolCall) *MockProvider

AddToolCallResponse adds a response that includes tool calls.

func (*MockProvider) BlockOnCallN

func (m *MockProvider) BlockOnCallN(n int) chan struct{}

BlockOnCallN causes the Nth Chat call (1-indexed) to block until the returned channel is closed. This is useful for cancellation tests where you want the first N-1 calls to succeed normally.

func (*MockProvider) BlockUntil

func (m *MockProvider) BlockUntil() chan struct{}

BlockUntil causes the next Chat call to block until the returned channel is closed. Use this in cancellation tests: cancel the channel to unblock the provider so the context cancellation is observed.

func (*MockProvider) CallCount

func (m *MockProvider) CallCount() int

CallCount returns the number of Chat calls made.

func (*MockProvider) Chat

Chat implements core.Provider.

func (*MockProvider) ChatStream

func (m *MockProvider) ChatStream(ctx context.Context, req *core.ChatRequest, handler core.StreamHandler) error

ChatStream implements core.Provider. When streaming is enabled (via WithStreaming), it delivers the configured response as chunked content via the StreamHandler. Otherwise, it calls OnDone with the response directly.

func (*MockProvider) EstimateTokens

func (m *MockProvider) EstimateTokens(_ *core.ChatRequest) int

EstimateTokens implements core.Provider.

func (*MockProvider) Info

func (m *MockProvider) Info() core.ProviderInfo

Info implements core.Provider.

func (*MockProvider) LastRequest

func (m *MockProvider) LastRequest() *core.ChatRequest

LastRequest returns the last Chat request, or nil.

func (*MockProvider) Reset

func (m *MockProvider) Reset()

Reset clears all recorded calls and resets the response index.

func (*MockProvider) SetResponses

func (m *MockProvider) SetResponses(resps []*core.ChatResponse) *MockProvider

SetResponses replaces the entire response sequence.

func (*MockProvider) WithInfo

func (m *MockProvider) WithInfo(info core.ProviderInfo) *MockProvider

WithInfo sets the provider info.

func (*MockProvider) WithStreamDelay

func (m *MockProvider) WithStreamDelay(delay time.Duration) *MockProvider

WithStreamDelay sets the delay between streaming chunks.

func (*MockProvider) WithStreaming

func (m *MockProvider) WithStreaming() *MockProvider

WithStreaming enables streaming mode. When true, ChatStream will deliver the response content as chunks via the StreamHandler instead of returning it all at once. Configure chunks with AddStreamChunks or use the content from AddTextResponse / AddToolCallResponse (split into word-sized chunks).

func (*MockProvider) WithTokenEstimate

func (m *MockProvider) WithTokenEstimate(count int) *MockProvider

WithTokenEstimate sets the token estimate returned by EstimateTokens.

type MockUI

type MockUI struct {

	// Recorded calls for assertion
	Prompts    []string
	Confirms   []string
	Prints     []string
	PrintLines []string
	// contains filtered or unexported fields
}

MockUI is a recording mock implementation of core.UI. It captures all Print/PrintLine output and can be pre-configured with prompt/confirm responses.

func NewMockUI

func NewMockUI() *MockUI

NewMockUI creates a new MockUI.

func (*MockUI) Confirm

func (m *MockUI) Confirm(message string) (bool, error)

Confirm implements core.UI.

func (*MockUI) LineOutput

func (m *MockUI) LineOutput() string

LineOutput returns all PrintLine output (with newlines).

func (*MockUI) Output

func (m *MockUI) Output() string

Output returns all Print output (without newlines).

func (*MockUI) Print

func (m *MockUI) Print(message string)

Print implements core.UI.

func (*MockUI) PrintLine

func (m *MockUI) PrintLine(message string)

PrintLine implements core.UI.

func (*MockUI) Prompt

func (m *MockUI) Prompt(message string) (string, error)

Prompt implements core.UI.

func (*MockUI) Reset

func (m *MockUI) Reset()

Reset clears all recorded output and calls.

func (*MockUI) WithConfirmResponse

func (m *MockUI) WithConfirmResponse(resp bool) *MockUI

WithConfirmResponse sets the response returned by the next Confirm call.

func (*MockUI) WithPromptResponse

func (m *MockUI) WithPromptResponse(resp string) *MockUI

WithPromptResponse sets the response returned by the next Prompt call.

Jump to

Keyboard shortcuts

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