char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
fa4ec4590
char/aliceai-t5-moe
1import { containsCodeSpan , isOffsetInCodeBlock , tokenizeContent } from '$lib/utils' ; 2import { describe , expect , it } from 'vitest' ; 3 4describe ( 'tokenizeContent' , () => { 5it ( 'tokenizes a plain text buffer with no badges' , () => { 6expect ( tokenizeContent ( 'hello world' )). toEqual ([{ kind :'text' , text :'hello world' }]); 7}); 8 9it ( 'tokenizes a single badge' , () => { 10expect ( tokenizeContent ( '[docs](file:///a/b)' )). toEqual ([ 11{ kind :'badge' , name :'docs' , path :'/a/b' } 12]); 13}); 14 15it ( 'tokenizes text around a single badge' , () => { 16expect ( tokenizeContent ( 'hello [docs](file:///a/b) world' )). toEqual ([ 17{ kind :'text' , text :'hello ' }, 18{ kind :'badge' , name :'docs' , path :'/a/b' }, 19{ kind :'text' , text :' world' } 20]); 21}); 22 23it ( 'tokenizes adjacent badges as separate tokens' , () => { 24expect ( tokenizeContent ( '[a](file:///x)[b](file:///y)' )). toEqual ([ 25{ kind :'badge' , name :'a' , path :'/x' }, 26{ kind :'badge' , name :'b' , path :'/y' } 27]); 28}); 29 30it ( 'leaves non-file links untouched in the stream' , () => { 31expect ( tokenizeContent ( 'see [foo](https://example.com) for details' )). toEqual ([ 32{ kind :'text' , text :'see [foo](https://example.com) for details' } 33]); 34}); 35 36it ( 'recognizes badges whose path contains spaces (macOS screenshots)' , () => { 37const path = '/Users/allozaur/Desktop/Screenshot 2026-07-28 at 17.21.50.png' ; 38const source = `[Screenshot 2026-07-28 at 17.21.50.png](file:// ${ path } ) ` ; 39 40expect ( tokenizeContent ( source )). toEqual ([ 41{ kind :'badge' , name :'Screenshot 2026-07-28 at 17.21.50.png' , path}, 42{ kind :'text' , text :' ' } 43]); 44}); 45 46it ( 'recognizes badges whose path lives in the macOS temp folder' , () => { 47const path = 48'/var/folders/78/j28m7pn57wb34bfjwlskh62h0000gn/T/TemporaryItems/NSIRD_screencaptureui_GD0A2R/Screenshot 2026-07-28 at 17.23.28.png' ; 49const source = `[Screenshot 2026-07-28 at 17.23.28.png](file:// ${ path } ) ` ; 50 51expect ( tokenizeContent ( source )). toEqual ([ 52{ kind :'badge' , name :'Screenshot 2026-07-28 at 17.23.28.png' , path}, 53{ kind :'text' , text :' ' } 54]); 55}); 56 57it ( 'keeps text around a badge with spaces in the path' , () => { 58const path = '/Users/allozaur/Desktop/Screenshot 2026-07-28 at 17.21.50.png' ; 59const source = `see [Screenshot 2026-07-28 at 17.21.50.png](file:// ${ path } ) done` ; 60 61expect ( tokenizeContent ( source )). toEqual ([ 62{ kind :'text' , text :'see ' }, 63{ kind :'badge' , name :'Screenshot 2026-07-28 at 17.21.50.png' , path}, 64{ kind :'text' , text :' done' } 65]); 66}); 67 68it ( 'recognizes badges whose path contains a close parenthesis (macOS duplicate files)' , () => { 69const path = '/Users/foo/Screenshot (1).png' ; 70const source = `[Screenshot (1).png](file:// ${ path } ) ` ; 71 72expect ( tokenizeContent ( source )). toEqual ([ 73{ kind :'badge' , name :'Screenshot (1).png' , path}, 74{ kind :'text' , text :' ' } 75]); 76}); 77 78it ( 'recognizes badges whose folder name is wrapped in parentheses' , () => { 79const path = '/Users/foo/Project (Stuff)/main.rs' ; 80const source = `[main.rs](file:// ${ path } ) ` ; 81 82expect ( tokenizeContent ( source )). toEqual ([ 83{ kind :'badge' , name :'main.rs' , path}, 84{ kind :'text' , text :' ' } 85]); 86}); 87 88it ( 'recognizes adjacent badges back-to-back with no separator' , () => { 89const source = '[a](file:///p)[b](file:///q)' ; 90 91expect ( tokenizeContent ( source )). toEqual ([ 92{ kind :'badge' , name :'a' , path :'/p' }, 93{ kind :'badge' , name :'b' , path :'/q' } 94]); 95}); 96 97it ( 'tokenizes inline code with the backticks included' , () => { 98expect ( tokenizeContent ( 'run `npm test` now' )). toEqual ([ 99{ kind :'text' , text :'run ' }, 100{ kind :'code_inline' , text :'`npm test`' }, 101{ kind :'text' , text :' now' } 102]); 103}); 104 105it ( 'tokenizes a fenced code block without a language' , () => { 106const source = 'before\n```\nconst a = 1;\n```\nafter' ; 107 108expect ( tokenizeContent ( source )). toEqual ([ 109{ kind :'text' , text :'before\n' }, 110{ kind :'code_block' , text :'```\nconst a = 1;\n```' }, 111{ kind :'text' , text :'\nafter' } 112]); 113}); 114 115it ( 'tokenizes a fenced code block with a language' , () => { 116const source = '```js\nconst a = 1;\n```' ; 117 118expect ( tokenizeContent ( source )). toEqual ([ 119{ kind :'code_block' , text :'```js\nconst a = 1;\n```' } 120]); 121}); 122 123it ( 'prefers the fenced block over inline spans at triple backticks' , () => { 124expect ( tokenizeContent ( '```a``` ```b```' )). toEqual ([ 125{ kind :'code_block' , text :'```a```' }, 126{ kind :'text' , text :' ' }, 127{ kind :'code_block' , text :'```b```' } 128]); 129}); 130 131it ( 'leaves an unclosed fence as plain text' , () => { 132expect ( tokenizeContent ( '```js\nconst a = 1;' )). toEqual ([ 133{ kind :'text' , text :'```js\nconst a = 1;' } 134]); 135}); 136 137it ( 'leaves an unclosed inline backtick as plain text' , () => { 138expect ( tokenizeContent ( 'run `npm test' )). toEqual ([{ kind :'text' , text :'run `npm test' }]); 139}); 140 141it ( 'does not recognize badges inside code spans' , () => { 142expect ( tokenizeContent ( '`[a](file:///p)`' )). toEqual ([ 143{ kind :'code_inline' , text :'`[a](file:///p)`' } 144]); 145}); 146 147it ( 'tokenizes badges and code spans side by side' , () => { 148expect ( tokenizeContent ( '[a](file:///p) `x`' )). toEqual ([ 149{ kind :'badge' , name :'a' , path :'/p' }, 150{ kind :'text' , text :' ' }, 151{ kind :'code_inline' , text :'`x`' } 152]); 153}); 154}); 155 156describe ( 'containsCodeSpan' , () => { 157it ( 'detects inline code' , () => { 158expect ( containsCodeSpan ( 'run `npm test` now' )). toBe ( true ); 159}); 160 161it ( 'detects a fenced block with a language' , () => { 162expect ( containsCodeSpan ( '```js\nconst a = 1;\n```' )). toBe ( true ); 163}); 164 165it ( 'detects a fenced block without a language' , () => { 166expect ( containsCodeSpan ( '```\ncode\n```' )). toBe ( true ); 167}); 168 169it ( 'ignores unclosed fences and lone backticks' , () => { 170expect ( containsCodeSpan ( '```js\nconst a = 1;' )). toBe ( false ); 171expect ( containsCodeSpan ( 'run `npm test' )). toBe ( false ); 172expect ( containsCodeSpan ( '``' )). toBe ( false ); 173}); 174 175it ( 'ignores plain text and mention links' , () => { 176expect ( containsCodeSpan ( 'hello world' )). toBe ( false ); 177expect ( containsCodeSpan ( '[a](file:///p)' )). toBe ( false ); 178}); 179}); 180 181describe ( 'isOffsetInCodeBlock' , () => { 182const BLOCK = '```js\nconst a = 1;\n```' ; 183 184it ( 'is false with no fences in the buffer' , () => { 185expect ( isOffsetInCodeBlock ( 'hello world' , 5 )). toBe ( false ); 186expect ( isOffsetInCodeBlock ( 'run `npm test` now' , 10 )). toBe ( false ); 187}); 188 189it ( 'is true right after the opening fence, before any content' , () => { 190expect ( isOffsetInCodeBlock ( '```' , 3 )). toBe ( true ); 191expect ( isOffsetInCodeBlock ( '```js' , 5 )). toBe ( true ); 192}); 193 194it ( 'is true inside a still-open block while it is being typed' , () => { 195const open = '```js\nconst a = 1;' ; 196 197expect ( isOffsetInCodeBlock ( open , open . length )). toBe ( true ); 198}); 199 200it ( 'is true inside a closed block and false outside it' , () => { 201expect ( isOffsetInCodeBlock ( BLOCK , 6 )). toBe ( true ); 202expect ( isOffsetInCodeBlock ( BLOCK , 0 )). toBe ( false ); 203expect ( isOffsetInCodeBlock ( BLOCK , BLOCK . length )). toBe ( false ); 204expect ( isOffsetInCodeBlock ( BLOCK + '\nafter' , BLOCK . length + 5 )). toBe ( false ); 205}); 206 207it ( 'toggles per fence across multiple blocks' , () => { 208const two = BLOCK + '\ntext\n' + BLOCK ; 209const secondBlock = two . lastIndexOf ( BLOCK ); 210 211expect ( isOffsetInCodeBlock ( two , secondBlock - 2 )). toBe ( false ); 212expect ( isOffsetInCodeBlock ( two , secondBlock + 6 )). toBe ( true ); 213expect ( isOffsetInCodeBlock ( two , two . length )). toBe ( false ); 214}); 215});