@@ -15,11 +15,10 @@ import {
1515 clearAgentGeneratorCache ,
1616 runProgrammaticStep ,
1717} from '../run-programmatic-step'
18- import { mockFileContext , MockWebSocket } from './test-utils'
18+ import { mockFileContext } from './test-utils'
1919import * as agentRun from '../agent-run'
2020import * as toolExecutor from '../tools/tool-executor'
2121import * as requestContext from '../websockets/request-context'
22- import * as websocketAction from '../websockets/websocket-action'
2322
2423import type { AgentTemplate , StepGenerator } from '../templates/types'
2524import type { PublicAgentState } from '@codebuff/common/types/agent-template'
@@ -29,7 +28,7 @@ import type {
2928} from '@codebuff/common/types/messages/content-part'
3029import type { AgentState } from '@codebuff/common/types/session-state'
3130import type { Logger } from '@codebuff/common/types/contracts/logger'
32- import type { WebSocket } from 'ws '
31+ import type { SendSubagentChunkFn } from '@codebuff/common/types/contracts/client '
3332
3433const logger : Logger = {
3534 debug : ( ) => { } ,
@@ -45,7 +44,8 @@ describe('runProgrammaticStep', () => {
4544 let executeToolCallSpy : any
4645 let getRequestContextSpy : any
4746 let addAgentStepSpy : any
48- let sendActionSpy : any
47+ let sendSubagentChunk : SendSubagentChunkFn
48+ let sentSubagentChunks : Parameters < SendSubagentChunkFn > [ 0 ] [ ]
4949
5050 beforeEach ( ( ) => {
5151 // Mock analytics
@@ -72,10 +72,10 @@ describe('runProgrammaticStep', () => {
7272 async ( ) => 'test-step-id' ,
7373 )
7474
75- // Mock sendAction
76- sendActionSpy = spyOn ( websocketAction , 'sendAction' ) . mockImplementation (
77- ( ) => { } ,
78- )
75+ sentSubagentChunks = [ ]
76+ sendSubagentChunk = ( data ) => {
77+ sentSubagentChunks . push ( data )
78+ }
7979
8080 // Mock crypto.randomUUID
8181 spyOn ( crypto , 'randomUUID' ) . mockImplementation (
@@ -132,10 +132,10 @@ describe('runProgrammaticStep', () => {
132132 fileContext : mockFileContext ,
133133 assistantMessage : undefined ,
134134 assistantPrefix : undefined ,
135- ws : new MockWebSocket ( ) as unknown as WebSocket ,
136135 localAgentTemplates : { } ,
137136 stepsComplete : false ,
138137 stepNumber : 1 ,
138+ sendSubagentChunk,
139139 logger,
140140 }
141141 } )
@@ -226,14 +226,6 @@ describe('runProgrammaticStep', () => {
226226 mockTemplate . handleSteps = ( ) => mockGenerator
227227 mockTemplate . toolNames = [ 'add_message' , 'read_files' , 'end_turn' ]
228228
229- // Track chunks sent via sendSubagentChunk
230- const sentChunks : string [ ] = [ ]
231- sendActionSpy . mockImplementation ( ( ws : any , action : any ) => {
232- if ( action . type === 'subagent-response-chunk' ) {
233- sentChunks . push ( action . chunk )
234- }
235- } )
236-
237229 const result = await runProgrammaticStep ( mockParams )
238230
239231 // Verify add_message tool was executed
@@ -253,15 +245,16 @@ describe('runProgrammaticStep', () => {
253245 )
254246
255247 // Check that no tool call chunk was sent for add_message
256- const addMessageToolCallChunk = sentChunks . find (
257- ( chunk ) =>
248+ const addMessageToolCallChunk = sentSubagentChunks . find (
249+ ( { chunk } ) =>
258250 chunk . includes ( 'add_message' ) && chunk . includes ( 'Hello world' ) ,
259251 )
260252 expect ( addMessageToolCallChunk ) . toBeUndefined ( )
261253
262254 // Check that tool call chunk WAS sent for read_files (normal behavior)
263- const readFilesToolCallChunk = sentChunks . find (
264- ( chunk ) => chunk . includes ( 'read_files' ) && chunk . includes ( 'test.txt' ) ,
255+ const readFilesToolCallChunk = sentSubagentChunks . find (
256+ ( { chunk } ) =>
257+ chunk . includes ( 'read_files' ) && chunk . includes ( 'test.txt' ) ,
265258 )
266259 expect ( readFilesToolCallChunk ) . toBeDefined ( )
267260
0 commit comments