testutil

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FastTimeout    = 2 * time.Second  // For unit tests and quick operations
	DefaultTimeout = 10 * time.Second // For most integration tests
	SlowTimeout    = 30 * time.Second // For complex operations (file I/O, network)
)

Common timeout durations for different test scenarios

Functions

func AssertOutputContains

func AssertOutputContains(t *testing.T, tm *teatest.TestModel, expected string)

AssertOutputContains checks if current output contains the expected text

func CleanupTeatestTmuxServer

func CleanupTeatestTmuxServer(t *testing.T)

CleanupTeatestTmuxServer cleans up the isolated tmux server for a specific test

func CleanupTmuxSessionsWithPrefix

func CleanupTmuxSessionsWithPrefix(t *testing.T, prefix string)

CleanupTmuxSessionsWithPrefix cleans up tmux sessions with a specific prefix. This is useful for cleaning up sessions created by legacy tests that don't use isolated servers.

func CreateMinimalApp

func CreateMinimalApp(t *testing.T) tea.Model

CreateMinimalApp creates a minimal app model for testing basic functionality

func CreateTUITest

func CreateTUITest(t *testing.T, model tea.Model, config TUITestConfig) *teatest.TestModel

CreateTUITest creates a new teatest instance with the given model

func NonEmptyContent

func NonEmptyContent(content string) bool

NonEmptyContent validates that content is not empty

func ReadOutput

func ReadOutput(t *testing.T, r io.Reader) string

ReadOutput reads all content from an io.Reader and returns it as a string

func RetryOperation

func RetryOperation(operation func() error, config WaitConfig) error

RetryOperation retries an operation with exponential backoff

func TempSessionName

func TempSessionName(prefix string, id int) string

TempSessionName generates a temporary session name with an ID

func WaitForCondition

func WaitForCondition(condition func() bool, config WaitConfig) error

WaitForCondition polls a condition until it returns true or timeout occurs

func WaitForConditionWithError

func WaitForConditionWithError(condition func() (bool, error), config WaitConfig) error

WaitForConditionWithError polls a condition that can return an error

func WaitForContent

func WaitForContent(getter func() (string, error), validator ContentValidator, config WaitConfig) (string, error)

WaitForContent polls a content getter until the validator returns true

func WaitForOutputContains

func WaitForOutputContains(t *testing.T, tm *teatest.TestModel, expected string, timeout time.Duration)

WaitForOutputContains waits until the output contains the expected text using teatest.WaitFor

func WaitForOutputContainsAfterAction

func WaitForOutputContainsAfterAction(t *testing.T, tm *teatest.TestModel, expected string, actionDelay time.Duration)

WaitForOutputContainsAfterAction waits for an action to complete, then checks if output contains expected text This version avoids output stream consumption issues by using a single read approach

func WaitForSession

func WaitForSession(instance *session.Instance) error

WaitForSession waits for a session to be fully ready

func WaitForSessionPreview

func WaitForSessionPreview(instance *session.Instance, validator ContentValidator) (string, error)

WaitForSessionPreview waits for session to have specific preview content

func WaitForSessionPreviewContaining

func WaitForSessionPreviewContaining(instance *session.Instance, text string) (string, error)

WaitForSessionPreviewContaining waits for session preview containing text

func WaitForSessionWithTimeout

func WaitForSessionWithTimeout(instance *session.Instance, config WaitConfig) error

WaitForSessionWithTimeout waits for session with custom timeout

func WaitForTmuxContent

func WaitForTmuxContent(session *tmux.TmuxSession, validator ContentValidator) (string, error)

WaitForTmuxContent waits for tmux session to have specific content

func WaitForTmuxContentContaining

func WaitForTmuxContentContaining(session *tmux.TmuxSession, text string) (string, error)

WaitForTmuxContentContaining waits for tmux content containing text

func WaitForTmuxSession

func WaitForTmuxSession(session *tmux.TmuxSession) error

WaitForTmuxSession waits for a tmux session to be ready

func WaitForTmuxSessionWithTimeout

func WaitForTmuxSessionWithTimeout(session *tmux.TmuxSession, config WaitConfig) error

WaitForTmuxSessionWithTimeout waits for tmux session with custom timeout

Types

type CommandExecutor

type CommandExecutor interface {
	// Command creates a new *exec.Cmd with the given name and arguments
	Command(name string, args ...string) *exec.Cmd

	// Output runs the command and returns its standard output
	Output(cmd *exec.Cmd) ([]byte, error)

	// LookPath searches for an executable named file in the directories listed in the PATH environment variable
	LookPath(file string) (string, error)
}

CommandExecutor defines the interface for executing external commands This allows for dependency injection and mocking in tests

type ContentValidator

type ContentValidator func(content string) bool

ContentValidator is a function that validates content

func ContainsText

func ContainsText(text string) ContentValidator

ContainsText validates that content contains specific text

type ExpectSessionConfig

type ExpectSessionConfig struct {
	// Command to run (defaults to "./stapler-squad")
	Command string
	// Arguments to pass to command
	Args []string
	// Environment variables
	Env []string
	// Working directory
	WorkDir string
	// Timeout for operations (default 30s)
	Timeout time.Duration
	// Width and height of terminal
	Cols, Rows uint16
}

ExpectSessionConfig configures expect-based TUI session behavior

func DefaultExpectConfig

func DefaultExpectConfig() ExpectSessionConfig

DefaultExpectConfig returns default expect testing configuration

type MockCommandExecutor

type MockCommandExecutor struct {
	// CommandFunc is called when Command is invoked
	CommandFunc func(name string, args ...string) *exec.Cmd

	// OutputFunc is called when Output is invoked
	OutputFunc func(cmd *exec.Cmd) ([]byte, error)

	// LookPathFunc is called when LookPath is invoked
	LookPathFunc func(file string) (string, error)
}

MockCommandExecutor implements CommandExecutor for testing

func NewMockCommandExecutor

func NewMockCommandExecutor() *MockCommandExecutor

NewMockCommandExecutor creates a new MockCommandExecutor with default behavior

func NewMockCommandExecutorWithClaudeFound

func NewMockCommandExecutorWithClaudeFound(claudePath string) *MockCommandExecutor

NewMockCommandExecutorWithClaudeFound creates a mock that simulates finding claude

func NewMockCommandExecutorWithClaudeNotFound

func NewMockCommandExecutorWithClaudeNotFound() *MockCommandExecutor

NewMockCommandExecutorWithClaudeNotFound creates a mock that simulates claude not being found

func (*MockCommandExecutor) Command

func (m *MockCommandExecutor) Command(name string, args ...string) *exec.Cmd

func (*MockCommandExecutor) LookPath

func (m *MockCommandExecutor) LookPath(file string) (string, error)

func (*MockCommandExecutor) Output

func (m *MockCommandExecutor) Output(cmd *exec.Cmd) ([]byte, error)

type RealCommandExecutor

type RealCommandExecutor struct{}

RealCommandExecutor implements CommandExecutor using actual system commands

func (*RealCommandExecutor) Command

func (r *RealCommandExecutor) Command(name string, args ...string) *exec.Cmd

func (*RealCommandExecutor) LookPath

func (r *RealCommandExecutor) LookPath(file string) (string, error)

func (*RealCommandExecutor) Output

func (r *RealCommandExecutor) Output(cmd *exec.Cmd) ([]byte, error)

type SessionWaiter

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

SessionWaiter provides utilities for waiting on session operations

func NewSessionWaiter

func NewSessionWaiter(instance *session.Instance) *SessionWaiter

NewSessionWaiter creates a new session waiter

func (*SessionWaiter) WaitForFullyReady

func (w *SessionWaiter) WaitForFullyReady() error

WaitForFullyReady waits for session to be started, tmux alive, and have content

func (*SessionWaiter) WaitForFullyReadyWithTimeout

func (w *SessionWaiter) WaitForFullyReadyWithTimeout(config WaitConfig) error

WaitForFullyReadyWithTimeout waits for session with custom timeout

func (*SessionWaiter) WaitForNonEmptyPreview

func (w *SessionWaiter) WaitForNonEmptyPreview() (string, error)

WaitForNonEmptyPreview waits for any non-empty preview content

func (*SessionWaiter) WaitForPreview

func (w *SessionWaiter) WaitForPreview(validator ContentValidator) (string, error)

WaitForPreview waits for the session to have preview content

func (*SessionWaiter) WaitForPreviewContaining

func (w *SessionWaiter) WaitForPreviewContaining(text string) (string, error)

WaitForPreviewContaining waits for preview containing specific text

func (*SessionWaiter) WaitForPreviewWithConfig

func (w *SessionWaiter) WaitForPreviewWithConfig(validator ContentValidator, config WaitConfig) (string, error)

WaitForPreviewWithConfig waits for preview with custom config

func (*SessionWaiter) WaitForStarted

func (w *SessionWaiter) WaitForStarted() error

WaitForStarted waits for the session to be started

func (*SessionWaiter) WaitForTmuxAlive

func (w *SessionWaiter) WaitForTmuxAlive() error

WaitForTmuxAlive waits for the tmux session to be alive

type TUISession

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

TUISession represents an interactive TUI testing session

func StartExpectSession

func StartExpectSession(t *testing.T, config ExpectSessionConfig) (*TUISession, error)

StartExpectSession starts a new interactive TUI session for testing with expect

func (*TUISession) Close

func (s *TUISession) Close() error

Close terminates the TUI session

func (*TUISession) ExpectOneOf

func (s *TUISession) ExpectOneOf(strings []string, timeout time.Duration) (string, error)

ExpectOneOf waits for any of the provided strings to appear

func (*TUISession) ExpectPattern

func (s *TUISession) ExpectPattern(pattern string, timeout time.Duration) (string, error)

ExpectPattern waits for output matching a regex pattern

func (*TUISession) ExpectString

func (s *TUISession) ExpectString(str string, timeout time.Duration) error

ExpectString waits for a specific string to appear in the output

func (*TUISession) GetOutput

func (s *TUISession) GetOutput() (string, error)

GetOutput gets all available output without blocking

func (*TUISession) IsRunning

func (s *TUISession) IsRunning() bool

IsRunning checks if the TUI process is still running

func (*TUISession) SendArrowDown

func (s *TUISession) SendArrowDown() error

SendArrowDown sends down arrow key

func (*TUISession) SendArrowLeft

func (s *TUISession) SendArrowLeft() error

SendArrowLeft sends left arrow key

func (*TUISession) SendArrowRight

func (s *TUISession) SendArrowRight() error

SendArrowRight sends right arrow key

func (*TUISession) SendArrowUp

func (s *TUISession) SendArrowUp() error

SendArrowUp sends up arrow key

func (*TUISession) SendCtrlC

func (s *TUISession) SendCtrlC() error

SendCtrlC sends Ctrl+C signal

func (*TUISession) SendCtrlD

func (s *TUISession) SendCtrlD() error

SendCtrlD sends Ctrl+D (EOF)

func (*TUISession) SendEnter

func (s *TUISession) SendEnter() error

SendEnter sends Enter/Return key

func (*TUISession) SendEscape

func (s *TUISession) SendEscape() error

SendEscape sends Escape key

func (*TUISession) SendKeys

func (s *TUISession) SendKeys(keys string) error

SendKeys sends a sequence of keys to the TUI

func (*TUISession) SendLine

func (s *TUISession) SendLine(text string) error

SendLine sends a line of text followed by Enter

func (*TUISession) SendTab

func (s *TUISession) SendTab() error

SendTab sends Tab key

func (*TUISession) Snapshot

func (s *TUISession) Snapshot() (string, error)

Snapshot captures current screen content

func (*TUISession) WaitForCondition

func (s *TUISession) WaitForCondition(condition func() bool, timeout time.Duration, pollInterval time.Duration) error

WaitForCondition waits for a custom condition with polling

func (*TUISession) WaitForExit

func (s *TUISession) WaitForExit(timeout time.Duration) error

WaitForExit waits for the TUI to exit

func (*TUISession) WaitForPrompt

func (s *TUISession) WaitForPrompt(timeout time.Duration) error

WaitForPrompt waits for a common prompt pattern

type TUITestConfig

type TUITestConfig struct {
	Width   int
	Height  int
	Timeout time.Duration
}

TUITestConfig contains configuration for TUI tests

func DefaultTUIConfig

func DefaultTUIConfig() TUITestConfig

DefaultTUIConfig returns sensible defaults for TUI testing

type TmuxTestServer

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

TmuxTestServer represents an isolated tmux server for testing. Each test gets its own tmux server socket to prevent interference between tests.

func CreateIsolatedTmuxServer

func CreateIsolatedTmuxServer(t *testing.T) *TmuxTestServer

CreateIsolatedTmuxServer creates a new isolated tmux server for testing. The server uses a unique socket name based on the test name to prevent conflicts. Cleanup is automatically registered with t.Cleanup().

func (*TmuxTestServer) Cleanup

func (s *TmuxTestServer) Cleanup()

Cleanup performs cleanup of the isolated tmux server. This is automatically called via t.Cleanup() but can also be called manually.

func (*TmuxTestServer) CreateSession

func (s *TmuxTestServer) CreateSession(sessionName string, command string) (*tmux.TmuxSession, error)

CreateSession creates and starts a new tmux session on this isolated server

func (*TmuxTestServer) CreateSessionWithoutStarting

func (s *TmuxTestServer) CreateSessionWithoutStarting(sessionName string, command string, prefix string) *tmux.TmuxSession

CreateSessionWithoutStarting creates a tmux session object without starting it. This is useful for testing timeout and hang scenarios.

func (*TmuxTestServer) GetSocketName

func (s *TmuxTestServer) GetSocketName() string

GetSocketName returns the socket name for this isolated server

func (*TmuxTestServer) KillAllSessions

func (s *TmuxTestServer) KillAllSessions() error

KillAllSessions kills all sessions on this isolated server

func (*TmuxTestServer) KillServer

func (s *TmuxTestServer) KillServer() error

KillServer kills the entire tmux server (all sessions)

func (*TmuxTestServer) KillSession

func (s *TmuxTestServer) KillSession(sessionName string) error

KillSession kills a specific session on this server

func (*TmuxTestServer) ListSessions

func (s *TmuxTestServer) ListSessions() ([]string, error)

ListSessions returns all session names on this isolated server

func (*TmuxTestServer) SessionExists

func (s *TmuxTestServer) SessionExists(sessionName string) bool

SessionExists checks if a session exists on this server

type TmuxWaiter

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

TmuxWaiter provides utilities for waiting on tmux operations

func NewTmuxWaiter

func NewTmuxWaiter(session *tmux.TmuxSession) *TmuxWaiter

NewTmuxWaiter creates a new tmux waiter

func (*TmuxWaiter) WaitForContent

func (w *TmuxWaiter) WaitForContent(validator ContentValidator) (string, error)

WaitForContent waits for tmux session to have specific content

func (*TmuxWaiter) WaitForContentContaining

func (w *TmuxWaiter) WaitForContentContaining(text string) (string, error)

WaitForContentContaining waits for content containing specific text

func (*TmuxWaiter) WaitForContentWithConfig

func (w *TmuxWaiter) WaitForContentWithConfig(validator ContentValidator, config WaitConfig) (string, error)

WaitForContentWithConfig waits for content with custom config

func (*TmuxWaiter) WaitForNonEmptyContent

func (w *TmuxWaiter) WaitForNonEmptyContent() (string, error)

WaitForNonEmptyContent waits for any non-empty content

func (*TmuxWaiter) WaitForSessionExists

func (w *TmuxWaiter) WaitForSessionExists() error

WaitForSessionExists waits for the tmux session to exist

func (*TmuxWaiter) WaitForSessionExistsWithConfig

func (w *TmuxWaiter) WaitForSessionExistsWithConfig(config WaitConfig) error

WaitForSessionExistsWithConfig waits for session with custom config

func (*TmuxWaiter) WaitForSessionReady

func (w *TmuxWaiter) WaitForSessionReady() error

WaitForSessionReady waits for session to exist and have content

func (*TmuxWaiter) WaitForSessionReadyWithTimeout

func (w *TmuxWaiter) WaitForSessionReadyWithTimeout(config WaitConfig) error

WaitForSessionReadyWithTimeout waits for session with custom timeout

type WaitConfig

type WaitConfig struct {
	Timeout      time.Duration
	PollInterval time.Duration
	Description  string // For better error messages
}

WaitConfig allows customizing wait behavior

func DefaultWaitConfig

func DefaultWaitConfig() WaitConfig

DefaultWaitConfig provides sensible defaults

func FastWaitConfig

func FastWaitConfig() WaitConfig

FastWaitConfig for quick operations

func SlowWaitConfig

func SlowWaitConfig() WaitConfig

SlowWaitConfig for complex operations

Jump to

Keyboard shortcuts

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