@@ -1510,6 +1510,121 @@ function five() {
15101510 expect ( result . success ) . toBe ( false )
15111511 expect ( result . error ) . toContain ( "'=======' found in your diff content" )
15121512 } )
1513+
1514+ describe ( "command line processing" , ( ) => {
1515+ let strategy : MultiSearchReplaceDiffStrategy
1516+ beforeEach ( ( ) => {
1517+ strategy = new MultiSearchReplaceDiffStrategy ( )
1518+ } )
1519+
1520+ it ( "should process diff from command line arguments" , async ( ) => {
1521+ // This test is designed to be run from the command line with file arguments
1522+ // Example: npx jest src/core/diff/strategies/__tests__/multi-search-replace.test.ts -t "should process diff" -- file.ts diff.diff
1523+
1524+ // Get command line arguments
1525+ const args = process . argv . slice ( 2 )
1526+
1527+ // Skip test if not run with arguments
1528+ // Parse command line arguments for --source and --diff flags
1529+ let sourceFile : string | undefined
1530+ let diffFile : string | undefined
1531+
1532+ for ( let i = 0 ; i < args . length ; i ++ ) {
1533+ if ( args [ i ] === "--source" && i + 1 < args . length ) {
1534+ sourceFile = args [ i + 1 ]
1535+ i ++ // Skip the next argument as it's the value
1536+ } else if ( args [ i ] === "--diff" && i + 1 < args . length ) {
1537+ diffFile = args [ i + 1 ]
1538+ i ++ // Skip the next argument as it's the value
1539+ }
1540+ }
1541+
1542+ if ( ! sourceFile || ! diffFile ) {
1543+ console . debug (
1544+ `Optional debug usage: npx jest multi-search-replace.test.ts -- --source <file.ts> --diff <diff.diff>\n` ,
1545+ )
1546+ // console.debug('All args:', args);
1547+ return
1548+ }
1549+
1550+ try {
1551+ // Read files
1552+ const fs = require ( "fs" )
1553+ const sourceContent = fs . readFileSync ( sourceFile , "utf8" )
1554+ let diffContent = fs . readFileSync ( diffFile , "utf8" )
1555+
1556+ // Show first 50 lines of source content
1557+ process . stdout . write (
1558+ `\n\n====================================================================\n` ,
1559+ )
1560+ process . stdout . write ( `== ${ sourceFile } first 50 lines ==\n` )
1561+ process . stdout . write (
1562+ `====================================================================\n` ,
1563+ )
1564+ sourceContent
1565+ . split ( "\n" )
1566+ . slice ( 0 , 50 )
1567+ . forEach ( ( line : string ) => {
1568+ process . stdout . write ( `${ line } \n` )
1569+ } )
1570+ process . stdout . write (
1571+ `=============================== END ================================\n` ,
1572+ )
1573+
1574+ process . stdout . write (
1575+ `\n\n====================================================================\n` ,
1576+ )
1577+ process . stdout . write ( `== ${ diffFile } first 50 lines ==\n` )
1578+ process . stdout . write (
1579+ `====================================================================\n` ,
1580+ )
1581+
1582+ // Show first 50 lines of diff content
1583+ diffContent
1584+ . split ( "\n" )
1585+ . slice ( 0 , 50 )
1586+ . forEach ( ( line : string ) => {
1587+ process . stdout . write ( `${ line } \n` )
1588+ } )
1589+ process . stdout . write (
1590+ `=============================== END ================================\n` ,
1591+ )
1592+
1593+ // Apply the diff
1594+ const result = await strategy . applyDiff ( sourceContent , diffContent )
1595+
1596+ if ( result . success ) {
1597+ process . stdout . write (
1598+ `\n\n====================================================================\n` ,
1599+ )
1600+ process . stdout . write ( `== Diff applied successfully ==\n` )
1601+ process . stdout . write (
1602+ `====================================================================\n` ,
1603+ )
1604+ process . stdout . write ( result . content + "\n" )
1605+ process . stdout . write (
1606+ `=============================== END ================================\n` ,
1607+ )
1608+ expect ( result . success ) . toBe ( true )
1609+ } else {
1610+ process . stdout . write (
1611+ `\n\n====================================================================\n` ,
1612+ )
1613+ process . stdout . write ( `== Failed to apply diff ==\n` )
1614+ process . stdout . write (
1615+ `====================================================================\n\n\n` ,
1616+ )
1617+ console . error ( result )
1618+ process . stdout . write (
1619+ `=============================== END ================================\n\n\n` ,
1620+ )
1621+ }
1622+ } catch ( err ) {
1623+ console . error ( "Error processing files:" , err . message )
1624+ console . error ( "Stack trace:" , err . stack )
1625+ }
1626+ } )
1627+ } )
15131628 } )
15141629
15151630 it ( "should not strip content that starts with pipe but no line number" , async ( ) => {
0 commit comments