@@ -19,46 +19,49 @@ def run(input)
1919
2020 describe "#run" do
2121 context "with constructor-based configuration" do
22- let ( :short_step ) do
23- Class . new ( MARS ::Runnable ) do
24- def run ( input )
25- "short: #{ input } "
26- end
27- end . new
28- end
22+ it "passes through when condition returns falsy" do
23+ gate = described_class . new (
24+ "PassGate" ,
25+ condition : -> ( _input ) { } ,
26+ branches : { fail : fallback_step }
27+ )
2928
30- let ( :long_step ) do
31- Class . new ( MARS ::Runnable ) do
32- def run ( input )
33- "long: #{ input } "
34- end
35- end . new
29+ expect ( gate . run ( "hello" ) ) . to eq ( "hello" )
3630 end
3731
38- let ( :gate ) do
39- described_class . new (
40- "LengthGate " ,
41- condition : -> ( input ) { input . length > 5 ? :long : :short } ,
42- branches : { short : short_step , long : long_step }
32+ it "halts with branch result when condition returns a key" do
33+ gate = described_class . new (
34+ "FailGate " ,
35+ condition : -> ( _input ) { :fail } ,
36+ branches : { fail : fallback_step }
4337 )
44- end
4538
46- it "executes the matched branch directly" do
47- expect ( gate . run ( "hi" ) ) . to eq ( "short: hi" )
39+ result = gate . run ( "hello" )
40+ expect ( result ) . to be_a ( MARS ::Halt )
41+ expect ( result . result ) . to eq ( "fallback: hello" )
4842 end
4943
50- it "executes the other branch for different input" do
51- expect ( gate . run ( "longstring" ) ) . to eq ( "long: longstring" )
44+ it "raises when condition returns an unregistered key" do
45+ gate = described_class . new (
46+ "BadGate" ,
47+ condition : -> ( _input ) { :unknown } ,
48+ branches : { fail : fallback_step }
49+ )
50+
51+ expect { gate . run ( "hello" ) } . to raise_error ( ArgumentError , /No branch registered for :unknown/ )
5252 end
5353
54- it "returns input when no branch matches " do
54+ it "selects among multiple branches " do
5555 gate = described_class . new (
56- "NoMatch " ,
57- condition : -> ( _input ) { :unknown } ,
58- branches : { short : short_step }
56+ "MultiBranch " ,
57+ condition : -> ( input ) { input [ :error_type ] } ,
58+ branches : { timeout : fallback_step , auth : error_step }
5959 )
6060
61- expect ( gate . run ( "hello" ) ) . to eq ( "hello" )
61+ input = { error_type : :auth }
62+ result = gate . run ( input )
63+ expect ( result ) . to be_a ( MARS ::Halt )
64+ expect ( result . result ) . to eq ( "error: #{ input } " )
6265 end
6366 end
6467
@@ -90,8 +93,46 @@ def run(input)
9093 end
9194
9295 gate = gate_class . new ( "DSLGate" )
93- expect ( gate . run ( "hi" ) ) . to eq ( "quick: hi" )
94- expect ( gate . run ( "longstring" ) ) . to eq ( "deep: longstring" )
96+ expect ( gate . run ( "hi" ) . result ) . to eq ( "quick: hi" )
97+ expect ( gate . run ( "longstring" ) . result ) . to eq ( "deep: longstring" )
98+ end
99+
100+ it "supports halt_scope DSL" do
101+ cls = short_step_class
102+ gate_class = Class . new ( described_class ) do
103+ condition { |_input | :fail }
104+ branch :fail , cls
105+ halt_scope :global
106+ end
107+
108+ result = gate_class . new ( "GlobalGate" ) . run ( "test" )
109+ expect ( result ) . to be_a ( MARS ::Halt )
110+ expect ( result ) . to be_global
111+ end
112+ end
113+
114+ context "with halt scope" do
115+ it "defaults to local scope" do
116+ gate = described_class . new (
117+ "LocalGate" ,
118+ condition : -> ( _input ) { :fail } ,
119+ branches : { fail : fallback_step }
120+ )
121+
122+ result = gate . run ( "hello" )
123+ expect ( result ) . to be_local
124+ end
125+
126+ it "respects constructor halt_scope" do
127+ gate = described_class . new (
128+ "GlobalGate" ,
129+ condition : -> ( _input ) { :fail } ,
130+ branches : { fail : fallback_step } ,
131+ halt_scope : :global
132+ )
133+
134+ result = gate . run ( "hello" )
135+ expect ( result ) . to be_global
95136 end
96137 end
97138
@@ -123,15 +164,15 @@ def run(input)
123164 end
124165
125166 it "routes to low branch" do
126- expect ( gate . run ( 5 ) ) . to eq ( "low:5" )
167+ expect ( gate . run ( 5 ) . result ) . to eq ( "low:5" )
127168 end
128169
129170 it "routes to medium branch" do
130- expect ( gate . run ( 25 ) ) . to eq ( "med:25" )
171+ expect ( gate . run ( 25 ) . result ) . to eq ( "med:25" )
131172 end
132173
133174 it "routes to high branch" do
134- expect ( gate . run ( 100 ) ) . to eq ( "high:100" )
175+ expect ( gate . run ( 100 ) . result ) . to eq ( "high:100" )
135176 end
136177 end
137178 end
0 commit comments