@@ -25,19 +25,35 @@ describe("toolCallDiff", () => {
2525
2626 it ( "renders added and deleted file contents as a file diff" , ( ) => {
2727 for ( const operation of [ "add" , "delete" ] ) {
28- for ( const ending of [ "" , "\n" ] ) {
28+ for ( const [ content , ending ] of [
29+ [ "first\nsecond" , "" ] ,
30+ [ "first\nsecond" , "\n" ] ,
31+ [ "--- notes\nsecond" , "\n" ] ,
32+ [ "diff --git notes\nsecond" , "\n" ] ,
33+ [ "--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after" , "\n" ] ,
34+ [
35+ "diff --git a/embedded.ts b/embedded.ts\n--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after" ,
36+ "\n" ,
37+ ] ,
38+ ] as const ) {
39+ const recordedContent = `${ content } ${ ending } ` ;
2940 const rendered = getRenderablePatch (
3041 toolCallDiff ( {
3142 fileName : "file with spaces.ts" ,
32- diffStr : `first\nsecond ${ ending } ` ,
43+ diffStr : recordedContent ,
3344 changes : [ { operation, path : "file with spaces.ts" } ] ,
3445 } ) . patch ,
3546 ) ;
3647 expect ( rendered ?. kind ) . toBe ( "files" ) ;
3748 if ( rendered ?. kind !== "files" ) throw new Error ( "Expected a file diff" ) ;
3849 expect ( resolveFileDiffPath ( rendered . files [ 0 ] ! ) ) . toBe ( "file with spaces.ts" ) ;
39- expect ( rendered . files [ 0 ] ! . additionLines ) . toHaveLength ( operation === "add" ? 2 : 0 ) ;
40- expect ( rendered . files [ 0 ] ! . deletionLines ) . toHaveLength ( operation === "delete" ? 2 : 0 ) ;
50+ const file = rendered . files [ 0 ] ! ;
51+ const lines = content . split ( "\n" ) . length ;
52+ expect ( file . additionLines ) . toHaveLength ( operation === "add" ? lines : 0 ) ;
53+ expect ( file . deletionLines ) . toHaveLength ( operation === "delete" ? lines : 0 ) ;
54+ expect ( ( operation === "add" ? file . additionLines : file . deletionLines ) . join ( "" ) ) . toBe (
55+ recordedContent . replace ( / \n $ / , "" ) ,
56+ ) ;
4157 }
4258 }
4359 } ) ;
@@ -73,6 +89,19 @@ describe("toolCallDiff", () => {
7389 } ) ;
7490
7591 it ( "preserves complete patches and unsupported provider output" , ( ) => {
92+ for ( const operation of [ "add" , "delete" ] ) {
93+ const diffStr = [
94+ "diff --git a/a.ts b/a.ts" ,
95+ `--- ${ operation === "add" ? "/dev/null" : "a/a.ts" } ` ,
96+ `+++ ${ operation === "add" ? "b/a.ts" : "/dev/null" } ` ,
97+ operation === "add" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@" ,
98+ operation === "add" ? "+content" : "-content" ,
99+ "" ,
100+ ] . join ( "\n" ) ;
101+ expect (
102+ toolCallDiff ( { fileName : "a.ts" , diffStr, changes : [ { operation, path : "a.ts" } ] } ) . patch ,
103+ ) . toBe ( diffStr ) ;
104+ }
76105 for ( const diffStr of [
77106 undefined ,
78107 "" ,
0 commit comments