From 6fac7eac10a68e73731228e86b05bf3963655bf4 Mon Sep 17 00:00:00 2001 From: JohnnyT Date: Fri, 27 Nov 2026 15:21:49 -0700 Subject: [PATCH 1/2] Adds the newer screen types to the palette The screen editor's palette gains an image, a divider and a link; a text question gains the date, time and datetime formats, a style hint (a slider among them) and a step; a choice question gains option descriptions; and every question gains checks. The walk turns each into riddler's shape, and the patron registration fixture uses one of each and still starts under Try. The Try renderer draws the image, divider and link, and the Journey's check has a reader-facing message for the date and time formats. Refs: se-gb7u --- lib/statifier_examples/screen_editor.ex | 50 ++++++- .../screen_editor/boolean_question.ex | 8 +- .../screen_editor/choice_question.ex | 80 ++++++++++- .../screen_editor/divider.ex | 33 +++++ .../screen_editor/element.ex | 86 ++++++++++++ lib/statifier_examples/screen_editor/image.ex | 59 ++++++++ lib/statifier_examples/screen_editor/link.ex | 52 +++++++ .../screen_editor/secret_question.ex | 10 +- .../screen_editor/text_question.ex | 83 +++++++++--- lib/statifier_examples/screen_editor/walk.ex | 66 +++++++-- lib/statifier_examples/signup/validation.ex | 10 +- .../components/signup_elements.ex | 39 ++++++ .../fixtures/patron_registration_screens.json | 61 +++++++++ .../screen_editor/try_test.exs | 46 +++++++ .../screen_editor/walk_test.exs | 127 ++++++++++++++++++ .../statifier_examples/screen_editor_test.exs | 106 ++++++++++++++- .../components/signup_elements_test.exs | 34 +++++ .../live/registration_journey_live_test.exs | 18 +++ 18 files changed, 919 insertions(+), 49 deletions(-) create mode 100644 lib/statifier_examples/screen_editor/divider.ex create mode 100644 lib/statifier_examples/screen_editor/image.ex create mode 100644 lib/statifier_examples/screen_editor/link.ex create mode 100644 test/statifier_examples_web/components/signup_elements_test.exs diff --git a/lib/statifier_examples/screen_editor.ex b/lib/statifier_examples/screen_editor.ex index bc2a656e..0141ca46 100644 --- a/lib/statifier_examples/screen_editor.ex +++ b/lib/statifier_examples/screen_editor.ex @@ -15,9 +15,18 @@ defmodule StatifierExamples.ScreenEditor do group and each named for the document's own type under `myapp.screens.`: the screen itself (`key`, `title`, and an `elements` slot), and the element types `heading`, `text`, `text_question`, `choice_question`, - `boolean_question`, `secret_question`, `button` and `variant`. Their - fields are spelled as the document spells them. `registrations/0` lists - them. + `boolean_question`, `secret_question`, `button`, `variant`, `image`, + `divider` and `link`. Their fields are spelled as the document spells + them, but for the few the walk turns back into the document's shape + (`StatifierExamples.ScreenEditor.Walk`). `registrations/0` lists them. + + A text question's date and time formats are choices of its `format` + field, and a slider is its `style` hint with a numeric format, `min`, + `max` and `step`, because the document spells both on a text question + rather than as types of their own. An option's description and a + question's checks are rows on the question + (`StatifierExamples.ScreenEditor.ChoiceQuestion`, + `StatifierExamples.ScreenEditor.Element.checks_field/0`). The `core.*` vocabulary is registered underneath, because a document's root is a block and `core.sequence` is the plain ordered one: its `body` @@ -51,7 +60,10 @@ defmodule StatifierExamples.ScreenEditor do BooleanQuestion, Button, ChoiceQuestion, + Divider, Heading, + Image, + Link, Screen, SecretQuestion, Text, @@ -70,7 +82,10 @@ defmodule StatifierExamples.ScreenEditor do {"myapp.screens.boolean_question", BooleanQuestion}, {"myapp.screens.secret_question", SecretQuestion}, {"myapp.screens.button", Button}, - {"myapp.screens.variant", Variant} + {"myapp.screens.variant", Variant}, + {"myapp.screens.image", Image}, + {"myapp.screens.divider", Divider}, + {"myapp.screens.link", Link} ] @doc """ @@ -103,8 +118,10 @@ defmodule StatifierExamples.ScreenEditor do end @doc """ - The values the screen editor offers for a choice question's `style`: an - open list, so a renderer's own style name can still be typed. + The values the screen editor offers for the hints a renderer reads - a + choice question's and a text question's `style`, an image's `size` and + `fit`, and a link's `style` - each as an open list, so a renderer's own + name can still be typed. """ @spec field_candidates() :: %{optional({String.t(), String.t()}) => term()} def field_candidates do @@ -116,7 +133,26 @@ defmodule StatifierExamples.ScreenEditor do {"dropdown", "Dropdown"}, {"checkboxes", "Checkboxes"}, {"list", "List"} - ]} + ]}, + {"myapp.screens.text_question", "style"} => + {:open, + [ + {"multiline", "More than one line"}, + {"slider", "Slider, for a number"}, + {"stepper", "Stepper, for a number"} + ]}, + {"myapp.screens.image", "size"} => + {:open, + [ + {"icon", "Icon"}, + {"avatar", "Avatar"}, + {"small", "Small"}, + {"medium", "Medium"}, + {"large", "Large"}, + {"banner", "Banner"} + ]}, + {"myapp.screens.image", "fit"} => {:open, [{"contain", "Contain"}, {"cover", "Cover"}]}, + {"myapp.screens.link", "style"} => {:open, [{"inline", "Inline"}, {"button", "Button"}]} } end end diff --git a/lib/statifier_examples/screen_editor/boolean_question.ex b/lib/statifier_examples/screen_editor/boolean_question.ex index cf56bc56..c2b42471 100644 --- a/lib/statifier_examples/screen_editor/boolean_question.ex +++ b/lib/statifier_examples/screen_editor/boolean_question.ex @@ -3,6 +3,8 @@ defmodule StatifierExamples.ScreenEditor.BooleanQuestion do `myapp.screens.boolean_question`: a lone checkbox or toggle submitting true or false - "I agree to the library's lending rules". `required` asks for true, and `help` is a line of guidance shown beside it. + `checks` are the question's own conditions, one row each + (`StatifierExamples.ScreenEditor.Element.checks_field/0`). """ use StatifierBlocks.BlockType @@ -15,7 +17,8 @@ defmodule StatifierExamples.ScreenEditor.BooleanQuestion do [ Element.field("label", :string, "Label", true, ""), Element.field("help", :string, "Help", false, ""), - Element.field("required", :boolean, "Required", false, false) + Element.field("required", :boolean, "Required", false, false), + Element.checks_field() ] end @@ -23,7 +26,8 @@ defmodule StatifierExamples.ScreenEditor.BooleanQuestion do def validate_config(config) do Element.validate( config, - Element.present(config, "label", "a question needs the label a patron is asked") + Element.present(config, "label", "a question needs the label a patron is asked") ++ + Element.check_findings(config) ) end diff --git a/lib/statifier_examples/screen_editor/choice_question.ex b/lib/statifier_examples/screen_editor/choice_question.ex index 3266ecdd..485177cf 100644 --- a/lib/statifier_examples/screen_editor/choice_question.ex +++ b/lib/statifier_examples/screen_editor/choice_question.ex @@ -23,6 +23,19 @@ defmodule StatifierExamples.ScreenEditor.ChoiceQuestion do key-shaped and so never holds an `=`, while a label may hold anything. Each value is checked here as the document will check it: key-shaped, and unique within the question. + + ## `option_descriptions`, one row per described option + + An option may carry a `description`, a line a renderer shows under its + label - what a family card covers, say. A row of `answer_options` already + splits at its first `=` and its label may hold anything, so a description + has rows of its own, each written `value=Description` - `family=For two + adults and their children` - naming an option of the question once. The + walk puts each description on its option and leaves the field itself out + of the document, which has no such field. + + `checks` are the question's own conditions, one row each + (`StatifierExamples.ScreenEditor.Element.checks_field/0`). """ use StatifierBlocks.BlockType @@ -38,12 +51,20 @@ defmodule StatifierExamples.ScreenEditor.ChoiceQuestion do Element.field("label", :string, "Label", true, ""), Element.field("select", {:select, @selects}, "Select", true, "one"), Element.field("answer_options", {:list, :string}, "Answer options", true, []), + Element.field( + "option_descriptions", + {:list, :string}, + "Option descriptions", + false, + [] + ), Element.field("style", :string, "Style", false, ""), Element.field("help", :string, "Help", false, ""), Element.field("default", :string, "Default", false, ""), Element.field("required", :boolean, "Required", false, false), Element.field("min_selections", :integer, "Min selections", false, nil), - Element.field("max_selections", :integer, "Max selections", false, nil) + Element.field("max_selections", :integer, "Max selections", false, nil), + Element.checks_field() ] end @@ -54,7 +75,9 @@ defmodule StatifierExamples.ScreenEditor.ChoiceQuestion do Element.validate( config, Element.present(config, "label", "a question needs the label a patron is asked") ++ - option_findings(options) ++ selection_findings(config, options) + option_findings(options) ++ + description_findings(Map.get(config, "option_descriptions"), options) ++ + selection_findings(config, options) ++ Element.check_findings(config) ) end @@ -97,6 +120,37 @@ defmodule StatifierExamples.ScreenEditor.ChoiceQuestion do end end + @doc """ + The descriptions `rows` give the options valued `values`, by value, or + `:error` when a row is not `value=Description`, names no option of + `values`, or names one a second time. + + iex> StatifierExamples.ScreenEditor.ChoiceQuestion.option_descriptions( + ...> ["family=For two adults"], + ...> ["standard", "family"] + ...> ) + {:ok, %{"family" => "For two adults"}} + + iex> StatifierExamples.ScreenEditor.ChoiceQuestion.option_descriptions(["student=Ages 12 to 18"], ["standard"]) + :error + """ + @spec option_descriptions([String.t()], [String.t()]) :: + {:ok, %{String.t() => String.t()}} | :error + def option_descriptions(rows, values) when is_list(rows) and is_list(values) do + Enum.reduce_while(rows, {:ok, %{}}, fn row, {:ok, acc} -> + case row_pair(row) do + {:ok, pair} -> describe(pair, values, acc) + :error -> {:halt, :error} + end + end) + end + + defp describe({value, description}, values, acc) do + if value in values and not is_map_key(acc, value), + do: {:cont, {:ok, Map.put(acc, value, description)}}, + else: {:halt, :error} + end + defp row_pair(row) when is_binary(row) do case String.split(row, "=", parts: 2) do [value, label] when label != "" -> {:ok, {value, label}} @@ -116,6 +170,28 @@ defmodule StatifierExamples.ScreenEditor.ChoiceQuestion do defp option_findings(_none), do: [{"answer_options", "a choice question needs at least one option"}] + defp description_findings(blank, _options) when blank in [nil, []], do: [] + + defp description_findings(rows, options) when is_list(rows) do + values = + case answer_options(List.wrap(options)) do + {:ok, pairs} -> Enum.map(pairs, &elem(&1, 0)) + :error -> [] + end + + case option_descriptions(rows, values) do + {:ok, _descriptions} -> [] + :error -> [description_finding()] + end + end + + defp description_findings(_other, _options), do: [description_finding()] + + defp description_finding do + {"option_descriptions", + "each description is written value=Description, naming an option of the question once"} + end + defp value_findings(values) do cond do not Enum.all?(values, &Element.key?/1) -> diff --git a/lib/statifier_examples/screen_editor/divider.ex b/lib/statifier_examples/screen_editor/divider.ex new file mode 100644 index 00000000..b244f0de --- /dev/null +++ b/lib/statifier_examples/screen_editor/divider.ex @@ -0,0 +1,33 @@ +defmodule StatifierExamples.ScreenEditor.Divider do + @moduledoc """ + `myapp.screens.divider`: a rule between the elements before it and the + elements after it - between a patron's contact details and the PIN they + choose, say. It carries no field of its own, only the two every element + carries (`StatifierExamples.ScreenEditor.Element`). + """ + + use StatifierBlocks.BlockType + + alias StatifierExamples.ScreenEditor.Element + + @impl StatifierBlocks.BlockType + def config_schema(_config), do: Element.common_fields() + + @impl StatifierBlocks.BlockType + def validate_config(config), do: Element.validate(config, []) + + @impl StatifierBlocks.BlockType + def palette_entry do + %{ + label: "Divider", + group: "Screens", + description: "A rule between one part of a screen and the next.", + icon: "minus", + keywords: ["divider", "rule", "separator", "section"], + order: 11 + } + end + + @impl StatifierBlocks.BlockType + def emit(_block, context), do: Element.emit_leaf(context) +end diff --git a/lib/statifier_examples/screen_editor/element.ex b/lib/statifier_examples/screen_editor/element.ex index a13179fb..1fd166e6 100644 --- a/lib/statifier_examples/screen_editor/element.ex +++ b/lib/statifier_examples/screen_editor/element.ex @@ -35,6 +35,7 @@ defmodule StatifierExamples.ScreenEditor.Element do alias StatifierBlocks.Core.Emit @key_shape ~r/\A[a-z][a-z0-9_]*\z/ + @check_code ~r/\Acheck(\.[a-z][a-z0-9_]*)+\z/ @doc """ The `key` and `condition` field declarations, in that order, ahead of a @@ -96,6 +97,91 @@ defmodule StatifierExamples.ScreenEditor.Element do end end + @doc """ + The `checks` field every question type declares after its own fields: + conditions a patron's response has to satisfy, one row per check. + + The document holds `checks` as a list of objects - `condition`, `code` + and `message` - and the editor's field types have no list of objects. So + each row is written `code: condition => message`, split at its first `:` + and then at the first ` => ` after it: `check.birth_date_in_future: + trim(responses.birth_date)::date <= context.today::date => A date of + birth cannot be in the future.` That is unambiguous for the code, which + is `check.` and key-shaped segments and so never holds a `:`, and for a + condition outside its string literals, where ` => ` is no operator; a + condition whose string literal holds ` => ` cannot be written this way. + """ + @spec checks_field() :: BlockType.field_decl() + def checks_field, do: field("checks", {:list, :string}, "Checks", false, []) + + @doc """ + The check objects `rows` hold, in order, or `:error` when a row is not + `code: condition => message` with a code in the `check.` shape. + + iex> StatifierExamples.ScreenEditor.Element.checks(["check.adult: responses.age >= 18 => You must be 18."]) + {:ok, [%{"code" => "check.adult", "condition" => "responses.age >= 18", "message" => "You must be 18."}]} + + iex> StatifierExamples.ScreenEditor.Element.checks(["adult: responses.age >= 18 => You must be 18."]) + :error + """ + @spec checks([String.t()]) :: {:ok, [%{String.t() => String.t()}]} | :error + def checks(rows) when is_list(rows) do + rows + |> Enum.reduce_while({:ok, []}, fn row, {:ok, acc} -> + case check_row(row) do + {:ok, check} -> {:cont, {:ok, [check | acc]}} + :error -> {:halt, :error} + end + end) + |> case do + {:ok, checks} -> {:ok, Enum.reverse(checks)} + :error -> :error + end + end + + @doc """ + The findings on a question's `checks` rows: each row readable, and each + code used once on the question, as the document will hold them. + """ + @spec check_findings(Block.config()) :: [BlockType.finding()] + def check_findings(config) do + case Map.get(config, "checks") do + blank when blank in [nil, []] -> + [] + + rows when is_list(rows) -> + case checks(rows) do + {:ok, checks} -> unique_code_findings(Enum.map(checks, & &1["code"])) + :error -> [{"checks", "each check is written check.: condition => message"}] + end + + _other -> + [{"checks", "each check is written check.: condition => message"}] + end + end + + defp unique_code_findings(codes) do + if Enum.uniq(codes) == codes, + do: [], + else: [{"checks", "each check's code is used once on the question"}] + end + + defp check_row(row) when is_binary(row) do + with [code, rest] <- String.split(row, ":", parts: 2), + code = String.trim(code), + true <- Regex.match?(@check_code, code), + [condition, message] <- String.split(rest, " => ", parts: 2), + condition = String.trim(condition), + message = String.trim(message), + true <- condition != "" and message != "" do + {:ok, %{"condition" => condition, "code" => code, "message" => message}} + else + _not_a_check -> :error + end + end + + defp check_row(_row), do: :error + @doc "The inert state a leaf element compiles to: entered, and done at once." @spec emit_leaf(Context.t()) :: {:ok, StatifierBlocks.Emission.t()} def emit_leaf(context) do diff --git a/lib/statifier_examples/screen_editor/image.ex b/lib/statifier_examples/screen_editor/image.ex new file mode 100644 index 00000000..2d7a64ee --- /dev/null +++ b/lib/statifier_examples/screen_editor/image.ex @@ -0,0 +1,59 @@ +defmodule StatifierExamples.ScreenEditor.Image do + @moduledoc """ + `myapp.screens.image`: a picture on a screen - the branch library's logo + at the top of a registration form, say - and the `alt` text a patron who + cannot see it is told. + + `src` and `alt` are required. `src` is the picture's URL: an `https:` + URL, a host-relative path such as `/images/logo.png`, or a `data:` image, + and which of those riddler admits is riddler's to say when the walked + document is admitted, so the editor holds `src` to being there and leaves + the URL rule to riddler. `alt` is held to being there and not blank: an + image the editor offers always says what it shows. `size` and `fit` are + hints a renderer reads, offered as open lists + (`StatifierExamples.ScreenEditor.field_candidates/0`). + """ + + use StatifierBlocks.BlockType + + alias StatifierExamples.ScreenEditor.Element + + @impl StatifierBlocks.BlockType + def config_schema(_config) do + Element.common_fields() ++ + [ + Element.field("src", :string, "Source URL", true, ""), + Element.field("alt", :string, "Alt text", true, ""), + Element.field("size", :string, "Size", false, ""), + Element.field("fit", :string, "Fit", false, "") + ] + end + + @impl StatifierBlocks.BlockType + def validate_config(config) do + Element.validate( + config, + Element.present(config, "src", "an image needs the URL of its picture") ++ + Element.present( + config, + "alt", + "an image needs the alt text a patron who cannot see it is told" + ) + ) + end + + @impl StatifierBlocks.BlockType + def palette_entry do + %{ + label: "Image", + group: "Screens", + description: "A picture, and the alt text read in its place.", + icon: "photo", + keywords: ["image", "picture", "logo", "photo"], + order: 10 + } + end + + @impl StatifierBlocks.BlockType + def emit(_block, context), do: Element.emit_leaf(context) +end diff --git a/lib/statifier_examples/screen_editor/link.ex b/lib/statifier_examples/screen_editor/link.ex new file mode 100644 index 00000000..be2374d3 --- /dev/null +++ b/lib/statifier_examples/screen_editor/link.ex @@ -0,0 +1,52 @@ +defmodule StatifierExamples.ScreenEditor.Link do + @moduledoc """ + `myapp.screens.link`: a place a patron may go from the screen - the + library's lending rules, say - and the `label` that names it. + + `label` and `href` are required. `href` is an `https:` URL or a + host-relative path such as `/lending-rules`, and, as with an image's + `src`, which of those riddler admits is riddler's to say when the walked + document is admitted. `style` is a hint a renderer reads, offered as an + open list (`StatifierExamples.ScreenEditor.field_candidates/0`). A link + raises no outcome and writes nothing: following one is the browser's + navigation, not a press the Journey hears. + """ + + use StatifierBlocks.BlockType + + alias StatifierExamples.ScreenEditor.Element + + @impl StatifierBlocks.BlockType + def config_schema(_config) do + Element.common_fields() ++ + [ + Element.field("label", :string, "Label", true, ""), + Element.field("href", :string, "URL", true, ""), + Element.field("style", :string, "Style", false, "") + ] + end + + @impl StatifierBlocks.BlockType + def validate_config(config) do + Element.validate( + config, + Element.present(config, "label", "a link needs the label a patron reads") ++ + Element.present(config, "href", "a link needs the URL it goes to") + ) + end + + @impl StatifierBlocks.BlockType + def palette_entry do + %{ + label: "Link", + group: "Screens", + description: "A place a patron may go from the screen.", + icon: "link", + keywords: ["link", "url", "href", "rules"], + order: 12 + } + end + + @impl StatifierBlocks.BlockType + def emit(_block, context), do: Element.emit_leaf(context) +end diff --git a/lib/statifier_examples/screen_editor/secret_question.ex b/lib/statifier_examples/screen_editor/secret_question.ex index fcfb4919..d1c5467d 100644 --- a/lib/statifier_examples/screen_editor/secret_question.ex +++ b/lib/statifier_examples/screen_editor/secret_question.ex @@ -8,7 +8,9 @@ defmodule StatifierExamples.ScreenEditor.SecretQuestion do the whole of it: a secret question carries no `placeholder`, no `format`, and neither `default` nor `prefill`, because riddler's `secret_question` names none of them and refuses the last two - a secret - is a value only the patron supplies. + is a value only the patron supplies. It may carry `checks` + (`StatifierExamples.ScreenEditor.Element.checks_field/0`), and riddler + refuses one whose condition reads a secret question's response. What a host does with the response is `StatifierExamples.Signup.Journey.submit/4`'s: it is validated as the @@ -26,7 +28,8 @@ defmodule StatifierExamples.ScreenEditor.SecretQuestion do [ Element.field("label", :string, "Label", true, ""), Element.field("help", :string, "Help", false, ""), - Element.field("required", :boolean, "Required", false, false) + Element.field("required", :boolean, "Required", false, false), + Element.checks_field() ] end @@ -34,7 +37,8 @@ defmodule StatifierExamples.ScreenEditor.SecretQuestion do def validate_config(config) do Element.validate( config, - Element.present(config, "label", "a question needs the label a patron is asked") + Element.present(config, "label", "a question needs the label a patron is asked") ++ + Element.check_findings(config) ) end diff --git a/lib/statifier_examples/screen_editor/text_question.ex b/lib/statifier_examples/screen_editor/text_question.ex index 9864ae32..62839ef3 100644 --- a/lib/statifier_examples/screen_editor/text_question.ex +++ b/lib/statifier_examples/screen_editor/text_question.ex @@ -1,15 +1,32 @@ defmodule StatifierExamples.ScreenEditor.TextQuestion do @moduledoc """ `myapp.screens.text_question`: a question a patron answers with a line of - text - their first name, or the email the library writes to. + text - their first name, the email the library writes to, or their date + of birth. `label` is what the patron is asked, `placeholder` the hint in the empty field and `help` a line of guidance shown beside it. `required` says an empty response will not do. `format` is one of the validation - formats the screen document names, or blank for none; `pattern` is the - expression the `pattern` format holds a response to, and `min` and - `max` the bounds the `integer` and `number` formats hold one between. The bounds are written as text, a number or blank, - because the editor's field types have no number that admits a fraction. + formats the screen document names, or blank for none: `email`, `phone`, + `pattern`, the numeric `integer` and `number`, and the date and time + formats `date`, `time` and `datetime`. The date and time formats are + choices of this one field and not block types of their own, because the + document spells them as a text question's `format`. + + `pattern` is the expression the `pattern` format holds a response to. + `min` and `max` are the bounds a format reads: a number for `integer` + and `number`, and a value in the format's own shape for `date`, `time` + and `datetime` (`1900-01-01` for a date). They are written as text, + because the editor's field types have no number that admits a fraction + and no date; the walk turns a numeric bound back into a number and + leaves a date or time bound as the text it is. + + `style` is a hint a renderer reads, offered as an open list + (`StatifierExamples.ScreenEditor.field_candidates/0`): `multiline`, and + `slider` and `stepper` for a numeric format. `step` is the increment a + slider or a stepper moves by, written as text and walked to a number. + `checks` are the question's own conditions, one row each + (`StatifierExamples.ScreenEditor.Element.checks_field/0`). """ use StatifierBlocks.BlockType @@ -22,9 +39,24 @@ defmodule StatifierExamples.ScreenEditor.TextQuestion do {"phone", "Phone"}, {"pattern", "Pattern"}, {"integer", "Integer"}, - {"number", "Number"} + {"number", "Number"}, + {"date", "Date"}, + {"time", "Time"}, + {"datetime", "Date and time"} ] + @dated ["date", "time", "datetime"] + + @doc """ + The formats whose bounds are written in the format's own shape rather + than as numbers. + + iex> StatifierExamples.ScreenEditor.TextQuestion.dated_formats() + ["date", "time", "datetime"] + """ + @spec dated_formats() :: [String.t()] + def dated_formats, do: @dated + @impl StatifierBlocks.BlockType def config_schema(_config) do Element.common_fields() ++ @@ -36,7 +68,10 @@ defmodule StatifierExamples.ScreenEditor.TextQuestion do Element.field("format", {:select, @formats}, "Format", false, ""), Element.field("pattern", :string, "Pattern", false, ""), Element.field("min", :string, "Min", false, ""), - Element.field("max", :string, "Max", false, "") + Element.field("max", :string, "Max", false, ""), + Element.field("style", :string, "Style", false, ""), + Element.field("step", :string, "Step", false, ""), + Element.checks_field() ] end @@ -45,7 +80,10 @@ defmodule StatifierExamples.ScreenEditor.TextQuestion do Element.validate( config, Element.present(config, "label", "a question needs the label a patron is asked") ++ - bound_findings(config, "min") ++ bound_findings(config, "max") + bound_findings(config, "min") ++ + bound_findings(config, "max") ++ + step_findings(Map.get(config, "step")) ++ + Element.check_findings(config) ) end @@ -54,9 +92,9 @@ defmodule StatifierExamples.ScreenEditor.TextQuestion do %{ label: "Text question", group: "Screens", - description: "A question answered with a line of text.", + description: "A question answered with a line of text, a number or a date.", icon: "chat-bubble-left-ellipsis", - keywords: ["question", "input", "field", "email"], + keywords: ["question", "input", "field", "email", "date", "slider"], order: 4 } end @@ -65,14 +103,25 @@ defmodule StatifierExamples.ScreenEditor.TextQuestion do def emit(_block, context), do: Element.emit_leaf(context) defp bound_findings(config, field) do - case Map.get(config, field) do - blank when blank in [nil, ""] -> [] - value when is_binary(value) -> if number?(value), do: [], else: [not_a_number(field)] - _other -> [not_a_number(field)] - end + if Map.get(config, "format") in @dated, + do: [], + else: + number_findings( + Map.get(config, field), + field, + "#{field} is a number, or blank for no bound" + ) end - defp number?(value), do: match?({_number, ""}, Float.parse(value)) + defp step_findings(step), + do: number_findings(step, "step", "step is a number, or blank for no step") + + defp number_findings(blank, _field, _message) when blank in [nil, ""], do: [] + + defp number_findings(value, field, message) when is_binary(value), + do: if(number?(value), do: [], else: [{field, message}]) - defp not_a_number(field), do: {field, "#{field} is a number, or blank for no bound"} + defp number_findings(_other, field, message), do: [{field, message}] + + defp number?(value), do: match?({_number, ""}, Float.parse(value)) end diff --git a/lib/statifier_examples/screen_editor/walk.ex b/lib/statifier_examples/screen_editor/walk.ex index 23f5be42..d1bf4f75 100644 --- a/lib/statifier_examples/screen_editor/walk.ex +++ b/lib/statifier_examples/screen_editor/walk.ex @@ -28,12 +28,21 @@ defmodule StatifierExamples.ScreenEditor.Walk do of writes) is left out, as a document that never wrote it would be, while `false` is a value and is kept. - Three fields are written differently in the editor, and the walk turns + Some fields are written differently in the editor, and the walk turns each back into the document's shape: * a choice question's `answer_options` rows, `value=Label`, become - `[%{"value" => value, "label" => label}]` in order; - * a text question's `min` and `max`, written as text, become numbers; + `[%{"value" => value, "label" => label}]` in order, and each of its + `option_descriptions` rows, `value=Description`, becomes the + `"description"` of the option it names; `option_descriptions` itself + is left out, since the document has no such field; + * a text question's `min` and `max`, written as text, become numbers, + except under the `date`, `time` and `datetime` formats, where a + bound is a value in the format's own shape and stays the text it is; + its `step`, written as text, becomes a number; + * a question's `checks` rows, `code: condition => message`, become + `[%{"condition" => condition, "code" => code, "message" => message}]` + in order (`StatifierExamples.ScreenEditor.Element.checks/1`); * a button's `writes` rows, `responses.=value`, become the map `%{"responses." => ["const", value]}`. The value is read as JSON when it is JSON - `3`, `true`, `["a", "b"]`, `"12"` - and as @@ -59,6 +68,7 @@ defmodule StatifierExamples.ScreenEditor.Walk do @prefix "myapp.screens." @screen @prefix <> "screen" @variant @prefix <> "variant" + @dated ScreenEditor.TextQuestion.dated_formats() @typedoc "One block the walk cannot turn into the document, and why." @type refusal :: {Block.id(), String.t()} @@ -136,7 +146,8 @@ defmodule StatifierExamples.ScreenEditor.Walk do # The fields the editor writes in a shape of its own, turned back into # the document's, one at a time; the first that cannot be is the refusal. defp convert(element, id) do - Enum.reduce_while([&options/1, &bounds/1, &writes/1], {:ok, element}, fn step, {:ok, acc} -> + [&options/1, &bounds/1, &step/1, &checks/1, &writes/1] + |> Enum.reduce_while({:ok, element}, fn step, {:ok, acc} -> case step.(acc) do {:ok, acc} -> {:cont, {:ok, acc}} {:error, message} -> {:halt, {:error, [{id, message}]}} @@ -146,16 +157,35 @@ defmodule StatifierExamples.ScreenEditor.Walk do defp options(%{"answer_options" => rows} = element) do case ScreenEditor.ChoiceQuestion.answer_options(List.wrap(rows)) do - {:ok, pairs} -> - options = Enum.map(pairs, fn {value, label} -> %{"value" => value, "label" => label} end) - {:ok, %{element | "answer_options" => options}} + {:ok, pairs} -> described(element, pairs) + :error -> {:error, "each option is written value=Label"} + end + end + + defp options(%{"option_descriptions" => _rows} = element), do: described(element, []) + defp options(element), do: {:ok, element} + + defp described(element, pairs) do + {rows, element} = Map.pop(element, "option_descriptions", []) + values = Enum.map(pairs, &elem(&1, 0)) + + case ScreenEditor.ChoiceQuestion.option_descriptions(List.wrap(rows), values) do + {:ok, descriptions} -> + options = + Enum.map(pairs, fn {value, label} -> + put_present(%{"value" => value, "label" => label}, "description", descriptions[value]) + end) + + {:ok, put_present(element, "answer_options", options)} :error -> - {:error, "each option is written value=Label"} + {:error, "each option description is written value=Description, naming an option once"} end end - defp options(element), do: {:ok, element} + defp bounds(%{"type" => "text_question", "format" => format} = element) + when format in @dated, + do: {:ok, element} defp bounds(%{"type" => "text_question"} = element) do Enum.reduce_while(["min", "max"], {:ok, element}, fn field, {:ok, acc} -> @@ -168,6 +198,24 @@ defmodule StatifierExamples.ScreenEditor.Walk do defp bounds(element), do: {:ok, element} + defp step(%{"type" => "text_question", "step" => text} = element) when is_binary(text) do + case number(element, "step", text) do + {:cont, answer} -> answer + {:halt, {:error, _bound}} -> {:error, "step is a number, or blank for no step"} + end + end + + defp step(element), do: {:ok, element} + + defp checks(%{"checks" => rows} = element) do + case ScreenEditor.Element.checks(List.wrap(rows)) do + {:ok, checks} -> {:ok, %{element | "checks" => checks}} + :error -> {:error, "each check is written check.: condition => message"} + end + end + + defp checks(element), do: {:ok, element} + defp number(element, field, text) do case Integer.parse(text) do {integer, ""} -> diff --git a/lib/statifier_examples/signup/validation.ex b/lib/statifier_examples/signup/validation.ex index ae8067b6..6716dd65 100644 --- a/lib/statifier_examples/signup/validation.ex +++ b/lib/statifier_examples/signup/validation.ex @@ -31,7 +31,8 @@ defmodule StatifierExamples.Signup.Validation do The one rule this module keeps is which formats a document may ask for. Riddler checks more than one, and this app has a reader-facing message - for `email`, `integer` and `number` alone, so a `text_question` naming + for `email`, `integer` and `number`, and for the date and time formats + `date`, `time` and `datetime`, alone, so a `text_question` naming any other format **raises**, for `StatifierExamplesWeb.SignupElements`' reason about an unknown element type: a check this app cannot explain to a reader is a screen that cannot be finished, which is worse than a page that will not @@ -63,7 +64,10 @@ defmodule StatifierExamples.Signup.Validation do @format_messages %{ "email" => "must look like an email address", "integer" => "must be a whole number", - "number" => "must be a number" + "number" => "must be a number", + "date" => "must be a date, written YYYY-MM-DD", + "time" => "must be a time, written HH:MM", + "datetime" => "must be a date and time, written YYYY-MM-DDTHH:MM:SS with Z or an offset" } @formats Map.keys(@format_messages) |> Enum.sort() @@ -135,7 +139,7 @@ defmodule StatifierExamples.Signup.Validation do moduledoc). iex> StatifierExamples.Signup.Validation.formats() - ["email", "integer", "number"] + ["date", "datetime", "email", "integer", "number", "time"] """ @spec formats() :: [String.t()] def formats, do: @formats diff --git a/lib/statifier_examples_web/components/signup_elements.ex b/lib/statifier_examples_web/components/signup_elements.ex index c3565446..9a42c0a7 100644 --- a/lib/statifier_examples_web/components/signup_elements.ex +++ b/lib/statifier_examples_web/components/signup_elements.ex @@ -58,6 +58,9 @@ defmodule StatifierExamplesWeb.SignupElements do def element(%{node: %{"type" => "boolean_question"}} = assigns), do: boolean_question(assigns) def element(%{node: %{"type" => "secret_question"}} = assigns), do: secret_question(assigns) def element(%{node: %{"type" => "button"}} = assigns), do: button(assigns) + def element(%{node: %{"type" => "image"}} = assigns), do: image(assigns) + def element(%{node: %{"type" => "divider"}} = assigns), do: divider(assigns) + def element(%{node: %{"type" => "link"}} = assigns), do: link_element(assigns) def element(%{node: node}) do raise ArgumentError, @@ -240,6 +243,42 @@ defmodule StatifierExamplesWeb.SignupElements do """ end + @doc """ + A picture and its alt text. The `size` and `fit` hints are not read. + """ + attr :node, :map, required: true + attr :responses, :map, default: %{} + + def image(assigns) do + ~H""" + {@node["alt"]} + """ + end + + @doc "A rule between the elements before it and the elements after it." + attr :node, :map, required: true + attr :responses, :map, default: %{} + + def divider(assigns) do + ~H""" +
+ """ + end + + @doc """ + A place the reader may go from the screen. The `style` hint is not read. + """ + attr :node, :map, required: true + attr :responses, :map, default: %{} + + def link_element(assigns) do + ~H""" + + {@node["label"]} + + """ + end + # What a question shows as answered: the reader's response, or else the # `value` riddler presents it with - a response the chart holds, or the # question's default. diff --git a/priv/fixtures/patron_registration_screens.json b/priv/fixtures/patron_registration_screens.json index 277be27b..4eb298e7 100644 --- a/priv/fixtures/patron_registration_screens.json +++ b/priv/fixtures/patron_registration_screens.json @@ -23,6 +23,17 @@ }, "slots": { "elements": [ + { + "type": "myapp.screens.image", + "id": "blk_prs_logo", + "type_version": 1, + "config": { + "key": "library_logo", + "src": "/images/logo.svg", + "alt": "Riverbend Public Library", + "size": "small" + } + }, { "type": "myapp.screens.heading", "id": "blk_prs_details_heading", @@ -79,6 +90,29 @@ "max": "120" } }, + { + "type": "myapp.screens.text_question", + "id": "blk_prs_birth_date", + "type_version": 1, + "config": { + "key": "birth_date", + "label": "Date of birth", + "help": "Patrons under 13 borrow on a family card.", + "format": "date", + "min": "1900-01-01", + "checks": [ + "check.birth_date_in_future: trim(responses.birth_date)::date <= context.today::date => A date of birth cannot be in the future." + ] + } + }, + { + "type": "myapp.screens.divider", + "id": "blk_prs_details_divider", + "type_version": 1, + "config": { + "key": "details_divider" + } + }, { "type": "myapp.screens.secret_question", "id": "blk_prs_library_pin", @@ -140,6 +174,9 @@ "standard=Standard card", "family=Family card, for two adults and their children", "student=Student card" + ], + "option_descriptions": [ + "student=For patrons in full-time education, with a longer loan period" ] } }, @@ -163,6 +200,20 @@ ] } }, + { + "type": "myapp.screens.text_question", + "id": "blk_prs_items_at_once", + "type_version": 1, + "config": { + "key": "items_at_once", + "label": "How many items would you like to borrow at once?", + "format": "integer", + "min": "1", + "max": "20", + "style": "slider", + "step": "1" + } + }, { "type": "myapp.screens.variant", "id": "blk_prs_card_notice", @@ -194,6 +245,16 @@ ] } }, + { + "type": "myapp.screens.link", + "id": "blk_prs_rules_link", + "type_version": 1, + "config": { + "key": "rules_link", + "label": "Read the lending rules", + "href": "https://library.example.com/lending-rules" + } + }, { "type": "myapp.screens.boolean_question", "id": "blk_prs_lending_rules", diff --git a/test/statifier_examples/screen_editor/try_test.exs b/test/statifier_examples/screen_editor/try_test.exs index 22ba4064..846f4960 100644 --- a/test/statifier_examples/screen_editor/try_test.exs +++ b/test/statifier_examples/screen_editor/try_test.exs @@ -138,6 +138,52 @@ defmodule StatifierExamples.ScreenEditor.TryTest do } end + # Sabotage: took the image block out of the fixture; the details + # screen presented no library_logo and this went red, then reverted + # from a copy. + test "presents the image, divider, link, date, slider and option description the fixture adds" do + {:ok, document} = + "priv/fixtures/patron_registration_screens.json" |> File.read!() |> Document.from_json() + + {:ok, %{execution_id: execution_id, opts: opts}} = Try.start(document) + {:ok, details} = Journey.current(execution_id, opts) + details_nodes = Map.new(details.nodes, &{&1["key"], &1}) + + assert %{ + "type" => "image", + "src" => "/images/logo.svg", + "alt" => "Riverbend Public Library" + } = + details_nodes["library_logo"] + + assert %{"type" => "divider"} = details_nodes["details_divider"] + + assert %{"type" => "text_question", "format" => "date"} = + birth = details_nodes["birth_date"] + + refute Map.has_key?(birth, "checks") + + responses = %{ + "first_name" => "Rosa", + "email" => "rosa@example.com", + "age" => "34", + "birth_date" => "1990-04-17", + "library_pin" => "1234" + } + + {:ok, card} = Journey.submit(execution_id, "details_submitted", responses, opts) + card_nodes = Map.new(card.nodes, &{&1["key"], &1}) + + assert %{"type" => "link", "href" => "https://library.example.com/lending-rules"} = + card_nodes["rules_link"] + + assert %{"style" => "slider", "step" => 1, "min" => 1, "max" => 20} = + card_nodes["items_at_once"] + + assert %{"value" => "student", "description" => "For patrons in full-time education" <> _} = + card_nodes["card_type"]["answer_options"] |> List.last() + end + # Sabotage: made walk/1 answer the walk's refusals unwrapped; the # {:walk, _} match went red, then reverted. test "refuses a document the walk refuses, starting nothing" do diff --git a/test/statifier_examples/screen_editor/walk_test.exs b/test/statifier_examples/screen_editor/walk_test.exs index 861fd79b..9878fd24 100644 --- a/test/statifier_examples/screen_editor/walk_test.exs +++ b/test/statifier_examples/screen_editor/walk_test.exs @@ -112,6 +112,93 @@ defmodule StatifierExamples.ScreenEditor.WalkTest do Enum.find(elements, &(&1["key"] == "card_notice")) end + # Sabotage: made described/2 drop the descriptions; the student + # option came out with no description, then reverted from a copy. + # Sabotage: made step/1 leave the step as its text; the slider match + # went red, then reverted from a copy. + test "walks each newer element and field into riddler's shape" do + {:ok, walked} = Walk.screen_document(registration()) + [details, card] = walked["screens"] + by_key = fn screen -> Map.new(screen["elements"], &{&1["key"], &1}) end + + details = by_key.(details) + card = by_key.(card) + + assert details["library_logo"] == %{ + "type" => "image", + "key" => "library_logo", + "src" => "/images/logo.svg", + "alt" => "Riverbend Public Library", + "size" => "small" + } + + assert details["details_divider"] == %{"type" => "divider", "key" => "details_divider"} + + assert %{ + "format" => "date", + "min" => "1900-01-01", + "checks" => [ + %{ + "code" => "check.birth_date_in_future", + "condition" => "trim(responses.birth_date)::date <= context.today::date", + "message" => "A date of birth cannot be in the future." + } + ] + } = details["birth_date"] + + assert %{"style" => "slider", "step" => 1, "min" => 1, "max" => 20} = + card["items_at_once"] + + assert card["rules_link"] == %{ + "type" => "link", + "key" => "rules_link", + "label" => "Read the lending rules", + "href" => "https://library.example.com/lending-rules" + } + + options = card["card_type"]["answer_options"] + refute Map.has_key?(card["card_type"], "option_descriptions") + assert [%{"value" => "standard"} = standard, _family, student] = options + refute Map.has_key?(standard, "description") + + assert student["description"] == + "For patrons in full-time education, with a longer loan period" + end + + # Sabotage: took the checks row off the fixture's date of birth + # question; the future date answered :ok, then reverted from a copy. + test "holds the date of birth to its check, decided against the host's today" do + {:ok, walked} = Walk.screen_document(registration()) + {:ok, document} = Screens.admit(walked) + + answered = %{ + "first_name" => "Rosa", + "email" => "rosa@example.com", + "age" => "34", + "library_pin" => "1234" + } + + root = fn birth_date -> + %{ + "responses" => Map.put(answered, "birth_date", birth_date), + "context" => %{"today" => "2026-11-27"} + } + end + + assert {:error, [finding]} = + Riddler.Screens.validate_screen(document, "patron_details", root.("2031-04-17")) + + assert %Riddler.Finding{ + code: "check.birth_date_in_future", + element_key: "birth_date", + field: "checks", + author_message: "A date of birth cannot be in the future." + } = finding + + assert :ok = + Riddler.Screens.validate_screen(document, "patron_details", root.("1990-04-17")) + end + test "is a document the screen palette authors, with no finding" do document = registration() schema = ScreenEditor.palette() |> Schema.for_palette() |> ExJsonSchema.Schema.resolve() @@ -223,6 +310,46 @@ defmodule StatifierExamples.ScreenEditor.WalkTest do Walk.screen_document(Document.new(root, id: "sdoc_walk")) end + # Sabotage: made checks/1 keep the rows as written; the checks case + # answered :ok, then reverted from a copy. + test "a check row, an option description or a step it cannot read" do + choice = %{ + "label" => "Card", + "select" => "one", + "answer_options" => ["standard=Standard card"], + "option_descriptions" => ["student=Ages 12 to 18"] + } + + cases = [ + element("text_question", "age", %{"label" => "Age", "checks" => ["responses.age > 0"]}), + element("choice_question", "card", choice), + element("text_question", "items", %{"label" => "Items", "step" => "one"}) + ] + + for block <- cases do + assert {:error, [{id, _message}]} = Walk.screen_document(one_screen([block])) + assert id == block.id + end + end + + # Sabotage: made bounds/1 convert a date bound like a number; the walk + # refused the date question, then reverted from a copy. + test "keeps a date or time bound as the text it is" do + for {format, min} <- [ + {"date", "1900-01-01"}, + {"time", "09:00"}, + {"datetime", "2026-01-01T09:00:00Z"} + ] do + question = %{"label" => "When", "format" => format, "min" => min} + + assert {:ok, walked} = + Walk.screen_document(one_screen([element("text_question", "when", question)])) + + assert %{"min" => ^min} = + walked |> Map.fetch!("screens") |> hd() |> Map.fetch!("elements") |> hd() + end + end + # Sabotage: made options/1 pass an unreadable option row through; the # choice question case answered :ok, then reverted. test "an option row, a bound or a write row it cannot read" do diff --git a/test/statifier_examples/screen_editor_test.exs b/test/statifier_examples/screen_editor_test.exs index 10a1e7c2..cb133e1e 100644 --- a/test/statifier_examples/screen_editor_test.exs +++ b/test/statifier_examples/screen_editor_test.exs @@ -12,8 +12,9 @@ defmodule StatifierExamples.ScreenEditorTest do doctest StatifierExamples.ScreenEditor.Element doctest StatifierExamples.ScreenEditor.ChoiceQuestion + doctest StatifierExamples.ScreenEditor.TextQuestion - @element_types ~w(heading text text_question choice_question boolean_question secret_question button variant) + @element_types ~w(heading text text_question choice_question boolean_question secret_question button variant image divider link) describe "the palette" do test "registers the screen and every element type under the document's own names" do @@ -41,15 +42,18 @@ defmodule StatifierExamples.ScreenEditorTest do assert fields("text") == ~w(key condition text) assert fields("text_question") == - ~w(key condition label placeholder help required format pattern min max) + ~w(key condition label placeholder help required format pattern min max style step checks) assert fields("choice_question") == - ~w(key condition label select answer_options style help default required min_selections max_selections) + ~w(key condition label select answer_options option_descriptions style help default required min_selections max_selections checks) - assert fields("boolean_question") == ~w(key condition label help required) - assert fields("secret_question") == ~w(key condition label help required) + assert fields("boolean_question") == ~w(key condition label help required checks) + assert fields("secret_question") == ~w(key condition label help required checks) assert fields("button") == ~w(key condition label outcome writes style validates) assert fields("variant") == ~w(key condition) + assert fields("image") == ~w(key condition src alt size fit) + assert fields("divider") == ~w(key condition) + assert fields("link") == ~w(key condition label href style) end test "requires no style of a button, and a select of a choice question" do @@ -87,6 +91,24 @@ defmodule StatifierExamples.ScreenEditorTest do assert Enum.map(styles, &elem(&1, 0)) == ~w(radio dropdown checkboxes list) end + + # Sabotage: took "slider" out of the text question's style candidates; + # the text question assertion went red, then reverted from a copy. + test "offers riddler's hint names for a text question, an image and a link" do + candidates = ScreenEditor.field_candidates() + + assert names(candidates, "text_question", "style") == ~w(multiline slider stepper) + assert names(candidates, "image", "size") == ~w(icon avatar small medium large banner) + assert names(candidates, "image", "fit") == ~w(contain cover) + assert names(candidates, "link", "style") == ~w(inline button) + end + + test "offers the date and time formats as choices of a text question's format" do + assert %{type: {:select, formats}} = field("text_question", "format") + + assert Enum.map(formats, &elem(&1, 0)) -- [""] == + ~w(email phone pattern integer number date time datetime) + end end describe "a patron registration document" do @@ -163,6 +185,69 @@ defmodule StatifierExamples.ScreenEditorTest do assert {:error, [{"outcome", _}]} = validate("button", %{button | "outcome" => ""}) end + # Sabotage: made validate_config/1 skip Element.present/3 on "alt"; + # the blank alt case answered :ok, then reverted from a copy. + test "holds an image to a source and an alt text, and a link to a label and a URL" do + image = %{"key" => "logo", "src" => "/images/logo.svg", "alt" => "Riverbend"} + assert :ok = validate("image", image) + assert {:error, [{"alt", _}]} = validate("image", %{image | "alt" => ""}) + assert {:error, [{"src", _}]} = validate("image", Map.delete(image, "src")) + + link = %{"key" => "rules", "label" => "Lending rules", "href" => "/lending-rules"} + assert :ok = validate("link", link) + assert {:error, [{"href", _}]} = validate("link", %{link | "href" => " "}) + assert :ok = validate("divider", %{"key" => "rule"}) + end + + # Sabotage: made bound_findings/2 hold a date bound to being a number; + # the date bound case went red, then reverted from a copy. + test "reads a date bound as text, and holds a step to a number" do + question = %{"key" => "birth_date", "label" => "Date of birth", "format" => "date"} + assert :ok = validate("text_question", Map.put(question, "min", "1900-01-01")) + + slider = %{"key" => "items", "label" => "Items", "format" => "integer"} + assert :ok = validate("text_question", Map.put(slider, "step", "1")) + assert {:error, [{"step", _}]} = validate("text_question", Map.put(slider, "step", "one")) + end + + # Sabotage: dropped the membership test from option_descriptions/2; the + # row naming no option validated :ok, then reverted from a copy. + test "holds an option description to value=Description, naming an option once" do + assert :ok = + validate( + "choice_question", + choice(%{"option_descriptions" => ["family=Two adults"]}) + ) + + for rows <- [["student=Ages 12 to 18"], ["family"], ["family=A", "family=B"]] do + assert {:error, [{"option_descriptions", _}]} = + validate("choice_question", choice(%{"option_descriptions" => rows})) + end + end + + # Sabotage: made check_row/1 accept a code outside the check. shape; the + # "adult:" row validated :ok, then reverted from a copy. + test "holds a question's checks to code: condition => message, each code once" do + row = "check.adult: responses.age::integer >= 18 => You must be 18 or over." + question = %{"key" => "age", "label" => "Age"} + + for type <- ~w(text_question boolean_question secret_question) do + assert :ok = validate(type, Map.put(question, "checks", [row])) + end + + assert :ok = validate("choice_question", choice(%{"checks" => [row]})) + + for rows <- [ + ["adult: responses.age >= 18 => You must be 18."], + ["check.adult: responses.age >= 18"], + ["check.adult: => You must be 18."], + [row, row] + ] do + assert {:error, [{"checks", _}]} = + validate("text_question", Map.put(question, "checks", rows)) + end + end + test "parses the option rows a walk reads" do assert ChoiceQuestion.answer_options(["family=Family card = two adults"]) == {:ok, [{"family", "Family card = two adults"}]} @@ -177,6 +262,11 @@ defmodule StatifierExamples.ScreenEditorTest do defp validate(type, config), do: module(type).validate_config(config) + defp names(candidates, type, field) do + {:open, pairs} = Map.fetch!(candidates, {"myapp.screens." <> type, field}) + Enum.map(pairs, &elem(&1, 0)) + end + defp choice(overrides) do Map.merge( %{ @@ -190,7 +280,8 @@ defmodule StatifierExamples.ScreenEditorTest do end # Every type once: one screen holding a heading, a text, each question, - # a variant with a conditional and a default candidate, and a button. + # a variant with a conditional and a default candidate, an image, a + # divider, a link and a button. defp registration do elements = [ element("heading", "title", %{"level" => 1, "text" => "Register for a library card"}), @@ -215,6 +306,9 @@ defmodule StatifierExamples.ScreenEditorTest do ] } ), + element("image", "logo", %{"src" => "/images/logo.svg", "alt" => "Riverbend"}), + element("divider", "rule", %{}), + element("link", "rules", %{"label" => "Lending rules", "href" => "/lending-rules"}), element("button", "continue", %{"label" => "Continue", "outcome" => "details_submitted"}) ] diff --git a/test/statifier_examples_web/components/signup_elements_test.exs b/test/statifier_examples_web/components/signup_elements_test.exs new file mode 100644 index 00000000..1fbebf11 --- /dev/null +++ b/test/statifier_examples_web/components/signup_elements_test.exs @@ -0,0 +1,34 @@ +defmodule StatifierExamplesWeb.SignupElementsTest do + @moduledoc """ + The screen editor's Try renderer draws the display elements an author + can place from the palette - an image, a divider and a link - rather + than raising on them as types it does not know. + """ + + use ExUnit.Case, async: true + + import Phoenix.LiveViewTest + + alias StatifierExamplesWeb.SignupElements + + # Sabotage: made element/1 draw an image as the divider; the img + # assertion went red, then reverted from a copy. + test "draws an image, a divider and a link as plain HTML" do + nodes = [ + %{"type" => "image", "key" => "logo", "src" => "/images/logo.svg", "alt" => "Riverbend"}, + %{"type" => "divider", "key" => "rule"}, + %{ + "type" => "link", + "key" => "rules", + "label" => "Lending rules", + "href" => "/lending-rules" + } + ] + + html = render_component(&SignupElements.screen/1, title: "Your details", nodes: nodes) + + assert html =~ ~r{]* id="logo" src="/images/logo.svg" alt="Riverbend"} + assert html =~ ~r{]* id="rule"} + assert html =~ ~r{]* id="rules" href="/lending-rules"[^>]*>\s*Lending rules\s*} + end +end diff --git a/test/statifier_examples_web/live/registration_journey_live_test.exs b/test/statifier_examples_web/live/registration_journey_live_test.exs index 92ab0d60..f9270830 100644 --- a/test/statifier_examples_web/live/registration_journey_live_test.exs +++ b/test/statifier_examples_web/live/registration_journey_live_test.exs @@ -46,6 +46,24 @@ defmodule StatifierExamplesWeb.RegistrationJourneyLiveTest do live end + # Sabotage: took the divider block out of the fixture; the details + # screen drew no rule and this went red, then reverted from a copy. + test "the details and card screens draw the image, divider, link, date input and slider", %{ + conn: conn + } do + {:ok, live, _html} = live(conn, @path) + live |> element("#start-journey") |> render_click() + + assert has_element?(live, "img" <> key("library_logo")) + assert has_element?(live, "hr" <> key("details_divider")) + assert has_element?(live, ~s(input[type="date"][name="birth_date"])) + + press(live, "details_continue", @details) + + assert has_element?(live, ~s(a[href="https://library.example.com/lending-rules"])) + assert has_element?(live, ~s(input[type="range"][name="items_at_once"][step="1"])) + end + # The component types the ticked box as `true` before the host sees it, # which riddler's check of a boolean question requires. # From 460457290d515ae14c37b3985e42e367e2262fd9 Mon Sep 17 00:00:00 2001 From: JohnnyT Date: Fri, 27 Nov 2026 15:31:14 -0700 Subject: [PATCH 2/2] Words the time formats as riddler admits them The reader messages for time and datetime name both the minutes and the seconds shapes, and a datetime's Z or offset. The date and time format choices test carries the mutation it was sabotaged with. Refs: se-gb7u --- lib/statifier_examples/signup/validation.ex | 5 +++-- test/statifier_examples/screen_editor_test.exs | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/statifier_examples/signup/validation.ex b/lib/statifier_examples/signup/validation.ex index 6716dd65..279d01a2 100644 --- a/lib/statifier_examples/signup/validation.ex +++ b/lib/statifier_examples/signup/validation.ex @@ -66,8 +66,9 @@ defmodule StatifierExamples.Signup.Validation do "integer" => "must be a whole number", "number" => "must be a number", "date" => "must be a date, written YYYY-MM-DD", - "time" => "must be a time, written HH:MM", - "datetime" => "must be a date and time, written YYYY-MM-DDTHH:MM:SS with Z or an offset" + "time" => "must be a time, written HH:MM or HH:MM:SS", + "datetime" => + "must be a date and time, written YYYY-MM-DDTHH:MM or YYYY-MM-DDTHH:MM:SS, then Z or an offset such as +02:00" } @formats Map.keys(@format_messages) |> Enum.sort() diff --git a/test/statifier_examples/screen_editor_test.exs b/test/statifier_examples/screen_editor_test.exs index cb133e1e..62c29669 100644 --- a/test/statifier_examples/screen_editor_test.exs +++ b/test/statifier_examples/screen_editor_test.exs @@ -103,6 +103,8 @@ defmodule StatifierExamples.ScreenEditorTest do assert names(candidates, "link", "style") == ~w(inline button) end + # Sabotage: dropped "time" from the text question's format choices; + # the choices assertion went red, then reverted from a copy. test "offers the date and time formats as choices of a text question's format" do assert %{type: {:select, formats}} = field("text_question", "format")