workflowtest

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package workflowtest provides test utilities for the workflow library. It follows the standard Go convention of separate test helper packages (net/http/httptest, io/iotest).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockActivity

func MockActivity(name string, result any) workflow.Activity

MockActivity creates a stub activity that always returns the given result.

func MockActivityError

func MockActivityError(name string, err error) workflow.Activity

MockActivityError creates a stub activity that always returns the given error.

func Run

func Run(
	t testing.TB,
	wf *workflow.Workflow,
	activities []workflow.Activity,
	inputs map[string]any,
) *workflow.ExecutionResult

Run executes a workflow with sensible defaults for testing. It uses an in-memory checkpointer, discards logs, and fails the test on infrastructure errors. Returns the execution result for assertions.

func RunWithOptions

func RunWithOptions(
	t testing.TB,
	wf *workflow.Workflow,
	activities []workflow.Activity,
	inputs map[string]any,
	opts TestOptions,
) *workflow.ExecutionResult

RunWithOptions is like Run but allows overriding execution options.

Types

type FakeContext

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

FakeContext is a workflow.Context implementation for consumer tests that want to unit-test activity code without constructing a full Execution. It is concurrent-safe and exposes the same surface as the engine-backed context.

func NewFakeContext

func NewFakeContext(opts FakeContextOptions) *FakeContext

NewFakeContext builds a FakeContext from the given options. The returned FakeContext satisfies workflow.Context.

func (*FakeContext) BranchID

func (f *FakeContext) BranchID() string

func (*FakeContext) Compiler

func (f *FakeContext) Compiler() script.Compiler

func (*FakeContext) Deadline

func (f *FakeContext) Deadline() (time.Time, bool)

func (*FakeContext) Delete

func (f *FakeContext) Delete(key string)

func (*FakeContext) Done

func (f *FakeContext) Done() <-chan struct{}

func (*FakeContext) Err

func (f *FakeContext) Err() error

func (*FakeContext) Get

func (f *FakeContext) Get(key string) (any, bool)

func (*FakeContext) History

func (f *FakeContext) History() *workflow.History

func (*FakeContext) Inputs

func (f *FakeContext) Inputs() workflow.Inputs

func (*FakeContext) Keys

func (f *FakeContext) Keys() []string

func (*FakeContext) Logger

func (f *FakeContext) Logger() *slog.Logger

func (*FakeContext) ReportProgress

func (f *FakeContext) ReportProgress(detail workflow.ProgressDetail)

func (*FakeContext) Set

func (f *FakeContext) Set(key string, value any)

func (*FakeContext) SetContext

func (f *FakeContext) SetContext(ctx context.Context)

SetContext replaces the underlying context.Context. Useful for tests that need to exercise cancellation or deadlines.

func (*FakeContext) StepName

func (f *FakeContext) StepName() string

func (*FakeContext) Value

func (f *FakeContext) Value(key any) any

func (*FakeContext) Wait

func (f *FakeContext) Wait(topic string, timeout time.Duration) (any, error)

type FakeContextOptions

type FakeContextOptions struct {
	// Inputs is the initial workflow inputs.
	Inputs map[string]any
	// Variables is the initial branch-local state.
	Variables map[string]any
	// Logger overrides the default discard logger.
	Logger *slog.Logger
	// Compiler overrides the default compiler. When nil, callers of
	// Compiler() receive nil — tests that exercise template
	// evaluation should always set this.
	Compiler script.Compiler
	// BranchID is the value returned by Context.BranchID. Defaults
	// to "fake-branch".
	BranchID string
	// StepName is the value returned by Context.StepName. Defaults
	// to "fake-step".
	StepName string
	// WaitFunc, when non-nil, is invoked by Context.Wait. Tests that
	// exercise wait semantics supply their own function; otherwise
	// Wait returns (nil, nil).
	WaitFunc func(topic string, timeout time.Duration) (any, error)
	// OnProgress, when non-nil, receives every ProgressDetail passed
	// to Context.ReportProgress. Tests can inspect the slice to
	// verify their activity reported progress correctly.
	OnProgress func(detail workflow.ProgressDetail)
}

FakeContextOptions configures a FakeContext. Every field is optional; any field left zero gets a sensible default so test code can construct a FakeContext with {} and still have a usable Context.

type MemoryCheckpointer

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

MemoryCheckpointer is an in-memory Checkpointer for use in tests. It is safe for concurrent use.

func NewMemoryCheckpointer

func NewMemoryCheckpointer() *MemoryCheckpointer

NewMemoryCheckpointer returns a new in-memory checkpointer.

func (*MemoryCheckpointer) AtomicUpdate

func (m *MemoryCheckpointer) AtomicUpdate(ctx context.Context, executionID string, fn func(*workflow.Checkpoint) error) error

AtomicUpdate implements workflow.AtomicCheckpointer. The in-memory backend serializes the entire read-modify-write under its write lock.

func (*MemoryCheckpointer) Checkpoints

func (m *MemoryCheckpointer) Checkpoints() map[string]*workflow.Checkpoint

Checkpoints returns a snapshot of all stored checkpoints, keyed by execution ID. Useful for test assertions.

func (*MemoryCheckpointer) DeleteCheckpoint

func (m *MemoryCheckpointer) DeleteCheckpoint(ctx context.Context, executionID string) error

func (*MemoryCheckpointer) LoadCheckpoint

func (m *MemoryCheckpointer) LoadCheckpoint(ctx context.Context, executionID string) (*workflow.Checkpoint, error)

func (*MemoryCheckpointer) SaveCheckpoint

func (m *MemoryCheckpointer) SaveCheckpoint(ctx context.Context, checkpoint *workflow.Checkpoint) error

type TestOptions

type TestOptions struct {
	// ExecutionID sets a fixed execution ID. Auto-generated if empty.
	ExecutionID string

	// Checkpointer overrides the default in-memory checkpointer.
	Checkpointer workflow.Checkpointer

	// Callbacks receives execution lifecycle events.
	Callbacks workflow.ExecutionCallbacks

	// StepProgressStore receives step progress updates.
	StepProgressStore workflow.StepProgressStore
}

TestOptions allows overriding execution settings for test runs. Only fields that make sense to customize in tests are exposed.

Jump to

Keyboard shortcuts

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