11import { describe , expect , test } from "bun:test"
22import { parseDiffFromFile } from "@pierre/diffs"
3+ import { createTwoFilesPatch } from "diff"
34import { completePatchContents , normalize , resolveFileDiff , text } from "./session-diff"
45
56const header = "diff --git a/a.ts b/a.ts\nindex 1a2b3c4..5d6e7f8 100644\n--- a/a.ts\n+++ b/a.ts\n"
@@ -103,7 +104,10 @@ describe("session diff", () => {
103104 } )
104105
105106 test . each ( [
106- [ "missing final newlines" , "@@ -1,2 +1,2 @@\n one\n-two\n\\ No newline at end of file\n+three\n\\ No newline at end of file\n" ] ,
107+ [
108+ "missing final newlines" ,
109+ "@@ -1,2 +1,2 @@\n one\n-two\n\\ No newline at end of file\n+three\n\\ No newline at end of file\n" ,
110+ ] ,
107111 [ "an added final newline" , "@@ -1,2 +1,2 @@\n one\n-two\n\\ No newline at end of file\n+two\n" ] ,
108112 [ "CRLF lines" , "@@ -1,3 +1,3 @@\n one\r\n-two\r\n+three\r\n four\r\n" ] ,
109113 [ "a new file" , "@@ -0,0 +1,3 @@\n+one\n+two\n+three\n" ] ,
@@ -119,13 +123,19 @@ describe("session diff", () => {
119123 { name : "a.ts" , contents : contents . after } ,
120124 )
121125
122- expect ( { ...resolveFileDiff ( { file : "a.ts" , patch } ) , cacheKey : undefined } ) . toEqual ( { ...expected , cacheKey : undefined } )
126+ expect ( { ...resolveFileDiff ( { file : "a.ts" , patch } ) , cacheKey : undefined } ) . toEqual ( {
127+ ...expected ,
128+ cacheKey : undefined ,
129+ } )
123130 } )
124131
125- test . each ( [ "a\tb.ts" , "a\nb.ts" , '"quoted"' , " padded.ts " ] ) ( "keeps the exact file name %j for complete patches" , ( file ) => {
126- expect ( resolveFileDiff ( { file, patch : `${ header } @@ -1,2 +1,2 @@\n one\n-old\n+new\n` } ) . name ) . toBe ( file )
127- expect ( resolveFileDiff ( { file, patch : `${ header } @@ -0,0 +0,0 @@\n` } ) . name ) . toBe ( file )
128- } )
132+ test . each ( [ "a\tb.ts" , "a\nb.ts" , '"quoted"' , " padded.ts " ] ) (
133+ "keeps the exact file name %j for complete patches" ,
134+ ( file ) => {
135+ expect ( resolveFileDiff ( { file, patch : `${ header } @@ -1,2 +1,2 @@\n one\n-old\n+new\n` } ) . name ) . toBe ( file )
136+ expect ( resolveFileDiff ( { file, patch : `${ header } @@ -0,0 +0,0 @@\n` } ) . name ) . toBe ( file )
137+ } ,
138+ )
129139
130140 test ( "collapses unchanged runs in complete patches into the hunks of a fresh file diff" , ( ) => {
131141 const before = Array . from ( { length : 40 } , ( _ , index ) => `line ${ index } \n` )
@@ -188,16 +198,16 @@ describe("session diff", () => {
188198 expect ( resolveFileDiff ( { file : "b.ts" , patch } ) . name ) . toBe ( "b.ts" )
189199 } )
190200
191- test . each ( [
192- "@@ -1 +1 @@\n-old\n+new\n " ,
193- "--- a.ts\t\n+++ a.ts\t\n@@ -1 +1 @@\n-old\n+new\n" ,
194- ] ) ( "reuses a highlight identity for the same cached patch: %s" , ( patch ) => {
195- const first = resolveFileDiff ( { file : "a.ts" , patch } )
196- expect ( first . cacheKey ) . toBeString ( )
197- expect ( resolveFileDiff ( { file : "a .ts" , patch } ) . cacheKey ) . toBe ( first . cacheKey )
198- expect ( resolveFileDiff ( { file : "b .ts" , patch } ) . cacheKey ) . not . toBe ( first . cacheKey )
199- expect ( resolveFileDiff ( { file : "a.ts" , patch : patch . replace ( "+new" , "+next" ) } ) . cacheKey ) . not . toBe ( first . cacheKey )
200- } )
201+ test . each ( [ "@@ -1 +1 @@\n-old\n+new\n" , "--- a.ts\t\n+++ a.ts\t\n@@ -1 +1 @@\n-old\n+new\n" ] ) (
202+ "reuses a highlight identity for the same cached patch: %s " ,
203+ ( patch ) => {
204+ const first = resolveFileDiff ( { file : "a.ts" , patch } )
205+ expect ( first . cacheKey ) . toBeString ( )
206+ expect ( resolveFileDiff ( { file : "a.ts" , patch } ) . cacheKey ) . toBe ( first . cacheKey )
207+ expect ( resolveFileDiff ( { file : "b .ts" , patch } ) . cacheKey ) . not . toBe ( first . cacheKey )
208+ expect ( resolveFileDiff ( { file : "a .ts" , patch : patch . replace ( "+new" , "+next" ) } ) . cacheKey ) . not . toBe ( first . cacheKey )
209+ } ,
210+ )
201211
202212 test ( "keys preloaded content diffs by file name and content" , ( ) => {
203213 const diff = { file : "a.ts" , before : "one\n" , after : "two\n" , additions : 1 , deletions : 1 }
@@ -212,8 +222,9 @@ describe("session diff", () => {
212222 const patch = "@@ -1 +1 @@\n-old\n+new\n"
213223 const first = resolveFileDiff ( { file : "evicted.ts" , patch } )
214224
215- const keys = Array . from ( { length : 20 } , ( _ , index ) =>
216- resolveFileDiff ( { file : "evicted.ts" , patch : patch . replace ( "+new" , `+new${ index } ` ) } ) . cacheKey ,
225+ const keys = Array . from (
226+ { length : 20 } ,
227+ ( _ , index ) => resolveFileDiff ( { file : "evicted.ts" , patch : patch . replace ( "+new" , `+new${ index } ` ) } ) . cacheKey ,
217228 )
218229
219230 expect ( keys ) . not . toContain ( first . cacheKey )
@@ -266,4 +277,75 @@ describe("session diff", () => {
266277 expect ( text ( view , "deletions" ) ) . toBe ( "" )
267278 expect ( text ( view , "additions" ) ) . toBe ( "" )
268279 } )
280+
281+ describe ( "real patches" , ( ) => {
282+ const before = Array . from ( { length : 30 } , ( _ , index ) => `const line${ index } = ${ index } \n` ) . join ( "" )
283+ const after = before . replace ( "const line3 = 3\n" , "const line3 = 33\n" ) . replace ( "const line25 = 25\n" , "" )
284+ const sum = 'import { a } from "./a"\n\nexport function sum(x: number, y: number) {\n return x + y\n}\n'
285+
286+ const git = ( header : string ) =>
287+ `diff --git ${ header } \nindex da208dc..e8d3c2d 100644\n--- ${ header . split ( " " ) [ 0 ] } \n+++ ${ header . split ( " " ) [ 1 ] } \n@@ -1,5 +1,5 @@\n import { a } from "./a"\n \n-export function sum(x: number, y: number) {\n- return x + y\n+export function sum(x: number, y: number, z = 0) {\n+ return x + y + z\n }\n`
288+
289+ test ( "reads an edit tool patch as partial hunks" , ( ) => {
290+ // The edit and write tools call createTwoFilesPatch with jsdiff's default context and no headers.
291+ const fileDiff = resolveFileDiff ( {
292+ file : "src/lines.ts" ,
293+ patch : createTwoFilesPatch ( "src/lines.ts" , "src/lines.ts" , before , after ) ,
294+ } )
295+
296+ expect ( fileDiff . name ) . toBe ( "src/lines.ts" )
297+ expect ( fileDiff . isPartial ) . toBe ( true )
298+ expect (
299+ fileDiff . hunks . map ( ( hunk ) => [ hunk . deletionStart , hunk . deletionCount , hunk . additionStart , hunk . additionCount ] ) ,
300+ ) . toEqual ( [
301+ [ 1 , 8 , 1 , 8 ] ,
302+ [ 22 , 9 , 22 , 8 ] ,
303+ ] )
304+ expect ( fileDiff . deletionLines ) . toContain ( "const line3 = 3\n" )
305+ expect ( fileDiff . additionLines ) . toContain ( "const line3 = 33\n" )
306+ expect ( fileDiff . additionLines ) . not . toContain ( "const line25 = 25\n" )
307+ } )
308+
309+ test . each ( [
310+ [ "an edit" , before , after ] ,
311+ [ "a new file" , "" , "one\ntwo\n" ] ,
312+ [ "a deleted file" , "one\ntwo\n" , "" ] ,
313+ [ "CRLF lines without a final newline" , "one\r\ntwo\r\nthree" , "one\r\n2\r\nthree" ] ,
314+ [ "an added final newline" , "one\ntwo" , "one\ntwo\n" ] ,
315+ ] ) ( "renders a full-context snapshot patch of %s like a fresh file diff" , ( _ , before , after ) => {
316+ const patch = createTwoFilesPatch ( "a.ts" , "a.ts" , before , after , "" , "" , { context : 2_147_483_647 } )
317+
318+ expect ( completePatchContents ( patch ) ) . toEqual ( { before, after } )
319+ expect ( { ...resolveFileDiff ( { file : "a.ts" , patch } ) , cacheKey : undefined } ) . toEqual ( {
320+ ...parseDiffFromFile ( { name : "a.ts" , contents : before } , { name : "a.ts" , contents : after } ) ,
321+ cacheKey : undefined ,
322+ } )
323+ } )
324+
325+ test . each ( [ "a/sum.ts b/sum.ts" , "sum.ts sum.ts" , "i/sum.ts w/sum.ts" ] ) (
326+ "renders a full-context git patch with header %j as a complete diff" ,
327+ ( header ) => {
328+ // Git's diff.noprefix and diff.mnemonicPrefix settings change the header, not the hunks.
329+ expect ( completePatchContents ( git ( header ) ) ) . toEqual ( {
330+ before : sum ,
331+ after : sum . replace ( "y: number)" , "y: number, z = 0)" ) . replace ( "x + y\n" , "x + y + z\n" ) ,
332+ } )
333+ expect ( resolveFileDiff ( { file : "sum.ts" , patch : git ( header ) } ) . isPartial ) . toBe ( false )
334+ } ,
335+ )
336+
337+ test ( "keeps a missing final newline from a real CRLF git patch" , ( ) => {
338+ const patch =
339+ "diff --git a/crlf.txt b/crlf.txt\nindex bc16163..88fdbd5 100644\n--- a/crlf.txt\n+++ b/crlf.txt\n@@ -1,3 +1,3 @@\n one\r\n-two\r\n+2\r\n three\n\\ No newline at end of file\n"
340+
341+ expect ( completePatchContents ( patch ) ) . toEqual ( { before : "one\r\ntwo\r\nthree" , after : "one\r\n2\r\nthree" } )
342+ } )
343+
344+ test ( "ignores an edit tool patch whose hunk lost a line" , ( ) => {
345+ const patch = createTwoFilesPatch ( "a.ts" , "a.ts" , before , after ) . replace ( " const line1 = 1\n" , "" )
346+
347+ expect ( resolveFileDiff ( { file : "a.ts" , patch } ) . hunks ) . toEqual ( [ ] )
348+ expect ( completePatchContents ( patch ) ) . toBeUndefined ( )
349+ } )
350+ } )
269351} )
0 commit comments