Documentation
¶
Overview ¶
Package testing provides testing utilities for state machine workflows.
Index ¶
- Variables
- func CreateTestConfig(name string, initialState string, finalStates []string) *statemachine.Config
- func CreateTestContext(data map[string]any) *statemachine.Context
- func LoadTestConfig(name string) (*statemachine.Config, error)
- func RunScenario(t *testing.T, scenario TestScenario)
- func SaveTestConfig(name string, config *statemachine.Config) error
- type Assertion
- type Matcher
- func All(matchers ...Matcher) Matcher
- func Any(matchers ...Matcher) Matcher
- func ContextContains(key string, value any) Matcher
- func ExecutionCompleted() Matcher
- func ExecutionFailed() Matcher
- func ExecutionTookLessThan(duration time.Duration) Matcher
- func StateWasVisited(name string) Matcher
- func TransitionWasTaken(from, to string) Matcher
- type MockElicitationClient
- type MockSamplingClient
- type TestEngine
- func (te *TestEngine) AssertContextValue(key string, expected any)
- func (te *TestEngine) AssertExecutionTime(maxDuration time.Duration)
- func (te *TestEngine) AssertFinalState(expected string)
- func (te *TestEngine) AssertStateVisited(stateName string)
- func (te *TestEngine) AssertTransitionTaken(from, to string)
- func (te *TestEngine) Execute(ctx context.Context, smCtx *statemachine.Context) error
- func (te *TestEngine) GetAssertions() []Assertion
- func (te *TestEngine) GetTrace() []TraceEntry
- type TestScenario
- type TraceEntry
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoMockResponseForPrompt = errors.New("no mock response for prompt") ErrNoMockResponseForQuestion = errors.New("no mock response for question") )
Test helper errors.
var ( ErrNoExecutionTrace = errors.New("no execution trace available") ErrExecutionCompletedNoError = errors.New("execution completed without error") ErrNoMatchersPassed = errors.New("no matchers passed") ErrStateNotVisited = errors.New("state was not visited") ErrTransitionNotTaken = errors.New("transition was not taken") ErrContextKeyNotExist = errors.New("context key does not exist") ErrContextValueMismatch = errors.New("context value mismatch") ErrExecutionTooSlow = errors.New("execution exceeded time limit") )
Matcher errors.
var CommonTestConfigs = struct { Linear func() *statemachine.Config Branching func() *statemachine.Config Loop func() *statemachine.Config Complex func() *statemachine.Config }{ Linear: func() *statemachine.Config { return &statemachine.Config{ Name: "linear", InitialState: stateStart, FinalStates: []string{stateEnd}, States: []statemachine.StateConfig{ actionState(stateStart), actionState(stateMiddle), finalState(stateEnd), }, Transitions: []statemachine.TransitionConfig{ {From: stateStart, To: stateMiddle, Condition: conditionAlways}, {From: stateMiddle, To: stateEnd, Condition: conditionAlways}, }, } }, Branching: func() *statemachine.Config { return &statemachine.Config{ Name: "branching", InitialState: stateStart, FinalStates: []string{stateSuccess, stateFailure}, States: []statemachine.StateConfig{ actionState(stateStart), finalState(stateSuccess), finalState(stateFailure), }, Transitions: []statemachine.TransitionConfig{ {From: stateStart, To: stateSuccess, Condition: "result.success"}, {From: stateStart, To: stateFailure, Condition: "result.failure"}, }, } }, Loop: func() *statemachine.Config { return &statemachine.Config{ Name: "loop", InitialState: stateStart, FinalStates: []string{stateComplete}, States: []statemachine.StateConfig{ actionState(stateStart), actionState(stateRetry), finalState(stateComplete), }, Transitions: []statemachine.TransitionConfig{ {From: stateStart, To: stateRetry, Condition: conditionAlways}, {From: stateRetry, To: stateRetry, Condition: "attempts < 3"}, {From: stateRetry, To: stateComplete, Condition: "attempts >= 3"}, }, } }, Complex: func() *statemachine.Config { return &statemachine.Config{ Name: "complex", InitialState: stateInit, FinalStates: []string{stateSuccess, stateFailure}, States: []statemachine.StateConfig{ actionState(stateInit), actionState(stateValidate), actionState(stateProcess), actionState(stateRetry), finalState(stateSuccess), finalState(stateFailure), }, Transitions: []statemachine.TransitionConfig{ {From: stateInit, To: stateValidate, Condition: conditionAlways}, {From: stateValidate, To: stateProcess, Condition: "valid"}, {From: stateValidate, To: stateFailure, Condition: "!valid"}, {From: stateProcess, To: stateSuccess, Condition: "success"}, {From: stateProcess, To: stateRetry, Condition: "retryable"}, {From: stateRetry, To: stateProcess, Condition: "attempts < 3"}, {From: stateRetry, To: stateFailure, Condition: "attempts >= 3"}, }, } }, }
CommonTestConfigs provides frequently used test configurations.
Functions ¶
func CreateTestConfig ¶
func CreateTestConfig(name string, initialState string, finalStates []string) *statemachine.Config
CreateTestConfig creates a simple test config.
func CreateTestContext ¶
func CreateTestContext(data map[string]any) *statemachine.Context
CreateTestContext creates a context with test data.
func LoadTestConfig ¶
func LoadTestConfig(name string) (*statemachine.Config, error)
LoadTestConfig loads a config from the testdata directory.
func RunScenario ¶
func RunScenario(t *testing.T, scenario TestScenario)
RunScenario executes a test scenario and validates results.
func SaveTestConfig ¶
func SaveTestConfig(name string, config *statemachine.Config) error
SaveTestConfig saves a config to the testdata directory.
Types ¶
type Matcher ¶
type Matcher interface {
Match(engine *TestEngine) (bool, error)
Description() string
}
Matcher defines an assertion matcher interface.
func ContextContains ¶
ContextContains creates a matcher that checks context values.
func ExecutionCompleted ¶
func ExecutionCompleted() Matcher
ExecutionCompleted creates a matcher that checks if execution completed successfully.
func ExecutionFailed ¶
func ExecutionFailed() Matcher
ExecutionFailed creates a matcher that checks if execution failed.
func ExecutionTookLessThan ¶
ExecutionTookLessThan creates a matcher that checks execution duration.
func StateWasVisited ¶
StateWasVisited creates a matcher that checks if a state was visited.
func TransitionWasTaken ¶
TransitionWasTaken creates a matcher that checks if a transition occurred.
type MockElicitationClient ¶
type MockElicitationClient struct {
// contains filtered or unexported fields
}
MockElicitationClient creates a mock elicitation client for testing.
func NewMockElicitationClient ¶
func NewMockElicitationClient(responses map[string]any) *MockElicitationClient
NewMockElicitationClient creates a mock elicitation client with predefined responses.
type MockSamplingClient ¶
type MockSamplingClient struct {
// contains filtered or unexported fields
}
MockSamplingClient creates a mock sampling client for testing.
func NewMockSamplingClient ¶
func NewMockSamplingClient(responses map[string]string) *MockSamplingClient
NewMockSamplingClient creates a mock sampling client with predefined responses.
type TestEngine ¶
type TestEngine struct {
*statemachine.Engine
// contains filtered or unexported fields
}
TestEngine wraps Engine with testing utilities.
func NewTestEngine ¶
func NewTestEngine(t *testing.T, config *statemachine.Config) *TestEngine
NewTestEngine creates a test engine for a config with default factory.
func NewTestEngineWithFactory ¶
func NewTestEngineWithFactory( t *testing.T, config *statemachine.Config, factory *statemachine.ActionFactory, ) *TestEngine
NewTestEngineWithFactory creates a test engine for a config with a custom factory.
func (*TestEngine) AssertContextValue ¶
func (te *TestEngine) AssertContextValue(key string, expected any)
AssertContextValue checks context value at end of execution.
func (*TestEngine) AssertExecutionTime ¶
func (te *TestEngine) AssertExecutionTime(maxDuration time.Duration)
AssertExecutionTime checks total execution time.
func (*TestEngine) AssertFinalState ¶
func (te *TestEngine) AssertFinalState(expected string)
AssertFinalState checks the final state matches expected.
func (*TestEngine) AssertStateVisited ¶
func (te *TestEngine) AssertStateVisited(stateName string)
AssertStateVisited checks if a state was visited during execution.
func (*TestEngine) AssertTransitionTaken ¶
func (te *TestEngine) AssertTransitionTaken(from, to string)
AssertTransitionTaken checks if a specific transition occurred.
func (*TestEngine) Execute ¶
func (te *TestEngine) Execute(ctx context.Context, smCtx *statemachine.Context) error
Execute runs the state machine and records execution trace.
func (*TestEngine) GetAssertions ¶
func (te *TestEngine) GetAssertions() []Assertion
GetAssertions returns all assertions made.
func (*TestEngine) GetTrace ¶
func (te *TestEngine) GetTrace() []TraceEntry
GetTrace returns the execution trace for inspection.
type TestScenario ¶
type TestScenario struct {
Name string
Config *statemachine.Config
InitialContext map[string]any
MockResponses map[string]any
Assertions []Assertion
}
TestScenario represents a complete test scenario for a state machine.
func BranchingWorkflowScenario ¶
func BranchingWorkflowScenario() TestScenario
BranchingWorkflowScenario creates a scenario for testing branching logic.
func ErrorRecoveryScenario ¶
func ErrorRecoveryScenario() TestScenario
ErrorRecoveryScenario creates a scenario for testing error recovery.
func LinearWorkflowScenario ¶
func LinearWorkflowScenario() TestScenario
LinearWorkflowScenario creates a scenario for testing linear workflows.
func RetryScenario ¶
func RetryScenario() TestScenario
RetryScenario creates a scenario for testing retry logic.