@@ -1355,6 +1355,121 @@ function five() {
13551355 expect ( result . success ) . toBe ( false )
13561356 expect ( result . error ) . toContain ( "'=======' found in your diff content" )
13571357 } )
1358+
1359+ describe ( "command line processing" , ( ) => {
1360+ let strategy : MultiSearchReplaceDiffStrategy
1361+ beforeEach ( ( ) => {
1362+ strategy = new MultiSearchReplaceDiffStrategy ( )
1363+ } )
1364+
1365+ it ( "should process diff from command line arguments" , async ( ) => {
1366+ // This test is designed to be run from the command line with file arguments
1367+ // Example: npx jest src/core/diff/strategies/__tests__/multi-search-replace.test.ts -t "should process diff" -- file.ts diff.diff
1368+
1369+ // Get command line arguments
1370+ const args = process . argv . slice ( 2 )
1371+
1372+ // Skip test if not run with arguments
1373+ // Parse command line arguments for --source and --diff flags
1374+ let sourceFile : string | undefined
1375+ let diffFile : string | undefined
1376+
1377+ for ( let i = 0 ; i < args . length ; i ++ ) {
1378+ if ( args [ i ] === "--source" && i + 1 < args . length ) {
1379+ sourceFile = args [ i + 1 ]
1380+ i ++ // Skip the next argument as it's the value
1381+ } else if ( args [ i ] === "--diff" && i + 1 < args . length ) {
1382+ diffFile = args [ i + 1 ]
1383+ i ++ // Skip the next argument as it's the value
1384+ }
1385+ }
1386+
1387+ if ( ! sourceFile || ! diffFile ) {
1388+ console . debug (
1389+ `Optional debug usage: npx jest multi-search-replace.test.ts -- --source <file.ts> --diff <diff.diff>\n` ,
1390+ )
1391+ // console.debug('All args:', args);
1392+ return
1393+ }
1394+
1395+ try {
1396+ // Read files
1397+ const fs = require ( "fs" )
1398+ const sourceContent = fs . readFileSync ( sourceFile , "utf8" )
1399+ let diffContent = fs . readFileSync ( diffFile , "utf8" )
1400+
1401+ // Show first 50 lines of source content
1402+ process . stdout . write (
1403+ `\n\n====================================================================\n` ,
1404+ )
1405+ process . stdout . write ( `== ${ sourceFile } first 50 lines ==\n` )
1406+ process . stdout . write (
1407+ `====================================================================\n` ,
1408+ )
1409+ sourceContent
1410+ . split ( "\n" )
1411+ . slice ( 0 , 50 )
1412+ . forEach ( ( line : string ) => {
1413+ process . stdout . write ( `${ line } \n` )
1414+ } )
1415+ process . stdout . write (
1416+ `=============================== END ================================\n` ,
1417+ )
1418+
1419+ process . stdout . write (
1420+ `\n\n====================================================================\n` ,
1421+ )
1422+ process . stdout . write ( `== ${ diffFile } first 50 lines ==\n` )
1423+ process . stdout . write (
1424+ `====================================================================\n` ,
1425+ )
1426+
1427+ // Show first 50 lines of diff content
1428+ diffContent
1429+ . split ( "\n" )
1430+ . slice ( 0 , 50 )
1431+ . forEach ( ( line : string ) => {
1432+ process . stdout . write ( `${ line } \n` )
1433+ } )
1434+ process . stdout . write (
1435+ `=============================== END ================================\n` ,
1436+ )
1437+
1438+ // Apply the diff
1439+ const result = await strategy . applyDiff ( sourceContent , diffContent )
1440+
1441+ if ( result . success ) {
1442+ process . stdout . write (
1443+ `\n\n====================================================================\n` ,
1444+ )
1445+ process . stdout . write ( `== Diff applied successfully ==\n` )
1446+ process . stdout . write (
1447+ `====================================================================\n` ,
1448+ )
1449+ process . stdout . write ( result . content + "\n" )
1450+ process . stdout . write (
1451+ `=============================== END ================================\n` ,
1452+ )
1453+ expect ( result . success ) . toBe ( true )
1454+ } else {
1455+ process . stdout . write (
1456+ `\n\n====================================================================\n` ,
1457+ )
1458+ process . stdout . write ( `== Failed to apply diff ==\n` )
1459+ process . stdout . write (
1460+ `====================================================================\n\n\n` ,
1461+ )
1462+ console . error ( result )
1463+ process . stdout . write (
1464+ `=============================== END ================================\n\n\n` ,
1465+ )
1466+ }
1467+ } catch ( err ) {
1468+ console . error ( "Error processing files:" , err . message )
1469+ console . error ( "Stack trace:" , err . stack )
1470+ }
1471+ } )
1472+ } )
13581473 } )
13591474
13601475 it ( "should not strip content that starts with pipe but no line number" , async ( ) => {
0 commit comments