Prohlížeč zdrojového kódu

spec/services/scenario_runner_spec.rb

require "rails_helper"
RSpec.describe ScenarioRunner do
let(:fixture_path) { Rails.root.join("spec/fixtures/examples") }
describe ".run" do
it "executes example code and returns success result" do
result = ScenarioRunner.run(
source_file: fixture_path.join("test_example/test_example.rb").to_s,
entry_class: "TestExample",
entry_method: "greet",
inputs: { name: "Alice" }
)
expect(result).to be_success
expect(result.output).to eq("Hello, Alice!")
expect(result.duration).to be_a(Float)
end
it "returns error result for exceptions" do
result = ScenarioRunner.run(
source_file: fixture_path.join("test_example/test_example.rb").to_s,
entry_class: "TestExample",
entry_method: "nonexistent"
)
expect(result).not_to be_success
expect(result.error).to include("NoMethodError")
end
it "times out long-running code" do
stub_const("ScenarioRunner::TIMEOUT_SECONDS", 0.1)
result = ScenarioRunner.run(
source_file: fixture_path.join("slow_example/slow_example.rb").to_s,
entry_class: "SlowExample",
entry_method: "run"
)
expect(result).not_to be_success
expect(result.error).to include("časový limit")
end
it "isolates code in anonymous module" do
ScenarioRunner.run(
source_file: fixture_path.join("test_example/test_example.rb").to_s,
entry_class: "TestExample",
entry_method: "greet",
inputs: { name: "Bob" }
)
# The class should not leak into the top-level namespace
expect(Object.const_defined?(:TestExample)).to be false
end
it "works without inputs" do
result = ScenarioRunner.run(
source_file: fixture_path.join("slow_example/slow_example.rb").to_s,
entry_class: "SlowExample",
entry_method: "run"
)
# Will timeout since no inputs needed but code sleeps
# Just checking it doesn't blow up on empty inputs
expect(result).to be_a(ScenarioRunner::Result)
end
end
end