char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
2396910
main
1import { assert , assertEquals } from "@std/assert" ; 2import { undo } from "@codemirror/commands" ; 3import { getChunks , getOriginalDoc } from "@codemirror/merge" ; 4import { withDom } from "@berry/util/dom.ts" ; 5import type { OpenFile } from "./edit-types.ts" ; 6import { diffEditor } from "./editor.ts" ; 7 8Deno . test ( "diff layout changes retain multiline edits, selection, and undo without serializing removed lines" , () => 9withDom (() => { 10const parent = document . createElement ( "div" ); 11document . body . append ( parent ); 12const opened :OpenFile = { 13working :{ machine :"local" , root :"/repo" , vcs :"jj" , identity :"change" }, 14file :{ 15path :"a.txt" , 16from :null , 17status :"modified" , 18binary :false , 19modes :{ old :"100644" , new :"100644" }, 20hunks :[], 21}, 22original :"first\r\nremoved\r\nend" , 23content :"first\r\nnew\r\nend" , 24version :"version" , 25}; 26let latest = opened . content ; 27const editor = diffEditor ( parent , opened , "unified" , { 28changed :( content ) => latest = content , 29save () {}, 30escape () {}, 31}); 32try { 33const start = editor . view . state . doc . line ( 2 ). from ; 34editor . view . dispatch ({ 35changes :{ from :start , to :start + 3 , insert :"edited\nextra" }, 36selection :{ anchor :start + 6 }, 37}); 38assertEquals ( latest , "first\r\nedited\r\nextra\r\nend" ); 39const selection = editor . view . state . selection . main ; 40editor . layout ( "split" ); 41assertEquals ( editor . content (), latest ); 42assertEquals ( editor . view . state . selection . main , selection ); 43assertEquals ( 44parent . querySelector ( '[aria-label="Original a.txt"]' ) ! . getAttribute ( "contenteditable" ), 45"false" , 46); 47editor . layout ( "unified" ); 48assertEquals ( editor . view . state . selection . main , selection ); 49assertEquals ( getOriginalDoc ( editor . view . state ). toString (), "first\nremoved\nend" ); 50undo ( editor . view ); 51assertEquals ( editor . content (), opened . content ); 52} finally { 53editor . destroy (); 54} 55})); 56 57Deno . test ( "sparse edits in long files leave unchanged code out of the editor's diff" , () => 58withDom (() => { 59const parent = document . createElement ( "div" ); 60document . body . append ( parent ); 61const unchanged = Array . from ( 62{ length :500 }, 63( _ , i ) => `function kept ${ i } () { return value + ${ i } ; }\n` , 64). join ( "" ); 65const opened :OpenFile = { 66working :{ machine :"local" , root :"/repo" , vcs :"jj" , identity :"change" }, 67file :{ 68path :"long.ts" , 69from :null , 70status :"modified" , 71binary :false , 72modes :{ old :"100644" , new :"100644" }, 73hunks :[], 74}, 75original :`function unused() { return 0; }\n\n ${ unchanged } export const label = "old";\n` , 76content :` ${ unchanged } export const label = "new";\n` , 77version :"version" , 78}; 79const editor = diffEditor ( parent , opened , "unified" , { 80changed () {}, 81save () {}, 82escape () {}, 83}); 84const check = () => { 85const oldStart = opened . original . indexOf ( unchanged ); 86const newStart = editor . content (). indexOf ( unchanged ); 87assert ( newStart >= 0 ); 88const chunks = getChunks ( editor . view . state ) ! . chunks ; 89assert ( chunks . length > 0 ); 90for ( const chunk of chunks ) { 91assert ( chunk . toA <= oldStart || chunk . fromA >= oldStart + unchanged . length ); 92assert ( chunk . toB <= newStart || chunk . fromB >= newStart + unchanged . length ); 93} 94}; 95try { 96check (); 97editor . layout ( "split" ); 98check (); 99const from = editor . view . state . doc . toString (). lastIndexOf ( '"new"' ); 100editor . view . dispatch ({ changes :{ from, to :from + 5 , insert :'"edited"' } }); 101check (); 102assertEquals ( editor . content (), ` ${ unchanged } export const label = "edited";\n` ); 103editor . layout ( "unified" ); 104check (); 105} finally { 106editor . destroy (); 107} 108}));