@@ -122,6 +122,163 @@ def test_before_hooks_merges_evaluation_contexts():
122122 assert context == EvaluationContext ("bar" , {"key_1" : "val_1" , "key_2" : "val_2" })
123123
124124
125+ def test_before_hooks_propagates_context_to_subsequent_hook ():
126+ """Requirement 4.3.4 / Issue #628: context returned by a before hook must reach the next hook."""
127+ # Given
128+ initial_context = EvaluationContext (attributes = {"initial" : "present" })
129+ hook_context_a = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
130+ hook_context_b = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
131+
132+ received_by_hook_b : list [EvaluationContext ] = []
133+
134+ hook_a = MagicMock (spec = Hook )
135+ hook_a .before .return_value = EvaluationContext (
136+ attributes = {"from_hook_a" : "visible" }
137+ )
138+
139+ hook_b = MagicMock (spec = Hook )
140+
141+ def hook_b_before (hook_context , hints ):
142+ received_by_hook_b .append (hook_context .evaluation_context )
143+ return None
144+
145+ hook_b .before .side_effect = hook_b_before
146+
147+ # When
148+ before_hooks (
149+ FlagType .BOOLEAN ,
150+ [(hook_a , hook_context_a ), (hook_b , hook_context_b )],
151+ )
152+
153+ # Then
154+ assert len (received_by_hook_b ) == 1
155+ assert received_by_hook_b [0 ].attributes .get ("from_hook_a" ) == "visible" , (
156+ "Hook B did not receive the evaluation context returned by Hook A"
157+ )
158+ assert received_by_hook_b [0 ].attributes .get ("initial" ) == "present"
159+
160+
161+ def test_before_hooks_accumulates_context_across_three_hooks ():
162+ """Requirement 4.3.4 / Issue #628: Hook C must see context returned by both A and B."""
163+ # Given
164+ initial_context = EvaluationContext (attributes = {"initial" : "present" })
165+ ctx_a = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
166+ ctx_b = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
167+ ctx_c = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
168+
169+ received_by_hook_b : list [EvaluationContext ] = []
170+ received_by_hook_c : list [EvaluationContext ] = []
171+
172+ hook_a = MagicMock (spec = Hook )
173+ hook_a .before .return_value = EvaluationContext (attributes = {"from_a" : "A" })
174+
175+ hook_b = MagicMock (spec = Hook )
176+
177+ def hook_b_before (hook_context , hints ):
178+ received_by_hook_b .append (hook_context .evaluation_context )
179+ return EvaluationContext (attributes = {"from_b" : "B" })
180+
181+ hook_b .before .side_effect = hook_b_before
182+
183+ hook_c = MagicMock (spec = Hook )
184+
185+ def hook_c_before (hook_context , hints ):
186+ received_by_hook_c .append (hook_context .evaluation_context )
187+ return None
188+
189+ hook_c .before .side_effect = hook_c_before
190+
191+ # When
192+ before_hooks (
193+ FlagType .BOOLEAN ,
194+ [(hook_a , ctx_a ), (hook_b , ctx_b ), (hook_c , ctx_c )],
195+ )
196+
197+ # Then
198+ assert received_by_hook_b [0 ].attributes .get ("from_a" ) == "A" , (
199+ "Hook B did not receive the evaluation context returned by Hook A"
200+ )
201+ assert received_by_hook_c [0 ].attributes .get ("from_a" ) == "A" , (
202+ "Hook C did not receive the evaluation context returned by Hook A"
203+ )
204+ assert received_by_hook_c [0 ].attributes .get ("from_b" ) == "B" , (
205+ "Hook C did not receive the evaluation context returned by Hook B"
206+ )
207+
208+
209+ def test_before_hooks_later_hook_overrides_earlier_on_conflict ():
210+ """Later hook results take precedence over earlier hook results on conflicting attributes."""
211+ # Given
212+ initial_context = EvaluationContext (attributes = {"initial" : "present" })
213+ ctx_a = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
214+ ctx_b = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
215+ ctx_c = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
216+
217+ received_by_hook_c : list [EvaluationContext ] = []
218+
219+ hook_a = MagicMock (spec = Hook )
220+ hook_a .before .return_value = EvaluationContext (attributes = {"shared" : "A" })
221+
222+ hook_b = MagicMock (spec = Hook )
223+ hook_b .before .return_value = EvaluationContext (attributes = {"shared" : "B" })
224+
225+ hook_c = MagicMock (spec = Hook )
226+
227+ def hook_c_before (hook_context , hints ):
228+ received_by_hook_c .append (hook_context .evaluation_context )
229+ return None
230+
231+ hook_c .before .side_effect = hook_c_before
232+
233+ # When
234+ before_hooks (
235+ FlagType .BOOLEAN ,
236+ [(hook_a , ctx_a ), (hook_b , ctx_b ), (hook_c , ctx_c )],
237+ )
238+
239+ # Then
240+ assert received_by_hook_c [0 ].attributes .get ("shared" ) == "B" , (
241+ "Later hook (B) result should override earlier hook (A) result for the same attribute"
242+ )
243+
244+
245+ def test_before_hooks_none_result_does_not_corrupt_accumulated_context ():
246+ """A hook returning None must not wipe the accumulated context for subsequent hooks."""
247+ # Given
248+ initial_context = EvaluationContext (attributes = {"initial" : "present" })
249+ ctx_a = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
250+ ctx_b = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
251+ ctx_c = HookContext ("flag_key" , FlagType .BOOLEAN , True , initial_context )
252+
253+ received_by_hook_c : list [EvaluationContext ] = []
254+
255+ hook_a = MagicMock (spec = Hook )
256+ hook_a .before .return_value = EvaluationContext (attributes = {"from_a" : "A" })
257+
258+ # Hook B returns None — must not erase Hook A's contribution
259+ hook_b = MagicMock (spec = Hook )
260+ hook_b .before .return_value = None
261+
262+ hook_c = MagicMock (spec = Hook )
263+
264+ def hook_c_before (hook_context , hints ):
265+ received_by_hook_c .append (hook_context .evaluation_context )
266+ return None
267+
268+ hook_c .before .side_effect = hook_c_before
269+
270+ # When
271+ before_hooks (
272+ FlagType .BOOLEAN ,
273+ [(hook_a , ctx_a ), (hook_b , ctx_b ), (hook_c , ctx_c )],
274+ )
275+
276+ # Then
277+ assert received_by_hook_c [0 ].attributes .get ("from_a" ) == "A" , (
278+ "Hook C should still see Hook A's context even though Hook B returned None"
279+ )
280+
281+
125282def test_after_hooks_run_after_method (mock_hook ):
126283 # Given
127284 hook_context = HookContext ("flag_key" , FlagType .BOOLEAN , True , "" )
0 commit comments