# `Alternera.Plugin.Test`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/test.ex#L1)

What a plugin's own tests build on, with no proxy around: a well-formed
context, a way to run a step's `call/3` in a phase the way the runner
does (through `Plugin.CallAdapter`), and the checks
of the contract as plain predicates, so they work under any test
framework. `Alternera.Plugin.Test.Contract` turns the predicates
into ExUnit tests.

    step = Plugin.Test.step!(MyPlugin, %{"direction" => "response"})
    {:ok, body, summary} = Plugin.Test.run_step(step, :response_body, "{}")

What a plugin asks of the proxy (`Alternera.Plugin.Host`) is answered by
`Alternera.Plugin.Test.Host`, with no setup.

# `body_result?`

```elixir
@spec body_result?(term()) :: boolean()
```

Whether `result` is one the adapter may hand the runner for a step run
in a body phase (`run/5`): `{:ok, body, summary[, extras]}`, a skip, or
a stop (`stop?/1`).

# `call`

```elixir
@spec call(module(), atom(), list()) :: term()
```

`apply/3` on the plugin: a test that names the module directly would be
told by the type checker that a check always or never holds for that one
plugin, which is the point of a contract test.

# `configured?`

```elixir
@spec configured?(module(), map()) :: boolean()
```

Whether `settings` hold every required field a Configuration step may
fill: the runner runs no step that lacks one (`not_configured`).

# `context`

```elixir
@spec context(map() | keyword()) :: Alternera.Plugin.context()
```

A context like the one the runner hands a step (`t:Alternera.Plugin.context/0`):
nil tenant and pipeline ids, a step path, the phase `:request`, an empty
view under `:view`; `overrides` are merged on top.

# `head_result?`

```elixir
@spec head_result?(term()) :: boolean()
```

Whether `result` is one the adapter may hand the runner for a step run
in a head phase (`run/5`): `{:cont, message[, extras]}`, a skip, a hold,
a refusal that skips to After, or a stop (`stop?/1`).

# `init`

```elixir
@spec init(module(), term()) :: {:ok, term()} | {:error, [String.t()]}
```

Checks settings as the host does, the adapter's own keys included (`Plugin.CallAdapter.init/2`).

# `part_skip_problems`

```elixir
@spec part_skip_problems(module()) :: [String.t()]
```

Problems with a plugin's head or body skip values (`skips_head`,
`skips_body`): only a plugin declared `checks_head: true` lists them.

# `phases`

```elixir
@spec phases() :: [Alternera.Plugin.phase()]
```

The phases a step may run in, in the order they run.

# `plain_data?`

```elixir
@spec plain_data?(term()) :: boolean()
```

Whether `term` is plain data the compiler can embed as a literal
(`Provisional.Source.plain_data?/1`).

# `prepare!`

```elixir
@spec prepare!(module(), Alternera.Plugin.settings()) :: Alternera.Plugin.opts()
```

The prepared settings, as the runner loads them (`load/1`, lenient), or
raises with the plugin's messages.

# `prepared_facts`

```elixir
@spec prepared_facts(module(), map(), term()) :: map()
```

What preparing a step decides from its settings and opts, which a
Configuration step's fill must leave as it is: the signature's
direction, reads, writes and `reads_any`, the pipelines `calls/1` names
and the errors `raises/1` lists.

# `release`

```elixir
@spec release(module() | map(), term(), term()) :: term()
```

Gives back what a step held, as the host does at its Close (`Plugin.CallAdapter.release/4`).

# `release`

```elixir
@spec release(map(), term(), term(), map()) :: term()
```

Gives back what a prepared step (`step!/2`) held at its Close, with `view`
the exchange as it stands there (the response head under `:response`),
as the host does: `leave/3` reads it through `context.inputs`, over the
step's signature.

# `run`

```elixir
@spec run(
  module(),
  Alternera.Plugin.phase(),
  map() | binary(),
  Alternera.Plugin.opts(),
  map() | nil
) ::
  term()
```

Runs the plugin's `call/3` in `phase` on `message` (a head map or a body
binary) with the prepared settings and a context (`context/1` with
`phase` set, unless one is given), the way the runner does, through
`Plugin.CallAdapter`: an `Alternera.Plugin.UnresolvedVariableError`
the plugin raises is the step's `unresolved_variable` failure, not a
crash. Returns the adapter's result, the runner's vocabulary
(`head_result?/1`, `body_result?/1`).

The step's signature comes from `context.adapter` (`Plugin.CallAdapter.extras/2`
of the step's settings, as `run_step/4` puts it there), or that of empty
settings when the context has none. The step reads the exchange from
`context.view`, as the runner hands it one: `message` goes into the
view's slot for the phase (`request`, `response`, `request_body` or
`response_body`) when the slot is empty. As the runner does, the view
keeps only the bodies the signature reads or writes, besides the phase's
own (`Location.only_bodies/2`): a body the step does not name is nil.

# `run_step`

```elixir
@spec run_step(map(), Alternera.Plugin.phase(), map() | binary(), map() | nil) ::
  term()
```

Runs a prepared step (`step!/2`) in one phase, as `run/5` does, with the
step's own signature: what a test of a plugin's `call/3` normally uses.

# `sample_values`

```elixir
@spec sample_values(map()) :: [term()]
```

Values a Configuration step might fill `field` with, for the contract's
check that a fill changes nothing prepared: each value of an `:enum`, both
booleans, the ends of an `:integer`'s range, a plain text. A list or a map
gets none: it is replaced whole, and its items say nothing alone.

# `schema_field_problems`

```elixir
@spec schema_field_problems(term()) :: [String.t()]
```

What is wrong with a `settings_schema/0` field (`t:Alternera.Plugin.schema_field/0`),
as a list of messages; `[]` for a well-formed one.

# `secret_store`

```elixir
@spec secret_store(String.t(), String.t()) :: :ok
```

Marks store `name` of `tenant` secret in the calling process, for
`Plugin.Provisional.Store.secret?/2`.

# `sign_in`

```elixir
@spec sign_in(String.t() | nil) :: :ok
```

Signs `subject` in as the exchange's caller in the calling process, for
`Plugin.Provisional.Identity`; `nil` signs it out (anonymous, the default).

# `skip_passes_through?`

```elixir
@spec skip_passes_through?(
  module() | map(),
  Alternera.Plugin.opts(),
  binary(),
  Alternera.Plugin.phase()
) :: boolean()
```

Whether a streaming step that skips passes its input on unchanged (a
`Plugin.BodyFilter` rule): when streaming `body` skips (`stream/5`),
every byte it sent is `body`'s. True when it does not skip.

# `source_context`

```elixir
@spec source_context(module() | nil, map() | keyword()) :: map()
```

A context like the one the proxy opens a message source with
(`t:Alternera.Plugin.Provisional.Source.context/0`): a nil tenant, a
proxy user's id, the `reveal: true` keys of `source` (a source module, or
nil for none) and 10 000 ms; `overrides` are merged on top. Put the
secrets a source reveals under `:test_secrets`, as for a step.

# `step!`

```elixir
@spec step!(module(), map()) :: %{module: module(), opts: term(), adapter: term()}
```

A step as the host prepares one: the plugin, its opts from `load/1`, and
the model's data beside them (`Plugin.CallAdapter.extras/2`: its signature),
for `run_step/4`, `release/4` and `stream/5`. Raises when the plugin
refuses the settings.

# `stop?`

```elixir
@spec stop?(term()) :: boolean()
```

Whether `result` ends the walk the way the contract allows: an answer, a
return, or a failure with the keys the contract names.

# `stream`

```elixir
@spec stream(
  module() | map(),
  Alternera.Plugin.opts(),
  binary(),
  Alternera.Plugin.phase(),
  pos_integer()
) :: {:ok, binary()} | {:skip, binary(), term()} | term()
```

Streams `body` through a streaming step (a `streams: true` plugin's
`call/3`, then its filter's `chunk/2` on pieces of at most
`piece_bytes`, then its `finish/1`, through `Plugin.CallAdapter`), as the
proxy does when the body streams. `step` is a prepared step (`step!/2`),
whose signature the start reads through, or a plugin, run with its opts
and an empty signature. Returns `{:ok, out}` with every byte the step
sent, `{:skip, out, reason}` when it skipped at its start or its finish,
or the failure.

# `stub_authorization`

```elixir
@spec stub_authorization((map(), String.t() | nil, String.t() | nil -&gt; term())) :: :ok
```

Sets what the test host answers for credentials
(`Plugin.Provisional.Credentials.authorization/3`) in the calling process:
`stub` gets the context, the target URL and the request URL and answers as
the host would. Without one no credentials apply.

# `stub_outbound`

```elixir
@spec stub_outbound((map(), map(), keyword() -&gt; term())) :: :ok
```

Answers the outbound calls (`Plugin.Provisional.Outbound.call/3`) the calling
process makes from here on with `stub`, a function of the request, the
context and the options (`:upstream` carries the plugin's id) that returns
what `call/3` would: `{:ok, %{status:, headers:, body:, exchange_id:}}` or
`{:error, type, message}`. Without one a call fails `unavailable`.

# `stub_resource_actions`

```elixir
@spec stub_resource_actions(%{
  required(String.t()) =&gt; %{required(String.t()) =&gt; (map() -&gt; term())}
}) ::
  :ok
```

Sets the resource actions the calling process runs through
`Alternera.Plugin.Host.resource_run/5`: a map of resource name to a
map of action name to a function of the input that answers what the host
would, e.g. `%{"finance.exchange_rate" => %{"convert" => fn _ -> {:ok, %{"result" => "8.75"}} end}}`.

# `stub_resources`

```elixir
@spec stub_resources(%{required(String.t()) =&gt; %{required(term()) =&gt; map()}}) :: :ok
```

Sets the resource records the calling process reads through
`Alternera.Plugin.Host.resource_get/3`: a map of resource name
(`"<package>.<resource>"`) to a map of lookup key to record, e.g.
`%{"finance.vat_code" => %{"SE25" => %{"code" => "SE25", "rate" => "25"}}}`.
A key not in a resource's map is `:not_found`, a name not in the map an
unknown resource.

# `stub_tenant_vars`

```elixir
@spec stub_tenant_vars(%{required(String.t()) =&gt; String.t()}) :: :ok
```

Sets the tenant variables `Plugin.Secret.values/4` reads in the calling
process from here on: `NAME` to a plain variable's value, `secret.NAME`
to a secret's, for every tenant.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
