Skip to content

Commit

Permalink
use Endpoint::Runtime.call.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Mar 18, 2024
1 parent 5556335 commit ec540ad
Showing 1 changed file with 87 additions and 5 deletions.
92 changes: 87 additions & 5 deletions test/advance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class AdvanceTest < Minitest::Spec
iteration_set = Trailblazer::Workflow::Introspect::Iteration::Set::Deserialize.(JSON.parse(serialized_iteration_set), lanes_cfg: lanes_cfg)

state_guards_from_user = {state_guards: {
"⏸︎ Create form " => {guard: ->(ctx, process_model:, **) { true }, id: ["catch-before-Activity_0wc2mcq"]},
"⏸︎ Create " => {guard: ->(ctx, process_model:, **) { true }, 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"]},
"⏸︎ Create form " => {guard: ->(ctx, model: nil, **) { model.nil? }, id: ["catch-before-Activity_0wc2mcq"]},
"⏸︎ Create " => {guard: ->(ctx, model: nil, **) { model.nil? }, id: ["catch-before-Activity_1psp91r"]},
"⏸︎ Update form♦Notify approver" => {guard: ->(ctx, model:, **) { model }, id: ["catch-before-Activity_1165bw9", "catch-before-Activity_1dt5di5"]},
"⏸︎ Update " => {guard: ->(ctx, model:, **) { model }, id: ["catch-before-Activity_0j78uzd"]},
"⏸︎ 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"]},
Expand Down Expand Up @@ -49,7 +49,7 @@ class AdvanceTest < Minitest::Spec




=begin
ctx = {params: [], seq: [], process_model: nil}
# TODO: this should be suitable to be dropped into an endpoint.
Expand All @@ -76,5 +76,87 @@ class AdvanceTest < Minitest::Spec
)
assert_equal signal.inspect, %(Trailblazer::Activity::Left)
=end
Posting = Struct.new(:id) do
def self.find_by(id:)
new(id)
end
end

require "trailblazer/endpoint"
require "trailblazer/macro/model/find"

protocol_template = Class.new(Trailblazer::Endpoint::Protocol) do
# step Trailblazer::Activity::Railway::Model::Find(Posting, find_by: :id, not_found_terminus: true), after: :authenticate # FIXME: why do we need FQ constant?

def authenticate(*)
true
end

def policy(*)
true
end
end

protocol_template_with_model = Class.new(protocol_template) do
step Trailblazer::Activity::Railway::Model::Find(Posting, find_by: :id, not_found_terminus: true), after: :authenticate # FIXME: why do we need FQ constant?
end



advance_protocol = Trailblazer::Endpoint.build_protocol(protocol_template, domain_activity: Trailblazer::Workflow::Advance,
protocol_block: ->(*) { {Trailblazer::Activity::Railway.Output(:not_authorized) => Trailblazer::Activity::Railway.Track(:not_authorized)} }
)

advance_protocol_with_model = Trailblazer::Endpoint.build_protocol(protocol_template_with_model, domain_activity: Trailblazer::Workflow::Advance,
protocol_block: ->(*) { {Trailblazer::Activity::Railway.Output(:not_authorized) => Trailblazer::Activity::Railway.Track(:not_authorized)} }
)

# action_protocol = Trailblazer::Endpoint.build_protocol(Protocol, domain_activity: Create, protocol_block: ->(*) { {Output(:not_found) => Track(:not_found)} })
action_adapter = Trailblazer::Endpoint::Adapter.build(advance_protocol) # build the simplest Adapter we got.
action_adapter_with_model = Trailblazer::Endpoint::Adapter.build(advance_protocol_with_model) # build the simplest Adapter we got.

ctx = {
}


original_flow_options = {
event_label: "☝ ⏵︎Create",
**schema.to_h,
iteration_set: iteration_set,
state_guards: state_guards,
}

default_matcher = Trailblazer::Endpoint::Matcher.new(
success: ->(*) { raise },
)
matcher_block = -> do
success { |ctx, seq:, **| render seq.inspect }
failure { |ctx, model:, **| render "failed: #{model}" }
not_found { |ctx, params:, **| render "404 not found: #{params[:id]}" }
end

def render(text)
@render = text
end

# Create
flow_options = original_flow_options.merge(event_label: "☝ ⏵︎Create",)
Trailblazer::Endpoint::Runtime.({params: {}, seq: []}, adapter: action_adapter, default_matcher: default_matcher, matcher_context: self, flow_options: flow_options, &matcher_block)
assert_equal @render, %([:ui_create, :create])

# Update
flow_options = original_flow_options.merge(event_label: "☝ ⏵︎Update",)
Trailblazer::Endpoint::Runtime.({params: {id: 1}, seq: []}, adapter: action_adapter_with_model, default_matcher: default_matcher, matcher_context: self, flow_options: flow_options, &matcher_block)
assert_equal @render, %([:ui_update, :update])

# Update doesn't find model
flow_options = original_flow_options.merge(event_label: "☝ ⏵︎Update",)
Trailblazer::Endpoint::Runtime.({params: {}, seq: []}, adapter: action_adapter_with_model, default_matcher: default_matcher, matcher_context: self, flow_options: flow_options, &matcher_block)
assert_equal @render, %(404 not found: )

# advance "☝ ⏵︎Create" do |ctx|

# end.Or() do |ctx|
end
end

0 comments on commit ec540ad

Please sign in to comment.