char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
806cc3c
main
1import { assertEquals } from "@std/assert" ; 2import { narrow , parsePatch } from "./patch.ts" ; 3 4Deno . test ( "renames, deletions, additions and mode changes read as what happened to each file" , () => { 5const files = parsePatch ([ 6"diff --git a/old.txt b/new.txt" , 7"similarity index 79%" , 8"rename from old.txt" , 9"rename to new.txt" , 10"index f384549..b2f931a 100644" , 11"--- a/old.txt" , 12"+++ b/new.txt" , 13"@@ -1,2 +1,3 @@" , 14" one" , 15" two" , 16"+three" , 17"diff --git a/keep.txt b/keep.txt" , 18"deleted file mode 100644" , 19"index de98044..0000000" , 20"--- a/keep.txt" , 21"+++ /dev/null" , 22"@@ -1 +0,0 @@" , 23"-a" , 24"diff --git a/empty b/empty" , 25"new file mode 100644" , 26"index 0000000..e69de29" , 27"diff --git a/script.sh b/script.sh" , 28"old mode 100644" , 29"new mode 100755" , 30"diff --git a/bin.dat b/bin.dat" , 31"index bdc955b..8835708 100644" , 32"Binary files a/bin.dat and b/bin.dat differ" , 33"" , 34]. join ( "\n" )); 35assertEquals ( 36files . map (({ path, from, status, modes, binary}) => ({ path, from, status, modes, binary})), 37[ 38{ 39path :"new.txt" , 40from :"old.txt" , 41status :"renamed" , 42modes :{ old :"100644" , new :"100644" }, 43binary :false , 44}, 45{ 46path :"keep.txt" , 47from :null , 48status :"deleted" , 49modes :{ old :"100644" , new :null }, 50binary :false , 51}, 52{ 53path :"empty" , 54from :null , 55status :"added" , 56modes :{ old :null , new :"100644" }, 57binary :false , 58}, 59{ 60path :"script.sh" , 61from :null , 62status :"modified" , 63modes :{ old :"100644" , new :"100755" }, 64binary :false , 65}, 66{ 67path :"bin.dat" , 68from :null , 69status :"modified" , 70modes :{ old :"100644" , new :"100644" }, 71binary :true , 72}, 73], 74); 75assertEquals ( files [ 0 ]. hunks , [{ old :1 , new :1 , lines :[ " one" , " two" , "+three" ] }]); 76assertEquals ( files [ 1 ]. hunks , [{ old :1 , new :0 , lines :[ "-a" ] }]); 77assertEquals ( files [ 2 ]. hunks , []); 78}); 79 80Deno . test ( "hunks keep missing newlines, carriage returns, and lines that look like headers" , () => { 81const [ file ] = parsePatch ([ 82"commit header that comes first" , 83"" , 84"diff --git a/x b/x" , 85"--- a/x" , 86"+++ b/x" , 87"@@ -1,3 +1,3 @@" , 88"-diff --git a/y b/y" , 89"+--- a/y\r" , 90"" , 91" last" , 92"\\ No newline at end of file" , 93"" , 94]. join ( "\n" )); 95assertEquals ( file . hunks , [{ 96old :1 , 97new :1 , 98lines :[ "-diff --git a/y b/y" , "+--- a/y\r" , " " , " last" , "\\ No newline at end of file" ], 99}]); 100}); 101 102Deno . test ( "unusual paths read as themselves, quoted or not" , () => { 103const files = parsePatch ([ 104'diff --git a/a.txt "b/b \\"c\\".txt"' , 105"similarity index 100%" , 106"rename from a.txt" , 107'rename to "b \\"c\\".txt"' , 108'diff --git "a/we ird\\\\t\\"q\\".txt" "b/we ird\\\\t\\"q\\".txt"' , 109"index 45b983b..ab4a981 100644" , 110'--- "a/we ird\\\\t\\"q\\".txt"\t' , 111'+++ "b/we ird\\\\t\\"q\\".txt"\t' , 112"@@ -1 +1 @@" , 113"-hi" , 114"+ho" , 115"diff --git a/ünï cødé.txt b/ünï cødé.txt" , 116"new file mode 100644" , 117"index 0000000..e69de29" , 118'diff --git "a/\\303\\274.txt" "b/\\303\\274.txt"' , 119"old mode 100644" , 120"new mode 100755" , 121"diff --git a/sp ace b/sp ace" , 122"--- a/sp ace\t" , 123"+++ b/sp ace\t" , 124"@@ -1 +1 @@" , 125"-a" , 126"+b" , 127]. join ( "\n" )); 128assertEquals ( files . map (({ path, from}) => [ path , from ]), [ 129[ 'b "c".txt' , "a.txt" ], 130[ 'we ird\\t"q".txt' , null ], 131[ "ünï cødé.txt" , null ], 132[ "ü.txt" , null ], 133[ "sp ace" , null ], 134]); 135}); 136 137Deno . test ( "narrowing a long file keeps the changes and their context, at their own line numbers" , () => { 138const lines = Array . from ({ length :30 }, ( _ , i ) => ` ${ i + 1 } ` ); 139lines [ 4 ] = "-5" ; 140lines . splice ( 5 , 0 , "+five" ); 141lines [ 25 ] = "-25" ; 142const [ file ] = parsePatch ( 143[ "diff --git a/f b/f" , "--- a/f" , "+++ b/f" , "@@ -1,30 +1,30 @@" , ...lines ]. join ( "\n" ), 144); 145assertEquals ( narrow ( file , 2 ). hunks , [ 146{ old :3 , new :3 , lines :[ " 3" , " 4" , "-5" , "+five" , " 6" , " 7" ] }, 147{ old :23 , new :23 , lines :[ " 23" , " 24" , "-25" , " 26" , " 27" ] }, 148]); 149});