char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { DiffLineKind } from '$lib/enums' ; 2import { computeLineDiff , type DiffLine , renderUnifiedDiff } from '$lib/utils' ; 3import { describe , expect , it } from 'vitest' ; 4 5describe ( 'computeLineDiff' , () => { 6it ( 'returns empty for two empty inputs' , () => { 7expect ( computeLineDiff ( '' , '' )). toEqual ([]); 8}); 9 10it ( 'marks every line as removed for an empty new text' , () => { 11expect ( computeLineDiff ( 'a\nb\nc' , '' )). toEqual ([ 12{ kind :'remove' , oldLine :1 , text :'a' }, 13{ kind :'remove' , oldLine :2 , text :'b' }, 14{ kind :'remove' , oldLine :3 , text :'c' } 15]); 16}); 17 18it ( 'marks every line as added for an empty old text' , () => { 19expect ( computeLineDiff ( '' , 'a\nb' )). toEqual ([ 20{ kind :'add' , newLine :1 , text :'a' }, 21{ kind :'add' , newLine :2 , text :'b' } 22]); 23}); 24 25it ( 'detects a single-line replace' , () => { 26expect ( computeLineDiff ( 'old' , 'new' )). toEqual ([ 27{ kind :'add' , newLine :1 , text :'new' }, 28{ kind :'remove' , oldLine :1 , text :'old' } 29]); 30}); 31 32it ( 'preserves interleaved context around additions' , () => { 33const oldText = [ 'a' , 'b' , 'c' ]. join ( '\n' ); 34const newText = [ 'a' , 'b' , 'B' , 'c' ]. join ( '\n' ); 35 36expect ( computeLineDiff ( oldText , newText )). toEqual ([ 37{ kind :'context' , newLine :1 , oldLine :1 , text :'a' }, 38{ kind :'context' , newLine :2 , oldLine :2 , text :'b' }, 39{ kind :'add' , newLine :3 , text :'B' }, 40{ kind :'context' , newLine :4 , oldLine :3 , text :'c' } 41]); 42}); 43 44it ( 'preserves interleaved context around an isolated replace' , () => { 45// Multi-line context around a one-line change -> the diff should 46// show context flanking the changed line at its natural position. 47const oldText = [ 'a' , 'b' , 'c' , 'd' ]. join ( '\n' ); 48const newText = [ 'a' , 'b' , 'X' , 'd' ]. join ( '\n' ); 49 50expect ( computeLineDiff ( oldText , newText )). toEqual ([ 51{ kind :'context' , newLine :1 , oldLine :1 , text :'a' }, 52{ kind :'context' , newLine :2 , oldLine :2 , text :'b' }, 53{ kind :'add' , newLine :3 , text :'X' }, 54{ kind :'remove' , oldLine :3 , text :'c' }, 55{ kind :'context' , newLine :4 , oldLine :4 , text :'d' } 56]); 57}); 58 59it ( 'preserves interleaved context around removals' , () => { 60const oldText = [ 'a' , 'b' , 'c' , 'd' ]. join ( '\n' ); 61const newText = [ 'a' , 'c' , 'd' ]. join ( '\n' ); 62 63expect ( computeLineDiff ( oldText , newText )). toEqual ([ 64{ kind :'context' , newLine :1 , oldLine :1 , text :'a' }, 65{ kind :'remove' , oldLine :2 , text :'b' }, 66{ kind :'context' , newLine :2 , oldLine :3 , text :'c' }, 67{ kind :'context' , newLine :3 , oldLine :4 , text :'d' } 68]); 69}); 70 71it ( 'handles purely identical inputs' , () => { 72const text = 'x\ny\nz' ; 73const result = computeLineDiff ( text , text ); 74 75expect ( result ). toEqual ([ 76{ kind :'context' , newLine :1 , oldLine :1 , text :'x' }, 77{ kind :'context' , newLine :2 , oldLine :2 , text :'y' }, 78{ kind :'context' , newLine :3 , oldLine :3 , text :'z' } 79]); 80}); 81 82it ( 'strips a trailing newline on the old/new inputs' , () => { 83expect ( computeLineDiff ( 'a\n' , 'a\nb\n' )). toEqual ([ 84{ kind :'context' , newLine :1 , oldLine :1 , text :'a' }, 85{ kind :'add' , newLine :2 , text :'b' } 86]); 87}); 88 89it ( 'normalizes trailing CR on each line' , () => { 90expect ( computeLineDiff ( 'a\r\nb\r\n' , 'a\nb' )). toEqual ([ 91{ kind :'context' , newLine :1 , oldLine :1 , text :'a' }, 92{ kind :'context' , newLine :2 , oldLine :2 , text :'b' } 93]); 94}); 95 96it ( 'keeps line numbers monotonic across mixed add/remove/context' , () => { 97const oldText = [ 'l1' , 'l2' , 'l3' , 'l4' , 'l5' ]. join ( '\n' ); 98const newText = [ 'l1' , 'l2-EDIT' , 'l3' , 'l4-NEW' , 'l5' ]. join ( '\n' ); 99const diff = computeLineDiff ( oldText , newText ); 100 101// Walk the diff: every oldLine must increase strictly, and every 102// newLine must increase strictly. Lines missing one side (add or 103// remove) carry no number on that side. 104let lastOld = 0 ; 105let lastNew = 0 ; 106 107for ( const line of diff ) { 108if ( line . oldLine !== undefined ) { 109expect ( line . oldLine ). toBeGreaterThan ( lastOld ); 110lastOld = line . oldLine ; 111} 112 113if ( line . newLine !== undefined ) { 114expect ( line . newLine ). toBeGreaterThan ( lastNew ); 115lastNew = line . newLine ; 116} 117} 118}); 119}); 120 121describe ( 'renderUnifiedDiff' , () => { 122it ( 'returns empty string for empty diff' , () => { 123expect ( renderUnifiedDiff ([])). toBe ( '' ); 124}); 125 126it ( 'prefixes each line with `+`, `-`, or a single space' , () => { 127const lines :DiffLine [] = [ 128{ kind :DiffLineKind . CONTEXT , text :'ctx' }, 129{ kind :DiffLineKind . ADD , text :'plus' }, 130{ kind :DiffLineKind . REMOVE , text :'minus' } 131]; 132 133expect ( renderUnifiedDiff ( lines )). toBe ( ' ctx\n+plus\n-minus' ); 134}); 135 136it ( 'ignores oldLine/newLine metadata when emitting prefixes' , () => { 137const lines :DiffLine [] = [ 138{ kind :DiffLineKind . CONTEXT , newLine :1 , oldLine :1 , text :'a' }, 139{ kind :DiffLineKind . ADD , newLine :2 , text :'b' } 140]; 141 142expect ( renderUnifiedDiff ( lines )). toBe ( ' a\n+b' ); 143}); 144});