Skip to content

Commit

Permalink
introduce Test#assert_advance.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Feb 12, 2024
1 parent bf25c28 commit e946f1e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
2 changes: 2 additions & 0 deletions lib/trailblazer/workflow/state/discovery/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Discovery
module Testing
module_function

# The structure is serialized and only contains IDs.

# DISCUSS: In the Testing JSON, we want
# 1. start event, start configuration, input => expected suspend configuration
# 2. a "comment" table above that which draws out the same in readable format.
Expand Down
58 changes: 58 additions & 0 deletions lib/trailblazer/workflow/test/plan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
module Trailblazer
module Workflow
module Test
module Assertions
def assert_positions(asserted_positions, expected_tuples, lanes:)
expected_positions =
expected_tuples.collect do |testing_row|
Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *testing_row[:tuple])
end

sorting_block = ->(a, b) { a.activity.object_id <=> b.activity.object_id }

sorted_asserted_positions = asserted_positions.to_a.sort(&sorting_block)
sorted_expected_positions = expected_positions.sort(&sorting_block)

sorted_asserted_positions.collect.with_index do |position, index|
assert_equal position, sorted_expected_positions[index], "baaaaal"
end

# FIXME: figure out why we can't just compare the entire array!
# assert_equal sorted_asserted_positions, sorted_expected_positions
end

def assert_advance(event_label, test_plan:, lanes:, schema:, message_flow:, **) # TODO: allow {ctx}
testing_row = test_plan.find { |row| row[:event_label] == event_label }

start_position = testing_row[:start_position]
start_tuple = start_position[:tuple] # #{start_position[:comment]}
start_position = Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *start_tuple)

# current position.
#DISCUSS: here, we could also ask the State layer for the start configuration, on a different level.
lane_positions = testing_row[:start_configuration].collect do |row|
Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *row[:tuple])
end

lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(lane_positions)

configuration, (ctx, flow) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
schema,
[{seq: []}, {throw: []}],
{}, # circuit_options

start_position: start_position,
lane_positions: lane_positions, # current position/"state"

message_flow: message_flow,
)

assert_positions configuration[:lane_positions], testing_row[:expected_lane_positions], lanes: lanes
assert_equal ctx.inspect, "asdf"
end

end

module Plan
# Code fragment with assertions for the discovered/configured test plan.
def self.for(test_structure, input:)
Expand All @@ -10,6 +62,8 @@ def self.for(test_structure, input:)
# raise row[:start_configuration].inspect
start_position = row[:start_position]

tuples_for_expected = row[:expected_lane_positions]


%(
# test: #{row[:event_label]}
Expand All @@ -36,6 +90,10 @@ def self.for(test_structure, input:)
message_flow: message_flow,
)
assert_positions configuration[:lane_positions], #{row[:expected_lane_positions].inspect}, lanes: lanes
# TODO: test {:last_lane}.
assert_equal configuration.lane_positions.keys, [lane_activity, lane_activity_ui]
assert_equal configuration.lane_positions.values.inspect, %([{"resumes"=>["catch-before-#{}"], :semantic=>[:suspend, "from initial_lane_positions"]}, \
Expand Down
35 changes: 21 additions & 14 deletions test/collaboration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,27 +595,32 @@ def render_states(states, lanes:, additional_state_data:, task_map:)

# test: ☝ ▶Revise

start_tuple = ["UI", "catch-before-Activity_1wiumzv"] # ["before", "Revise"]
start_position = Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *start_tuple)
assert_advance "☝ ▶Revise", ctx: {}, test_plan: testing_structure, lanes: lanes, schema: schema, message_flow: message_flow

# current position.
#DISCUSS: here, we could also ask the State layer for the start configuration, on a different level.
lane_positions = [Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *["lifecycle", "suspend-Gateway_01p7uj7"]), Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *["UI", "suspend-Gateway_1xs96ik"])]
# start_tuple = ["UI", "catch-before-Activity_1wiumzv"] # ["before", "Revise"]
# start_position = Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *start_tuple)

lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(lane_positions)
# # current position.
# #DISCUSS: here, we could also ask the State layer for the start configuration, on a different level.
# lane_positions = [Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *["lifecycle", "suspend-Gateway_01p7uj7"]), Trailblazer::Workflow::State::Discovery.position_from_tuple(lanes, *["UI", "suspend-Gateway_1xs96ik"])]

# lane_positions = Trailblazer::Workflow::Collaboration::Positions.new(lane_positions)

configuration, (ctx, flow) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
schema,
[{seq: []}, {throw: []}],
{}, # circuit_options

start_position: start_position,
lane_positions: lane_positions, # current position/"state"
# configuration, (ctx, flow) = Trailblazer::Workflow::Collaboration::Synchronous.advance(
# schema,
# [{seq: []}, {throw: []}],
# {}, # circuit_options

message_flow: message_flow,
)
# start_position: start_position,
# lane_positions: lane_positions, # current position/"state"

# message_flow: message_flow,
# )

# assert_positions configuration[:lane_positions], [{:tuple=>["approver", "End.success"], :comment=>[:terminus, :success]}, {:tuple=>["lifecycle", "suspend-Gateway_1kl7pnm"], :comment=>["before", ["Revise", "Notify approver"]]}, {:tuple=>["UI", "suspend-Gateway_1g3fhu2"], :comment=>["before", ["Update form", "Notify approver"]]}], lanes: lanes

# assert_positions "☝ ▶Revise", configuration[:lane_positions], lanes: lanes



Expand Down Expand Up @@ -673,4 +678,6 @@ def render_states(states, lanes:, additional_state_data:, task_map:)
# we can actually see the last signal and its semantic is {[:suspend, "suspend-Gateway_0kknfje"]}
assert_equal configuration.signal.inspect, %(#<Trailblazer::Workflow::Event::Suspend resumes=["catch-before-Activity_1165bw9", "catch-before-Activity_1dt5di5"] type=:suspend semantic=[:suspend, "suspend-Gateway_0kknfje"]>)
end
include Trailblazer::Workflow::Test::Assertions

end

0 comments on commit e946f1e

Please sign in to comment.