@@ -199,4 +199,75 @@ describe('createUseStep', () => {
199199 await myStepFunction ( ) ;
200200 expect ( ctx . onWorkflowError ) . not . toHaveBeenCalled ( ) ;
201201 } ) ;
202+
203+ it ( 'should capture closure variables when provided' , async ( ) => {
204+ const ctx = setupWorkflowContext ( [
205+ {
206+ eventId : 'evnt_0' ,
207+ runId : 'wrun_123' ,
208+ eventType : 'step_completed' ,
209+ correlationId : 'step_01K11TFZ62YS0YYFDQ3E8B9YCV' ,
210+ eventData : {
211+ result : [ 'Result: 42' ] ,
212+ } ,
213+ createdAt : new Date ( ) ,
214+ } ,
215+ ] ) ;
216+
217+ const useStep = createUseStep ( ctx ) ;
218+ const count = 42 ;
219+ const prefix = 'Result: ' ;
220+
221+ // Create step with closure variables function
222+ const calculate = useStep ( 'calculate' , ( ) => ( { count, prefix } ) ) ;
223+
224+ // Call the step
225+ const result = await calculate ( ) ;
226+
227+ // Verify result
228+ expect ( result ) . toBe ( 'Result: 42' ) ;
229+
230+ // Verify closure variables were added to invocation queue
231+ expect ( ctx . invocationsQueue ) . toHaveLength ( 1 ) ;
232+ expect ( ctx . invocationsQueue [ 0 ] ) . toMatchObject ( {
233+ type : 'step' ,
234+ stepName : 'calculate' ,
235+ args : [ ] ,
236+ closureVars : { count : 42 , prefix : 'Result: ' } ,
237+ } ) ;
238+ } ) ;
239+
240+ it ( 'should handle empty closure variables' , async ( ) => {
241+ const ctx = setupWorkflowContext ( [
242+ {
243+ eventId : 'evnt_0' ,
244+ runId : 'wrun_123' ,
245+ eventType : 'step_completed' ,
246+ correlationId : 'step_01K11TFZ62YS0YYFDQ3E8B9YCV' ,
247+ eventData : {
248+ result : [ 5 ] ,
249+ } ,
250+ createdAt : new Date ( ) ,
251+ } ,
252+ ] ) ;
253+
254+ const useStep = createUseStep ( ctx ) ;
255+
256+ // Create step without closure variables
257+ const add = useStep ( 'add' ) ;
258+
259+ // Call the step
260+ const result = await add ( 2 , 3 ) ;
261+
262+ // Verify result
263+ expect ( result ) . toBe ( 5 ) ;
264+
265+ // Verify empty closure variables were added to invocation queue
266+ expect ( ctx . invocationsQueue ) . toHaveLength ( 1 ) ;
267+ expect ( ctx . invocationsQueue [ 0 ] ) . toMatchObject ( {
268+ type : 'step' ,
269+ stepName : 'add' ,
270+ args : [ 2 , 3 ] ,
271+ } ) ;
272+ } ) ;
202273} ) ;
0 commit comments