@@ -1287,4 +1287,53 @@ function sum(a, b) {
12871287 expect ( result ) . toBe ( "<<<<<<< SEARCH\n" + "original\n" + "=======\n" + "new content\n" + ">>>>>>> REPLACE" )
12881288 } )
12891289 } )
1290+
1291+ // Regression guards for #186: Grok sometimes truncates the streamed diff and drops
1292+ // the closing markers, which previously surfaced as "Unable to apply diff - Expected
1293+ // '=======' was not found". These fixtures exercise the full applyDiff() path end-to-end.
1294+ describe ( "truncated Grok diff regression (#186)" , ( ) => {
1295+ const grokStrategy = new MultiSearchReplaceDiffStrategy ( 1.0 , 5 )
1296+ const originalContent = 'function greet() {\n\treturn "hello"\n}\n'
1297+ const expectedContent = 'function greet() {\n\treturn "hi there"\n}\n'
1298+
1299+ it ( "applies a diff whose closing >>>>>>> REPLACE marker was truncated" , async ( ) => {
1300+ const diff =
1301+ "src/greet.ts\n" + "<<<<<<< SEARCH\n" + '\treturn "hello"\n' + "=======\n" + '\treturn "hi there"'
1302+ const result = await grokStrategy . applyDiff ( originalContent , diff )
1303+ expect ( result . success ) . toBe ( true )
1304+ if ( result . success ) {
1305+ expect ( result . content ) . toBe ( expectedContent )
1306+ }
1307+ } )
1308+
1309+ it ( "applies a diff truncated before the ======= separator" , async ( ) => {
1310+ const diff = "src/greet.ts\n" + "<<<<<<< SEARCH\n" + '\treturn "hello"\n' + '\treturn "hi there"'
1311+ const result = await grokStrategy . applyDiff ( originalContent , diff )
1312+ expect ( result . success ) . toBe ( true )
1313+ if ( result . success ) {
1314+ expect ( result . content ) . toBe ( expectedContent )
1315+ }
1316+ } )
1317+
1318+ it ( "leaves a well-formed multi-block diff unchanged" , async ( ) => {
1319+ const multiBlock = "export const a = 1\nexport const b = 2\n"
1320+ const diff =
1321+ "src/consts.ts\n" +
1322+ "<<<<<<< SEARCH\n" +
1323+ "export const a = 1\n" +
1324+ "=======\n" +
1325+ "export const a = 10\n" +
1326+ ">>>>>>> REPLACE\n" +
1327+ "<<<<<<< SEARCH\n" +
1328+ "export const b = 2\n" +
1329+ "=======\n" +
1330+ "export const b = 20\n" +
1331+ ">>>>>>> REPLACE"
1332+ const result = await grokStrategy . applyDiff ( multiBlock , diff )
1333+ expect ( result . success ) . toBe ( true )
1334+ if ( result . success ) {
1335+ expect ( result . content ) . toBe ( "export const a = 10\nexport const b = 20\n" )
1336+ }
1337+ } )
1338+ } )
12901339} )
0 commit comments