testutil

package
v0.0.361 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertApprovalShown

func AssertApprovalShown(t *testing.T, output, description string)

AssertApprovalShown verifies that an approval prompt was displayed.

func AssertContains

func AssertContains(t *testing.T, output, expected string)

AssertContains fails the test if output does not contain expected.

func AssertContainsPlain

func AssertContainsPlain(t *testing.T, output, expected string)

AssertContainsPlain fails if output (after stripping ANSI) does not contain expected.

func AssertLineCount

func AssertLineCount(t *testing.T, output string, expected int)

AssertLineCount fails if the output doesn't have the expected number of lines.

func AssertMatches

func AssertMatches(t *testing.T, output string, pattern *regexp.Regexp)

AssertMatches fails the test if output does not match the regex pattern.

func AssertMatchesPlain

func AssertMatchesPlain(t *testing.T, output string, pattern *regexp.Regexp)

AssertMatchesPlain fails if output (after stripping ANSI) does not match pattern.

func AssertMatchesString

func AssertMatchesString(t *testing.T, output, pattern string)

AssertMatchesString fails the test if output does not match the regex pattern string.

func AssertNotContains

func AssertNotContains(t *testing.T, output, unexpected string)

AssertNotContains fails the test if output contains unexpected.

func AssertNotContainsPlain

func AssertNotContainsPlain(t *testing.T, output, unexpected string)

AssertNotContainsPlain fails if output (after stripping ANSI) contains unexpected.

func AssertToolPhaseShown

func AssertToolPhaseShown(t *testing.T, output, toolName, info string)

AssertToolPhaseShown verifies that a tool execution phase was displayed. It checks for common patterns like "Reading main.go" or "Searching: query".

func DebugScreensEnabled

func DebugScreensEnabled() bool

DebugScreensEnabled returns true if DEBUG_SCREENS environment variable is set.

func ProcessHasExited added in v0.0.339

func ProcessHasExited(pid int) bool

ProcessHasExited reports whether pid no longer has executable code. On Linux, zombies count as exited because kill(pid, 0) continues to report them until their parent or init process reaps them.

func SaveFramesEnabled

func SaveFramesEnabled() bool

SaveFramesEnabled returns true if SAVE_FRAMES environment variable is set.

func StripANSI

func StripANSI(s string) string

StripANSI removes ANSI escape sequences from a string.

func WaitForProcessExit added in v0.0.339

func WaitForProcessExit(t testing.TB, pid int, timeout time.Duration)

WaitForProcessExit waits until pid is gone or is a zombie.

Types

type ApprovalAction

type ApprovalAction struct {
	WaitFor  string           // Wait for this text to appear before responding
	Response ApprovalResponse // How to respond
}

ApprovalAction defines what to do when an approval prompt appears.

type ApprovalRequest

type ApprovalRequest struct {
	Description string
	ToolName    string
	ToolInfo    string
}

ApprovalRequest is sent when approval is needed.

type ApprovalResponse

type ApprovalResponse int

ApprovalResponse represents how to respond to an approval prompt.

const (
	ApprovalApprove ApprovalResponse = iota
	ApprovalDeny
	ApprovalAbort
)

type ApprovalResponseMsg

type ApprovalResponseMsg struct {
	Approved bool
	Path     string
}

ApprovalResponseMsg is the response to an approval request.

type ByteCapture added in v0.0.358

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

ByteCapture records exact output bytes and the boundaries between Write calls. It is safe for renderers and delayed output callbacks to use concurrently.

func NewByteCapture added in v0.0.358

func NewByteCapture() *ByteCapture

NewByteCapture creates an empty byte-level output capture.

func (*ByteCapture) Bytes added in v0.0.358

func (c *ByteCapture) Bytes() []byte

Bytes returns a copy of all captured bytes.

func (*ByteCapture) Reset added in v0.0.358

func (c *ByteCapture) Reset()

Reset clears all captured output.

func (*ByteCapture) Write added in v0.0.358

func (c *ByteCapture) Write(p []byte) (int, error)

Write records p as one output write.

func (*ByteCapture) Writes added in v0.0.358

func (c *ByteCapture) Writes() [][]byte

Writes returns copies of the bytes passed to each Write call, in order.

type EngineHarness

type EngineHarness struct {
	Provider *llm.MockProvider
	Engine   *llm.Engine
	Registry *llm.ToolRegistry
	Screen   *ScreenCapture

	// Recorded data
	TextOutput     strings.Builder
	ToolCalls      []llm.ToolCall
	ToolExecutions []ToolExecution
	Events         []llm.Event
	// contains filtered or unexported fields
}

EngineHarness provides a test harness for engine-level testing without TUI.

func NewEngineHarness

func NewEngineHarness() *EngineHarness

NewEngineHarness creates a new engine test harness.

func (*EngineHarness) AddMockTool

func (h *EngineHarness) AddMockTool(name, result string) *MockTool

AddMockTool creates and registers a simple mock tool.

func (*EngineHarness) AddTool

func (h *EngineHarness) AddTool(tool llm.Tool)

AddTool registers a tool with the engine.

func (*EngineHarness) AutoApproveAll

func (h *EngineHarness) AutoApproveAll()

AutoApproveAll sets the harness to automatically approve all tool calls.

func (*EngineHarness) AutoDenyAll

func (h *EngineHarness) AutoDenyAll()

AutoDenyAll sets the harness to automatically deny all tool calls.

func (*EngineHarness) DumpScreen

func (h *EngineHarness) DumpScreen()

DumpScreen prints the final screen for debugging.

func (*EngineHarness) EnableScreenCapture

func (h *EngineHarness) EnableScreenCapture()

EnableScreenCapture enables screen capture.

func (*EngineHarness) GetOutput

func (h *EngineHarness) GetOutput() string

GetOutput returns the accumulated text output.

func (*EngineHarness) Reset

func (h *EngineHarness) Reset()

Reset clears all recorded data and resets the provider.

func (*EngineHarness) Run

func (h *EngineHarness) Run(ctx context.Context, req llm.Request) (string, error)

Run is an alias for Stream that returns the text output.

func (*EngineHarness) SaveFrames

func (h *EngineHarness) SaveFrames(dir string) error

SaveFrames saves all frames to a directory.

func (*EngineHarness) SaveScreen

func (h *EngineHarness) SaveScreen(path string) error

SaveScreen saves the screen to a file.

func (*EngineHarness) Stream

func (h *EngineHarness) Stream(ctx context.Context, req llm.Request) (string, error)

Stream runs a streaming request and collects all output.

type Frame

type Frame struct {
	Timestamp time.Time
	Raw       string // Raw output with ANSI codes
	Plain     string // Text without ANSI codes
	Phase     string // Phase at capture time (if known)
}

Frame represents a single screen capture.

type MockTool

type MockTool struct {
	SpecData    llm.ToolSpec
	ExecuteFn   func(ctx context.Context, args json.RawMessage) (llm.ToolOutput, error)
	PreviewFn   func(args json.RawMessage) string
	Invocations []MockToolInvocation
}

MockTool is a configurable tool for testing.

func NewMockTool

func NewMockTool(name string, result string) *MockTool

NewMockTool creates a mock tool with the given name that returns a fixed result.

func NewMockToolWithSchema

func NewMockToolWithSchema(name, description string, schema map[string]interface{}, executeFn func(ctx context.Context, args json.RawMessage) (llm.ToolOutput, error)) *MockTool

NewMockToolWithSchema creates a mock tool with a custom schema.

func (*MockTool) Execute

func (m *MockTool) Execute(ctx context.Context, args json.RawMessage) (llm.ToolOutput, error)

Execute implements llm.Tool.

func (*MockTool) InvocationCount

func (m *MockTool) InvocationCount() int

InvocationCount returns the number of times the tool was invoked.

func (*MockTool) LastArgs

func (m *MockTool) LastArgs() json.RawMessage

LastArgs returns the arguments from the last invocation, or nil if never invoked.

func (*MockTool) Preview

func (m *MockTool) Preview(args json.RawMessage) string

Preview implements llm.Tool.

func (*MockTool) Spec

func (m *MockTool) Spec() llm.ToolSpec

Spec implements llm.Tool.

type MockToolInvocation

type MockToolInvocation struct {
	Args   json.RawMessage
	Output llm.ToolOutput
	Result string // Shortcut for Output.Content
	Error  error
}

MockToolInvocation records a single tool invocation.

type ScreenCapture

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

ScreenCapture stores captured screen frames.

func NewScreenCapture

func NewScreenCapture() *ScreenCapture

NewScreenCapture creates a new screen capture.

func (*ScreenCapture) Capture

func (s *ScreenCapture) Capture(raw, phase string)

Capture records a new frame.

func (*ScreenCapture) Clear

func (s *ScreenCapture) Clear()

Clear removes all captured frames.

func (*ScreenCapture) Disable

func (s *ScreenCapture) Disable()

Disable turns off screen capture.

func (*ScreenCapture) Dump

func (s *ScreenCapture) Dump() string

Dump returns a debug string showing all frames.

func (*ScreenCapture) Enable

func (s *ScreenCapture) Enable()

Enable turns on screen capture.

func (*ScreenCapture) FinalScreen

func (s *ScreenCapture) FinalScreen() string

FinalScreen returns the raw content of the last frame.

func (*ScreenCapture) FinalScreenPlain

func (s *ScreenCapture) FinalScreenPlain() string

FinalScreenPlain returns the plain text content of the last frame.

func (*ScreenCapture) FrameCount

func (s *ScreenCapture) FrameCount() int

FrameCount returns the number of captured frames.

func (*ScreenCapture) Frames

func (s *ScreenCapture) Frames() []Frame

Frames returns all captured frames.

func (*ScreenCapture) IsEnabled

func (s *ScreenCapture) IsEnabled() bool

IsEnabled returns true if capture is enabled.

func (*ScreenCapture) LastFrame

func (s *ScreenCapture) LastFrame() Frame

LastFrame returns the most recent frame, or empty frame if none.

func (*ScreenCapture) RenderAllFrames

func (s *ScreenCapture) RenderAllFrames()

RenderAllFrames prints all frames with timestamps.

func (*ScreenCapture) RenderPlain

func (s *ScreenCapture) RenderPlain()

RenderPlain prints the final screen to stdout without ANSI codes.

func (*ScreenCapture) RenderScreen

func (s *ScreenCapture) RenderScreen()

RenderScreen prints the final screen to stdout with ANSI codes.

func (*ScreenCapture) SaveFrames

func (s *ScreenCapture) SaveFrames(dir string) error

SaveFrames saves each frame to a separate file in the given directory.

func (*ScreenCapture) SaveScreen

func (s *ScreenCapture) SaveScreen(path string) error

SaveScreen saves the final screen to a file.

func (*ScreenCapture) SaveScreenPlain

func (s *ScreenCapture) SaveScreenPlain(path string) error

SaveScreenPlain saves the final screen without ANSI codes to a file.

type TUIHarness

type TUIHarness struct {
	*EngineHarness
	// contains filtered or unexported fields
}

TUIHarness provides a test harness for TUI-level testing with bubbletea.

func NewTUIHarness

func NewTUIHarness() *TUIHarness

NewTUIHarness creates a new TUI test harness.

func (*TUIHarness) CapturedOutput

func (h *TUIHarness) CapturedOutput() string

CapturedOutput returns the captured TUI output.

func (*TUIHarness) GetApprovalResponse

func (h *TUIHarness) GetApprovalResponse() ApprovalResponse

GetApprovalResponse returns the response for the current approval.

func (*TUIHarness) OnApprovalPrompt

func (h *TUIHarness) OnApprovalPrompt(action ApprovalAction)

OnApprovalPrompt adds an approval action to the queue.

func (*TUIHarness) RunWithOutput

func (h *TUIHarness) RunWithOutput(ctx context.Context, req llm.Request) (textContent string, tuiOutput string, err error)

RunWithOutput runs a request and returns both the text content and TUI output.

func (*TUIHarness) SetTimeout

func (h *TUIHarness) SetTimeout(d time.Duration)

SetTimeout sets the maximum time to wait for operations.

type TerminalOutputHarness added in v0.0.358

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

TerminalOutputHarness runs Bubble Tea with deterministic terminal settings and captures both the complete byte stream and individual output writes.

func NewTerminalOutputHarness added in v0.0.358

func NewTerminalOutputHarness() *TerminalOutputHarness

NewTerminalOutputHarness creates a deterministic byte-level terminal harness.

func (*TerminalOutputHarness) Bytes added in v0.0.358

func (h *TerminalOutputHarness) Bytes() []byte

Bytes returns a copy of the complete terminal byte stream.

func (*TerminalOutputHarness) Run added in v0.0.358

func (h *TerminalOutputHarness) Run(model tea.Model) (tea.Model, error)

Run executes model with fixed terminal dimensions and environment.

func (*TerminalOutputHarness) RunCommands added in v0.0.358

func (h *TerminalOutputHarness) RunCommands(commands ...tea.Cmd) error

RunCommands executes commands in sequence and then exits the program.

func (*TerminalOutputHarness) Writes added in v0.0.358

func (h *TerminalOutputHarness) Writes() [][]byte

Writes returns copies of individual terminal writes in output order.

type ToolExecution

type ToolExecution struct {
	Name   string
	Args   json.RawMessage
	Result string
	Error  error
}

ToolExecution records a tool execution.

Jump to

Keyboard shortcuts

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