diff --git a/lib/statifier_examples/library/onboarding.ex b/lib/statifier_examples/library/onboarding.ex index 4cf4e182..8f5ffe54 100644 --- a/lib/statifier_examples/library/onboarding.ex +++ b/lib/statifier_examples/library/onboarding.ex @@ -83,6 +83,8 @@ defmodule StatifierExamples.Library.Onboarding do use StatifierBlocks.Runtime.Subchart + require Logger + alias Statifier.Invoke.Handler alias Statifier.Machine.Identity alias StatifierBlocks.{Compiled, Decode, Document, Palette} @@ -185,15 +187,41 @@ defmodule StatifierExamples.Library.Onboarding do StatifierPersistence.Execution.t() defp newest_pass(children), do: Enum.max_by(children, &pass_order(&1.execution_id)) + # `statifier_persistence` exposes no function that reads a pass back out of + # a child id, so the suffix is parsed here. An id with no `@` is the first + # pass's. An id with an `@` whose suffix is not `.` is + # read as the first pass too, but is logged: the id shape changed, and the + # pick would otherwise degrade without a sign. @spec pass_order(String.t()) :: {integer(), integer()} defp pass_order(child_id) do - with [_first, invocation] <- String.split(child_id, "@", parts: 2), - [macrostep, round] <- String.split(invocation, ".", parts: 2), + case String.split(child_id, "@", parts: 2) do + [_first_pass] -> + {-1, -1} + + [_first, invocation] -> + case parse_invocation(invocation) do + {:ok, order} -> + order + + :error -> + Logger.warning( + "onboarding child #{child_id} has a pass suffix that is not " <> + ".; read as the first pass" + ) + + {-1, -1} + end + end + end + + @spec parse_invocation(String.t()) :: {:ok, {integer(), integer()}} | :error + defp parse_invocation(invocation) do + with [macrostep, round] <- String.split(invocation, ".", parts: 2), {macrostep, ""} <- Integer.parse(macrostep), {round, ""} <- Integer.parse(round) do - {macrostep, round} + {:ok, {macrostep, round}} else - _first_pass -> {-1, -1} + _unparseable -> :error end end diff --git a/test/statifier_examples/library/onboarding_test.exs b/test/statifier_examples/library/onboarding_test.exs index a5337f59..7c607359 100644 --- a/test/statifier_examples/library/onboarding_test.exs +++ b/test/statifier_examples/library/onboarding_test.exs @@ -10,6 +10,8 @@ defmodule StatifierExamples.Library.OnboardingTest do use ExUnit.Case, async: false + import ExUnit.CaptureLog + alias Ecto.Adapters.SQL.Sandbox alias Statifier.Machine.Identity alias StatifierBlocks.Compiled @@ -213,6 +215,38 @@ defmodule StatifierExamples.Library.OnboardingTest do assert opts[:child] end + # Sabotage: made `pass_order/1` read an unparseable suffix as the first + # pass without logging it. This test went red: no warning was captured. + test "a child id whose pass suffix does not parse is logged and read as the first pass" do + id = start() + {:ok, _view} = pick(id, ["bdoc_catalogue_sync"]) + first = first_child(id) + + {:ok, %Compiled{} = compiled} = Onboarding.resolve_chart("bdoc_catalogue_sync", %{}) + {:ok, setup} = Setups.fetch_document("bdoc_catalogue_sync") + {:ok, {durable, _run}} = Durable.resume(compiled, setup, first) + {:ok, %{metadata: metadata}} = Storage.fetch_execution(store(), first) + assert {:ok, linkage} = Linkage.from_metadata(metadata) + + stray = Linkage.child_execution_id(id, "blk_bo_setup", 0, "not-a-pass") + + assert {:ok, _execution, _state} = + Executions.create(store(), stray, durable.machine, + executor: fn _effect, _context -> :ok end, + serialization: {ExecutionLock, ExecutionLock}, + linkage: linkage + ) + + log = + capture_log(fn -> + assert {:ok, {picked, _opts}} = Onboarding.focus(id) + assert picked in [first, stray] + end) + + assert log =~ stray + assert log =~ "read as the first pass" + end + # The moduledoc and the fixture's description say the setups run in the # order the response lists them; the foreach walks it as submitted. test "the setups run in the order the response lists them" do