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