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
- func AllText(b *mock.Backend) string
- func Lines(text string) []string
- func Render(t *testing.T, node trail.Node, width, height int) string
- func ScrollbackText(b ScrollbackSource) string
- func SurfaceLines(b *mock.Backend) []string
- func SurfaceText(b *mock.Backend) string
- func SurfaceTextKeepBlanks(b *mock.Backend) string
- func TrimmedLines(text string) []string
- func TrimmedLinesText(text string) string
- type App
- func (a *App) AwaitFrame(timeout time.Duration) string
- func (a *App) HasFrame() bool
- func (a *App) Press(k key.Key) string
- func (a *App) Stop()
- func (a *App) Text() string
- func (a *App) Type(text string) string
- func (a *App) WaitFor(cond func(string) bool) string
- func (a *App) WaitUntil(cond func(string) bool, timeout time.Duration) string
- type ClearingBackend
- type Env
- type ScrollbackSource
Constants ¶
const DefaultWaitTimeout = 2 * time.Second
DefaultWaitTimeout bounds App.WaitFor when no other timeout is set.
Variables ¶
This section is empty.
Functions ¶
func AllText ¶
AllText returns committed scrollback followed by the last rendered frame, which is the full text a user would see in the terminal.
func Render ¶
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 ¶
SurfaceLines returns a mock frame as non-blank lines.
func SurfaceText ¶
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 ¶
SurfaceTextKeepBlanks is SurfaceText but preserves blank lines, for assertions that depend on vertical spacing.
func TrimmedLines ¶
TrimmedLines splits rendered text into lines with trailing spaces removed, so assertions can reason about indentation without the terminal's right padding.
func TrimmedLinesText ¶
TrimmedLinesText trims trailing whitespace per line and the whole block, which is the shape most assertions compare against.
Types ¶
type App ¶
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 ¶
NewApp boots node in a trail instance of the given size, renders one frame, and unmounts with the test.
func StartApp ¶
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 ¶
AwaitFrame waits up to timeout for the first painted frame and returns its text.
func (*App) Press ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) Press ¶
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 ¶
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.
type ScrollbackSource ¶
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.