# `Alternera.PluginWasmFramework.Test`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/test.ex#L1)

The contract a language plugin keeps, as ExUnit tests, the way the SDK's
`plugin_contract/2` checks a plugin. The plugin gives, in its own
language, the source for each named scenario; the kit runs each through
the step with the SDK's test host (`Plugin.Test`) and checks the result.

    defmodule Alternera.Plugins.JavascriptContractTest do
      use ExUnit.Case, async: true
      use Alternera.PluginWasmFramework.Test

      language_contract(Alternera.Plugins.Javascript,
        sources: %{
          body_unchanged: "return req.body.read()",
          ...
        }
      )
    end

A scenario with no source fails, so the list is the contract; one a
plugin cannot keep goes under `skip:` with the reason, and the test
says so. Grow this list rather than writing per-language one-offs.

## The scenarios

Each names what the script does and what the kit then checks. A
request step runs on the body `"kit body"` unless the scenario says
otherwise; "returns" means the script's returned value.

| Scenario | The script | The kit checks |
|---|---|---|
| `body_unchanged` | returns its request body as it got it | the body stays, nothing is written |
| `body_changed` | returns its request body with `" changed"` appended | the body is `"kit body changed"` |
| `nothing` | returns nothing | the step skips |
| `read_header` | returns the request header `x-kit` | `"from header"`, written to the variable `got` |
| `read_query` | returns the query parameter `kit` | `"from query"`, to `got` |
| `read_var` | returns the variable `kit` | `"from var"`, to `got` |
| `read_input` | returns the input `kit` | `"from input"`, to `got` |
| `content_type` | a response step: returns `{"kit":true}` with the content type `application/json` | the body and its `Content-Type` |
| `two_outputs` | sets the outputs `a` to 1 and `b` to `"two"`, returns nothing | the `outputs` output |
| `store_put_get` | puts `"stored"` under `k` in the store `kit`, gets it back and returns it | `"stored"` |
| `secret_store_scrub` | gets `k` from the secret store `kitsecret` and fails with the type `kit_failure` and the message `"saw "` followed by the value | the message is scrubbed |
| `tenant_var` | returns the tenant variable `kit_var` | `"plain value"` |
| `tenant_secret` | returns the tenant secret `kit_secret` | its reference, `{{secret.kit_secret}}`, never the value |
| `pipeline_call` | calls `kit_callee` with the input `n` = 1 and returns its result | the call and `"callee result"` |
| `pipeline_call_failure` | calls `kit_missing` and does not handle the failure | the step fails with the callee's type, `pipeline_not_found` |
| `fail` | fails with the type `kit_failure`, the status 418 and the message `"on purpose"` | that error, on purpose |
| `syntax_error` | a source whose line 2 does not compile | the save is refused, naming line 2 |
| `runtime_error` | raises an error on line 3 | `script_error`, naming line 3 |
| `endless_loop` | loops forever | `timeout` |
| `allocation_loop` | allocates until memory runs out (Flat 8 MiB) | `memory_limit` |
| `allocation_loop_caught` | the same, catching the error it gets and returning `"survived"` | still `memory_limit` |
| `global_fresh` | returns `"reused"` when a global it sets is already set, else sets it and returns `"fresh"` | `"fresh"` on two runs in a row |

# `check`

```elixir
@spec check(module(), atom(), String.t() | nil) :: :ok | {:fail, String.t()}
```

Runs one scenario's source and checks it: `:ok` or `{:fail, why}`.

# `language_contract`
*macro* 

One test per scenario for `plugin`. `sources:` maps a scenario to its
source; `skip:` maps a scenario the plugin cannot keep to the reason.

# `scenarios`

```elixir
@spec scenarios() :: [atom()]
```

The scenarios, in order.

---

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