Skip to content

Commit

Permalink
we can produce a state_guards file.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Mar 11, 2024
1 parent d08ff8a commit 605a944
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/trailblazer/workflow/discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def unstub_configuration(activity_2_stub, configuration, lanes:)
module DSL
module_function

def configuration_for_branching(branch_cfg, lanes:, **)
def configuration_for_branching_from_user_hash(branch_cfg, lanes:, **)
branch_cfg.collect do |(lane_label, cdt_task_label), cfg|
activity = lanes.(label: lane_label)[:activity]

Expand Down
79 changes: 51 additions & 28 deletions lib/trailblazer/workflow/task/discover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Discover
module_function

# DISCUSS: what about running this before we have a schema?
def call(schema:, start_activity_json_id:, iteration_set_filename:, run_multiple_times: {}, test_filename:, collaboration_name:)
def call(schema:, start_activity_json_id:, iteration_set_filename:, run_multiple_times: {}, test_filename:, state_guard_filename: default_filename_for_state_guards(iteration_set_filename), collaboration_namespace:)
lanes_cfg = schema.to_h[:lanes]

start_task_position = find_start_task_position(start_activity_json_id, lanes_cfg) # FIXME: handle nil case
Expand All @@ -22,15 +22,25 @@ def call(schema:, start_activity_json_id:, iteration_set_filename:, run_multiple
run_multiple_times: run_multiple_times,
)

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

create_serialized_iteration_set(iteration_set, iteration_set_filename: iteration_set_filename, lanes_cfg: lanes_cfg)
create_serialized_iteration_set(iteration_set, iteration_set_filename: iteration_set_filename, lanes_cfg: lanes_cfg)

RenderTestPlan.(iteration_set, lanes_cfg: lanes_cfg,
test_filename: test_filename,
collaboration_name: collaboration_name,
iteration_set_filename: iteration_set_filename
)
Produce::TestPlan.(iteration_set, lanes_cfg: lanes_cfg,
test_filename: test_filename,
collaboration_namespace: collaboration_namespace,
iteration_set_filename: iteration_set_filename
)

Produce::StateGuards.(iteration_set, lanes_cfg: lanes_cfg,
filename: state_guard_filename,
namespace: collaboration_namespace,
)
end

# TODO: move to {generator}.
def default_filename_for_state_guards(iteration_set_filename, filename: "state_guards.rb")
File.join(File.dirname(iteration_set_filename), filename)
end


Expand All @@ -54,38 +64,51 @@ def create_serialized_iteration_set(iteration_set, iteration_set_filename:, lane
File.write iteration_set_filename, interation_set_json
end

module RenderTestPlan
module_function

def call(iteration_set, lanes_cfg:, test_filename:, collaboration_name:, input: {}, iteration_set_filename:)
test_plan_comment_header = Trailblazer::Workflow::Test::Plan.render_comment_header(iteration_set, lanes_cfg: lanes_cfg)
module Produce
module TestPlan
module_function

def call(iteration_set, lanes_cfg:, test_filename:, collaboration_namespace:, input: {}, iteration_set_filename:)
test_plan_comment_header = Trailblazer::Workflow::Test::Plan.render_comment_header(iteration_set, lanes_cfg: lanes_cfg)

assertions = Trailblazer::Workflow::Test::Plan.for(iteration_set, lanes_cfg: lanes_cfg, input: input)
assertions = Trailblazer::Workflow::Test::Plan.for(iteration_set, lanes_cfg: lanes_cfg, input: input)


test_content = %(=begin
#{test_plan_comment_header}
=end
test_content = %(=begin
#{test_plan_comment_header}
=end
require "test_helper"
require "test_helper"
class #{collaboration_name.gsub("::", "_")}CollaborationTest < Minitest::Spec
include Trailblazer::Workflow::Test::Assertions
require "trailblazer/test/assertions"
include Trailblazer::Test::Assertions # DISCUSS: this is for assert_advance and friends.
class #{collaboration_namespace.gsub("::", "_")}CollaborationTest < Minitest::Spec
include Trailblazer::Workflow::Test::Assertions
require "trailblazer/test/assertions"
include Trailblazer::Test::Assertions # DISCUSS: this is for assert_advance and friends.
it "can run the collaboration" do
schema = #{collaboration_name}
test_plan = Trailblazer::Workflow::Introspect::Iteration::Set::Deserialize.(JSON.parse(File.read("#{iteration_set_filename}")), lanes_cfg: schema.to_h[:lanes])
it "can run the collaboration" do
schema = #{collaboration_namespace}::Schema
test_plan = Trailblazer::Workflow::Introspect::Iteration::Set::Deserialize.(JSON.parse(File.read("#{iteration_set_filename}")), lanes_cfg: schema.to_h[:lanes])
#{assertions.join("\n")}
#{assertions.join("\n")}
end
end
end
)
)

File.write(test_filename, test_content)


end
end

File.write(test_filename, test_content)
module StateGuards
module_function

def call(iteration_set, filename:, lanes_cfg:, namespace:, **)
ruby_output = Trailblazer::Workflow::Introspect::StateTable::Generate.(iteration_set, lanes_cfg: lanes_cfg, namespace: namespace)

File.write(filename, ruby_output)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/advance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AdvanceTest < Minitest::Spec
"⏸︎ Revise " => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_1wiumzv"]},
}}[:state_guards]

state_guards = Trailblazer::Workflow::Collaboration::StateGuards.from_user_hash(
state_guards = Trailblazer::Workflow::Collaboration::StateGuards.from_user_hash( # TODO: unify naming, DSL.state_guards_from_user or something like that.
state_guards_from_user,
iteration_set: iteration_set
)
Expand Down
18 changes: 17 additions & 1 deletion test/discovery_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def build_schema()
schema = build_schema()

Trailblazer::Workflow::Task::Discover.(schema: schema, start_activity_json_id: "<ui> author workflow", iteration_set_filename: "test/tmp/bla.json", test_filename: "test/tmp/bla_test.rb",
collaboration_name: "App::Bla")
collaboration_namespace: "App::Bla")

#@ We serialized the discovered iterations, so we don't need to run discovery on every startup.
assert_equal (serialized_iteration_set = File.read("test/tmp/bla.json")).size, 20925
Expand All @@ -86,6 +86,22 @@ def build_schema()

#@ Assert test plan

#@ Assert {state_guards.rb}
assert_equal File.read("test/tmp/state_guards.rb"),
%(App::Bla::StateGuards = {
"⏸︎ Create form" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_0wc2mcq"]},
"⏸︎ Create" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_1psp91r"]},
"⏸︎ Update form♦Notify approver" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_1165bw9", "catch-before-Activity_1dt5di5"]},
"⏸︎ Update" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_0j78uzd"]},
"⏸︎ Approve♦Reject" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_13fw5nm", "catch-before-Activity_1j7d8sd"]},
"⏸︎ Delete? form♦Publish" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_0bsjggk", "catch-before-Activity_0ha7224"]},
"⏸︎ Revise form" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_0zsock2"]},
"⏸︎ Delete♦Cancel" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_15nnysv", "catch-before-Activity_1uhozy1"]},
"⏸︎ Archive" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_0fy41qq"]},
"⏸︎ Revise" => {guard: ->(ctx, process_model:, **) { raise "implement me!" }, id: ["catch-before-Activity_1wiumzv"]},
}
)

end

it "Test plan " do
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def states
:"approver:xxx" => Trailblazer::Activity::Left, # FIXME: {:decision} must be translated to {:"approver:xxx"}
}, config_payload: {outcome: :failure}},

**Trailblazer::Workflow::Discovery::DSL.configuration_for_branching(
**Trailblazer::Workflow::Discovery::DSL.configuration_for_branching_from_user_hash(
{
# Click [UI Create] again, with invalid data.
["UI", "Create"] => {ctx_merge: {:"lifecycle:Create" => Trailblazer::Activity::Left}, config_payload: {outcome: :failure}},
Expand Down

0 comments on commit 605a944

Please sign in to comment.