|
| 1 | +import {describe, expect, it} from 'vitest'; |
| 2 | + |
| 3 | +import {cleanCodeSnippet} from './index'; |
| 4 | + |
| 5 | +describe('cleanCodeSnippet', () => { |
| 6 | + describe('consecutive newlines', () => { |
| 7 | + it('should reduce two consecutive newlines to a single newline', () => { |
| 8 | + const input = 'line1\n\nline2\n\n\n line3'; |
| 9 | + const result = cleanCodeSnippet(input, {}); |
| 10 | + expect(result).toBe('line1\nline2\n\n line3'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should handle input with single newlines', () => { |
| 14 | + const input = 'line1\nline2\nline3'; |
| 15 | + const result = cleanCodeSnippet(input, {}); |
| 16 | + expect(result).toBe('line1\nline2\nline3'); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('diff markers', () => { |
| 21 | + it('should remove diff markers (+/-) from the beginning of lines by default', () => { |
| 22 | + const input = '+added line\n- removed line\n normal line'; |
| 23 | + const result = cleanCodeSnippet(input); |
| 24 | + expect(result).toBe('added line\nremoved line\n normal line'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should preserve diff markers when cleanDiffMarkers is set to false', () => { |
| 28 | + const input = '+ added line\n- removed line'; |
| 29 | + const result = cleanCodeSnippet(input, {cleanDiffMarkers: false}); |
| 30 | + expect(result).toBe('+ added line\n- removed line'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should remove diff markers based on real-life code example', () => { |
| 34 | + const input = |
| 35 | + '-Sentry.init({\n' + |
| 36 | + "- dsn: '\n" + |
| 37 | + '\n' + |
| 38 | + "https://[email protected]/0',\n" + |
| 39 | + '- tracesSampleRate: 1.0,\n' + |
| 40 | + '-\n' + |
| 41 | + '- // uncomment the line below to enable Spotlight (https://spotlightjs.com)\n' + |
| 42 | + '- // spotlight: import.meta.env.DEV,\n' + |
| 43 | + '-});\n' + |
| 44 | + '-\n' + |
| 45 | + '-export const handle = sentryHandle();\n' + |
| 46 | + '+export const handle = sequence(\n' + |
| 47 | + '+ initCloudflareSentryHandle({\n' + |
| 48 | + "+ dsn: '\n" + |
| 49 | + '\n' + |
| 50 | + "https://[email protected]/0',\n" + |
| 51 | + '+ tracesSampleRate: 1.0,\n' + |
| 52 | + '+ }),\n' + |
| 53 | + '+ sentryHandle()\n' + |
| 54 | + '+);'; |
| 55 | + |
| 56 | + const result = cleanCodeSnippet(input); |
| 57 | + expect(result).toBe( |
| 58 | + 'Sentry.init({\n' + |
| 59 | + " dsn: '\n" + |
| 60 | + "https://[email protected]/0',\n" + |
| 61 | + ' tracesSampleRate: 1.0,\n' + |
| 62 | + ' // uncomment the line below to enable Spotlight (https://spotlightjs.com)\n' + |
| 63 | + ' // spotlight: import.meta.env.DEV,\n' + |
| 64 | + '});\n' + |
| 65 | + 'export const handle = sentryHandle();\n' + |
| 66 | + 'export const handle = sequence(\n' + |
| 67 | + ' initCloudflareSentryHandle({\n' + |
| 68 | + " dsn: '\n" + |
| 69 | + "https://[email protected]/0',\n" + |
| 70 | + ' tracesSampleRate: 1.0,\n' + |
| 71 | + ' }),\n' + |
| 72 | + ' sentryHandle()\n' + |
| 73 | + ');' |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('bash prompt', () => { |
| 79 | + it('should remove bash prompt in bash/shell language', () => { |
| 80 | + const input = '$ ls -la\nsome output'; |
| 81 | + const result = cleanCodeSnippet(input, {language: 'bash'}); |
| 82 | + expect(result).toBe('ls -la\nsome output'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should remove bash prompt in shell language', () => { |
| 86 | + const input = '$ git status\nsome output'; |
| 87 | + const result = cleanCodeSnippet(input, {language: 'shell'}); |
| 88 | + expect(result).toBe('git status\nsome output'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should not remove bash prompt for non-bash/shell languages', () => { |
| 92 | + const input = '$ some text'; |
| 93 | + const result = cleanCodeSnippet(input, {language: 'python'}); |
| 94 | + expect(result).toBe('$ some text'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle bash prompt with multiple spaces', () => { |
| 98 | + const input = '$ ls -la\nsome output'; |
| 99 | + const result = cleanCodeSnippet(input, {language: 'bash'}); |
| 100 | + expect(result).toBe('ls -la\nsome output'); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('combination of options', () => { |
| 105 | + it('should handle multiple cleaning operations together', () => { |
| 106 | + const input = '+ $ ls -la\n\n- $ git status'; |
| 107 | + const result = cleanCodeSnippet(input, {language: 'bash'}); |
| 108 | + expect(result).toBe('ls -la\ngit status'); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('edge cases', () => { |
| 113 | + it('should handle empty input', () => { |
| 114 | + const input = ''; |
| 115 | + const result = cleanCodeSnippet(input, {}); |
| 116 | + expect(result).toBe(''); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle input with only newlines', () => { |
| 120 | + const input = '\n\n\n'; |
| 121 | + const result = cleanCodeSnippet(input, {}); |
| 122 | + expect(result).toBe('\n\n'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should preserve leading whitespace not associated with diff markers', () => { |
| 126 | + const input = ' normal line\n+ added line'; |
| 127 | + const result = cleanCodeSnippet(input, {}); |
| 128 | + expect(result).toBe(' normal line\nadded line'); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments