char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { highlightCode , splitGluedClosingCodeFences , trimCodePadding } from '$lib/utils/code' ; 2import { describe , expect , it } from 'vitest' ; 3 4describe ( 'trimCodePadding' , () => { 5it ( 'removes a single leading newline' , () => { 6expect ( trimCodePadding ( '\nfunction foo() {}' )). toBe ( 'function foo() {}' ); 7}); 8 9it ( 'removes multiple leading newlines' , () => { 10expect ( trimCodePadding ( '\n\n\nfunction foo() {}' )). toBe ( 'function foo() {}' ); 11}); 12 13it ( 'removes whitespace-only leading lines' , () => { 14expect ( trimCodePadding ( '\n \n\t\nfunction foo() {}' )). toBe ( 'function foo() {}' ); 15}); 16 17it ( 'removes a single trailing newline' , () => { 18expect ( trimCodePadding ( 'function foo() {}\n' )). toBe ( 'function foo() {}' ); 19}); 20 21it ( 'removes multiple trailing newlines' , () => { 22expect ( trimCodePadding ( 'function foo() {}\n\n\n' )). toBe ( 'function foo() {}' ); 23}); 24 25it ( 'removes whitespace-only trailing lines' , () => { 26expect ( trimCodePadding ( 'function foo() {}\n \n\t\n' )). toBe ( 'function foo() {}' ); 27}); 28 29it ( 'removes newlines on both sides at once' , () => { 30expect ( trimCodePadding ( '\nfunction foo() {}\n' )). toBe ( 'function foo() {}' ); 31}); 32 33it ( 'preserves internal blank lines' , () => { 34expect ( trimCodePadding ( '\nfunction foo() {\n\n return 1;\n}\n' )). toBe ( 35'function foo() {\n\n return 1;\n}' 36); 37}); 38 39it ( 'drops a leading whitespace-only line but keeps following code intact' , () => { 40expect ( trimCodePadding ( ' \nfunction foo() {}' )). toBe ( 'function foo() {}' ); 41}); 42 43it ( 'passes through already-trimmed input unchanged' , () => { 44expect ( trimCodePadding ( 'function foo() {}' )). toBe ( 'function foo() {}' ); 45expect ( trimCodePadding ( 'function foo() {\n return 1;\n}' )). toBe ( 46'function foo() {\n return 1;\n}' 47); 48}); 49 50it ( 'returns empty string when input is whitespace only' , () => { 51expect ( trimCodePadding ( '\n\n\n' )). toBe ( '' ); 52expect ( trimCodePadding ( '\n \n\t\n' )). toBe ( '' ); 53}); 54}); 55 56describe ( 'highlightCode' , () => { 57it ( 'returns empty string for empty input' , () => { 58expect ( highlightCode ( '' , 'javascript' )). toBe ( '' ); 59}); 60 61it ( 'does not produce a leading newline in the highlighted html' , () => { 62const html = highlightCode ( '\nfunction multiply(a, b) {\n return a * b;\n}\n' , 'javascript' ); 63 64expect ( html . startsWith ( '\n' )). toBe ( false ); 65expect ( html . startsWith ( ' ' )). toBe ( false ); 66}); 67 68it ( 'does not produce a trailing newline in the highlighted html' , () => { 69const html = highlightCode ( '\nfunction foo() {}\n' , 'javascript' ); 70 71expect ( html . endsWith ( '\n' )). toBe ( false ); 72}); 73 74it ( 'preserves internal blank lines in highlighted code' , () => { 75const html = highlightCode ( '\nfunction foo() {\n\n return 1;\n}\n' , 'javascript' ); 76 77expect ( html ). toContain ( '\n\n' ); 78}); 79 80it ( 'produces the same body for framed and unframed input' , () => { 81const trimmed = highlightCode ( 'function foo() {}' , 'javascript' ); 82const framed = highlightCode ( '\nfunction foo() {}\n' , 'javascript' ); 83 84expect ( framed ). toBe ( trimmed ); 85}); 86 87it ( 'auto-detects an unknown language by default' , () => { 88const html = highlightCode ( 'const answer = 42;' , 'not-a-language' ); 89 90expect ( html ). toContain ( 'hljs-' ); 91}); 92 93it ( 'escapes instead of auto-detecting when autoDetect is false' , () => { 94const html = highlightCode ( 'const answer = 42;' , 'not-a-language' , false ); 95 96expect ( html ). not . toContain ( 'hljs-' ); 97expect ( html ). toBe ( 'const answer = 42;' ); 98}); 99 100it ( 'still highlights a known language when autoDetect is false' , () => { 101const html = highlightCode ( 'const answer = 42;' , 'javascript' , false ); 102 103expect ( html ). toContain ( 'hljs-' ); 104}); 105 106it ( 'escapes html metacharacters when falling back to plain text' , () => { 107const html = highlightCode ( '<script>a && b</script>' , 'not-a-language' , false ); 108 109expect ( html ). toBe ( '<script>a && b</script>' ); 110}); 111}); 112 113describe ( 'splitGluedClosingCodeFences' , () => { 114it ( 'splits text glued to a closing fence onto its own line' , () => { 115const input = "```ts\nlet foo = 'bar';\n```create this file on [Desktop](file:///a/b/)" ; 116 117expect ( splitGluedClosingCodeFences ( input )). toBe ( 118"```ts\nlet foo = 'bar';\n```\ncreate this file on [Desktop](file:///a/b/)" 119); 120}); 121 122it ( 'leaves a well-formed code block untouched' , () => { 123const input = "```ts\nlet foo = 'bar';\n```\ncreate this file on [Desktop](file:///a/b/)" ; 124 125expect ( splitGluedClosingCodeFences ( input )). toBe ( input ); 126}); 127 128it ( 'leaves content without fences untouched' , () => { 129expect ( splitGluedClosingCodeFences ( 'hello world' )). toBe ( 'hello world' ); 130}); 131 132it ( 'keeps nested markdown fences inside a block intact' , () => { 133const input = '```md\n# Example\n```python\nprint(1)\n```\n```' ; 134 135expect ( splitGluedClosingCodeFences ( input )). toBe ( input ); 136}); 137 138it ( 'splits every glued closing fence when several blocks are present' , () => { 139const input = '```ts\na\n```first words\n\n```js\nb\n```second words' ; 140 141expect ( splitGluedClosingCodeFences ( input )). toBe ( 142'```ts\na\n```\nfirst words\n\n```js\nb\n```\nsecond words' 143); 144}); 145 146it ( 'leaves a still-open fence untouched' , () => { 147const input = '```ts\nlet foo = 1;' ; 148 149expect ( splitGluedClosingCodeFences ( input )). toBe ( input ); 150}); 151});