1515import path from "path"
1616
1717import type { ToolUse } from "../../../shared/tools"
18- import type { Task } from "../../task/Task"
18+ import type { Task , ToolExecutionContext } from "../../task/Task"
1919
2020import { isBinaryFile } from "isbinaryfile"
2121
@@ -154,7 +154,7 @@ function createMockTask(options: MockTaskOptions = {}) {
154154 consecutiveMistakeCount : 0 ,
155155 didToolFailInCurrentTurn : false ,
156156 didRejectTool : false ,
157- activeToolContexts : new Map ( ) ,
157+ activeToolContexts : new Map < string , ToolExecutionContext > ( ) ,
158158 ask : vi . fn ( ) . mockResolvedValue ( { response : "yesButtonClicked" , text : undefined , images : undefined } ) ,
159159 say : vi . fn ( ) . mockResolvedValue ( undefined ) ,
160160 sayAndCreateMissingParamError : vi . fn ( ) . mockResolvedValue ( "Missing required parameter: path" ) ,
@@ -804,18 +804,22 @@ describe("ReadFileTool", () => {
804804 } )
805805
806806 describe ( "batch approval denial" , ( ) => {
807- it . each ( [
808- [ "the user clicks no" , { response : "noButtonClicked" , text : undefined , images : undefined } ] ,
809- [ "individual permissions deny a file" , { response : "messageResponse" , text : "{}" , images : undefined } ] ,
810- [
811- "individual permissions are not valid JSON" ,
812- { response : "messageResponse" , text : "not json" , images : undefined } ,
813- ] ,
814- ] ) ( "marks the tool call rejected when %s" , async ( _name , askResult ) => {
807+ const requestBatchApproval = async ( answer : {
808+ response : string
809+ text ?: string
810+ approvedIndexes ?: number [ ]
811+ } ) => {
815812 const mockTask = createMockTask ( )
816813 const context = { toolCallId : "batch-call" , didReject : false }
817814 mockTask . activeToolContexts . set ( "batch-call" , context )
818- mockTask . ask . mockResolvedValue ( askResult )
815+ mockTask . ask . mockImplementation ( async ( _type : string , message : string ) => {
816+ if ( ! answer . approvedIndexes ) return { response : answer . response , text : answer . text }
817+ const { batchFiles } = JSON . parse ( message ) as { batchFiles : { key : string } [ ] }
818+ const permissions = Object . fromEntries (
819+ batchFiles . map ( ( file , index ) => [ file . key , answer . approvedIndexes ! . includes ( index ) ] ) ,
820+ )
821+ return { response : answer . response , text : JSON . stringify ( permissions ) }
822+ } )
819823 const updateFileResult = vi . fn ( )
820824 const filesToApprove = [ "file1.ts" , "file2.ts" ] . map ( ( path ) => ( {
821825 path,
@@ -825,11 +829,45 @@ describe("ReadFileTool", () => {
825829
826830 await readFileTool [ "requestApproval" ] ( mockTask as never , "batch-call" , filesToApprove , updateFileResult )
827831
832+ return { mockTask, context, updateFileResult }
833+ }
834+
835+ it . each ( [
836+ [ "the user clicks no" , { response : "noButtonClicked" } ] ,
837+ [ "individual permissions deny every file" , { response : "messageResponse" , text : "{}" } ] ,
838+ [ "individual permissions are not valid JSON" , { response : "messageResponse" , text : "not json" } ] ,
839+ ] ) ( "marks the tool call rejected and denies both files when %s" , async ( _name , askResult ) => {
840+ const { mockTask, context, updateFileResult } = await requestBatchApproval ( askResult )
841+
828842 expect ( mockTask . didRejectTool ) . toBe ( true )
829843 expect ( context . didReject ) . toBe ( true )
830844 expect ( updateFileResult ) . toHaveBeenCalledWith ( "file1.ts" , expect . objectContaining ( { status : "denied" } ) )
831845 expect ( updateFileResult ) . toHaveBeenCalledWith ( "file2.ts" , expect . objectContaining ( { status : "denied" } ) )
832846 } )
847+
848+ it ( "marks the tool call rejected when individual permissions approve one file and deny the other" , async ( ) => {
849+ const { mockTask, context, updateFileResult } = await requestBatchApproval ( {
850+ response : "messageResponse" ,
851+ approvedIndexes : [ 0 ] ,
852+ } )
853+
854+ expect ( mockTask . didRejectTool ) . toBe ( true )
855+ expect ( context . didReject ) . toBe ( true )
856+ expect ( updateFileResult ) . toHaveBeenCalledWith ( "file1.ts" , { status : "approved" } )
857+ expect ( updateFileResult ) . toHaveBeenCalledWith ( "file2.ts" , expect . objectContaining ( { status : "denied" } ) )
858+ } )
859+
860+ it ( "leaves the tool call unrejected when individual permissions approve every file" , async ( ) => {
861+ const { mockTask, context, updateFileResult } = await requestBatchApproval ( {
862+ response : "messageResponse" ,
863+ approvedIndexes : [ 0 , 1 ] ,
864+ } )
865+
866+ expect ( mockTask . didRejectTool ) . toBe ( false )
867+ expect ( context . didReject ) . toBe ( false )
868+ expect ( updateFileResult ) . toHaveBeenCalledWith ( "file1.ts" , { status : "approved" } )
869+ expect ( updateFileResult ) . toHaveBeenCalledWith ( "file2.ts" , { status : "approved" } )
870+ } )
833871 } )
834872
835873 it ( "should block rooignore-protected files in legacy format" , async ( ) => {
0 commit comments