@@ -15,10 +15,11 @@ import {
1515 clearAgentGeneratorCache ,
1616 runProgrammaticStep ,
1717} from '../run-programmatic-step'
18- import { mockFileContext } from './test-utils'
18+ import { mockFileContext , MockWebSocket } 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'
2223
2324import type { AgentTemplate , StepGenerator } from '../templates/types'
2425import type { PublicAgentState } from '@codebuff/common/types/agent-template'
@@ -28,7 +29,7 @@ import type {
2829} from '@codebuff/common/types/messages/content-part'
2930import type { AgentState } from '@codebuff/common/types/session-state'
3031import type { Logger } from '@codebuff/common/types/contracts/logger'
31- import type { SendSubagentChunkFn } from '@codebuff/common/types/contracts/client '
32+ import type { WebSocket } from 'ws '
3233
3334const logger : Logger = {
3435 debug : ( ) => { } ,
@@ -44,8 +45,7 @@ describe('runProgrammaticStep', () => {
4445 let executeToolCallSpy : any
4546 let getRequestContextSpy : any
4647 let addAgentStepSpy : any
47- let sendSubagentChunk : SendSubagentChunkFn
48- let sentSubagentChunks : Parameters < SendSubagentChunkFn > [ 0 ] [ ]
48+ let sendActionSpy : any
4949
5050 beforeEach ( ( ) => {
5151 // Mock analytics
@@ -72,10 +72,10 @@ describe('runProgrammaticStep', () => {
7272 async ( ) => 'test-step-id' ,
7373 )
7474
75- sentSubagentChunks = [ ]
76- sendSubagentChunk = ( data ) => {
77- sentSubagentChunks . push ( data )
78- }
75+ // Mock sendAction
76+ sendActionSpy = spyOn ( websocketAction , 'sendAction' ) . mockImplementation (
77+ ( ) => { } ,
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 ,
135136 localAgentTemplates : { } ,
136137 stepsComplete : false ,
137138 stepNumber : 1 ,
138- sendSubagentChunk,
139139 logger,
140140 }
141141 } )
@@ -226,6 +226,14 @@ 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+
229237 const result = await runProgrammaticStep ( mockParams )
230238
231239 // Verify add_message tool was executed
@@ -245,16 +253,15 @@ describe('runProgrammaticStep', () => {
245253 )
246254
247255 // Check that no tool call chunk was sent for add_message
248- const addMessageToolCallChunk = sentSubagentChunks . find (
249- ( { chunk } ) =>
256+ const addMessageToolCallChunk = sentChunks . find (
257+ ( chunk ) =>
250258 chunk . includes ( 'add_message' ) && chunk . includes ( 'Hello world' ) ,
251259 )
252260 expect ( addMessageToolCallChunk ) . toBeUndefined ( )
253261
254262 // Check that tool call chunk WAS sent for read_files (normal behavior)
255- const readFilesToolCallChunk = sentSubagentChunks . find (
256- ( { chunk } ) =>
257- chunk . includes ( 'read_files' ) && chunk . includes ( 'test.txt' ) ,
263+ const readFilesToolCallChunk = sentChunks . find (
264+ ( chunk ) => chunk . includes ( 'read_files' ) && chunk . includes ( 'test.txt' ) ,
258265 )
259266 expect ( readFilesToolCallChunk ) . toBeDefined ( )
260267
0 commit comments