moduletest

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 22 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 moduletest runs one Surface module in a test, without the core: a Host with an in-memory desk, live settings, an event bus and marks, and a temporary SQLite store with the module's migrations applied.

h := moduletest.Start(t, mymodule.New())
out, err := h.Tool("mymodule_add", `{"text":"hi"}`)
h.Do("GET", "/items", "") // the module's routes, as /api/m/<id>/items

It is a stand-in, not the core: the desk keeps windows and layouts and does nothing else, and RunCommand runs the module's own commands plus any the test gives with OnCommand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Agent added in v0.4.0

type Agent struct {
	Asks []module.Ask
	Err  error
	// GW is what Gateway returns; nil gives module.ErrNoAgent.
	GW *module.Gateway
	// contains filtered or unexported fields
}

Agent records the errands a module hands the agent. Err, when set, is what Ask returns (module.ErrAgentBusy, module.ErrNoAgent) instead of taking the errand.

func (*Agent) Ask added in v0.4.0

func (a *Agent) Ask(_ context.Context, x module.Ask) (string, error)

func (*Agent) Gateway added in v0.4.0

func (a *Agent) Gateway(context.Context) (module.Gateway, error)

Gateway returns GW, or module.ErrNoAgent when it is nil.

func (*Agent) Taken added in v0.4.0

func (a *Agent) Taken() []module.Ask

Taken is a copy of the errands taken so far.

type Bus

type Bus struct {

	// Published is every event so far, oldest first.
	Published []module.Event
	// contains filtered or unexported fields
}

Bus is an in-memory event bus that also keeps what was published.

func (*Bus) Publish

func (b *Bus) Publish(topic string, data any)

func (*Bus) Subscribe

func (b *Bus) Subscribe(prefix string, fn func(module.Event)) func()

func (*Bus) Topics

func (b *Bus) Topics() []string

Topics lists the topics published so far.

type Cards

type Cards struct {
	Shown []module.Card
	// contains filtered or unexported fields
}

Cards is a module.Cards kept in memory. A test answers a card with Respond and reads what the module showed from Shown.

func (*Cards) Await

func (c *Cards) Await(ctx context.Context, id string) (module.Card, error)

Await returns the card as it is: a test answers before the module waits, or reads the open card back.

func (*Cards) End

func (c *Cards) End(_ context.Context, id, status string) (module.Card, error)

func (*Cards) Get

func (c *Cards) Get(_ context.Context, id string) (module.Card, bool, error)

func (*Cards) List

func (c *Cards) List(_ context.Context, all bool) ([]module.Card, error)

func (*Cards) Respond

func (c *Cards) Respond(_ context.Context, id string, o module.CardOutcome) (module.Card, error)

func (*Cards) Show

func (c *Cards) Show(_ context.Context, card module.Card) (module.Card, error)

func (*Cards) Update

func (c *Cards) Update(_ context.Context, id, title string, payload json.RawMessage) (module.Card, error)

type Config

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

Config is the module's live settings, in memory.

func (*Config) Bool

func (c *Config) Bool(key string) bool

func (*Config) Int

func (c *Config) Int(key string) int

func (*Config) Set

func (c *Config) Set(_ context.Context, key string, value any) error

func (*Config) String

func (c *Config) String(key string) string

func (*Config) Watch

func (c *Config) Watch(key string, fn func(any)) func()

type Desk

type Desk struct {

	// Mode is the last mode a layout's setup set.
	Mode string
	// Contexts, when set for a window id, is what Context returns for it.
	Contexts map[string]module.WindowContext
	// contains filtered or unexported fields
}

Desk is an in-memory desk: it keeps windows, the focus and saved layouts.

func (*Desk) Arrange

func (d *Desk) Arrange(ctx context.Context, l module.Layout) error

func (*Desk) Capture

func (d *Desk) Capture(ctx context.Context) module.Layout

func (*Desk) Close

func (d *Desk) Close(_ context.Context, id string) error

func (*Desk) Context

func (d *Desk) Context(ctx context.Context, id string) (module.WindowContext, bool)

func (*Desk) DeleteLayout

func (d *Desk) DeleteLayout(_ context.Context, name string) error

func (*Desk) Focus

func (d *Desk) Focus(_ context.Context, id string) error

func (*Desk) Focused

func (d *Desk) Focused(context.Context) (module.Window, bool)

func (*Desk) Layout

func (d *Desk) Layout(_ context.Context, name string) (module.Layout, bool, error)

func (*Desk) Layouts

func (d *Desk) Layouts(context.Context) ([]module.Layout, error)

func (*Desk) Open

func (d *Desk) Open(_ context.Context, view string, o module.OpenOptions) (module.Window, error)

func (*Desk) SaveLayout

func (d *Desk) SaveLayout(_ context.Context, l module.Layout) error

func (*Desk) SetArgs

func (d *Desk) SetArgs(_ context.Context, id string, args map[string]any) error

func (*Desk) SetState

func (d *Desk) SetState(id string, state map[string]any)

SetState is the page reporting a window's state (desk.report).

func (*Desk) SetTitle

func (d *Desk) SetTitle(_ context.Context, id, title string) error

func (*Desk) SetWorking added in v0.2.0

func (d *Desk) SetWorking(id string, dirty, composing bool)

SetWorking is the page reporting that the owner has unsaved changes (dirty) or unsent input (composing) in a window.

func (*Desk) Windows

func (d *Desk) Windows(context.Context) []module.Window

type Host

type Host struct {
	// KeyStore, when set, gives the module sign-in keys (module.KeysOf); see NewKeys.
	KeyStore *Keys
	// PublicHostNames are the host names the module last set (module.PublicHostsOf), normalized.
	PublicHostNames []string

	DeskState *Desk
	Bus       *Bus
	MarkStore *Marks
	// CardStore records the cards the module showed.
	CardStore *Cards
	// AgentState records the errands the module handed the agent.
	AgentState *Agent
	Settings   *Config
	// contains filtered or unexported fields
}

Host is the module's Host in a test, and what the test reads back.

func New

func New(t testing.TB, m module.Module) *Host

New builds the Host for m (its store migrated) without starting the module.

func Start

func Start(t testing.TB, m module.Module) *Host

Start builds the Host and starts the module; it is stopped when the test ends.

func (*Host) Agent added in v0.4.0

func (h *Host) Agent() module.Agent

func (*Host) Around added in v0.4.0

func (h *Host) Around() (string, error)

Around reads the module's Around, as the core would for a message.

func (*Host) Cards

func (h *Host) Cards() module.Cards

func (*Host) Config

func (h *Host) Config() module.Config

func (*Host) DataDir

func (h *Host) DataDir() string

func (*Host) Desk

func (h *Host) Desk() module.Desk

func (*Host) Do

func (h *Host) Do(method, p, body string) (int, string)

Do calls one of the module's routes. p is relative to the module's prefix ("/items"). It returns the status and the body.

func (*Host) Events

func (h *Host) Events() module.Events

func (*Host) Handle

func (h *Host) Handle(pattern string, handler http.Handler)

func (*Host) Keys added in v0.17.0

func (h *Host) Keys() module.Keys

Keys gives the module the test's key store (nil, and so no keys, unless KeyStore is set).

func (*Host) Log

func (h *Host) Log() *slog.Logger

func (*Host) Manifest

func (h *Host) Manifest() module.Manifest

Manifest is the module's manifest.

func (*Host) Marks

func (h *Host) Marks() module.Marks

func (*Host) ModuleID

func (h *Host) ModuleID() string

func (*Host) OnCommand

func (h *Host) OnCommand(id string, fn func(ctx context.Context, args map[string]any) (any, error))

OnCommand answers RunCommand for a command the module does not have (a core one).

func (*Host) PublicHosts added in v0.18.0

func (h *Host) PublicHosts() module.PublicHosts

PublicHosts records the host names the module adds (module.PublicHostsOf) in PublicHostNames.

func (*Host) RunCommand

func (h *Host) RunCommand(ctx context.Context, id string, args map[string]any) (any, error)

func (*Host) ServeHTTP added in v0.2.0

func (h *Host) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the module's routes, so a test can put the host behind httptest.NewServer (a WebSocket, a stream: what a recorder cannot carry).

func (*Host) Store

func (h *Host) Store() *sql.DB

func (*Host) Tool

func (h *Host) Tool(name, input string) (any, error)

Tool calls one of the module's tools by name with a JSON input.

func (*Host) ToolAs added in v0.2.0

func (h *Host) ToolAs(actor module.Actor, name, input string) (any, error)

ToolAs calls a tool as an actor would: the agent, a reflex, an outside client, the owner. "" is no one in particular, as Tool.

type Keys added in v0.17.0

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

Keys is an in-memory module.Keys for tests: set Host.KeyStore = moduletest.NewKeys() to give a module sign-in keys (module.KeysOf then reports true). It applies the same scope rule as an app, with the module's id standing for the app's name.

func NewKeys added in v0.17.0

func NewKeys() *Keys

NewKeys returns an empty key store.

func (*Keys) Live added in v0.17.0

func (k *Keys) Live() []string

Live lists the keys not revoked, by name (for assertions).

func (*Keys) Revoke added in v0.17.0

func (k *Keys) Revoke(_ context.Context, id string) error

Revoke implements module.Keys.

func (*Keys) Verify added in v0.17.0

func (k *Keys) Verify(_ context.Context, tok string) (module.Caller, error)

Verify implements module.Keys.

type Marks

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

Marks are the collaboration marks, in memory.

func (*Marks) Add

func (m *Marks) Add(_ context.Context, mk module.Mark) (module.Mark, error)

func (*Marks) List

func (m *Marks) List(_ context.Context, ref string) ([]module.Mark, error)

func (*Marks) Reply

func (m *Marks) Reply(_ context.Context, id string, r module.MarkReply) (module.Mark, error)

func (*Marks) Resolve

func (m *Marks) Resolve(_ context.Context, id string) error

Jump to

Keyboard shortcuts

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