testenv

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0, MIT Imports: 24 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 testenv runs the whole edge for integration tests: a fake core on a Unix socket, a SQLite store in a temp dir, the background loops and the HTTP API on an httptest server.

Index

Constants

View Source
const AdminKey = "test-admin-key-0123456789"

AdminKey is the admin key every test environment uses.

Variables

View Source
var Divergences = map[string]string{
	"GET /api/{session}/groups 200": "GOWS answers the list as an array (PaginatorInMemory over the engine's list); the document says object",
}

Divergences are places where WAHA itself does not follow its OpenAPI document, and this edge follows WAHA. Key: "METHOD /path STATUS"; value: why. A response matching one of these is not checked against the schema.

Functions

func CheckContract

func CheckContract(t testing.TB, req *http.Request, status int, header http.Header, body []byte)

CheckContract validates one response against the operation's schema for its status. A status the operation does not document, or a body that is not JSON, is not checked: WAHA documents only its success answers.

func CheckWebhook

func CheckWebhook(t testing.TB, ev map[string]any)

CheckWebhook validates a received envelope's payload against the schema WAHA documents for its event: the payload of the webhook schema whose event defaults to that name (WAHAWebhookMessage, WebhookGroupV2Join, ...).

Types

type Env

type Env struct {
	T    testing.TB
	URL  string
	Core *fakecore.Core
	App  *app.App
	HTTP *httptest.Server
	// contains filtered or unexported fields
}

Env is one running edge.

func Start

func Start(t testing.TB, opts Options) *Env

Start brings an edge up and tears it down when the test ends.

func (*Env) Do

func (e *Env) Do(method, path string, body any) Response

Do sends a request with the admin key. body may be nil, a string, []byte or any value to marshal.

func (*Env) DoKey

func (e *Env) DoKey(key, method, path string, body any) Response

DoKey sends a request with the given key ("" sends none).

func (*Env) Eventually

func (e *Env) Eventually(what string, timeout time.Duration, cond func() bool)

Eventually polls cond until it holds or the timeout passes.

func (*Env) NewFileServer

func (e *Env) NewFileServer(body []byte, contentType string) string

NewFileServer serves body with the given content type at every path and returns its URL, for routes that fetch a file by URL.

func (*Env) NewHook

func (e *Env) NewHook(status func(n int, r *http.Request) int) *Hook

NewHook starts a receiver. status, if set, picks the answer to the n-th request (0-based); default 200.

func (*Env) Restart

func (e *Env) Restart()

Restart stops this edge process and starts a new one, as another worker, on the same store and the same core: a deploy, or a lease handover.

func (*Env) Status

func (e *Env) Status(session string) string

Status is the session's current WAHA status, via the API.

func (*Env) WorkingSession

func (e *Env) WorkingSession(name, jid string, config any) string

WorkingSession creates a session, starts it, pairs it as jid and waits for WORKING. It returns the core's account uuid.

type Hook

type Hook struct {
	URL string
	// contains filtered or unexported fields
}

Hook is a webhook receiver that records every request.

func (*Hook) Events

func (h *Hook) Events(name string) []map[string]any

Events is the received envelopes whose event is name ("" for all).

func (*Hook) Requests

func (h *Hook) Requests() []HookRequest

Requests is every request received so far.

type HookRequest

type HookRequest struct {
	Header http.Header
	Body   []byte
}

HookRequest is one received webhook.

func (HookRequest) Event

func (h HookRequest) Event() map[string]any

Event decodes the request as a WAHA envelope.

type Options

type Options struct {
	Core   fakecore.Options
	Config func(*config.Config)
}

Options tunes an environment.

type Response

type Response struct {
	Status int
	Header http.Header
	Body   []byte
}

Response is a finished request.

func (Response) JSON

func (r Response) JSON(t testing.TB, v any)

JSON decodes the body into v, failing the test on error.

func (Response) Map

func (r Response) Map(t testing.TB) map[string]any

Map decodes the body as an object.

Jump to

Keyboard shortcuts

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