Skip to content

Commit

Permalink
move StateTable to Introspect.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 20, 2024
1 parent 16088b0 commit 63bdc3a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 102 deletions.
4 changes: 2 additions & 2 deletions lib/trailblazer/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module Workflow
require "trailblazer/workflow/test/assertions"

require "trailblazer/workflow/discovery"
require "trailblazer/workflow/discovery/present"
require "trailblazer/workflow/discovery/present/state_table"
require "trailblazer/workflow/discovery/present/event_table"
require "terminal-table" # TODO: only require when discovery is "loaded".

require "trailblazer/workflow/introspect"
require "trailblazer/workflow/introspect/iteration"
require "trailblazer/workflow/introspect/state_table"
70 changes: 0 additions & 70 deletions lib/trailblazer/workflow/discovery/present/state_table.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Trailblazer
module Workflow
module Discovery
module Introspect
# Rendering-specific code using {Discovery:states}.
# https://stackoverflow.com/questions/22885702/html-for-the-pause-symbol-in-audio-and-video-control
module Present
Expand Down
6 changes: 3 additions & 3 deletions lib/trailblazer/workflow/introspect/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.from_discovered_states(discovered_states, **options)
iterations = discovered_states.collect do |row|
triggered_catch_event_position = row[:positions_before][1]

id = Discovery::Present.id_for_task(triggered_catch_event_position)
id = Introspect::Present.id_for_task(triggered_catch_event_position)
event_label = Test::Plan::CommentHeader.start_position_label(row[:positions_before][1], row, **options)

Iteration.new(
Expand Down Expand Up @@ -83,7 +83,7 @@ def self.serialize_suspend_position(activity, suspend, **options)
if suspend.to_h["resumes"].nil? # FIXME: for termini.
comment = [:terminus, suspend.to_h[:semantic]]
else
[:before, Discovery::Present.readable_name_for_suspend_or_terminus(activity, suspend, **options)]
[:before, Introspect::Present.readable_name_for_suspend_or_terminus(activity, suspend, **options)]
end

{
Expand All @@ -96,7 +96,7 @@ def self.serialize_suspend_position(activity, suspend, **options)
def serialize_position(activity, catch_event, **options)
position_tuple = id_tuple_for(activity, catch_event, **options)

comment = [:before, Discovery::Present.readable_name_for_catch_event(activity, catch_event, **options)]
comment = [:before, Introspect::Present.readable_name_for_catch_event(activity, catch_event, **options)]

{
tuple: position_tuple,
Expand Down
69 changes: 69 additions & 0 deletions lib/trailblazer/workflow/introspect/state_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Trailblazer
module Workflow
module Introspect
# Rendering-specific code using {Iteration::Set}.
module StateTable
module_function

# Each row represents a configuration of suspends aka "state".
# The state knows its possible resume events.
# does the state know which state fields belong to it?
def call(iteration_set, lanes_cfg:)
# raise discovery_state_table.inspect
start_position_to_catch = {}

# Key by lane_positions, which represent a state.
# State (lane_positions) => [events (start position)]
states = {}

# Collect the invoked start positions per Positions configuration.
# This implies the possible "catch events" per configuration.
iteration_set.collect do |iteration|
start_positions = iteration.start_positions
start_task_position = iteration.start_task_position

events = states[start_positions]
events = [] if events.nil?

events += [start_task_position]

states[start_positions] = events
end

# render
cli_rows = states.flat_map do |positions, catch_events|
suggested_state_name = suggested_state_name_for(catch_events)

suggested_state_name = "⏸︎ #{suggested_state_name}".inspect

triggerable_events = catch_events
.collect { |event_position| Present.readable_name_for_catch_event(*event_position.to_a, lanes_cfg: lanes_cfg).inspect }
.uniq
.join(", ")


Hash[
"state name",
suggested_state_name,

"triggerable events",
triggerable_events
]
end

columns = ["state name", "triggerable events"]
Present::Table.render(columns, cli_rows)
end

# @private
# TODO: move to StateTable
def suggested_state_name_for(catch_events)
catch_events
.collect { |event_position| Present.label_for_next_task(*event_position.to_a) }
.uniq
.join("/")
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/trailblazer/workflow/test/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def assert_positions(asserted_positions, expected_positions, lanes_cfg:, test_pl
# Compile error message when the expected lane position doesn't match the actual one.
def self.error_message_for(position, expected_position, lanes_cfg:) # TODO: test me.
# TODO: make the labels use UTF8 icons etc, as in the CLI rendering code.
expected_label = Discovery::Present.readable_name_for_suspend_or_terminus(*position.to_a, lanes_cfg: lanes_cfg)
actual_label = Discovery::Present.readable_name_for_suspend_or_terminus(*expected_position.to_a, lanes_cfg: lanes_cfg)
expected_label = Introspect::Present.readable_name_for_suspend_or_terminus(*position.to_a, lanes_cfg: lanes_cfg)
actual_label = Introspect::Present.readable_name_for_suspend_or_terminus(*expected_position.to_a, lanes_cfg: lanes_cfg)

lane_label = Discovery::Present.lane_options_for(*position.to_a, lanes_cfg: lanes_cfg)[:label]
lane_label = Introspect::Present.lane_options_for(*position.to_a, lanes_cfg: lanes_cfg)[:label]

"Lane #{lane_label}:\n expected #{expected_label}\n actual #{actual_label}"
end
Expand Down
10 changes: 5 additions & 5 deletions lib/trailblazer/workflow/test/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def call(iteration_set, **options)
]
end

Discovery::Present::Table.render(["triggered catch", "start configuration", "expected reached configuration"], rows)
Introspect::Present::Table.render(["triggered catch", "start configuration", "expected reached configuration"], rows)
end

def start_position_label(start_position, row, **options)
Expand All @@ -51,9 +51,9 @@ def start_position_label(start_position, row, **options)
end

def start_position_label_for(position, expected_outcome:, **options)
event_label = Discovery::Present.readable_name_for_catch_event(*position.to_a, **options)
event_label = Introspect::Present.readable_name_for_catch_event(*position.to_a, **options)

event_label += " #{Discovery::Present::ICONS[:failure]}" if expected_outcome == :failure # FIXME: what happens to :symbol after serialization?
event_label += " #{Introspect::Present::ICONS[:failure]}" if expected_outcome == :failure # FIXME: what happens to :symbol after serialization?

event_label
end
Expand All @@ -67,7 +67,7 @@ def render_combined_column_labels(positions_rows, **options)
positions.collect do |activity, task|
[
activity,
Discovery::Present.readable_name_for_suspend_or_terminus(activity, task, **options)
Introspect::Present.readable_name_for_suspend_or_terminus(activity, task, **options)
]
end
end
Expand All @@ -76,7 +76,7 @@ def render_combined_column_labels(positions_rows, **options)
end

def compute_combined_column_widths(position_rows, lanes_cfg:, **)
chars_to_filter = Discovery::Present::ICONS.values + lanes_cfg.collect { |_, cfg| cfg[:icon] } # TODO: do this way up in the code path.
chars_to_filter = Introspect::Present::ICONS.values + lanes_cfg.collect { |_, cfg| cfg[:icon] } # TODO: do this way up in the code path.

# Find out the longest entry per lane.
columns = lanes_cfg.collect { |_, cfg| [cfg[:activity], []] }.to_h # {<lifecycle> => [], ...}
Expand Down
19 changes: 1 addition & 18 deletions test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def assert_position_after(actual_configuration, expected_ids, lanes:)

iteration_set = Trailblazer::Workflow::Introspect::Iteration::Set.from_discovered_states(states, lanes_cfg: lanes_cfg)

cli_state_table = Trailblazer::Workflow::Discovery::Present::StateTable.(iteration_set, lanes_cfg: lanes_cfg)
cli_state_table = Trailblazer::Workflow::Introspect::StateTable.(iteration_set, lanes_cfg: lanes_cfg)
puts cli_state_table
assert_equal cli_state_table,
%(+---------------------------------+----------------------------------------+
Expand Down Expand Up @@ -348,23 +348,6 @@ class DiscoveryTestPlanTest < Minitest::Spec
| ☝ ⏵︎Revise | ⛾ ⏵︎Revise ☝ ⏵︎Revise ☑ ◉End.success | ⛾ ⏵︎Revise ⏵︎Notify approver ☝ ⏵︎Update form ⏵︎Notify approver ☑ ◉End.success |
+----------------------+---------------------------------------------------------------------------------+---------------------------------------------------------------------------------+)
end

it "render structure" do
states, lanes_sorted, lanes_cfg = DiscoveryTest.states()

plan_structure = Trailblazer::Workflow::Test::Plan::Structure.serialize(states, lanes_cfg: lanes_cfg)
# pp plan_structure
testing_json = JSON.pretty_generate(plan_structure)
# File.write "test/discovery_testing_json.json", testing_json
assert_equal testing_json, File.read("test/discovery_testing_json.json")

structure = Trailblazer::Workflow::Test::Plan::Structure.deserialize(plan_structure, lanes_cfg: lanes_cfg)

assert_equal structure.position_from_tuple("lifecycle", "suspend-gw-to-catch-before-Activity_0wwfenp").to_a,
[lifecycle = lanes_cfg.values[0][:activity], Trailblazer::Activity::Introspect.Nodes(lifecycle, id: "suspend-gw-to-catch-before-Activity_0wwfenp").task]


end
end

class TestPlanExecutionTest < Minitest::Spec
Expand Down

0 comments on commit 63bdc3a

Please sign in to comment.