char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { FileMentionEntryType } from '$lib/enums' ; 2import { 3buildMentionInsertion , 4containsFileMentionLink , 5decodeFileLinkPath , 6encodeFileLinkPath , 7fileMentionLinkRe , 8getMentionBadgeIconPaths , 9getMentionBadgeLabel , 10MENTION_BADGE_FILE_ICON_PATHS , 11MENTION_BADGE_FOLDER_ICON_PATHS 12} from '$lib/utils' ; 13import { describe , expect , it } from 'vitest' ; 14 15describe ( 'encodeFileLinkPath' , () => { 16it ( 'leaves a clean path unchanged' , () => { 17expect ( encodeFileLinkPath ( '/Users/foo/bar.txt' )). toBe ( '/Users/foo/bar.txt' ); 18}); 19 20it ( 'encodes spaces per path segment' , () => { 21expect ( 22encodeFileLinkPath ( '/Users/allozaur/Desktop/Screenshot 2026-08-05 at 11.33.45.png' ) 23). toBe ( '/Users/allozaur/Desktop/Screenshot%202026-08-05%20at%2011.33.45.png' ); 24}); 25 26it ( 'preserves the leading and trailing slash (directory marker)' , () => { 27expect ( encodeFileLinkPath ( '/Users/foo/bar/' )). toBe ( '/Users/foo/bar/' ); 28}); 29 30it ( 'encodes parentheses in macOS screenshot names' , () => { 31expect ( encodeFileLinkPath ( '/Users/foo/Pic (1).png' )). toBe ( '/Users/foo/Pic%20(1).png' ); 32}); 33}); 34 35describe ( 'fileMentionLinkRe' , () => { 36it ( 'matches a standard mention link' , () => { 37expect ( fileMentionLinkRe (). test ( '[docs](file:///a/b)' )). toBe ( true ); 38expect ( containsFileMentionLink ( '[docs](file:///a/b)' )). toBe ( true ); 39}); 40 41it ( 'does not match non-file links' , () => { 42expect ( fileMentionLinkRe (). test ( '[foo](https://example.com)' )). toBe ( false ); 43expect ( fileMentionLinkRe (). test ( 'plain text' )). toBe ( false ); 44}); 45 46it ( 'admits a close paren in a macOS-style file name' , () => { 47const match = fileMentionLinkRe (). exec ( 48'[Screenshot (1).png](file:///Users/foo/Screenshot (1).png)' 49); 50 51expect ( match ). not . toBeNull (); 52expect ( match ?.[ 1 ]). toBe ( 'Screenshot (1).png' ); 53expect ( match ?.[ 2 ]). toBe ( '/Users/foo/Screenshot (1).png' ); 54}); 55 56it ( 'admits a parenthesized folder segment' , () => { 57expect ( 58fileMentionLinkRe (). exec ( '[main.rs](file:///Users/foo/Project (Stuff)/main.rs)' )?.[ 2 ] 59). toBe ( '/Users/foo/Project (Stuff)/main.rs' ); 60}); 61 62it ( 'stops at the closing paren of an adjacent badge' , () => { 63expect ( fileMentionLinkRe (). exec ( '[a](file:///p)[b](file:///q)' )?.[ 0 ]). toBe ( '[a](file:///p)' ); 64}); 65}); 66 67describe ( 'getMentionBadgeIconPaths' , () => { 68it ( 'returns the folder glyphs for a trailing-separator path' , () => { 69expect ( getMentionBadgeIconPaths ( '/Users/foo/bar/' )). toBe ( MENTION_BADGE_FOLDER_ICON_PATHS ); 70}); 71 72it ( 'returns the file glyphs otherwise' , () => { 73expect ( getMentionBadgeIconPaths ( '/Users/foo/bar.txt' )). toBe ( MENTION_BADGE_FILE_ICON_PATHS ); 74}); 75}); 76 77describe ( 'getMentionBadgeLabel' , () => { 78it ( 'returns the name by default' , () => { 79expect ( getMentionBadgeLabel ( 'bar' , '/Users/foo/bar/' , false )). toBe ( 'bar' ); 80}); 81 82it ( 'renders the decoded full path without the trailing separator' , () => { 83expect ( getMentionBadgeLabel ( 'bar' , '/Users/foo/bar/' , true )). toBe ( '/Users/foo/bar' ); 84expect ( getMentionBadgeLabel ( 'shot' , '/Users/foo/Screenshot%20(1).png' , true )). toBe ( 85'/Users/foo/Screenshot (1).png' 86); 87}); 88 89it ( 'abbreviates a known home prefix to a tilde' , () => { 90expect ( getMentionBadgeLabel ( 'main.rs' , '/home/user/src/main.rs' , true , '/home/user' )). toBe ( 91'~/src/main.rs' 92); 93}); 94 95it ( 'falls back to the name when the decoded path is empty' , () => { 96expect ( getMentionBadgeLabel ( 'root' , '/' , true )). toBe ( 'root' ); 97}); 98}); 99 100describe ( 'decodeFileLinkPath' , () => { 101it ( 'decodes encoded segments back to the original path' , () => { 102expect ( 103decodeFileLinkPath ( '/Users/allozaur/Desktop/Screenshot%202026-08-05%20at%2011.33.45.png' ) 104). toBe ( '/Users/allozaur/Desktop/Screenshot 2026-08-05 at 11.33.45.png' ); 105}); 106 107it ( 'is the inverse of encodeFileLinkPath' , () => { 108for ( const path of [ 109'/a/b.txt' , 110'/Users/foo/Desktop/Screenshot 2026-08-05 at 11.33.45.png' , 111'/Users/foo/bar (1)/dir/' , 112'/sp ace/pa%th.txt' 113]) { 114expect ( decodeFileLinkPath ( encodeFileLinkPath ( path ))). toBe ( path ); 115} 116}); 117 118it ( 'falls back to the input on malformed percent sequences' , () => { 119expect ( decodeFileLinkPath ( '/a/%zz.txt' )). toBe ( '/a/%zz.txt' ); 120}); 121}); 122 123describe ( 'buildMentionInsertion' , () => { 124const file = ( path :string , name :string ) => ({ 125 name, 126 path, 127type :FileMentionEntryType . FILE 128}); 129const dir = ( path :string , name :string ) => ({ 130 name, 131 path, 132type :FileMentionEntryType . DIRECTORY 133}); 134 135it ( 'splices a root-anchored file link in place of the token' , () => { 136const value = 'hello @repo' ; 137const result = buildMentionInsertion ( file ( '/Users/foo/myRepo' , 'myRepo' ), value , { 138end :11 , 139start :6 140}); 141 142expect ( result ). not . toBeNull (); 143const { caretOffset, newValue} = result ! ; 144 145expect ( newValue ). toBe ( 'hello [myRepo](file:///Users/foo/myRepo) ' ); 146expect ( caretOffset ). toBe ( 6 + '[myRepo](file:///Users/foo/myRepo) ' . length ); 147}); 148 149it ( 'keeps the trailing slash on the directory marker' , () => { 150const value = 'see @src' ; 151const { newValue} = buildMentionInsertion ( dir ( '/Users/foo/myRepo/src/' , 'src' ), value , { 152end :8 , 153start :4 154}) ! ; 155 156expect ( newValue ). toBe ( 'see [src](file:///Users/foo/myRepo/src/) ' ); 157}); 158 159it ( 'escapes spaces and parens in the target' , () => { 160const value = '@pic' ; 161const { newValue} = buildMentionInsertion ( 162file ( '/Users/foo/Desktop/Pic (1).png' , 'Pic (1).png' ), 163value , 164{ end :4 , start :0 } 165) ! ; 166 167expect ( newValue ). toBe ( '[Pic (1).png](file:///Users/foo/Desktop/Pic%20(1).png) ' ); 168}); 169 170it ( 're-adds the directory marker when the cleaned path empties' , () => { 171const { newValue} = buildMentionInsertion ( dir ( '/' , 'root' ), '/' , { end :1 , start :0 }) ! ; 172 173expect ( newValue ). toBe ( '[root](file:///) ' ); 174}); 175 176it ( 'returns null for an out-of-range token' , () => { 177expect ( buildMentionInsertion ( file ( '/a/b.txt' , 'b.txt' ), 'x' , { end :5 , start :0 })). toBeNull (); 178expect ( buildMentionInsertion ( file ( '/a/b.txt' , 'b.txt' ), 'x' , { end :1 , start :2 })). toBeNull (); 179}); 180});