Documentation
¶
Overview ¶
Package test provides utilities for testing Kit extensions.
This package allows extension authors to write standard Go tests that load and exercise their extensions in a controlled environment. Extensions are loaded into a Yaegi interpreter with all Kit API symbols available.
Basic usage:
package main
import (
"testing"
"github.com/mark3labs/kit/internal/extensions/test"
)
func TestMyExtension(t *testing.T) {
// Create a test harness
harness := test.New(t)
// Load your extension file
ext := harness.LoadFile("my-ext.go")
// Emit events and check results
result := harness.Emit(test.ToolCallEvent{
ToolName: "my_tool",
Input: `{"key": "value"}`,
})
// Use assertion helpers
test.AssertNotBlocked(t, result)
test.AssertPrinted(t, harness, "expected output")
}
The harness provides a mock Context that records all interactions, allowing you to verify that your extension called SetWidget, Print, etc.
Index ¶
- func AssertBlocked(t *testing.T, result extensions.Result, expectedReason string)
- func AssertCancelAndSend(t *testing.T, harness *Harness, expected string)
- func AssertCommandRegistered(t *testing.T, harness *Harness, cmdName string)
- func AssertFooterSet(t *testing.T, harness *Harness)
- func AssertHasHandlers(t *testing.T, harness *Harness, eventType extensions.EventType)
- func AssertHeaderSet(t *testing.T, harness *Harness)
- func AssertInputHandled(t *testing.T, result extensions.Result, expectedAction string)
- func AssertInputTransformed(t *testing.T, result extensions.Result, expectedText string)
- func AssertMessageSent(t *testing.T, harness *Harness, expected string)
- func AssertNoHandlers(t *testing.T, harness *Harness, eventType extensions.EventType)
- func AssertNotBlocked(t *testing.T, result extensions.Result)
- func AssertPrintError(t *testing.T, harness *Harness, expected string)
- func AssertPrintInfo(t *testing.T, harness *Harness, expected string)
- func AssertPrinted(t *testing.T, harness *Harness, expected string)
- func AssertPrintedContains(t *testing.T, harness *Harness, substring string)
- func AssertStatusSet(t *testing.T, harness *Harness, key string)
- func AssertStatusText(t *testing.T, harness *Harness, key string, expected string)
- func AssertToolRegistered(t *testing.T, harness *Harness, toolName string)
- func AssertWidgetNotSet(t *testing.T, harness *Harness, id string)
- func AssertWidgetSet(t *testing.T, harness *Harness, id string)
- func AssertWidgetText(t *testing.T, harness *Harness, id string, expected string)
- func AssertWidgetTextContains(t *testing.T, harness *Harness, id string, substring string)
- func GetInputResult(result extensions.Result) *extensions.InputResult
- func GetToolCallResult(result extensions.Result) *extensions.ToolCallResult
- func GetToolResultResult(result extensions.Result) *extensions.ToolResultResult
- type Harness
- func (h *Harness) Context() *MockContext
- func (h *Harness) Emit(event extensions.Event) (extensions.Result, error)
- func (h *Harness) EmitJSON(toolName string, input string) (*extensions.ToolCallResult, error)
- func (h *Harness) HasHandlers(eventType extensions.EventType) bool
- func (h *Harness) LoadFile(path string) *extensions.LoadedExtension
- func (h *Harness) LoadString(src string, path string) *extensions.LoadedExtension
- func (h *Harness) MustLoad(path string) *Harness
- func (h *Harness) RegisteredCommands() []extensions.CommandDef
- func (h *Harness) RegisteredTools() []extensions.ToolDef
- func (h *Harness) Runner() *extensions.Runner
- type MockContext
- func (m *MockContext) GetFooter() *extensions.HeaderFooterConfig
- func (m *MockContext) GetHeader() *extensions.HeaderFooterConfig
- func (m *MockContext) GetPrintErrors() []string
- func (m *MockContext) GetPrintInfos() []string
- func (m *MockContext) GetPrints() []string
- func (m *MockContext) GetStatus(key string) (extensions.StatusBarEntry, bool)
- func (m *MockContext) GetWidget(id string) (extensions.WidgetConfig, bool)
- func (m *MockContext) HasWidget(id string) bool
- func (m *MockContext) SetPromptConfirmResult(result extensions.PromptConfirmResult)
- func (m *MockContext) SetPromptInputResult(result extensions.PromptInputResult)
- func (m *MockContext) SetPromptMultiSelectResult(result extensions.PromptMultiSelectResult)
- func (m *MockContext) SetPromptSelectResult(result extensions.PromptSelectResult)
- func (m *MockContext) ToContext() extensions.Context
- type StatusBarEntry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertBlocked ¶
func AssertBlocked(t *testing.T, result extensions.Result, expectedReason string)
AssertBlocked fails the test if the tool call result does not indicate the tool was blocked.
func AssertCancelAndSend ¶
AssertCancelAndSend fails the test if the expected text was not sent via CancelAndSend.
func AssertCommandRegistered ¶
AssertCommandRegistered fails the test if the command with the given name was not registered.
func AssertFooterSet ¶
AssertFooterSet fails the test if no footer was set.
func AssertHasHandlers ¶
func AssertHasHandlers(t *testing.T, harness *Harness, eventType extensions.EventType)
AssertHasHandlers fails the test if no handlers are registered for the given event type.
func AssertHeaderSet ¶
AssertHeaderSet fails the test if no header was set.
func AssertInputHandled ¶
func AssertInputHandled(t *testing.T, result extensions.Result, expectedAction string)
AssertInputHandled fails the test if the input result does not indicate the input was handled.
func AssertInputTransformed ¶
func AssertInputTransformed(t *testing.T, result extensions.Result, expectedText string)
AssertInputTransformed fails the test if the input was not transformed to the expected text.
func AssertMessageSent ¶
AssertMessageSent fails the test if the expected message was not sent.
func AssertNoHandlers ¶
func AssertNoHandlers(t *testing.T, harness *Harness, eventType extensions.EventType)
AssertNoHandlers fails the test if any handlers are registered for the given event type.
func AssertNotBlocked ¶
func AssertNotBlocked(t *testing.T, result extensions.Result)
AssertNotBlocked fails the test if the tool call result indicates the tool was blocked.
func AssertPrintError ¶
AssertPrintError fails the test if the expected error message was not printed.
func AssertPrintInfo ¶
AssertPrintInfo fails the test if the expected info message was not printed.
func AssertPrinted ¶
AssertPrinted fails the test if the expected text was not printed.
func AssertPrintedContains ¶
AssertPrintedContains fails the test if no printed text contains the expected substring.
func AssertStatusSet ¶
AssertStatusSet fails the test if the status with the given key was not set.
func AssertStatusText ¶
AssertStatusText fails the test if the status with the given key does not have the expected text.
func AssertToolRegistered ¶
AssertToolRegistered fails the test if the tool with the given name was not registered.
func AssertWidgetNotSet ¶
AssertWidgetNotSet fails the test if the widget with the given ID was set.
func AssertWidgetSet ¶
AssertWidgetSet fails the test if the widget with the given ID was not set.
func AssertWidgetText ¶
AssertWidgetText fails the test if the widget with the given ID does not have the expected text.
func AssertWidgetTextContains ¶
AssertWidgetTextContains fails the test if the widget text does not contain the expected substring.
func GetInputResult ¶
func GetInputResult(result extensions.Result) *extensions.InputResult
GetInputResult extracts an InputResult from a Result, or nil if not applicable.
func GetToolCallResult ¶
func GetToolCallResult(result extensions.Result) *extensions.ToolCallResult
GetToolCallResult extracts a ToolCallResult from a Result, or nil if not applicable.
func GetToolResultResult ¶
func GetToolResultResult(result extensions.Result) *extensions.ToolResultResult
GetToolResultResult extracts a ToolResultResult from a Result, or nil if not applicable.
Types ¶
type Harness ¶
type Harness struct {
// contains filtered or unexported fields
}
Harness provides a testing environment for Kit extensions. It loads extensions into an isolated Yaegi interpreter and provides methods to emit events and verify extension behavior.
func New ¶
New creates a new test harness for the given test. The harness must be used within a single test function.
func (*Harness) Context ¶
func (h *Harness) Context() *MockContext
Context returns the mock context for inspection. Use this to verify Print calls, widget settings, etc.
func (*Harness) Emit ¶
func (h *Harness) Emit(event extensions.Event) (extensions.Result, error)
Emit sends an event to the loaded extension(s) and returns the result. Events are dispatched in order and blocking results stop propagation.
func (*Harness) EmitJSON ¶
func (h *Harness) EmitJSON(toolName string, input string) (*extensions.ToolCallResult, error)
EmitJSON is a convenience method for emitting a ToolCallEvent with JSON input.
func (*Harness) HasHandlers ¶
func (h *Harness) HasHandlers(eventType extensions.EventType) bool
HasHandlers reports whether any handlers are registered for the given event type.
func (*Harness) LoadFile ¶
func (h *Harness) LoadFile(path string) *extensions.LoadedExtension
LoadFile loads an extension from a file path. The extension is evaluated in a fresh Yaegi interpreter with all Kit API symbols available. The Init function is called automatically.
Returns the loaded extension or fails the test on error.
func (*Harness) LoadString ¶
func (h *Harness) LoadString(src string, path string) *extensions.LoadedExtension
LoadString loads an extension from a source string. Useful for inline extension tests. The path is used for error reporting.
func (*Harness) MustLoad ¶
MustLoad is like LoadFile but fails the test immediately on error. It returns the harness for chaining.
func (*Harness) RegisteredCommands ¶
func (h *Harness) RegisteredCommands() []extensions.CommandDef
RegisteredCommands returns all commands registered by the extension.
func (*Harness) RegisteredTools ¶
func (h *Harness) RegisteredTools() []extensions.ToolDef
RegisteredTools returns all tools registered by the extension.
func (*Harness) Runner ¶
func (h *Harness) Runner() *extensions.Runner
Runner returns the underlying runner for advanced use cases.
type MockContext ¶
type MockContext struct {
// Recorded calls
Prints []string
PrintInfos []string
PrintErrors []string
PrintBlocks []extensions.PrintBlockOpts
Messages []string
CancelSends []string
// Widget state
Widgets map[string]extensions.WidgetConfig
RemovedIDs []string
Header *extensions.HeaderFooterConfig
HeaderRemoved bool
// Context properties
SessionID string
CWD string
Model string
Interactive bool
// UI visibility
UIVisibility *extensions.UIVisibility
// Status entries
StatusEntries map[string]extensions.StatusBarEntry
RemovedStatus []string
// Editor
EditorConfig *extensions.EditorConfig
EditorReset bool
EditorTexts []string
// Options
Options map[string]string
// Prompt results (configurable for testing)
PromptSelectResult extensions.PromptSelectResult
PromptConfirmResult extensions.PromptConfirmResult
PromptInputResult extensions.PromptInputResult
PromptMultiSelectResult extensions.PromptMultiSelectResult
// Overlay
Overlays []extensions.OverlayConfig
// contains filtered or unexported fields
}
MockContext records all interactions with the extension context. It provides a Context object that captures Print calls, widget settings, and other context operations for verification in tests.
func NewMockContext ¶
func NewMockContext() *MockContext
NewMockContext creates a new mock context with default values.
func (*MockContext) GetFooter ¶
func (m *MockContext) GetFooter() *extensions.HeaderFooterConfig
GetFooter returns the recorded footer configuration.
func (*MockContext) GetHeader ¶
func (m *MockContext) GetHeader() *extensions.HeaderFooterConfig
GetHeader returns the recorded header configuration.
func (*MockContext) GetPrintErrors ¶
func (m *MockContext) GetPrintErrors() []string
GetPrintErrors returns all recorded PrintError calls.
func (*MockContext) GetPrintInfos ¶
func (m *MockContext) GetPrintInfos() []string
GetPrintInfos returns all recorded PrintInfo calls.
func (*MockContext) GetPrints ¶
func (m *MockContext) GetPrints() []string
GetPrints returns all recorded Print calls.
func (*MockContext) GetStatus ¶
func (m *MockContext) GetStatus(key string) (extensions.StatusBarEntry, bool)
GetStatus returns a recorded status entry by key.
func (*MockContext) GetWidget ¶
func (m *MockContext) GetWidget(id string) (extensions.WidgetConfig, bool)
GetWidget returns a recorded widget by ID.
func (*MockContext) HasWidget ¶
func (m *MockContext) HasWidget(id string) bool
HasWidget reports whether a widget with the given ID was set.
func (*MockContext) SetPromptConfirmResult ¶
func (m *MockContext) SetPromptConfirmResult(result extensions.PromptConfirmResult)
SetPromptConfirmResult configures the result returned by PromptConfirm.
func (*MockContext) SetPromptInputResult ¶
func (m *MockContext) SetPromptInputResult(result extensions.PromptInputResult)
SetPromptInputResult configures the result returned by PromptInput.
func (*MockContext) SetPromptMultiSelectResult ¶
func (m *MockContext) SetPromptMultiSelectResult(result extensions.PromptMultiSelectResult)
SetPromptMultiSelectResult configures the result returned by PromptMultiSelect.
func (*MockContext) SetPromptSelectResult ¶
func (m *MockContext) SetPromptSelectResult(result extensions.PromptSelectResult)
SetPromptSelectResult configures the result returned by PromptSelect.
func (*MockContext) ToContext ¶
func (m *MockContext) ToContext() extensions.Context
ToContext returns a extensions.Context wired to record all interactions.
type StatusBarEntry ¶
StatusBarEntry represents a recorded status bar entry