@cratis/pi 7.193.0 → 7.193.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,25 +71,55 @@ defmodule Prism.Access.Credentials.AddingCredentials.ForAddCredential.WhenAdding
71
71
 
72
72
  ## One scenario, several `should` assertions
73
73
 
74
- The `setup` establishes the context and performs the behavior once per assertion (ExUnit runs `setup` for each
75
- `test`); each `test` states one expectation and starts with **`should`**, so the file reads as a sentence:
74
+ Use the **scenarios** of Arc and Chronicle for Elixir: the counterpart of the C# `Specification` with its
75
+ `CommandScenario`, `EventScenario`, `ReadModelScenario`, `ReactorScenario` and `QueryScenario`. Facts are given,
76
+ the behavior happens **once** (`setup_all`: the shared lifecycle), and each `test` is one expectation, a
77
+ `should_*` function that starts the test name with **`should`**:
76
78
 
77
79
  ```elixir
78
- defmodule Prism.Access.ApiCredentials.ForState.WhenDerivingTheStatus do
79
- use ExUnit.Case, async: true
80
+ defmodule MyApp.Authors.ForRegisterAuthor.WhenRegistering.WithAValidName do
81
+ use Cratis.Arc.Test.CommandScenario,
82
+ application: MyApp.Arc,
83
+ event_types: [MyApp.Authors.AuthorRegistered]
84
+
85
+ alias MyApp.Authors.{AuthorRegistered, RegisterAuthor}
80
86
 
81
- alias Prism.Access.ApiCredentials.State
87
+ setup_all %{scenario: scenario} do
88
+ scenario =
89
+ scenario
90
+ |> given_events("ada", %AuthorRegistered{name: "Ada"})
91
+ |> execute(%RegisterAuthor{id: "grace", name: "Grace"}, as: principal("tester", ["maintainer"]))
82
92
 
83
- test "should be active by default" do
84
- assert State.status(%State{id: "c", exists?: true}, ~U[2026-03-01 12:00:00Z]) == :active
93
+ %{scenario: scenario}
94
+ end
95
+
96
+ test "should be successful", %{scenario: scenario}, do: should_be_successful(scenario)
97
+
98
+ test "should append the author as registered", %{scenario: scenario} do
99
+ should_have_appended(scenario, %AuthorRegistered{name: "Grace"}, event_source_id: "grace")
85
100
  end
86
101
  end
87
102
  ```
88
103
 
89
- - `test "should refuse a duplicate label"`, not `test "a duplicate label is refused"` and not
90
- `test "refuses..."`. Name the expected result, not the call.
91
- - A file with several unrelated scenarios is several files. Split `describe` blocks into
92
- `when_<behavior>/<preposition>_<condition>.exs` files; do not use `describe` to group scenarios.
104
+ A scenario is an immutable value: **given** and **because** functions (`given_events/3`, `execute/3`, `query/5`,
105
+ `append/3`) return the next scenario, **should** functions return the one they checked, so they chain. Use
106
+ `lifecycle: :per_test` (and `setup`) only for a spec whose tests change the shared state. Only the events the
107
+ behavior appended count for `should_have_appended`, not the given ones.
108
+
109
+ | Scenario | Module | Use for |
110
+ |---|---|---|
111
+ | Command | `Cratis.Arc.Test.CommandScenario` | a command through the real pipeline: result, validation, authorization, constraints, events |
112
+ | Query | `Cratis.Arc.Test.QueryScenario` | a query's arguments, authorization and data (the in-memory kernel runs no read-model queries) |
113
+ | Event | `Cratis.Chronicle.Testing.EventScenario` | an event's constraints (`unique/2`, `unique_event_type/1`), concurrency, sequence numbers |
114
+ | Read model | `Cratis.Chronicle.Testing.ReadModelScenario` | what a projection or reducer builds from given events, by module and key |
115
+ | Reactor | `Cratis.Chronicle.Testing.ReactorScenario` | the events a reactor produced and whether it failed |
116
+
117
+ For assertions on any other value use `Cratis.Arc.Test.Should` (`should_equal`, `should_contain_only`,
118
+ `should_match`, ...): the value first, so they pipe, and they return it. A plain `ExUnit.Case` with `assert` is
119
+ fine for a pure function; there is no scenario to set up.
120
+
121
+ - Name the expected result, not the call: `test "should refuse a duplicate label"`.
122
+ - A file with several unrelated scenarios is several files; do not use `describe` to group them.
93
123
  - Assert on the **contract**: the result, the events appended, what a collaborator was asked to do. Do not
94
124
  assert on internals, and do not spec logging or trivial delegation.
95
125
  - `doctest Module` belongs in its own spec (`when_building/with_the_documented_examples.exs`).
@@ -119,9 +149,9 @@ Chronicle kernel and gives `exec/3`, `facts/2` and the stand-ins.
119
149
 
120
150
  | Kind | Run it through | Assert |
121
151
  |---|---|---|
122
- | Command | the real pipeline (`Cratis.Arc.Test.execute/3`, in-memory Chronicle) | result, events appended (types and data), what the integration was asked |
123
- | Reactor / automation | the real reactor against the in-memory kernel | the command it ran, the event that records the outcome |
124
- | Read model, projection | the events folded (`State`, projection function) | the document, never `Process.sleep` |
152
+ | Command | `CommandScenario` (the real pipeline, in-memory Chronicle) | result, events appended (types and data), what the integration was asked |
153
+ | Reactor / automation | `ReactorScenario` (the real reactor against the in-memory kernel) | the command it ran, the event that records the outcome |
154
+ | Read model, projection | `ReadModelScenario`, or the events folded (`State`) for the pure part | the document, never `Process.sleep` |
125
155
  | Pure function | a direct call | return values; one file per rule |
126
156
  | LiveView / component | `Phoenix.LiveViewTest` (`render_component/2`, `live/2`) | the markup that matters: structure, state classes, the events wired, not every attribute |
127
157
 
@@ -70,25 +70,55 @@ defmodule Prism.Access.Credentials.AddingCredentials.ForAddCredential.WhenAdding
70
70
 
71
71
  ## One scenario, several `should` assertions
72
72
 
73
- The `setup` establishes the context and performs the behavior once per assertion (ExUnit runs `setup` for each
74
- `test`); each `test` states one expectation and starts with **`should`**, so the file reads as a sentence:
73
+ Use the **scenarios** of Arc and Chronicle for Elixir: the counterpart of the C# `Specification` with its
74
+ `CommandScenario`, `EventScenario`, `ReadModelScenario`, `ReactorScenario` and `QueryScenario`. Facts are given,
75
+ the behavior happens **once** (`setup_all`: the shared lifecycle), and each `test` is one expectation, a
76
+ `should_*` function that starts the test name with **`should`**:
75
77
 
76
78
  ```elixir
77
- defmodule Prism.Access.ApiCredentials.ForState.WhenDerivingTheStatus do
78
- use ExUnit.Case, async: true
79
+ defmodule MyApp.Authors.ForRegisterAuthor.WhenRegistering.WithAValidName do
80
+ use Cratis.Arc.Test.CommandScenario,
81
+ application: MyApp.Arc,
82
+ event_types: [MyApp.Authors.AuthorRegistered]
83
+
84
+ alias MyApp.Authors.{AuthorRegistered, RegisterAuthor}
79
85
 
80
- alias Prism.Access.ApiCredentials.State
86
+ setup_all %{scenario: scenario} do
87
+ scenario =
88
+ scenario
89
+ |> given_events("ada", %AuthorRegistered{name: "Ada"})
90
+ |> execute(%RegisterAuthor{id: "grace", name: "Grace"}, as: principal("tester", ["maintainer"]))
81
91
 
82
- test "should be active by default" do
83
- assert State.status(%State{id: "c", exists?: true}, ~U[2026-03-01 12:00:00Z]) == :active
92
+ %{scenario: scenario}
93
+ end
94
+
95
+ test "should be successful", %{scenario: scenario}, do: should_be_successful(scenario)
96
+
97
+ test "should append the author as registered", %{scenario: scenario} do
98
+ should_have_appended(scenario, %AuthorRegistered{name: "Grace"}, event_source_id: "grace")
84
99
  end
85
100
  end
86
101
  ```
87
102
 
88
- - `test "should refuse a duplicate label"`, not `test "a duplicate label is refused"` and not
89
- `test "refuses..."`. Name the expected result, not the call.
90
- - A file with several unrelated scenarios is several files. Split `describe` blocks into
91
- `when_<behavior>/<preposition>_<condition>.exs` files; do not use `describe` to group scenarios.
103
+ A scenario is an immutable value: **given** and **because** functions (`given_events/3`, `execute/3`, `query/5`,
104
+ `append/3`) return the next scenario, **should** functions return the one they checked, so they chain. Use
105
+ `lifecycle: :per_test` (and `setup`) only for a spec whose tests change the shared state. Only the events the
106
+ behavior appended count for `should_have_appended`, not the given ones.
107
+
108
+ | Scenario | Module | Use for |
109
+ |---|---|---|
110
+ | Command | `Cratis.Arc.Test.CommandScenario` | a command through the real pipeline: result, validation, authorization, constraints, events |
111
+ | Query | `Cratis.Arc.Test.QueryScenario` | a query's arguments, authorization and data (the in-memory kernel runs no read-model queries) |
112
+ | Event | `Cratis.Chronicle.Testing.EventScenario` | an event's constraints (`unique/2`, `unique_event_type/1`), concurrency, sequence numbers |
113
+ | Read model | `Cratis.Chronicle.Testing.ReadModelScenario` | what a projection or reducer builds from given events, by module and key |
114
+ | Reactor | `Cratis.Chronicle.Testing.ReactorScenario` | the events a reactor produced and whether it failed |
115
+
116
+ For assertions on any other value use `Cratis.Arc.Test.Should` (`should_equal`, `should_contain_only`,
117
+ `should_match`, ...): the value first, so they pipe, and they return it. A plain `ExUnit.Case` with `assert` is
118
+ fine for a pure function; there is no scenario to set up.
119
+
120
+ - Name the expected result, not the call: `test "should refuse a duplicate label"`.
121
+ - A file with several unrelated scenarios is several files; do not use `describe` to group them.
92
122
  - Assert on the **contract**: the result, the events appended, what a collaborator was asked to do. Do not
93
123
  assert on internals, and do not spec logging or trivial delegation.
94
124
  - `doctest Module` belongs in its own spec (`when_building/with_the_documented_examples.exs`).
@@ -118,9 +148,9 @@ Chronicle kernel and gives `exec/3`, `facts/2` and the stand-ins.
118
148
 
119
149
  | Kind | Run it through | Assert |
120
150
  |---|---|---|
121
- | Command | the real pipeline (`Cratis.Arc.Test.execute/3`, in-memory Chronicle) | result, events appended (types and data), what the integration was asked |
122
- | Reactor / automation | the real reactor against the in-memory kernel | the command it ran, the event that records the outcome |
123
- | Read model, projection | the events folded (`State`, projection function) | the document, never `Process.sleep` |
151
+ | Command | `CommandScenario` (the real pipeline, in-memory Chronicle) | result, events appended (types and data), what the integration was asked |
152
+ | Reactor / automation | `ReactorScenario` (the real reactor against the in-memory kernel) | the command it ran, the event that records the outcome |
153
+ | Read model, projection | `ReadModelScenario`, or the events folded (`State`) for the pure part | the document, never `Process.sleep` |
124
154
  | Pure function | a direct call | return values; one file per rule |
125
155
  | LiveView / component | `Phoenix.LiveViewTest` (`render_component/2`, `live/2`) | the markup that matters: structure, state classes, the events wired, not every attribute |
126
156
 
@@ -34,6 +34,15 @@ Cratis.Arc.Test.assert_invalid(result, :name)
34
34
  Cratis.Arc.Test.refute_appended(arc, AuthorRegistered)
35
35
  ```
36
36
 
37
+ For specs in the shape of a Specification use the scenarios: `use Cratis.Arc.Test.CommandScenario,
38
+ application: MyApp.Arc, event_types: [...]` (or `QueryScenario`) starts the kernel once per module, gives the
39
+ scenario as `:scenario`, and you chain `given_events/3`, `execute/3` (`as:` a `principal/2`) or `query/5`, then one
40
+ `should_*` per test: `should_be_successful/1`, `should_not_be_authorized/1`, `should_have_validation_errors/1,2`,
41
+ `should_have_constraint_violation/1,2`, `should_respond_with/2`, `should_have_appended/2,3`,
42
+ `should_have_appended_exactly/2`, `should_not_have_appended_any_events/1`, `should_return_items/2`. The
43
+ `Cratis.Arc.Test.Should` module is the counterpart of the C# `Should` extensions (`should_equal/2`,
44
+ `should_contain_only/2`, `should_match/2`, ...). Read `rules/specs.elixir.md` for the layout of the files.
45
+
37
46
  Also `Cratis.Arc.Test.query/5`, `principal/2`, `assert_success/1`, `assert_forbidden/1`,
38
47
  `assert_appended/3` and `wait_for_observers/1`. For a plain Plug application without
39
48
  Chronicle, call `Cratis.Arc.execute/3` and `Cratis.Arc.query/5` with `principal:` and `tenant:` and assert
@@ -302,7 +302,7 @@ compiler checks and sink choice. Pin `id:`; renaming it registers a different pr
302
302
  Read the reference that matches the task:
303
303
 
304
304
  - [references/decision-reads.md](references/decision-reads.md) when an append depends on state a concurrent append could change: `Cratis.Chronicle.decide/2`, `DecisionReads`, `ConcurrencyScope` builders, the optimistic default.
305
- - [references/testing.md](references/testing.md) when writing specs: `Cratis.Chronicle.Testing.Case`, assertions, the in-memory kernel's limits.
305
+ - [references/testing.md](references/testing.md) when writing specs: `Cratis.Chronicle.Testing.Case`, assertions, the in-memory kernel's limits, and the `EventScenario`, `ReadModelScenario` and `ReactorScenario` that state one behavior in the shape of a Specification (given, because, `should_*`).
306
306
  - [references/constraints.md](references/constraints.md) when declaring uniqueness, standalone constraint modules, `remove_constraint/2`, violation details or closing events.
307
307
  - [references/discovery-and-generations.md](references/discovery-and-generations.md) when pinning ids, setting `:drift_policy`, running `mix chronicle.discover`, or handling older event generations.
308
308
  - [references/captures.md](references/captures.md) when turning external API, webhook or other-store data into events.
@@ -39,6 +39,17 @@ end
39
39
 
40
40
  `start_supervised_client/1` (outside ExUnit templates; pair with `stop_supervised_client/1`) accepts `:event_types`, `:event_sources`, `:constraints` (standalone constraint modules), `:reactors`, `:reducers`, `:projections`, `:name`, `:event_store` and `:namespace`.
41
41
 
42
+ ## Scenarios: one behavior, one `should_*` per test
43
+
44
+ `use Cratis.Chronicle.Testing.EventScenario` (events and their constraints), `ReadModelScenario` (a projection
45
+ or reducer, asked for by module and key) or `ReactorScenario` (what a reactor produced, and whether it failed)
46
+ starts the kernel once per spec module (`lifecycle: :per_test` for one per test) and gives the scenario as
47
+ `:scenario`. Chain `given_events/3` and `append/3,4`, then assert with `should_be_successful/1`,
48
+ `should_have_constraint_violation/1,2`, `should_have_concurrency_violation/1`, `should_have_appended/2,3`,
49
+ `should_have_appended_exactly/2`, `should_not_have_appended_any_events/1`, `should_have_tail_sequence_number/2`,
50
+ `should_have_read_model/4`, `should_have_observer_failures/1,2`. Only what the `append` added counts, not the
51
+ given events. The layout and naming of the files is `rules/specs.elixir.md`.
52
+
42
53
  ## Waiting and reading
43
54
 
44
55
  An append returns once the event is committed; reactors and reducers run afterward in `Cratis.Chronicle.Testing.Dispatcher`, synchronously after each committed append. Call `Cratis.Chronicle.Testing.wait_for_observers(client)` before asserting on their effects; it is a sleep-free barrier that returns `{:error, failures}` when a reactor or reducer raised.
@@ -610,7 +610,10 @@ or V4 execution. Run the CLI or MCP for those verdicts.
610
610
  | PLAY0816 | a specification states `then invokes <Command>`, but no reaction declared in its own slice invokes that command; a specification runs its own slice only, so the invocation can never be made |
611
611
  | PLAY0286 | spec value is not a member of the enum |
612
612
  | PLAY0287 | producer/capture/spec assigns a field the event lacks |
613
- | PLAY0291-0294 | bad single-line JSON / unknown key / wrong shape / duplicate key |
613
+ | PLAY0291 | Malformed structured value; check the inline object or list, or the indented block's syntax and indentation |
614
+ | PLAY0292 | Unknown member; use a member declared by the target type |
615
+ | PLAY0293 | Incompatible shape; match the structured value to the declared target type |
616
+ | PLAY0294 | Duplicate member; declare each member only once in the object |
614
617
 
615
618
  ## Batches, described handlers, text rules and protected reads
616
619
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "7.193.0",
3
+ "version": "7.193.2",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [