testutils

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testutils renders TaskSmith's terminal UI inside tests.

Two rendering strategies exist because components have different needs:

  • Render and NewEnv run headlessly through testenv and kitex.Render. This is the default: fast, deterministic, and enough for anything that only reads props and kitex state.
  • NewApp boots a real trail runtime on a mock backend. Use it when the component needs trail.UseApp, trail.UseInput, Static output, or anything else that only exists inside a running instance.

Both return rendered text rather than a node tree, so assertions read what a user would actually see. Widths are always explicit: a test should state the terminal size it assumes instead of inheriting a default that can change under it.

Index

Constants

View Source
const DefaultWaitTimeout = 2 * time.Second

DefaultWaitTimeout bounds App.WaitFor when no other timeout is set.

Variables

This section is empty.

Functions

func AllText

func AllText(b *mock.Backend) string

AllText returns committed scrollback followed by the last rendered frame, which is the full text a user would see in the terminal.

func Lines

func Lines(text string) []string

Lines splits rendered text into lines.

func Render

func Render(t *testing.T, node trail.Node, width, height int) string

Render mounts node in a fresh headless terminal of the given size and returns the visible text. Use NewEnv instead when a test renders more than once.

func ScrollbackText

func ScrollbackText(b ScrollbackSource) string

ScrollbackText renders buffers committed above the live viewport, oldest first. It goes through the backend accessors rather than reading exported fields, so it stays race-free while an engine goroutine appends scrollback.

func SurfaceLines

func SurfaceLines(b *mock.Backend) []string

SurfaceLines returns a mock frame as non-blank lines.

func SurfaceText

func SurfaceText(b *mock.Backend) string

SurfaceText renders a mock frame as text with trailing spaces stripped and blank lines omitted, which is what most assertions want to read.

func SurfaceTextKeepBlanks

func SurfaceTextKeepBlanks(b *mock.Backend) string

SurfaceTextKeepBlanks is SurfaceText but preserves blank lines, for assertions that depend on vertical spacing.

func TrimmedLines

func TrimmedLines(text string) []string

TrimmedLines splits rendered text into lines with trailing spaces removed, so assertions can reason about indentation without the terminal's right padding.

func TrimmedLinesText

func TrimmedLinesText(text string) string

TrimmedLinesText trims trailing whitespace per line and the whole block, which is the shape most assertions compare against.

Types

type App

type App struct {
	*trail.Instance
	Backend *mock.Backend
	// contains filtered or unexported fields
}

App is a running trail instance on a mock backend, for components that need the real runtime (trail.UseApp, trail.UseInput, Static).

Instance is nil for an app started with StartApp, whose loop paints frames on its own; guard before using it.

func NewApp

func NewApp(t *testing.T, node trail.Node, width, height int) *App

NewApp boots node in a trail instance of the given size, renders one frame, and unmounts with the test.

func StartApp

func StartApp(t *testing.T, node trail.Node, width, height int) *App

StartApp boots node with a running trail loop in a background goroutine, which is what trail.Run does. Prefer NewApp unless the component depends on the loop actually running (timers, scheduled macro tasks, Static output).

It returns immediately; the first frame may not be painted yet. Use App.AwaitFrame or App.WaitUntil before asserting.

func (*App) AwaitFrame

func (a *App) AwaitFrame(timeout time.Duration) string

AwaitFrame waits up to timeout for the first painted frame and returns its text.

func (*App) HasFrame

func (a *App) HasFrame() bool

HasFrame reports whether the backend has painted a frame yet.

func (*App) Press

func (a *App) Press(k key.Key) string

Press delivers one key and returns the resulting text. On a NewApp instance it also renders a frame, since nothing else drives the loop.

func (*App) Stop

func (a *App) Stop()

Stop tears down a StartApp runtime. Call it when the helper returns so the engine's goroutine does not outlive the render, which keeps a single test from running several live engines at once. Safe to call more than once; a NewApp instance is unmounted by the test cleanup and does not need this.

func (*App) Text

func (a *App) Text() string

Text renders a frame and returns it as text with trailing whitespace removed, matching the shape Env.Text returns so assertions read the same either way.

func (*App) Type

func (a *App) Type(text string) string

Type delivers one printable-key event per rune, then returns the resulting text, mirroring Env.Type for apps that drive an inline input field.

func (*App) WaitFor

func (a *App) WaitFor(cond func(string) bool) string

WaitFor renders frames until cond accepts the text, failing the test with the last frame if the timeout elapses first. Use it for work that lands asynchronously, such as a timer-driven update; prefer App.Press when a key press causes the change. To assert that something does *not* happen, use App.Press and assert on the text it returns instead.

func (*App) WaitUntil

func (a *App) WaitUntil(cond func(string) bool, timeout time.Duration) string

WaitUntil polls frames until cond accepts the text or the timeout elapses, then returns the last text seen. It never fails the test, so callers can assert with their own message. Use App.WaitFor to fail fast with the last frame included.

type ClearingBackend

type ClearingBackend struct {
	*mock.Backend
	// contains filtered or unexported fields
}

ClearingBackend wraps a mock backend for apps that call Clear to redraw a clean screen. Clear only wipes the live viewport, so scrollback committed before it would otherwise leak into later assertions and let a test pass on output the app no longer shows. The wrapper records how much scrollback existed at each Clear and reports only what was committed after the last one.

Clear runs on the engine goroutine while tests read the counter from the test goroutine, so it is guarded separately from the backend's own lock.

func NewClearingBackend

func NewClearingBackend(b *mock.Backend) *ClearingBackend

NewClearingBackend wraps b so scrollback assertions ignore anything committed before the most recent Clear.

func (*ClearingBackend) AllInsertedAbove

func (c *ClearingBackend) AllInsertedAbove() []*backend.Buffer

AllInsertedAbove returns the buffers committed since the last Clear.

func (*ClearingBackend) AllText

func (c *ClearingBackend) AllText() string

AllText returns the surviving scrollback followed by the last rendered frame.

func (*ClearingBackend) Clear

func (c *ClearingBackend) Clear()

Clear marks the current scrollback as discarded, then clears the viewport.

func (*ClearingBackend) ClearCount

func (c *ClearingBackend) ClearCount() int

ClearCount reports how many times Clear ran.

func (*ClearingBackend) ScrollbackText

func (c *ClearingBackend) ScrollbackText() string

ScrollbackText renders the scrollback that survived the last Clear.

type Env

type Env struct {
	*testenv.Environment
	// contains filtered or unexported fields
}

Env is a headless terminal with a mounted container, for tests that render the same component more than once or drive it with input.

Rendering the same container repeatedly reconciles against the previous tree, which is what you want when asserting that a state change updated the screen.

func NewEnv

func NewEnv(t *testing.T, width, height int) *Env

NewEnv returns a headless environment of the given size, closed with the test.

Closing also unmounts and destroys the mounted tree, so nothing this test rendered leaks into the next one via kitex's process-global component state.

func (*Env) KeyPress

func (e *Env) KeyPress(keyOrChar string, mods ...key.Mod) string

KeyPress delivers a named key such as "esc", flushes, and returns the text.

func (*Env) Press

func (e *Env) Press(k key.Key) string

Press delivers one key, flushes pending work, and returns the resulting text. Flushing is not optional in practice: rendering is scheduled, so an assertion right after a key press would otherwise read the pre-key frame.

func (*Env) Render

func (e *Env) Render(node trail.Node) string

Render mounts or updates node in this environment and returns the visible text.

The result is the terminal dump with surrounding whitespace removed, matching what the repo's tests have always asserted against: interior lines keep their right padding, so column-aligned output still lines up. Use TrimmedLinesText when trailing padding gets in the way instead.

func (*Env) Text

func (e *Env) Text() string

Text returns the current visible text without rendering.

func (*Env) Type

func (e *Env) Type(text string) string

Type types text, flushes, and returns the resulting text.

type ScrollbackSource

type ScrollbackSource interface {
	AllInsertedAbove() []*backend.Buffer
}

ScrollbackSource is the read-only view of a mock backend that the scrollback text helpers need. *mock.Backend satisfies it, and so does ClearingBackend, which reports a filtered scrollback.

Jump to

Keyboard shortcuts

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