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: "start", FinalStates: []string{"end"}, States: []statemachine.StateConfig{ {Name: "start", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "middle", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "end", Type: "final"}, }, Transitions: []statemachine.TransitionConfig{ {From: "start", To: "middle", Condition: "always"}, {From: "middle", To: "end", Condition: "always"}, }, } }, Branching: func() *statemachine.Config { return &statemachine.Config{ Name: "branching", InitialState: "start", FinalStates: []string{"success", "failure"}, States: []statemachine.StateConfig{ {Name: "start", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "success", Type: "final"}, {Name: "failure", Type: "final"}, }, Transitions: []statemachine.TransitionConfig{ {From: "start", To: "success", Condition: "result.success"}, {From: "start", To: "failure", Condition: "result.failure"}, }, } }, Loop: func() *statemachine.Config { return &statemachine.Config{ Name: "loop", InitialState: "start", FinalStates: []string{"complete"}, States: []statemachine.StateConfig{ {Name: "start", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "retry", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "complete", Type: "final"}, }, Transitions: []statemachine.TransitionConfig{ {From: "start", To: "retry", Condition: "always"}, {From: "retry", To: "retry", Condition: "attempts < 3"}, {From: "retry", To: "complete", Condition: "attempts >= 3"}, }, } }, Complex: func() *statemachine.Config { return &statemachine.Config{ Name: "complex", InitialState: "init", FinalStates: []string{"success", "failure"}, States: []statemachine.StateConfig{ {Name: "init", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "validate", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "process", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "retry", Type: "action", Actions: []statemachine.ActionConfig{{Type: "noop", Name: "test"}}}, {Name: "success", Type: "final"}, {Name: "failure", Type: "final"}, }, Transitions: []statemachine.TransitionConfig{ {From: "init", To: "validate", Condition: "always"}, {From: "validate", To: "process", Condition: "valid"}, {From: "validate", To: "failure", Condition: "!valid"}, {From: "process", To: "success", Condition: "success"}, {From: "process", To: "retry", Condition: "retryable"}, {From: "retry", To: "process", Condition: "attempts < 3"}, {From: "retry", To: "failure", 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.