mock

package
v0.191.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mock provides a pty-backed fake Flipper CLI so serial.go and the command wrappers can be exercised without real hardware.

The mock opens a /dev/ptmx master, hands the caller the /dev/pts/<n> slave path, and runs a goroutine that reads writes from the slave (i.e. bytes the CLI-under-test "sends" to the Flipper), dispatches them to a scripted command table, and writes canned responses back. A handshake banner is emitted as soon as the slave is first read from.

Linux-only: uses Linux-specific TIOCSPTLCK / TIOCGPTN ioctls. Tests on other platforms must skip or use a different harness.

Index

Constants

View Source
const DefaultBanner = "\r\nWelcome to the Mock Flipper CLI!\r\nFirmware: XFW-MOCK\r\n\r\n>: "

DefaultBanner is emitted once when the slave is first opened, as the welcome banner a real Flipper prints on DTR rising. Must end with a prompt so the handshake observer sees ">: ".

View Source
const DefaultDeviceInfo = `` /* 301-byte string literal not displayed */

DefaultDeviceInfo is the canned device_info output returned when no override is provided. It mirrors the shape of a real Xtreme-fork Flipper so Capabilities parses the expected fork / UID / name.

View Source
const MomentumDeviceInfo = `` /* 304-byte string literal not displayed */

MomentumDeviceInfo is a canned device_info for tests that need Momentum firmware capabilities (SubGHzRxRawHasFilePath=false, NFCFlaggedArgs=true).

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(args []string) string

Handler renders a response body for a given command line. The returned string is what the mock will echo back BEFORE the closing prompt (the prompt is appended automatically). Commands not in the table produce a bare prompt (same as an unknown Flipper CLI command).

type Mock

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

Mock is a running pty-backed fake Flipper. Returned by Spawn. Call Close (or register as t.Cleanup) to tear down.

func Spawn

func Spawn(t *testing.T, opts ...Option) *Mock

Spawn creates a new mock Flipper. The returned path may be passed to flipper.Connect. The t.Cleanup hook closes it automatically.

func (*Mock) BytesReceived added in v0.2.0

func (m *Mock) BytesReceived() []byte

BytesReceived returns a snapshot of every raw byte received from the CLI-under-test since Spawn. Unlike Lines, this includes control bytes such as \x03 (Ctrl+C) that are consumed without becoming complete command lines.

func (*Mock) Close

func (m *Mock) Close() error

Close tears down the pty pair and waits for the responder goroutine to exit. Safe to call multiple times.

func (*Mock) Count

func (m *Mock) Count() int64

Count returns the number of commands the mock has processed since Spawn.

func (*Mock) Lines

func (m *Mock) Lines() []string

Lines returns a copy of every non-empty command line the mock has dispatched since Spawn, in order. Useful for asserting what the flipper-under-test actually sent on the wire.

func (*Mock) Path

func (m *Mock) Path() string

Path returns the /dev/pts/<n> slave path the caller should pass to flipper.Connect.

func (*Mock) URL

func (m *Mock) URL() string

URL returns the mock:// transport URL for this mock. Prefer this over Path when calling flipper.ConnectURL, so the test exercises the dedicated mockTransport dialer in internal/flipper/transport rather than the serial dialer's go.bug.st/serial fallback path. Both work against a pty slave today, but routing through the mock scheme keeps test intent obvious and mirrors how a future BLE-backed mock would be wired.

type Option

type Option func(*Mock)

Option tunes the mock at construction time.

func WithBanner

func WithBanner(s string) Option

WithBanner overrides the one-shot welcome banner.

func WithHandler

func WithHandler(command string, h Handler) Option

WithHandler registers a response handler for a command token (the first whitespace-separated word). Overwrites any previous handler.

func WithSuppressPrompt added in v0.2.0

func WithSuppressPrompt(command string) Option

WithSuppressPrompt suppresses the automatic "\r\n>: " prompt that dispatch appends after handling the named command. Use this to simulate commands that stream indefinitely without emitting a closing prompt (like `subghz rx` or `log`). The handler's returned body is still written; only the trailing prompt is withheld until a Ctrl+C arrives.

Jump to

Keyboard shortcuts

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