# `Datumo.Scenario`
[🔗](https://github.com/ortegacmanuel/datumo/blob/v0.1.0/lib/datumo/scenario.ex#L1)

The board's scenarios as tests: `given`, then `decide` or `read`, then
`expect`.

    import Datumo.Scenario

    # a write slice
    given([%LotOpened{lot_id: "L-1"}])
    |> decide(Decider, %CloseLot{lot_id: "L-1"})
    |> expect({:ok, [%LotClosed{lot_id: "L-1"}]})

    # a read slice
    given([%PlotRegistered{plot_id: "P-1", name: "La Vega"}])
    |> read(ReadModel, "P-1")
    |> expect(%{name: "La Vega"})

## Why not fold the `given` by hand

In production a decision only sees what its `query/1` reads. Folding the
`given` straight into `apply_event/2` skips that read, so a query that forgets
a type, asks for the wrong tag or misses a follow-up passes every test and
silently never fires. Here the `given` is the store:

  * `given/1` stores each event the way `Datumo.Decide` appends it: through
    `Datumo.Event.to_event/1`, so its tags are the production ones, and through
    JSON, so `apply_event/2` gets the same string-keyed data it gets from FACT.
  * `decide/3` and `read/3` run the production loop (`Datumo.Decide.fold/3`,
    `Datumo.StateView.view/3`) reading that store with `Datumo.Reader.selects?/2`,
    the same match `Datumo.Reader` applies.
  * `expect/2` matches the result against a pattern. When it fails and some
    `given` events were never read, it says which: a scenario may include an
    event on purpose to show it's ignored, so being unread is a hint, not a
    failure by itself.

## The `given`

An event struct — the normal case. An event no code writes any more (it's
still in the store) or that comes from outside has no struct: give it as the
shape FACT appends, `%{type: "…", data: %{…}, tags: ["…"]}`, plus `id: "…"`
when the fold keys on the event id.

# `decide`

Runs a write slice's decision against the `given`.

# `expect`
*macro* 

Asserts the result matches `pattern` (a struct or map pattern matches the keys
it names). Returns the result.

# `given`

Stores the scenario's `given` events, in order.

# `read`

Computes a read slice's view from the `given`.

---

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