Class: RubyReactor::RSpec::TestSubject
- Inherits:
-
Object
- Object
- RubyReactor::RSpec::TestSubject
- Includes:
- RSpec::Mocks::ExampleMethods
- Defined in:
- lib/ruby_reactor/rspec/test_subject.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: StepProxy
Instance Attribute Summary collapse
-
#reactor_instance ⇒ Object
readonly
Returns the value of attribute reactor_instance.
-
#run_result ⇒ Object
readonly
Returns the value of attribute run_result.
Instance Method Summary collapse
-
#composed(step_name) ⇒ Object
(also: #compose)
Fluent API for mocking nested compose steps.
-
#current_step ⇒ Symbol?
Get the current step where the reactor is paused (interrupt step) Note: When multiple interrupts are ready, this returns just one of them.
- #ensure_executed! ⇒ Object
- #error ⇒ Object
-
#failing_at(step_name, *nested_steps, element_index: nil, &block) ⇒ Object
--- Configuration DSL ---.
- #failure? ⇒ Boolean
-
#initialize(reactor_class:, inputs:, context: {}, async: nil, process_jobs: true) ⇒ TestSubject
constructor
A new instance of TestSubject.
-
#map(step_name) ⇒ Object
Fluent API for mocking nested map steps.
- #map_element(step_name, index: 0) ⇒ Object
-
#map_elements(step_name) ⇒ Object
--- Traversal ---.
-
#mock_step(step_name, *nested_steps, element_index: nil) {|args, context, original_impl| ... } ⇒ Object
Intercept a step and provide a custom implementation.
-
#paused? ⇒ Boolean
Check if the reactor is paused at an interrupt.
-
#ready_interrupt_steps ⇒ Array<Symbol>
Get all ready interrupt steps (steps that can be resumed) This is useful when multiple interrupts are waiting concurrently.
-
#result ⇒ Object
--- Introspection (Auto-Run) ---.
-
#resume(payload: {}, step: nil) ⇒ TestSubject
Resume a paused reactor with the given payload.
-
#run ⇒ Object
--- Execution ---.
- #run_async(boolean) ⇒ Object
-
#skipped_result(ctx) ⇒ Object
A clean halt: either a
with_periodgate or a step returningRubyReactor.Skipped(...). - #step_result(name) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(reactor_class:, inputs:, context: {}, async: nil, process_jobs: true) ⇒ TestSubject
Returns a new instance of TestSubject.
11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 11 def initialize(reactor_class:, inputs:, context: {}, async: nil, process_jobs: true) @reactor_class = reactor_class @inputs = inputs @context_data = context @async = async @process_jobs = process_jobs @interceptors = [] @executed = false end |
Instance Attribute Details
#reactor_instance ⇒ Object (readonly)
Returns the value of attribute reactor_instance.
9 10 11 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 9 def reactor_instance @reactor_instance end |
#run_result ⇒ Object (readonly)
Returns the value of attribute run_result.
9 10 11 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 9 def run_result @run_result end |
Instance Method Details
#composed(step_name) ⇒ Object Also known as: compose
Fluent API for mocking nested compose steps
61 62 63 64 65 66 67 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 61 def composed(step_name) # If already executed, return the traversed subject return traverse_composed(step_name) if @executed # Otherwise return a configuration proxy StepProxy.new(self, step_name) end |
#current_step ⇒ Symbol?
Get the current step where the reactor is paused (interrupt step)
Note: When multiple interrupts are ready, this returns just one of them.
Use ready_interrupt_steps to get all ready interrupt steps.
314 315 316 317 318 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 314 def current_step ensure_executed! step = @reactor_instance.context.current_step step&.to_sym end |
#ensure_executed! ⇒ Object
425 426 427 428 429 430 431 432 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 425 def ensure_executed! run unless @executed # Process jobs if status is running and processing is enabled return unless @process_jobs && @reactor_instance.context.status.to_s == "running" process_pending_jobs end |
#error ⇒ Object
420 421 422 423 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 420 def error res = result res.respond_to?(:error) ? res.error : nil end |
#failing_at(step_name, *nested_steps, element_index: nil, &block) ⇒ Object
--- Configuration DSL ---
23 24 25 26 27 28 29 30 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 23 def failing_at(step_name, *nested_steps, element_index: nil, &block) @interceptors << { type: :failure, step_path: [step_name, *nested_steps], conditions: { element_index: element_index, block: block } } self end |
#failure? ⇒ Boolean
294 295 296 297 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 294 def failure? ensure_executed! @reactor_instance.context.status.to_s == "failed" end |
#map(step_name) ⇒ Object
Fluent API for mocking nested map steps
54 55 56 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 54 def map(step_name) StepProxy.new(self, step_name) end |
#map_element(step_name, index: 0) ⇒ Object
128 129 130 131 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 128 def map_element(step_name, index: 0) elements = map_elements(step_name) elements[index] end |
#map_elements(step_name) ⇒ Object
--- Traversal ---
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 94 def map_elements(step_name) ensure_executed! # Check composed_contexts entry = @reactor_instance.context.composed_contexts[step_name] || @reactor_instance.context.composed_contexts[step_name.to_s] || @reactor_instance.context.composed_contexts[step_name.to_sym] return [] unless entry && entry[:type] == :map_ref map_id = entry[:map_id] storage = RubyReactor.configuration.storage_adapter # This requires the storage adapter to implement retrieval of map element context IDs # If it's not implemented in MemoryAdapter (if used), we might need to fallback? # But tests use Redis. child_ids = storage.retrieve_map_element_context_ids(map_id, @reactor_instance.class.name) child_ids.map do |id| klass = RubyReactor::Context.resolve_reactor_class(entry[:element_reactor_class]) child_instance = klass.find(id) self.class.new( reactor_class: child_instance.class, inputs: child_instance.context.inputs, context: child_instance.context, async: @async, process_jobs: @process_jobs ).tap do |s| s.instance_variable_set(:@executed, true) s.instance_variable_set(:@reactor_instance, child_instance) end end end |
#mock_step(step_name, *nested_steps, element_index: nil) {|args, context, original_impl| ... } ⇒ Object
Intercept a step and provide a custom implementation
42 43 44 45 46 47 48 49 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 42 def mock_step(step_name, *nested_steps, element_index: nil, &block) @interceptors << { type: :mock, step_path: [step_name, *nested_steps], conditions: { element_index: element_index, block: block } } self end |
#paused? ⇒ Boolean
Check if the reactor is paused at an interrupt
304 305 306 307 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 304 def paused? ensure_executed! @reactor_instance.context.status.to_s == "paused" end |
#ready_interrupt_steps ⇒ Array<Symbol>
Get all ready interrupt steps (steps that can be resumed) This is useful when multiple interrupts are waiting concurrently.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 324 def ready_interrupt_steps ensure_executed! return [] unless paused? # Build the dependency graph and get ready steps graph = RubyReactor::DependencyGraph.new graph_manager = RubyReactor::Executor::GraphManager.new( @reactor_class, graph, @reactor_instance.context ) graph_manager.build_and_validate! graph_manager.mark_completed_steps_from_context # Filter to only interrupt steps (using interrupt? predicate method) ready = graph_manager.dependency_graph.ready_steps ready.select { |step_config| step_config.respond_to?(:interrupt?) && step_config.interrupt? } .map { |step_config| step_config.name.to_sym } end |
#result ⇒ Object
--- Introspection (Auto-Run) ---
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 227 def result ensure_executed! ctx = @reactor_instance.context status = ctx.status.to_s case status when "failed" return ctx.failure_reason if ctx.failure_reason.is_a?(RubyReactor::Failure) RubyReactor::Failure.new(ctx.failure_reason || {}) when "completed" # Determine the success value val = if @reactor_class.return_step ctx.intermediate_results[@reactor_class.return_step.to_sym] else # Return result of the last executed step # Execution trace contains: { step: name, ... } # Trace does not strictly contain result, so we look up in intermediate_results last_trace = ctx.execution_trace.last if last_trace step_name = last_trace[:step] # Handle symbol/string mismatch ctx.intermediate_results[step_name.to_sym] || ctx.intermediate_results[step_name.to_s] end end RubyReactor::Success.new(val) when "skipped" skipped_result(ctx) when "running" # Try to determine if it is truly running or if we just missed the completion if @process_jobs && AsyncTestHelpers.active? # Force one more check process_pending_jobs # Reload status @reactor_instance = @reactor_class.find(@reactor_instance.context.context_id) return result unless @reactor_instance.context.status.to_s == "running" end # If still running, return a Pending/Running result instead of nil # This allows matchers to report "expected success but was running" RubyReactor::Failure("Reactor is still running (Async operations pending?)", retryable: true) when "paused" RubyReactor::InterruptResult.new( execution_id: ctx.context_id, intermediate_results: ctx.intermediate_results # We assume no error if paused normally ) end end |
#resume(payload: {}, step: nil) ⇒ TestSubject
Resume a paused reactor with the given payload
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 350 def resume(payload: {}, step: nil) ensure_executed! unless paused? raise RubyReactor::Error::ValidationError, "Cannot resume: reactor is not paused (status: #{@reactor_instance.context.status})" end step_name = determine_resume_step(step) # Use the reactor's continue method @reactor_instance.continue(payload: payload, step_name: step_name) # Process any pending async jobs process_pending_jobs if @process_jobs && AsyncTestHelpers.active? # Reload the reactor instance to get updated state @reactor_instance = @reactor_class.find(@reactor_instance.context.context_id) # Return self for chaining and introspection self end |
#run ⇒ Object
--- Execution ---
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 171 def run return self if @executed # 1. Apply Interceptors (Dynamic Subclassing) execution_class = prepare_execution_class # 2. Capture Context ID captured_context_id = nil allow(RubyReactor::Context).to receive(:new).and_wrap_original do |m, *args| ctx = m.call(*args) captured_context_id ||= ctx.context_id ctx end # 3. Native Run if @async == false allow(execution_class).to receive(:async?).and_return(false) elsif @async == true allow(execution_class).to receive(:async?).and_return(true) end @run_result = nil if @process_jobs && AsyncTestHelpers.sidekiq_testing? # Ensure the Sidekiq router is used to capture jobs in fake mode allow(RubyReactor.configuration).to receive(:async_router).and_return(RubyReactor::Adapters::Sidekiq::Router) # Avoid nesting error which happens in Sidekiq 7+ if a mode is already set begin Sidekiq::Testing.fake! do @run_result = execution_class.run(@inputs) end rescue Sidekiq::Testing::TestModeAlreadySetError @run_result = execution_class.run(@inputs) end elsif @process_jobs && AsyncTestHelpers.active_job_testing? # Ensure the ActiveJob router is used to capture jobs in the :test adapter allow(RubyReactor.configuration).to receive(:async_router).and_return(RubyReactor::Adapters::ActiveJob::Router) @run_result = execution_class.run(@inputs) else @run_result = execution_class.run(@inputs) end # 4. Reload raise "Could not capture context ID during execution" unless captured_context_id # Reload using the execution class (which might be the mocked subclass with unique name) @reactor_instance = execution_class.find(captured_context_id) # Update our reference to the class so future reloads (e.g. in result introspection) work @reactor_class = execution_class @executed = true self end |
#run_async(boolean) ⇒ Object
164 165 166 167 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 164 def run_async(boolean) @async = boolean self end |
#skipped_result(ctx) ⇒ Object
A clean halt: either a with_period gate or a step returning
RubyReactor.Skipped(...). The sync run already produced the exact
Skipped (reason/step intact) — surface it. For async runs the worker
swallows the return value, so rebuild from the trace.
282 283 284 285 286 287 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 282 def skipped_result(ctx) return @run_result if @run_result.is_a?(RubyReactor::Skipped) entry = ctx.execution_trace.reverse.find { |t| t[:type].to_s == "skipped" } RubyReactor::Skipped.new(reason: entry&.dig(:reason), step_name: entry&.dig(:step)) end |
#step_result(name) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 402 def step_result(name) ensure_executed! # Prefer intermediate_results as it is the data store # Logic to handle symbol vs string mismatch key_sym = name.to_sym key_str = name.to_s if @reactor_instance.context.intermediate_results.key?(key_sym) @reactor_instance.context.intermediate_results[key_sym] elsif @reactor_instance.context.intermediate_results.key?(key_str) @reactor_instance.context.intermediate_results[key_str] else # Fallback to execution trace if available (e.g. for inspection) entry = @reactor_instance.context.execution_trace.find { |t| t[:step].to_s == key_str } entry ? entry[:result] : nil end end |
#success? ⇒ Boolean
289 290 291 292 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 289 def success? ensure_executed! @reactor_instance.context.status.to_s == "completed" end |