@@ -14,10 +14,15 @@ vi.mock("@roo-code/telemetry", () => ({
1414
1515vi . mock ( "fs/promises" , ( ) => ( {
1616 default : {
17- readFile : vi . fn ( ) . mockResolvedValue ( "" ) ,
17+ readFile : vi . fn ( ) . mockResolvedValue ( Buffer . alloc ( 0 ) ) ,
1818 } ,
1919} ) )
2020
21+ // ApplyDiffTool reads the file with no encoding, so the call the double stands in for is the
22+ // byte-returning one: fs.readFile returns Buffer unless an encoding is passed. vi.mocked resolves
23+ // the encoding overload, so the mock is typed against the signature the tool actually calls.
24+ const mockedReadFile = vi . mocked ( fs . readFile as ( path : string ) => Promise < Buffer > )
25+
2126vi . mock ( "../../../utils/fs" , ( ) => ( {
2227 fileExistsAtPath : vi . fn ( ) . mockResolvedValue ( true ) ,
2328} ) )
@@ -63,7 +68,7 @@ describe("ApplyDiffTool failure reporting", () => {
6368 vi . clearAllMocks ( )
6469 pushedResult = undefined
6570 vi . mocked ( fileExistsAtPath ) . mockResolvedValue ( true )
66- vi . mocked ( fs . readFile ) . mockResolvedValue ( "line one\nline two\nline three\n" )
71+ mockedReadFile . mockResolvedValue ( Buffer . from ( "line one\nline two\nline three\n" , "utf8" ) )
6772
6873 mockAskApproval = vi . fn ( ) . mockResolvedValue ( true )
6974 mockHandleError = vi . fn ( ) . mockResolvedValue ( undefined )
@@ -150,6 +155,65 @@ describe("ApplyDiffTool failure reporting", () => {
150155 return failures
151156 }
152157
158+ describe ( "bytes the decoder cannot read" , ( ) => {
159+ async function runOnBytes ( bytes : Buffer ) : Promise < string > {
160+ mockedReadFile . mockResolvedValue ( bytes )
161+ const applyDiff = vi . fn ( ) . mockResolvedValue ( { success : true , content : "unused" } )
162+ mockTask . diffStrategy = { getName : ( ) => "test" , applyDiff }
163+ await applyDiffTool . execute ( { path : testFilePath , diff : buildDiffContent ( 1 ) } , mockTask , {
164+ askApproval : mockAskApproval ,
165+ handleError : mockHandleError ,
166+ toolCallId : "tool-call-1" ,
167+ pushToolResult : vi . fn ( ( result : ToolResponse ) => {
168+ pushedResult = result
169+ } ) ,
170+ } )
171+ return String ( pushedResult ?? "" )
172+ }
173+
174+ // A byte the UTF-8 decoder rejects sits at index 3; everything around it is plain ASCII, so the
175+ // offset the tool reports has to be that index and nothing else.
176+ const UNDECODABLE = Buffer . from ( [ 0x68 , 0x69 , 0x20 , 0xff , 0x61 , 0x0a ] )
177+
178+ it ( "refuses the edit and names the file and the first undecodable byte" , async ( ) => {
179+ const output = await runOnBytes ( UNDECODABLE )
180+
181+ expect ( output ) . toContain ( "is not valid UTF-8" )
182+ expect ( output ) . toContain ( "first undecodable byte at offset 3" )
183+ expect ( mockTask . say ) . toHaveBeenCalledWith ( "error" , output )
184+ expect ( mockTask . recordToolError ) . toHaveBeenCalledWith ( "apply_diff" )
185+ expect ( mockTask . consecutiveMistakeCount ) . toBe ( 1 )
186+ expect ( mockTask . didToolFailInCurrentTurn ) . toBe ( true )
187+ } )
188+
189+ it ( "refuses before the diff is computed, approved, or written" , async ( ) => {
190+ await runOnBytes ( UNDECODABLE )
191+
192+ expect ( mockedReadFile ) . toHaveBeenCalledWith ( path . resolve ( "/workspace" , testFilePath ) )
193+ expect ( mockAskApproval ) . not . toHaveBeenCalled ( )
194+ expect ( mockTask . diffViewProvider . open ) . not . toHaveBeenCalled ( )
195+ expect ( mockTask . diffViewProvider . saveChanges ) . not . toHaveBeenCalled ( )
196+ expect ( mockTask . diffViewProvider . saveDirectly ) . not . toHaveBeenCalled ( )
197+ expect ( mockTask . didEditFile ) . toBe ( false )
198+ } )
199+
200+ it ( "still edits a file whose bytes are valid multi-byte UTF-8" , async ( ) => {
201+ const output = await runOnBytes ( Buffer . from ( "héllo 中文 ✅\n" , "utf8" ) )
202+
203+ expect ( output ) . not . toContain ( "is not valid UTF-8" )
204+ expect ( output ) . toContain ( "Changes successfully applied." )
205+ expect ( mockTask . consecutiveMistakeCount ) . toBe ( 0 )
206+ expect ( mockTask . diffViewProvider . saveChanges ) . toHaveBeenCalled ( )
207+ } )
208+
209+ it ( "reports the first undecodable byte when invalid bytes repeat" , async ( ) => {
210+ const output = await runOnBytes ( Buffer . from ( [ 0x41 , 0xc3 , 0x28 , 0x42 , 0xfe , 0x43 , 0xff ] ) )
211+
212+ expect ( output ) . toContain ( "first undecodable byte at offset 1" )
213+ expect ( output ) . not . toContain ( "offset 4" )
214+ } )
215+ } )
216+
153217 describe ( "failure accumulation and rendering" , ( ) => {
154218 it ( "reports every failed block, not just the last one" , async ( ) => {
155219 const output = await runApplyDiff ( { success : false , failParts : makeFailureSet ( 2 ) } , 2 )
@@ -330,7 +394,7 @@ describe("ApplyDiffTool failure reporting", () => {
330394 const applyingBlock = "<<<<<<< SEARCH\nconst header = 1\n=======\nconst header = 3\n>>>>>>> REPLACE"
331395
332396 async function runRealStrategy ( diff : string ) : Promise < string > {
333- vi . mocked ( fs . readFile ) . mockResolvedValue ( fileText )
397+ mockedReadFile . mockResolvedValue ( Buffer . from ( fileText , "utf8" ) )
334398 const strategy = new MultiSearchReplaceDiffStrategy ( )
335399 const realResult = await strategy . applyDiff ( fileText , diff )
336400 // Synthetic details exercise the tool's own guard against forwarding strategy detail objects.
0 commit comments