testutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const TestHMACSecret = "test-hmac-secret-for-testing"

Variables

This section is empty.

Functions

func NewEventsAPIHarness added in v0.3.0

func NewEventsAPIHarness(t *testing.T, pool *pgxpool.Pool, store *identity.Store, outbox webhookpub.Outbox) *httptest.Server

NewEventsAPIHarness spins up a minimal httptest server with the agent.API wired for the slice 6/7 events API. Used by the internal/e2e events tests that need to hit /api/v1/events and /api/v1/events/{id}/redeliver against a real HTTP layer.

Returns the *httptest.Server; caller must Close() on cleanup.

func OpenPreparedTestDB

func OpenPreparedTestDB(ctx context.Context, dbURL string) (*pgxpool.Pool, error)

func TestDB

func TestDB(t *testing.T) *pgxpool.Pool

func TestDBURL

func TestDBURL() string

func TruncateAll

func TruncateAll(t *testing.T, pool *pgxpool.Pool)

Types

type ContractServer

type ContractServer struct {
	BaseURL  string
	APIKey   string
	UserID   string
	DBPool   *pgxpool.Pool
	Store    *identity.Store
	WSHub    *ws.Hub
	Signer   *headers.Signer
	SMTPAddr string
	// contains filtered or unexported fields
}

func StartContractServer

func StartContractServer(ctx context.Context, dbURL string) (*ContractServer, error)

func (*ContractServer) Close

func (s *ContractServer) Close(ctx context.Context) error

type E2ATestServer

type E2ATestServer struct {
	HTTPServer *httptest.Server
	SMTPAddr   string
	Store      *identity.Store
	Signer     *headers.Signer
	WSHub      *ws.Hub

	// Webhooks-as-a-resource wiring (post-PR-180). Tests that exercise
	// the new path can read these directly without re-deriving them.
	// Publisher is wired into both the agent API (so /send etc. fire
	// email.sent) and the SMTP server (so inbound mail fires
	// email.received). SubscriberStore + Worker let tests insert /
	// inspect delivery rows and force a drain without waiting on the
	// 30s production tick.
	Publisher        webhookpub.Publisher
	SubscriberStore  *webhook.SubscriberStore
	SubscriberWorker *webhook.SubscriberRetryWorker
	// contains filtered or unexported fields
}

func TestServer

func TestServer(t *testing.T, pool *pgxpool.Pool, opts ...TestServerOption) *E2ATestServer

type ReceivedPayload

type ReceivedPayload struct {
	Body    webhook.Payload
	Headers http.Header
	RawBody []byte
}

type SMTPAddr

type SMTPAddr struct {
	Host string
	Port int
}

SMTPAddr holds parsed host and port for a fake SMTP server.

func FakeSMTPServer

func FakeSMTPServer(t *testing.T) (SMTPAddr, func() []SMTPMessage)

FakeSMTPServer starts a minimal SMTP server that accepts messages. Returns the address and a function to call to stop the server and get received messages.

type SMTPMessage

type SMTPMessage struct {
	From       string
	To         string   // first RCPT TO (backward compat)
	Recipients []string // all RCPT TO addresses
	Data       string
}

SMTPMessage represents a message received by the fake SMTP server.

type SubscriberCaptured added in v0.3.0

type SubscriberCaptured struct {
	URL      string
	Envelope map[string]any
	RawBody  []byte
	Headers  http.Header
}

SubscriberCaptured is one POST the SubscriberReceiver caught. The envelope is the parsed JSON body ({event, id, created_at, data}) and RawBody is the verbatim bytes — useful for HMAC verification, which signs `t.body` and must use the exact bytes the worker POSTed.

type SubscriberReceiverResult added in v0.3.0

type SubscriberReceiverResult struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

SubscriberReceiverResult is a multi-path receiver for the new webhooks-as-a-resource path. Distinct from WebhookReceiverResult (which decodes the legacy webhook.Payload shape) because the new envelope is {event, id, created_at, data} and we need raw bytes for signature verification.

func SubscriberReceiver added in v0.3.0

func SubscriberReceiver(t *testing.T) *SubscriberReceiverResult

SubscriberReceiver returns a multi-path HTTP receiver wired for the new webhook-resource envelope. Routes work as plain paths under the receiver's base URL — e.g. receiver.Server.URL + "/sent" + ".../fail".

func (*SubscriberReceiverResult) Captured added in v0.3.0

func (*SubscriberReceiverResult) Reset added in v0.3.0

func (s *SubscriberReceiverResult) Reset()

Reset clears captured payloads. Useful between phases of a long test so per-phase assertions don't see prior posts.

func (*SubscriberReceiverResult) SetStatus added in v0.3.0

func (s *SubscriberReceiverResult) SetStatus(path string, code int)

SetStatus pins a non-200 response for the given path. Used by the auto-disable test to force the worker into the failure path.

func (*SubscriberReceiverResult) WaitFor added in v0.3.0

func (s *SubscriberReceiverResult) WaitFor(t *testing.T, timeout time.Duration, predicate func([]SubscriberCaptured) bool) []SubscriberCaptured

WaitFor polls until predicate returns true or the timeout expires. Returns the captured list at the moment predicate first matched (or the last seen list on timeout). Tests typically call Tick(ctx) on the worker once then WaitFor(..., 0) for an immediate check; the timeout exists for cases where the publisher may still be in flight.

type TestServerOption added in v0.3.0

type TestServerOption func(*testServerOpts)

func WithOutboundSMTP added in v0.3.0

func WithOutboundSMTP(host string, port int, fromDomain string) TestServerOption

WithOutboundSMTP wires an upstream relay for /send + HITL approve paths so they don't error with "outbound SMTP relay not configured". Tests that don't trigger outbound omit this. Pointing at Mailpit on localhost:1025 (started via `make docker-up`) is the typical pattern.

type WebhookReceiverResult

type WebhookReceiverResult struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

func WebhookReceiver

func WebhookReceiver(t *testing.T) *WebhookReceiverResult

func (*WebhookReceiverResult) Payloads

func (w *WebhookReceiverResult) Payloads() []ReceivedPayload

func (*WebhookReceiverResult) WaitForPayloads

func (w *WebhookReceiverResult) WaitForPayloads(t *testing.T, count int, timeout time.Duration) []ReceivedPayload

Jump to

Keyboard shortcuts

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