Skip to content

Commit

Permalink
Wtf.call returns the ctx and not positional args.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Jun 3, 2024
1 parent 8a21668 commit 0d58faa
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/trailblazer/pro/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Pro
def self.invoke_debugger(**kws)
_, (ctx, _) = Activity::TaskWrap.invoke(Debugger, [{now: DateTime.now, **kws, output: []}, {}])

return ctx[:output], [ctx[:session], ctx[:id], ctx[:debugger_url], ctx[:data_to_store], ctx[:session_updated]]
return ctx[:output], ctx#[:session], ctx[:id], ctx[:debugger_url], ctx[:data_to_store], ctx[:session_updated]]
end

# This is the {:render_method} implementation (for Trace::Present)
Expand Down
4 changes: 3 additions & 1 deletion lib/trailblazer/pro/trace/wtf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def call(*args, present_options: {}, **options)
**options
)

(session, _trace_id, _debugger_url, _trace_envelope, session_updated) = returned[-1]
returned_ctx = returned[-1]

session, session_updated = returned_ctx[:session], returned_ctx[:session_updated]

update_session!(session) if session_updated # DISCUSS: this is a hook for pro-rails, not a massive fan.

Expand Down
12 changes: 8 additions & 4 deletions test/debugger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def self.call(ctx, **)
assert_equal ctx[:session].trailblazer_pro_host, "https://testbackend-pro.trb.to"
end

it "what" do
it "{Pro.invoke_debugger}" do
class Create < Trailblazer::Activity::Railway
step :model

Expand All @@ -138,8 +138,9 @@ def model(ctx, **)
debugger_trace = returned_args

# NOTE: this tests private internals and hence looks a bit clumsy (especially retrieving the {debugger_trace}).
signal, (ctx, _) = Trailblazer::Developer.wtf?(Trailblazer::Pro::Debugger, [
{
# signal, (ctx, _) = Trailblazer::Developer.wtf?(Trailblazer::Pro::Debugger, [
output, ctx = Trailblazer::Pro.invoke_debugger(
**{
debugger_trace: debugger_trace,
activity: Create,
renderer: Trailblazer::Developer::Trace::Present.method(:default_renderer),
Expand All @@ -149,7 +150,10 @@ def model(ctx, **)
# http: stubbed_http,
# data_to_store: {fields: {a: 1}},
# firestore_fields_template: session_static_options[:firestore_fields_template],
}, {}])
}
)

assert_equal output.split(".to")[0], %(\e[1m[TRB PRO] view trace (DebuggerAPITest::Create) at \e[22mhttps://ide.trailblazer)

assert_equal ctx[:session].class, Trailblazer::Pro::Session # initialized session.
assert_equal ctx[:session_updated], true
Expand Down
15 changes: 8 additions & 7 deletions test/present_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def model(ctx, **)
it "with {render_wtf: true}" do
ctx = {}

signal, (ctx, _), _, output, (token, trace_id, debugger_url, trace_envelope) = Trailblazer::Developer.wtf?(
signal, (ctx, _), _, output, returned_ctx = Trailblazer::Developer.wtf?(
Create,
[ctx, {}],
present_options: {render_method: Trailblazer::Pro.method(:invoke_debugger), session: uninitialized_session, render_wtf: true}, # FIXME: why do we have to pass {:session} here?
Expand All @@ -24,34 +24,35 @@ def model(ctx, **)
|-- \e[32mStart.default\e[0m
|-- \e[32mmodel\e[0m
`-- End.success
\e[1m[TRB PRO] view trace (PresentOptionsTest::Create) at \e[22mhttps://ide.trailblazer.to/#{trace_id})
\e[1m[TRB PRO] view trace (PresentOptionsTest::Create) at \e[22mhttps://ide.trailblazer.to/#{returned_ctx[:id]})
end

it "with {render_wtf: false}" do
ctx = {}

signal, (ctx, _), _, output, (token, trace_id, debugger_url, trace_envelope) = Trailblazer::Developer.wtf?(
signal, (ctx, _), _, output, returned_ctx = Trailblazer::Developer.wtf?(
Create,
[ctx, {}],
present_options: {render_method: Trailblazer::Pro.method(:invoke_debugger), session: uninitialized_session, render_wtf: false}, # FIXME: why do we have to pass {:session} here?
)

assert_equal output, %(\e[1m[TRB PRO] view trace (PresentOptionsTest::Create) at \e[22mhttps://ide.trailblazer.to/#{trace_id})
assert_equal output, %(\e[1m[TRB PRO] view trace (PresentOptionsTest::Create) at \e[22mhttps://ide.trailblazer.to/#{returned_ctx[:id]})
end

# test if trace has expected elements
it "returned trace data is correct" do
ctx = {}

signal, (ctx, _), _, output, (token, trace_id, debugger_url, trace_envelope) = Trailblazer::Developer.wtf?(
signal, (ctx, _), _, output, returned_ctx = Trailblazer::Developer.wtf?(
Create,
[ctx, {}],
present_options: {render_method: Trailblazer::Pro.method(:invoke_debugger), session: uninitialized_session},
)

assert_equal trace_id.size, 20
assert_equal debugger_url, "https://ide.trailblazer.to/#{trace_id}"
assert_equal returned_ctx[:id].size, 20
assert_equal returned_ctx[:debugger_url], "https://ide.trailblazer.to/#{returned_ctx[:id]}"

trace_envelope = returned_ctx[:data_to_store]
assert_equal trace_envelope[:fields].keys, [:activity_name, :trace, :created_at]
assert_equal trace_envelope[:fields][:activity_name], {:stringValue=>PresentOptionsTest::Create}
assert trace_envelope[:fields][:created_at][:timestampValue] < DateTime.now
Expand Down
37 changes: 30 additions & 7 deletions test/wtf_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def model(ctx, **)
# Uninitialized session.
assert_equal Trailblazer::Pro::Session.session.to_h, {api_key: api_key, trailblazer_pro_host: trailblazer_pro_host}

signal, (ctx, _), _, output, (session, trace_id, debugger_url, _trace_envelope) = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])
signal, (ctx, _), _, output, returned_ctx = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])

trace_id = returned_ctx[:id]
session = returned_ctx[:session]
debugger_url = returned_ctx[:debugger_url]

assert_equal trace_id.size, 20
assert_equal debugger_url, "https://ide.trailblazer.to/#{trace_id}"
Expand All @@ -38,7 +41,11 @@ def model(ctx, **)
session_1_hash = assert_session({session: session}, **session_static_options)

#@ while session is valid, do another call.
signal, (ctx, _), _, output, (session_2, trace_id_2, debugger_url_2, _trace_envelope) = Trailblazer::Pro::Trace::Wtf.call(Create, [ctx, {}])
signal, (ctx, _), _, output, returned_ctx_2 = Trailblazer::Pro::Trace::Wtf.call(Create, [ctx, {}])

trace_id_2 = returned_ctx_2[:id]
session_2 = returned_ctx_2[:session]
debugger_url_2 = returned_ctx_2[:debugger_url]

assert_equal trace_id_2.size, 20
assert_equal debugger_url_2, "https://ide.trailblazer.to/#{trace_id_2}"
Expand All @@ -50,9 +57,13 @@ def model(ctx, **)
assert_equal session_1_hash[:id_token], session_2_hash[:id_token]

#@ simulate time out, new token required.
signal, (ctx, _), _, output, (session_3, trace_id_3, debugger_url_3, _trace_envelope) =
signal, (ctx, _), _, output, returned_ctx_3 =
Trailblazer::Pro::Trace::Wtf.call(Create, [ctx, {}], present_options: {now: DateTime.now + (60 * 1000)})

trace_id_3 = returned_ctx_3[:id]
session_3 = returned_ctx_3[:session]
debugger_url_3 = returned_ctx_3[:debugger_url]

assert_equal trace_id_3.size, 20
assert_equal debugger_url_3, "https://ide.trailblazer.to/#{trace_id_3}"
assert trace_id != trace_id_3
Expand All @@ -73,7 +84,11 @@ def model(ctx, **)

Trailblazer::Pro.initialize!(**session_4)

signal, (ctx, _), _, output, (session_5, trace_id_5, debugger_url_5, _trace_envelope) = Trailblazer::Pro::Trace::Wtf.call(Create, [ctx, {}])
signal, (ctx, _), _, output, returned_ctx_5 = Trailblazer::Pro::Trace::Wtf.call(Create, [ctx, {}])

trace_id_5 = returned_ctx_5[:id]
session_5 = returned_ctx_5[:session]
debugger_url_5 = returned_ctx_5[:debugger_url]

assert_equal trace_id_5.size, 20
assert_equal debugger_url_5, "https://ide.trailblazer.to/#{trace_id_5}"
Expand All @@ -91,7 +106,11 @@ def model(ctx, **)
render_wtf: false,
)

signal, (ctx, _), _, output, (session, trace_id, debugger_url, _trace_envelope) = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])
signal, (ctx, _), _, output, returned_ctx = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])

trace_id = returned_ctx[:id]
session = returned_ctx[:session]
debugger_url = returned_ctx[:debugger_url]

assert_equal output, %(\e[1m[TRB PRO] view trace (WtfTest::Create) at \e[22mhttps://ide.trailblazer.to/#{trace_id})
assert_equal trace_id.size, 20
Expand All @@ -105,7 +124,11 @@ def model(ctx, **)
render_wtf: true,
)

signal, (ctx, _), _, output, (session, trace_id, debugger_url, _trace_envelope) = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])
signal, (ctx, _), _, output, returned_ctx = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])

trace_id = returned_ctx[:id]
session = returned_ctx[:session]
debugger_url = returned_ctx[:debugger_url]

assert_equal output, %(WtfTest::Create
|-- \e[32mStart.default\e[0m
Expand All @@ -124,7 +147,7 @@ def model(ctx, **)
# render_wtf: false,
)

signal, (ctx, _), _, output, (session, trace_id, debugger_url, _trace_envelope) = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])
signal, (ctx, _), _, output, _returned_ctx = Trailblazer::Pro::Trace::Wtf.call(Create, [{}, {}])

assert_equal output, %(WtfTest::Create
|-- \e[32mStart.default\e[0m
Expand Down

0 comments on commit 0d58faa

Please sign in to comment.