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 ¶
- type Agent
- type Bus
- type Cards
- func (c *Cards) Await(ctx context.Context, id string) (module.Card, error)
- func (c *Cards) End(_ context.Context, id, status string) (module.Card, error)
- func (c *Cards) Get(_ context.Context, id string) (module.Card, bool, error)
- func (c *Cards) List(_ context.Context, all bool) ([]module.Card, error)
- func (c *Cards) Respond(_ context.Context, id string, o module.CardOutcome) (module.Card, error)
- func (c *Cards) Show(_ context.Context, card module.Card) (module.Card, error)
- func (c *Cards) Update(_ context.Context, id, title string, payload json.RawMessage) (module.Card, error)
- type Config
- type Desk
- func (d *Desk) Arrange(ctx context.Context, l module.Layout) error
- func (d *Desk) Capture(ctx context.Context) module.Layout
- func (d *Desk) Close(_ context.Context, id string) error
- func (d *Desk) Context(ctx context.Context, id string) (module.WindowContext, bool)
- func (d *Desk) DeleteLayout(_ context.Context, name string) error
- func (d *Desk) Focus(_ context.Context, id string) error
- func (d *Desk) Focused(context.Context) (module.Window, bool)
- func (d *Desk) Layout(_ context.Context, name string) (module.Layout, bool, error)
- func (d *Desk) Layouts(context.Context) ([]module.Layout, error)
- func (d *Desk) Open(_ context.Context, view string, o module.OpenOptions) (module.Window, error)
- func (d *Desk) SaveLayout(_ context.Context, l module.Layout) error
- func (d *Desk) SetArgs(_ context.Context, id string, args map[string]any) error
- func (d *Desk) SetState(id string, state map[string]any)
- func (d *Desk) SetTitle(_ context.Context, id, title string) error
- func (d *Desk) SetWorking(id string, dirty, composing bool)
- func (d *Desk) Windows(context.Context) []module.Window
- type Host
- func (h *Host) Agent() module.Agent
- func (h *Host) Around() (string, error)
- func (h *Host) Cards() module.Cards
- func (h *Host) Config() module.Config
- func (h *Host) DataDir() string
- func (h *Host) Desk() module.Desk
- func (h *Host) Do(method, p, body string) (int, string)
- func (h *Host) Events() module.Events
- func (h *Host) Handle(pattern string, handler http.Handler)
- func (h *Host) Keys() module.Keys
- func (h *Host) Log() *slog.Logger
- func (h *Host) Manifest() module.Manifest
- func (h *Host) Marks() module.Marks
- func (h *Host) ModuleID() string
- func (h *Host) OnCommand(id string, fn func(ctx context.Context, args map[string]any) (any, error))
- func (h *Host) PublicHosts() module.PublicHosts
- func (h *Host) RunCommand(ctx context.Context, id string, args map[string]any) (any, error)
- func (h *Host) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Host) Store() *sql.DB
- func (h *Host) Tool(name, input string) (any, error)
- func (h *Host) ToolAs(actor module.Actor, name, input string) (any, error)
- type Keys
- type Marks
- func (m *Marks) Add(_ context.Context, mk module.Mark) (module.Mark, error)
- func (m *Marks) List(_ context.Context, ref string) ([]module.Mark, error)
- func (m *Marks) Reply(_ context.Context, id string, r module.MarkReply) (module.Mark, error)
- func (m *Marks) Resolve(_ context.Context, id string) error
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.
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.
type Cards ¶
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 ¶
Await returns the card as it is: a test answers before the module waits, or reads the open card back.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is the module's live settings, in memory.
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) SetWorking ¶ added in v0.2.0
SetWorking is the page reporting that the owner has unsaved changes (dirty) or unsent input (composing) in a 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 (*Host) Around ¶ added in v0.4.0
Around reads the module's Around, as the core would for a message.
func (*Host) Do ¶
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) Keys ¶ added in v0.17.0
Keys gives the module the test's key store (nil, and so no keys, unless KeyStore is set).
func (*Host) OnCommand ¶
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 (*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).
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.