mocks

package
v1.2.13 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: MIT Imports: 14 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 mocks hosts the M9 shared mock RoundTrippers + PluginContext builder consumed by `*_test.go` files across the project.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPluginContext

func NewPluginContext(t *testing.T, opts ...PluginContextOption) *plugins.PluginContext

NewPluginContext bundles a *GraphQLMux + *RESTMux into a *plugins.PluginContext ready for `Plugin.Run`. Defaults:

  • Settings: &config.Settings{Repositories: 100}
  • Inputs: map[string]any{"user": "octocat"}
  • Data: plugins.NewData()
  • Logger: slog.New(slog.NewTextHandler(io.Discard, nil))
  • Token: config.NewToken("MOCKED_TOKEN")

Either WithGraphQL or WithREST (or both) MUST be supplied; without them the returned PluginContext has nil clients and most plugins will Skipped-out at their early-return guard.

Types

type GraphQLMux

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

GraphQLMux dispatches genqlient GraphQL requests by the JSON request body's `operationName`. Implements http.RoundTripper — callers wire it into `internal/githubapi.NewGraphQL` via `httpx.Options.Transport` + `DisableRetries: true`.

Goroutine-safe: dispatch (RoundTrip) takes an RLock; registration (On*) takes a write lock.

Unknown operationName triggers t.Fatalf so missing handlers surface immediately with an actionable list of registered ops.

func NewGraphQLMux

func NewGraphQLMux(t *testing.T) *GraphQLMux

NewGraphQLMux constructs an empty mux. The returned value is goroutine-safe and registers a t.Cleanup that resets handler state at end of test.

func (*GraphQLMux) Calls

func (m *GraphQLMux) Calls(opName string) int

Calls returns the number of times RoundTrip dispatched to the given operationName.

func (*GraphQLMux) OnBody

func (m *GraphQLMux) OnBody(opName string, status int, body string)

OnBody registers an inline-body handler.

func (*GraphQLMux) OnFile

func (m *GraphQLMux) OnFile(opName, fixturePath string)

OnFile registers a 200 OK fixture-file-backed handler keyed by operationName. `fixturePath` is relative to `tests/fixtures/`.

func (*GraphQLMux) OnFunc

func (m *GraphQLMux) OnFunc(opName string, fn func(vars map[string]any) (status int, body string))

OnFunc registers a per-call dynamic handler. The decoded `variables` map is passed in so cursor-aware paging mocks can branch on it.

func (*GraphQLMux) RoundTrip

func (m *GraphQLMux) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies http.RoundTripper. The genqlient client posts `{"operationName": "...", "query": "...", "variables": {...}}` — we decode it, look up the handler by operationName, and return the canned response.

type PluginContextOption

type PluginContextOption func(*pluginContextConfig)

PluginContextOption is the functional-options surface for NewPluginContext.

func WithData

func WithData(d *plugins.Data) PluginContextOption

WithData overrides the default empty Data envelope.

func WithGraphQL

func WithGraphQL(mux *GraphQLMux) PluginContextOption

WithGraphQL installs the given mux as the PluginContext's GraphQL client.

func WithInputs

func WithInputs(in map[string]any) PluginContextOption

WithInputs overrides the default inputs map.

func WithREST

func WithREST(mux *RESTMux) PluginContextOption

WithREST installs the given mux as the PluginContext's REST client.

type RESTMux

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

RESTMux is a per-path RoundTripper for REST tests. Construct with NewRESTMux(t); register handlers via OnFile / OnBody / OnHeader / OnFunc; consume the *http.Response via your code-under-test through the project's `internal/githubapi.NewREST` (pass it as `httpx.Options.Transport` together with `DisableRetries: true`).

Goroutine-safe: dispatch (RoundTrip) takes an RLock; registration (On*) takes a write lock. Multiple `t.Run` + `t.Parallel` subtests sharing one mux do not race.

func NewRESTMux

func NewRESTMux(t *testing.T) *RESTMux

NewRESTMux constructs a RESTMux ready to register handlers on. Registers a t.Cleanup that resets handler state at end of test.

func (*RESTMux) Calls

func (m *RESTMux) Calls(path string) int

Calls returns the number of times RoundTrip dispatched to `path`.

func (*RESTMux) OnBody

func (m *RESTMux) OnBody(path string, status int, body string)

OnBody registers an inline-body handler.

func (*RESTMux) OnFile

func (m *RESTMux) OnFile(path, fixturePath string)

OnFile registers a 200 OK fixture-file-backed handler for the given request path. `fixturePath` is relative to `tests/fixtures/`. The file MUST exist at first matching dispatch; missing files trigger t.Fatalf.

func (*RESTMux) OnFunc

func (m *RESTMux) OnFunc(path string, fn func(req *http.Request) (status int, body string, header http.Header))

OnFunc registers a per-call dynamic handler that sees the full *http.Request (use req.URL.RawQuery to inspect query strings).

func (*RESTMux) OnHeader

func (m *RESTMux) OnHeader(path string, status int, body string, header http.Header)

OnHeader registers an inline-body handler with a custom response header (e.g., Link header for paginated endpoints).

func (*RESTMux) RoundTrip

func (m *RESTMux) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies http.RoundTripper.

Jump to

Keyboard shortcuts

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