@@ -17,9 +17,9 @@ import {
1717 spyOn ,
1818} from 'bun:test'
1919
20- import { loopAgentSteps } from '../run-agent-step'
21- import { clearAgentGeneratorCache } from '../run-programmatic-step'
20+ import { loopAgentSteps , clearAgentGeneratorCache } from '@codebuff/agent-runtime'
2221import { mockFileContext , MockWebSocket } from './test-utils'
22+ import { createMockAgentRuntimeEnvironment } from './test-env-mocks'
2323
2424import type { AgentTemplate } from '../templates/types'
2525import type { StepGenerator } from '@codebuff/common/types/agent-template'
@@ -193,8 +193,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
193193 } ,
194194 )
195195
196+ const env = createMockAgentRuntimeEnvironment ( )
197+
196198 const result = await loopAgentSteps (
197- new MockWebSocket ( ) as unknown as WebSocket ,
198199 {
199200 userInputId : 'test-user-input' ,
200201 agentType : 'test-agent' ,
@@ -209,6 +210,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
209210 clientSessionId : 'test-session' ,
210211 onResponseChunk : ( ) => { } ,
211212 } ,
213+ env ,
212214 )
213215
214216 console . log ( `LLM calls made: ${ llmCallCount } ` )
@@ -243,8 +245,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
243245 'test-agent' : mockTemplate ,
244246 }
245247
248+ const env = createMockAgentRuntimeEnvironment ( )
249+
246250 const result = await loopAgentSteps (
247- new MockWebSocket ( ) as unknown as WebSocket ,
248251 {
249252 userInputId : 'test-user-input' ,
250253 agentType : 'test-agent' ,
@@ -259,6 +262,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
259262 clientSessionId : 'test-session' ,
260263 onResponseChunk : ( ) => { } ,
261264 } ,
265+ env ,
262266 )
263267
264268 // Should NOT call LLM since the programmatic agent ended with end_turn
@@ -303,8 +307,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
303307 } ,
304308 )
305309
310+ const env = createMockAgentRuntimeEnvironment ( )
311+
306312 const result = await loopAgentSteps (
307- new MockWebSocket ( ) as unknown as WebSocket ,
308313 {
309314 userInputId : 'test-user-input' ,
310315 agentType : 'test-agent' ,
@@ -319,6 +324,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
319324 clientSessionId : 'test-session' ,
320325 onResponseChunk : ( ) => { } ,
321326 } ,
327+ env ,
322328 )
323329
324330 // Verify execution order:
@@ -361,8 +367,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
361367 } ,
362368 )
363369
370+ const env = createMockAgentRuntimeEnvironment ( )
371+
364372 const result = await loopAgentSteps (
365- new MockWebSocket ( ) as unknown as WebSocket ,
366373 {
367374 userInputId : 'test-user-input' ,
368375 agentType : 'test-agent' ,
@@ -377,6 +384,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
377384 clientSessionId : 'test-session' ,
378385 onResponseChunk : ( ) => { } ,
379386 } ,
387+ env ,
380388 )
381389
382390 expect ( stepCount ) . toBe ( 1 ) // Generator function called once
@@ -403,8 +411,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
403411 'test-agent' : mockTemplate ,
404412 }
405413
414+ const env = createMockAgentRuntimeEnvironment ( )
415+
406416 const result = await loopAgentSteps (
407- new MockWebSocket ( ) as unknown as WebSocket ,
408417 {
409418 userInputId : 'test-user-input' ,
410419 agentType : 'test-agent' ,
@@ -419,6 +428,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
419428 clientSessionId : 'test-session' ,
420429 onResponseChunk : ( ) => { } ,
421430 } ,
431+ env ,
422432 )
423433
424434 expect ( llmCallCount ) . toBe ( 0 ) // No LLM calls should be made
@@ -446,8 +456,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
446456 } ,
447457 )
448458
459+ const env = createMockAgentRuntimeEnvironment ( )
460+
449461 const result = await loopAgentSteps (
450- new MockWebSocket ( ) as unknown as WebSocket ,
451462 {
452463 userInputId : 'test-user-input' ,
453464 agentType : 'test-agent' ,
@@ -462,6 +473,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
462473 clientSessionId : 'test-session' ,
463474 onResponseChunk : ( ) => { } ,
464475 } ,
476+ env ,
465477 )
466478
467479 expect ( llmCallCount ) . toBe ( 1 ) // LLM should be called once
@@ -491,8 +503,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
491503 } ,
492504 )
493505
506+ const env = createMockAgentRuntimeEnvironment ( )
507+
494508 const result = await loopAgentSteps (
495- new MockWebSocket ( ) as unknown as WebSocket ,
496509 {
497510 userInputId : 'test-user-input' ,
498511 agentType : 'test-agent' ,
@@ -507,6 +520,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
507520 clientSessionId : 'test-session' ,
508521 onResponseChunk : ( ) => { } ,
509522 } ,
523+ env ,
510524 )
511525
512526 // After programmatic step error, should end turn and not call LLM
@@ -553,8 +567,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
553567 } ,
554568 )
555569
570+ const env = createMockAgentRuntimeEnvironment ( )
571+
556572 const result = await loopAgentSteps (
557- new MockWebSocket ( ) as unknown as WebSocket ,
558573 {
559574 userInputId : 'test-user-input' ,
560575 agentType : 'test-agent' ,
@@ -569,6 +584,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
569584 clientSessionId : 'test-session' ,
570585 onResponseChunk : ( ) => { } ,
571586 } ,
587+ env ,
572588 )
573589
574590 expect ( stepCount ) . toBe ( 1 ) // Generator function called once
@@ -611,8 +627,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
611627 } ,
612628 )
613629
630+ const env = createMockAgentRuntimeEnvironment ( )
631+
614632 const result = await loopAgentSteps (
615- new MockWebSocket ( ) as unknown as WebSocket ,
616633 {
617634 userInputId : 'test-user-input' ,
618635 agentType : 'test-agent' ,
@@ -627,6 +644,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
627644 clientSessionId : 'test-session' ,
628645 onResponseChunk : ( ) => { } ,
629646 } ,
647+ env ,
630648 )
631649
632650 // Should continue when async messages are present
@@ -640,14 +658,15 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
640658 let runProgrammaticStepCalls : any [ ] = [ ]
641659
642660 // Mock runProgrammaticStep module to capture calls and verify stepsComplete parameter
643- mockModule ( '@codebuff/backend/run-programmatic-step ' , ( ) => ( {
661+ mockModule ( '@codebuff/agent-runtime ' , ( ) => ( {
644662 runProgrammaticStep : async ( agentState : any , options : any ) => {
645663 runProgrammaticStepCalls . push ( { agentState, options } )
646664 // Return default behavior
647665 return { agentState, endTurn : false }
648666 } ,
649667 clearAgentGeneratorCache : ( ) => { } ,
650- agentIdToStepAll : new Set ( ) ,
668+ loopAgentSteps : require ( '@codebuff/agent-runtime' ) . loopAgentSteps ,
669+ runAgentStep : require ( '@codebuff/agent-runtime' ) . runAgentStep ,
651670 } ) )
652671
653672 const mockGeneratorFunction = function * ( ) {
@@ -686,7 +705,9 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
686705 ( ) => true ,
687706 )
688707
689- await loopAgentSteps ( new MockWebSocket ( ) as unknown as WebSocket , {
708+ const env = createMockAgentRuntimeEnvironment ( )
709+
710+ await loopAgentSteps ( {
690711 userInputId : 'test-user-input' ,
691712 agentType : 'test-agent' ,
692713 agentState : mockAgentState ,
@@ -699,7 +720,7 @@ describe('loopAgentSteps - runAgentStep vs runProgrammaticStep behavior', () =>
699720 userId : TEST_USER_ID ,
700721 clientSessionId : 'test-session' ,
701722 onResponseChunk : ( ) => { } ,
702- } )
723+ } , env )
703724
704725 // Verify that runProgrammaticStep was called twice:
705726 // 1. First with stepsComplete: false (initial call)
0 commit comments