@@ -303,4 +303,94 @@ describe('textEditor', () => {
303
303
) ;
304
304
} ) . rejects . toThrow ( / F o u n d 2 o c c u r r e n c e s / ) ;
305
305
} ) ;
306
+
307
+ it ( 'should overwrite an existing file with create command' , async ( ) => {
308
+ const initialContent = 'Initial content' ;
309
+ const newContent = 'New content that overwrites the file' ;
310
+ const testPath = join ( testDir , `${ randomUUID ( ) } .txt` ) ;
311
+
312
+ // Create initial file
313
+ await textEditorTool . execute (
314
+ {
315
+ command : 'create' ,
316
+ path : testPath ,
317
+ file_text : initialContent ,
318
+ description : 'test' ,
319
+ } ,
320
+ toolContext ,
321
+ ) ;
322
+
323
+ // Verify initial content
324
+ let content = await readFile ( testPath , 'utf8' ) ;
325
+ expect ( content ) . toBe ( initialContent ) ;
326
+
327
+ // Overwrite the file using create command
328
+ const result = await textEditorTool . execute (
329
+ {
330
+ command : 'create' ,
331
+ path : testPath ,
332
+ file_text : newContent ,
333
+ description : 'test' ,
334
+ } ,
335
+ toolContext ,
336
+ ) ;
337
+
338
+ // Verify return value
339
+ expect ( result . success ) . toBe ( true ) ;
340
+ expect ( result . message ) . toContain ( 'File overwritten' ) ;
341
+
342
+ // Verify content has been updated
343
+ content = await readFile ( testPath , 'utf8' ) ;
344
+ expect ( content ) . toBe ( newContent ) ;
345
+ } ) ;
346
+
347
+ it ( 'should be able to undo file overwrite' , async ( ) => {
348
+ const initialContent = 'Initial content that will be restored' ;
349
+ const overwrittenContent = 'This content will be undone' ;
350
+ const testPath = join ( testDir , `${ randomUUID ( ) } .txt` ) ;
351
+
352
+ // Create initial file
353
+ await textEditorTool . execute (
354
+ {
355
+ command : 'create' ,
356
+ path : testPath ,
357
+ file_text : initialContent ,
358
+ description : 'test' ,
359
+ } ,
360
+ toolContext ,
361
+ ) ;
362
+
363
+ // Overwrite the file
364
+ await textEditorTool . execute (
365
+ {
366
+ command : 'create' ,
367
+ path : testPath ,
368
+ file_text : overwrittenContent ,
369
+ description : 'test' ,
370
+ } ,
371
+ toolContext ,
372
+ ) ;
373
+
374
+ // Verify overwritten content
375
+ let content = await readFile ( testPath , 'utf8' ) ;
376
+ expect ( content ) . toBe ( overwrittenContent ) ;
377
+
378
+ // Undo the overwrite
379
+ const result = await textEditorTool . execute (
380
+ {
381
+ command : 'undo_edit' ,
382
+ path : testPath ,
383
+ description : 'test' ,
384
+ } ,
385
+ toolContext ,
386
+ ) ;
387
+
388
+ // Verify return value
389
+ expect ( result . success ) . toBe ( true ) ;
390
+ expect ( result . message ) . toContain ( 'Successfully reverted' ) ;
391
+
392
+ // Verify content is back to initial
393
+ content = await readFile ( testPath , 'utf8' ) ;
394
+ expect ( content ) . toBe ( initialContent ) ;
395
+ } ) ;
306
396
} ) ;
0 commit comments