diff --git a/lib/statifier_examples/charts.ex b/lib/statifier_examples/charts.ex index 231de335..aef79822 100644 --- a/lib/statifier_examples/charts.ex +++ b/lib/statifier_examples/charts.ex @@ -306,6 +306,7 @@ defmodule StatifierExamples.Charts do [ CardAuth.block_types(), Signup.block_types(), + Library.block_types(), Messaging.block_types(), %{TypedSendStep.type_name() => TypedSendStep} ] diff --git a/lib/statifier_examples/library.ex b/lib/statifier_examples/library.ex index 6266aa84..7eded35f 100644 --- a/lib/statifier_examples/library.ex +++ b/lib/statifier_examples/library.ex @@ -15,9 +15,15 @@ defmodule StatifierExamples.Library do them to the fixtures every page offers, for the reason the card and signup domains each keep their own list: which documents a domain ships is a fact about the domain. + + The one block type the library world registers, `myapp.setup` + (`block_types/0`), is used by neither document: it is the root of a + branch library setup that can end more than one way + (`StatifierExamples.Library.Setups`). """ alias StatifierExamples.Charts.Fixture + alias StatifierExamples.Library.Setup # `{key, file}`. Listed rather than globbed, as `StatifierExamples.CardAuth` # lists its own: a stray file in `priv/` should not silently become an @@ -33,4 +39,12 @@ defmodule StatifierExamples.Library do """ @spec fixtures() :: [Fixture.t()] def fixtures, do: Enum.map(@documents, fn {key, file} -> Fixture.load!(key, file) end) + + @doc """ + The library world's own block type, as a `type_name => module` map: the + root a branch library setup that can end more than one way is built on + (`StatifierExamples.Library.Setup`). + """ + @spec block_types() :: %{optional(String.t()) => module()} + def block_types, do: %{"myapp.setup" => Setup} end diff --git a/lib/statifier_examples/library/onboarding.ex b/lib/statifier_examples/library/onboarding.ex index f3e28f7f..4cf4e182 100644 --- a/lib/statifier_examples/library/onboarding.ex +++ b/lib/statifier_examples/library/onboarding.ex @@ -24,10 +24,13 @@ defmodule StatifierExamples.Library.Onboarding do options in), and the Path goes on to its closing screen once the last one has finished. - The subchart declares the one outcome `done`, which is the one root - outcome each setup declares (`StatifierExamples.Library.Setups`' "How a - setup finishes"). statifier_blocks refuses, at child creation, a child - whose root declares an outcome the block did not. + The subchart declares `done` and `site_visit_needed`, every root outcome + a setup declares (`StatifierExamples.Library.Setups`' "How a setup + finishes"): statifier_blocks refuses, at child creation, a child whose + root declares an outcome the block did not. It writes what each setup + finished with to `last_setup`, the setup's done data, whose `outcome` is + the setup's root outcome, so the onboarding can read how the setup of + the latest pass ended. ## The allow-list: `resolve_chart/2` diff --git a/lib/statifier_examples/library/setup.ex b/lib/statifier_examples/library/setup.ex new file mode 100644 index 00000000..2eb3c6b2 --- /dev/null +++ b/lib/statifier_examples/library/setup.ex @@ -0,0 +1,167 @@ +defmodule StatifierExamples.Library.Setup do + @moduledoc """ + `myapp.setup`: the root of a branch library setup that can end more than + one way. + + A setup's steps are `myapp.step` blocks (`StatifierExamples.Signup.ReturnableStep`), + and a step finishes as `done` whichever button was pressed, so a setup + whose root is a `core.sequence` declares the one root outcome `done` and + how it went lives only in its `responses`. A parent that runs the setup + as a child (`StatifierExamples.Library.Onboarding`) sees nothing of a + child but the root outcome it finished with, so a setup that should be + able to tell its parent "this needs a site visit" says it through this + root instead. + + ## What an author configures + + The steps go in the `body` slot, in order, exactly as they went in a + sequence's `body`. Beside them, three params name the one other way the + setup can end: + + * `ending` - the outcome it ends with, such as `site_visit_needed`. + Blank, the setup declares `done` alone and runs as a sequence would. + * `ending_on` - the outcome of the button whose press can end it. + * `ending_when` - a condition on that press, read from what the press + carried (`_event.data.`), so the press ends the setup + only when the answer calls for it. Blank, every press of the button + ends it. + + The press is recorded first, as every press is: the step it belongs to + writes its responses, and then the whole setup ends with `ending`, + whatever steps are left. A setup whose steps all finish without that + press ends with `done`. + + ## What it is made of + + A **composite**: a `core.group` whose `body` this type exposes as its own + pass-through slot (`statifier_blocks`' ADR-0002 pass-through amendment), + so the author's steps keep their own ids, and whose `interrupts` hold one + `core.on_event` when an ending is named. The handler listens for the + button's press, `StatifierExamples.Signup.Screen.outcome_event/1`, under + the configured condition, abandons the group and names the outcome it + finishes with under `finish_as` (the ADR's `C8`). `declared_outcomes/1` + answers `done` and then the ending, from the block's own config (`C9`), + which is what a compile for use as a child reads as the document's root + outcomes, one top-level `` each (ADR-0004's Amendment of + 2026-08-29, `C1`). + + The handler runs in a region of its own beside the body, so the step's + own transition on the same press is taken too: the step records the + press and the group then leaves with the ending. + """ + + use StatifierBlocks.Composite, + name: "myapp.setup", + params: [ + %{ + key: "ending", + type: :string, + label: "It can also end with", + required?: false, + default: "" + }, + %{ + key: "ending_on", + type: :string, + label: "When this button is pressed", + required?: false, + default: "" + }, + %{ + key: "ending_when", + type: :expression, + label: "And this holds of the press", + required?: false, + default: "" + } + ], + slots: [%{name: "body", to: {"setup", "body"}, label: "Steps"}], + sentence: "Run the setup's steps", + palette_entry: %{ + label: "Setup", + group: "Library", + description: + "Runs a setup's steps; a button's press may end it with an outcome of its own.", + icon: "wrench-screwdriver", + keywords: ["setup", "steps", "outcome", "ending"], + order: 0 + }, + version: 1 + + alias StatifierBlocks.Block + alias StatifierExamples.Signup.Screen + + @doc """ + The group the author's steps land in, with the ending's handler on its + rail when the config names an ending. + + Pure, and the ids are local. The group's `body` is written empty, which + is what makes it a legal pass-through target. + """ + @impl StatifierBlocks.Composite + def subtree(params) do + handlers = + case ending(params) do + nil -> [] + ending -> [handler(ending, params)] + end + + [Block.new("core.group", id: "setup", slots: %{"body" => [], "interrupts" => handlers})] + end + + @doc """ + The outcomes this block declares: `done`, then the ending when the config + names one. + + iex> StatifierExamples.Library.Setup.declared_outcomes(%{ + ...> "ending" => "site_visit_needed", + ...> "ending_on" => "test_reported" + ...> }) + ["done", "site_visit_needed"] + + iex> StatifierExamples.Library.Setup.declared_outcomes(%{"ending" => ""}) + ["done"] + """ + @impl StatifierBlocks.Composite + def declared_outcomes(config) do + case ending(config) do + nil -> ["done"] + ending -> ["done", ending] + end + end + + @spec handler(String.t(), map()) :: Block.t() + defp handler(ending, params) do + Block.new("core.on_event", + id: "ending", + config: %{ + "event" => Screen.outcome_event(text(params, "ending_on")), + "payload" => "", + "cond" => text(params, "ending_when"), + "outcome" => "abandon", + "finish_as" => ending, + "capture" => %{} + } + ) + end + + # The ending a config names, or `nil` for a blank one or one with no + # button to end on, so a config still being typed into answers no handler + # rather than one listening for nothing. + @spec ending(map()) :: String.t() | nil + defp ending(config) do + case {text(config, "ending"), text(config, "ending_on")} do + {"", _button} -> nil + {_ending, ""} -> nil + {ending, _button} -> ending + end + end + + @spec text(map(), String.t()) :: String.t() + defp text(config, key) do + case Map.get(config, key) do + value when is_binary(value) -> value + _absent -> "" + end + end +end diff --git a/lib/statifier_examples/library/setups.ex b/lib/statifier_examples/library/setups.ex index 74d0edbf..6d51aa35 100644 --- a/lib/statifier_examples/library/setups.ex +++ b/lib/statifier_examples/library/setups.ex @@ -22,12 +22,31 @@ defmodule StatifierExamples.Library.Setups do ## How a setup finishes - Each document's root is a `core.sequence`, so it declares one root - outcome, `done`, and a document compiled for use as a child finishes - with it (`outcomes/1`). What happened inside a setup - which catalogue - source, whether the reader read a test card - is in its `responses`, - where each step's press recorded it, and not in the outcome: a - `myapp.step` finishes as `done` whichever button was pressed. + A setup whose root is a `core.sequence` declares one root outcome, + `done`, and a document compiled for use as a child finishes with it + (`outcomes/1`). The catalogue sync and the reading-list import are such + setups. What happened inside one - which catalogue source, which term - + is in its `responses`, where each step's press recorded it, and not in + the outcome: a `myapp.step` finishes as `done` whichever button was + pressed. + + A setup that should be able to tell its parent how it went has a + `myapp.setup` root instead (`StatifierExamples.Library.Setup`), which + declares `done` and the one ending its config names. The card-reader + setup is one: a Finish on its test-card screen saying the reader read + nothing ends it with `site_visit_needed`, which `outcomes/1` answers + beside `done` and the onboarding reads + (`StatifierExamples.Library.Onboarding`). + + ## What a librarian reads + + Each document's `metadata` description is written for whoever edits the + document: it names block types and fixture files. The settings page + draws `description/1` instead, one sentence per setup for the librarian + choosing which to run. It is kept here, beside the one list, rather than + in the documents, because which setups a page offers and how it words + them is this list's, and the document's own description already has a + reader: the person editing it. None of the three is among the fixtures `StatifierExamples.Charts.fixtures/0` lists. Like the patron registration steps, each is run as a supplied @@ -38,13 +57,16 @@ defmodule StatifierExamples.Library.Setups do alias StatifierBlocks.{Decode, Document} alias StatifierExamples.Charts.Subchart - # `{key, path file, screens file}`, in the order a page offers them. - # Listed rather than globbed, as `StatifierExamples.Library` lists its - # documents: a stray file in `priv/` should not become a setup. + # `{key, path file, screens file, description}`, in the order a page + # offers them. Listed rather than globbed, as `StatifierExamples.Library` + # lists its documents: a stray file in `priv/` should not become a setup. @setups [ - {"catalogue_sync", "catalogue_sync.json", "catalogue_sync_screens.json"}, - {"reading_list_import", "reading_list_import.json", "reading_list_import_screens.json"}, - {"card_reader_setup", "card_reader_setup.json", "card_reader_setup_screens.json"} + {"catalogue_sync", "catalogue_sync.json", "catalogue_sync_screens.json", + "Keep this branch's catalogue in step: choose where its records come from, how often they sync and who hears when a sync fails."}, + {"reading_list_import", "reading_list_import.json", "reading_list_import_screens.json", + "Bring in the reading lists local schools send for a term, and choose whether their titles are held at the branch."}, + {"card_reader_setup", "card_reader_setup.json", "card_reader_setup_screens.json", + "Set up the branch's library-card reader and check it reads a card; a reader that reads nothing is sent for a site visit."} ] @doc "The setups' keys, in the order a page offers them." @@ -57,7 +79,7 @@ defmodule StatifierExamples.Library.Setups do """ @spec document(String.t()) :: {:ok, Document.t()} | :error def document(key) do - with {:ok, {_key, path_file, _screens_file}} <- setup(key) do + with {:ok, {_key, path_file, _screens_file, _description}} <- setup(key) do case Decode.decode(File.read!(fixture(path_file))) do {:ok, document} -> {:ok, document} {:error, error} -> raise "#{path_file} is not a block document: #{inspect(error)}" @@ -65,13 +87,24 @@ defmodule StatifierExamples.Library.Setups do end end + @doc """ + What the setup does, in one sentence for a librarian (the moduledoc's + "What a librarian reads"), or `:error` for a key this module does not + list. + """ + @spec description(String.t()) :: {:ok, String.t()} | :error + def description(key) do + with {:ok, {_key, _path_file, _screens_file, description}} <- setup(key), + do: {:ok, description} + end + @doc """ The screens document the setup's steps name, decoded: `{:ok, screens}`, or `:error` for a key this module does not list. """ @spec screens(String.t()) :: {:ok, map()} | :error def screens(key) do - with {:ok, {_key, _path_file, screens_file}} <- setup(key), + with {:ok, {_key, _path_file, screens_file, _description}} <- setup(key), do: {:ok, screens_file |> fixture() |> File.read!() |> Jason.decode!()} end @@ -116,7 +149,8 @@ defmodule StatifierExamples.Library.Setups do end end - @spec setup(String.t()) :: {:ok, {String.t(), String.t(), String.t()}} | :error + @spec setup(String.t()) :: + {:ok, {String.t(), String.t(), String.t(), String.t()}} | :error defp setup(key) do case List.keyfind(@setups, key, 0) do nil -> :error diff --git a/lib/statifier_examples_web/live/settings_live.ex b/lib/statifier_examples_web/live/settings_live.ex index 680dea80..a2d65e90 100644 --- a/lib/statifier_examples_web/live/settings_live.ex +++ b/lib/statifier_examples_web/live/settings_live.ex @@ -6,7 +6,9 @@ defmodule StatifierExamplesWeb.SettingsLive do The page lists what `StatifierExamples.Library.Setups` lists - a catalogue sync, a reading-list import and a card-reader setup - with each one's name, what it does and the root outcomes its document - declares. Pressing a setup's Start starts it alone as a durable Journey + declares. What it does is the sentence `StatifierExamples.Library.Setups` + keeps for a librarian (its "What a librarian reads"), not the document's + own description, which is written for whoever edits the document. Pressing a setup's Start starts it alone as a durable Journey under the librarian's branch scope, through `StatifierExamplesWeb.JourneyHost.start/2`, and sends the reader to the Journey page @@ -116,11 +118,12 @@ defmodule StatifierExamplesWeb.SettingsLive do for key <- Setups.keys() do {:ok, %Document{metadata: metadata}} = Setups.document(key) {:ok, outcomes} = Setups.outcomes(key) + {:ok, description} = Setups.description(key) %{ key: key, name: Map.get(metadata, "name", key), - description: Map.get(metadata, "description", ""), + description: description, outcomes: outcomes } end diff --git a/lib/statifier_examples_web/live/signup_journey_live.ex b/lib/statifier_examples_web/live/signup_journey_live.ex index f2c03e02..bddbd271 100644 --- a/lib/statifier_examples_web/live/signup_journey_live.ex +++ b/lib/statifier_examples_web/live/signup_journey_live.ex @@ -94,6 +94,16 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do therefore draws the finished line and not a bare screen title with nothing under it. + ## Which Path the page names + + The heading names the Path the page runs, and so does the notice of a + press that did not land: the signup Path is "Signup journey" and "this + signup", as it always was, and any other Path is named by its own + document, so a card-reader setup reads "Card-reader setup" and "this + card-reader setup". The branch onboarding is named as the onboarding + while one of its setups is presented; the line under the execution id + says which setup's screen is drawn. + ## A press that did not land An execution stopped somewhere else - abandoned, or finished - takes no @@ -166,6 +176,11 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do # label (the moduledoc's "What the page draws is the view's state"). @receipt_fields [{"email", "Account for"}, {"collected", "Responses collected"}] + # The heading and the notice's word for the signup Path (the moduledoc's + # "Which Path the page names"). + @signup_title "Signup journey" + @signup_noun "signup" + @impl Phoenix.LiveView def mount(_params, session, socket) do {:ok, @@ -177,7 +192,9 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do topic: nil, path: nil, opts: [], - root: nil + root: nil, + title: @signup_title, + noun: @signup_noun )} end @@ -188,6 +205,7 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do {:noreply, socket |> assign(path: params["path"], opts: opts, root: params["execution"]) + |> assign(title(params["path"], opts)) |> load(params["execution"])} :error -> @@ -265,7 +283,7 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do
-

Signup journey

+

{@title}

@@ -303,7 +321,7 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do role="status" class="alert alert-warning text-sm" > - This signup had already ended, so your last press was not recorded. + This {@noun} had already ended, so your last press was not recorded. The page shows where it stopped.

@@ -447,6 +465,35 @@ defmodule StatifierExamplesWeb.SignupJourneyLive do end end + # The heading the page draws and the word its discarded-press notice + # names the Path by (the moduledoc's "Which Path the page names"): the + # signup Path's own for no key, and otherwise the name the Path's block + # document carries, or its screens document's when the block document + # names none, the key itself last. + @spec title(String.t() | nil, keyword()) :: [title: String.t(), noun: String.t()] + defp title(nil, _opts), do: [title: @signup_title, noun: @signup_noun] + + defp title(key, opts) do + name = + document_name(Keyword.get(opts, :path)) || screens_name(Keyword.get(opts, :screens)) || + key + + {first, rest} = String.split_at(name, 1) + [title: name, noun: String.downcase(first) <> rest] + end + + @spec document_name(term()) :: String.t() | nil + defp document_name(%{metadata: %{"name" => name}}) when is_binary(name) and name != "", + do: name + + defp document_name(_document), do: nil + + @spec screens_name(term()) :: String.t() | nil + defp screens_name(%{"metadata" => %{"name" => name}}) when is_binary(name) and name != "", + do: name + + defp screens_name(_screens), do: nil + @spec journey_url(String.t() | nil, String.t()) :: String.t() defp journey_url(nil, execution_id), do: ~p"/signup-journey?#{[execution: execution_id]}" diff --git a/priv/fixtures/branch_onboarding.json b/priv/fixtures/branch_onboarding.json index 1b28f36e..e45d5d9e 100644 --- a/priv/fixtures/branch_onboarding.json +++ b/priv/fixtures/branch_onboarding.json @@ -4,13 +4,17 @@ "revision": 1, "metadata": { "name": "Branch onboarding", - "description": "A branch library joining: the branch says who it is, picks the integrations it needs, and runs the setup of each one it picked, in the order the response lists them, each as a durable child execution of its own. The pick is a multi-select whose values are the setups' document ids; a core.foreach walks the response and a core.subchart names its child from the loop's item (chart_from), so which children run is data. The branch's own steps are myapp.step blocks, so Back works between them; Back does not cross into or out of a setup.", + "description": "A branch library joining: the branch says who it is, picks the integrations it needs, and runs the setup of each one it picked, in the order the response lists them, each as a durable child execution of its own. The pick is a multi-select whose values are the setups' document ids; a core.foreach walks the response and a core.subchart names its child from the loop's item (chart_from), so which children run is data. The subchart declares every outcome a setup can finish with and writes what each setup finished with to last_setup. The branch's own steps are myapp.step blocks, so Back works between them; Back does not cross into or out of a setup.", "domain": "library" }, "datamodel": [ { "id": "responses", "description": "What the branch's own steps collected, keyed by question key: the branch's name and the integrations it picked, as the setups' document ids. Each setup's own responses stay in the setup's own execution." + }, + { + "id": "last_setup", + "description": "What the setup of the latest pass finished with, written by the subchart as each setup finishes: the setup's done data, whose outcome is its root outcome - done, or site_visit_needed when a card-reader setup's test card was not read." } ], "root": { @@ -56,7 +60,8 @@ "type_version": 1, "config": { "chart_from": "setup_document", - "outcomes": "done" + "outcomes": "done\nsite_visit_needed", + "assign_to": "last_setup" } } ] diff --git a/priv/fixtures/card_reader_setup.json b/priv/fixtures/card_reader_setup.json index eaeffea1..42880a0a 100644 --- a/priv/fixtures/card_reader_setup.json +++ b/priv/fixtures/card_reader_setup.json @@ -4,7 +4,7 @@ "revision": 1, "metadata": { "name": "Card-reader setup", - "description": "One setup of a branch library's onboarding: which card reader the branch has, where it stands, and whether it read a test card. Three myapp.step blocks over priv/fixtures/card_reader_setup_screens.json, so the person setting it up may go back to the step before. Runnable alone, and written to run as a child of the onboarding: its root finishes with the outcome done.", + "description": "One setup of a branch library's onboarding: which card reader the branch has, where it stands, and whether it read a test card. Three myapp.step blocks over priv/fixtures/card_reader_setup_screens.json, so the person setting it up may go back to the step before, under a myapp.setup root that declares one ending besides done. Runnable alone, and written to run as a child of the onboarding: it finishes with the outcome done, or with site_visit_needed when the test card was not read.", "domain": "library" }, "datamodel": [ @@ -14,9 +14,14 @@ } ], "root": { - "type": "core.sequence", + "type": "myapp.setup", "id": "blk_cr_root", "type_version": 1, + "config": { + "ending": "site_visit_needed", + "ending_on": "test_reported", + "ending_when": "_event.data.reader_test_result == 'not_read'" + }, "slots": { "body": [ { diff --git a/test/statifier_examples/charts_test.exs b/test/statifier_examples/charts_test.exs index fd744a35..c449ef86 100644 --- a/test/statifier_examples/charts_test.exs +++ b/test/statifier_examples/charts_test.exs @@ -40,6 +40,7 @@ defmodule StatifierExamples.ChartsTest do "myapp.resolve_review", "myapp.risk_rating", "myapp.screen", + "myapp.setup", "myapp.signup_step", "myapp.step", "myapp.three_ds_challenge", diff --git a/test/statifier_examples/library/onboarding_end_to_end_test.exs b/test/statifier_examples/library/onboarding_end_to_end_test.exs index f1e0ab23..78de1794 100644 --- a/test/statifier_examples/library/onboarding_end_to_end_test.exs +++ b/test/statifier_examples/library/onboarding_end_to_end_test.exs @@ -166,10 +166,10 @@ defmodule StatifierExamples.Library.OnboardingEndToEndTest do } end - # Not covered here, on purpose: the parent's routing on a child's outcome. - # Every setup declares the single outcome `done`, so there is one outcome - # to route on; a second one needs a setup that can end with it (the open - # bead se-qb73), and the routing assertion belongs with that fixture. + # Not covered here, on purpose: the parent reading a child's outcome. The + # card-reader setup can end with `site_visit_needed` as well as `done`, + # and `StatifierExamples.Library.OnboardingTest`'s "how a setup ended" + # walks both and reads what the onboarding recorded. # # Sabotage notes, one per mutation, each run and turned red on an # assertion, then reverted from a copy: diff --git a/test/statifier_examples/library/onboarding_test.exs b/test/statifier_examples/library/onboarding_test.exs index ed48a245..a5337f59 100644 --- a/test/statifier_examples/library/onboarding_test.exs +++ b/test/statifier_examples/library/onboarding_test.exs @@ -96,8 +96,9 @@ defmodule StatifierExamples.Library.OnboardingTest do test "answers each of the three setups by its document id, compiled as a child" do for key <- Setups.keys() do {:ok, document} = Setups.document(key) + {:ok, outcomes} = Setups.outcomes(key) - assert {:ok, %Compiled{interface: %{declared_outcomes: ["done"]}}} = + assert {:ok, %Compiled{interface: %{declared_outcomes: ^outcomes}}} = Onboarding.resolve_chart(document.id, %{}) end end @@ -243,6 +244,53 @@ defmodule StatifierExamples.Library.OnboardingTest do end end + describe "how a setup ended" do + # The onboarding's datamodel, read from the stored execution. + defp last_setup(execution_id) do + document = Onboarding.document() + {:ok, compiled} = Durable.compile(document, []) + {:ok, {durable, _run}} = Durable.resume(compiled, document, execution_id) + durable.machine_state.datamodel["last_setup"] + end + + defp donedata(execution_id) do + {:ok, record} = Storage.fetch_execution(store(), execution_id) + StatifierPersistence.Execution.from_record(record).donedata + end + + # Sabotage: made `myapp.setup`'s `declared_outcomes/1` answer `done` + # alone. The child finished with `done` on the not-read card and this + # went red. Restored from a copy. + # + # Sabotage: dropped the subchart's `assign_to` from + # `branch_onboarding.json`. The onboarding recorded nothing and this + # went red on `last_setup`. Restored from a copy. + test "a card-reader setup that read nothing ends with site_visit_needed, and the onboarding reads it" do + for {result, outcome} <- [{"not_read", "site_visit_needed"}, {"card_read", "done"}] do + id = start() + {:ok, _view} = pick(id, ["bdoc_card_reader_setup", "bdoc_catalogue_sync"]) + reader = first_child(id) + + walk = + List.replace_at( + @card_reader_setup, + 2, + {"test_reported", %{"reader_test_result" => result}} + ) + + presses(id, walk) + + assert donedata(reader) == %{"outcome" => outcome}, "#{result} ended otherwise" + assert last_setup(id) == %{"outcome" => outcome} + + # The onboarding moves on to the next pick either way. + {next, view} = focused_view(id) + assert next != id and next != reader + assert view.screen.key == "catalogue_source" + end + end + end + describe "a trace of another execution" do # Sabotage: took `Execution.absorb/2`'s clause for a trace of another # execution out. The child's configuration marked the onboarding diff --git a/test/statifier_examples/library/setups_test.exs b/test/statifier_examples/library/setups_test.exs index 606b692d..08c443e7 100644 --- a/test/statifier_examples/library/setups_test.exs +++ b/test/statifier_examples/library/setups_test.exs @@ -16,6 +16,8 @@ defmodule StatifierExamples.Library.SetupsTest do alias StatifierExamples.Repo alias StatifierExamples.Signup.Journey + doctest StatifierExamples.Library.Setup + # Each setup's walk, a press per step: the screen it rests on, the # outcome pressed and what the press carried. @walks %{ @@ -77,10 +79,30 @@ defmodule StatifierExamples.Library.SetupsTest do # Sabotage: made `outcomes/1` answer the interface's `references` (`[]`) # in place of its declared outcomes. This went red. Reverted from a copy. - test "each document declares the root outcome done, compiled for use as a child" do + # + # Sabotage: made `myapp.setup`'s `declared_outcomes/1` answer `done` + # alone. The card-reader setup declared one outcome and this went red. + # Restored from a copy. + test "each document declares its root outcomes, compiled for use as a child" do + assert Setups.outcomes("catalogue_sync") == {:ok, ["done"]} + assert Setups.outcomes("reading_list_import") == {:ok, ["done"]} + assert Setups.outcomes("card_reader_setup") == {:ok, ["done", "site_visit_needed"]} + end + + # Pins the sentence's register; that the page draws it is + # `StatifierExamplesWeb.SettingsLiveTest`'s. + # + # Sabotage: named the catalogue sync's file in its sentence. This went + # red on the file refute. Restored from a copy. + test "each setup has a sentence for a librarian, and no block type or file in it" do for key <- Setups.keys() do - assert Setups.outcomes(key) == {:ok, ["done"]} + assert {:ok, description} = Setups.description(key) + assert description != "" + refute description =~ "myapp." + refute description =~ ".json" end + + assert Setups.description("branch_closure") == :error end end @@ -153,6 +175,35 @@ defmodule StatifierExamples.Library.SetupsTest do end end + # The card-reader setup's ending is on its last screen, so this walks a + # test-only copy whose ending is on the first: a press that ends the + # setup leaves the steps after it unvisited. + # + # Sabotage: made the ending's handler drop its `ending_when` condition. + # The desk reader's press ended the setup too and this went red on its + # screen. Restored from a copy. + test "a press its root names as an ending ends it, with the steps after it unvisited" do + {:ok, [screens: screens, path: %Document{root: root} = path]} = + Setups.options("card_reader_setup") + + config = %{ + "ending" => "no_reader_yet", + "ending_on" => "model_chosen", + "ending_when" => "_event.data.reader_kind == 'self_checkout'" + } + + opts = [screens: screens, path: %Document{path | root: %{root | config: config}}] + + for {kind, expected} <- [{"self_checkout", nil}, {"desk_reader", "reader_place"}] do + {:ok, id} = Journey.start("setup-ending-#{System.unique_integer([:positive])}", opts) + {:ok, view} = Journey.submit(id, "model_chosen", %{"reader_kind" => kind}, opts) + + assert screen_key(view) == expected, "#{kind} did not rest on #{inspect(expected)}" + assert view.responses == %{"reader_kind" => kind} + assert view.state == "done" == is_nil(expected) + end + end + # Sabotage: misspelled `Journey.back_event/0`. The back request raised an # event no step listens for, the execution stayed on the second step and # this went red. Reverted from a copy. diff --git a/test/statifier_examples_web/live/settings_live_test.exs b/test/statifier_examples_web/live/settings_live_test.exs index 3574aa84..72d96630 100644 --- a/test/statifier_examples_web/live/settings_live_test.exs +++ b/test/statifier_examples_web/live/settings_live_test.exs @@ -69,6 +69,59 @@ defmodule StatifierExamplesWeb.SettingsLiveTest do refute html =~ "settings-error" end + # Sabotage: drew the document's own `metadata` description again. The + # librarian's sentence was not on the page and this went red. Restored + # from a copy. + test "describes each setup in the sentence for a librarian, not the document's own", %{ + conn: conn + } do + {:ok, live, _html} = live(conn, ~p"/settings") + + for key <- Setups.keys() do + {:ok, description} = Setups.description(key) + {:ok, document} = Setups.document(key) + + assert has_element?(live, "#setup-#{key} p", description) + refute render(live) =~ document.metadata["description"] + end + + assert has_element?(live, "#setup-card_reader_setup code", "site_visit_needed") + end + + # Sabotage: made the Journey page's heading and notice read the signup + # wording for every Path. The heading read "Signup journey" and this went + # red. Restored from a copy. + test "the Journey page names the setup it runs, in its heading and its ended notice", %{ + conn: conn + } do + {:ok, settings, _html} = live(conn, ~p"/settings") + + {:error, {:live_redirect, %{to: to}}} = + settings |> element("#start-card_reader_setup") |> render_click() + + %{"execution" => execution_id} = to |> URI.parse() |> Map.fetch!(:query) |> URI.decode_query() + {:ok, journey, _html} = live(conn, to) + + assert has_element?(journey, "h1", "Card-reader setup") + refute has_element?(journey, "h1", "Signup journey") + + # Stopped out of band while the page still shows the first screen. + {:ok, [screens: _screens, path: document]} = Setups.options("card_reader_setup") + {:ok, compiled} = Durable.compile(document, []) + {:ok, {durable, _run}} = Durable.resume(compiled, document, execution_id) + :ok = Durable.abandon(durable) + + journey |> form("#journey form", %{"reader_kind" => "desk_reader"}) |> render_change() + + journey + |> form("#journey form") + |> put_submitter(~s(button[data-riddler-key="reader_model_continue"])) + |> render_submit() + + assert has_element?(journey, "#press-discarded", "This card-reader setup had already ended") + refute has_element?(journey, "#press-discarded", "signup") + end + # Sabotage: made the start clause navigate to the Journey page without # `path`. The URL named no setup and this went red. Reverted from a copy. #