|
| 1 | +import { cleanAnsi, stripAnsi } from '../src'; |
| 2 | + |
| 3 | +describe('cleanAnsi', () => { |
| 4 | + it('should return empty string for null or undefined input', () => { |
| 5 | + expect(cleanAnsi('')).toBe(''); |
| 6 | + expect(cleanAnsi(null as any)).toBe(''); |
| 7 | + expect(cleanAnsi(undefined as any)).toBe(''); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should return the same string if no ANSI codes are present', () => { |
| 11 | + const text = 'Hello, World!'; |
| 12 | + expect(cleanAnsi(text)).toBe(text); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should remove CSI color codes', () => { |
| 16 | + const text = '\u001b[31mRed text\u001b[0m'; |
| 17 | + expect(cleanAnsi(text)).toBe('Red text'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should remove multiple color codes', () => { |
| 21 | + const text = '\u001b[31mRed\u001b[0m \u001b[32mGreen\u001b[0m \u001b[34mBlue\u001b[0m'; |
| 22 | + expect(cleanAnsi(text)).toBe('Red Green Blue'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should remove cursor movement codes', () => { |
| 26 | + const text = 'Hello\u001b[AWorld'; |
| 27 | + expect(cleanAnsi(text)).toBe('HelloWorld'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should remove clear screen codes', () => { |
| 31 | + const text = 'Before\u001b[2JAfter'; |
| 32 | + expect(cleanAnsi(text)).toBe('BeforeAfter'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should remove cursor visibility codes', () => { |
| 36 | + const text = 'Text\u001b[?25hMore text\u001b[?25l'; |
| 37 | + expect(cleanAnsi(text)).toBe('TextMore text'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should remove OSC sequences with BEL terminator', () => { |
| 41 | + const text = '\u001b]0;Window Title\u0007Content'; |
| 42 | + expect(cleanAnsi(text)).toBe('Content'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should remove OSC sequences with ESC\\ terminator', () => { |
| 46 | + const text = '\u001b]2;Title\u001b\\Content'; |
| 47 | + expect(cleanAnsi(text)).toBe('Content'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should remove other escape sequences', () => { |
| 51 | + const text = 'Save\u001b7Position\u001b8Restore'; |
| 52 | + expect(cleanAnsi(text)).toBe('SavePositionRestore'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle complex strings with multiple ANSI codes', () => { |
| 56 | + const text = '\u001b[31m\u001b[1mBold Red\u001b[0m\u001b[32m Green \u001b[0m\u001b]0;Title\u0007Normal'; |
| 57 | + expect(cleanAnsi(text)).toBe('Bold Red Green Normal'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should preserve newlines and other whitespace', () => { |
| 61 | + const text = '\u001b[31mLine 1\u001b[0m\nLine 2\n\tTabbed'; |
| 62 | + expect(cleanAnsi(text)).toBe('Line 1\nLine 2\n\tTabbed'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle strings with only ANSI codes', () => { |
| 66 | + const text = '\u001b[31m\u001b[0m\u001b[32m\u001b[0m'; |
| 67 | + expect(cleanAnsi(text)).toBe(''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle complex real-world terminal output', () => { |
| 71 | + const text = '\u001b[?25l\u001b[2J\u001b[H\u001b[31mError:\u001b[0m Something went wrong\u001b[?25h'; |
| 72 | + expect(cleanAnsi(text)).toBe('Error: Something went wrong'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle 256 color codes', () => { |
| 76 | + const text = '\u001b[38;5;208mOrange text\u001b[0m'; |
| 77 | + expect(cleanAnsi(text)).toBe('Orange text'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle RGB color codes', () => { |
| 81 | + const text = '\u001b[38;2;255;0;0mRed text\u001b[0m'; |
| 82 | + expect(cleanAnsi(text)).toBe('Red text'); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('stripAnsi (alias)', () => { |
| 87 | + it('should work the same as cleanAnsi', () => { |
| 88 | + const text = '\u001b[31mRed text\u001b[0m'; |
| 89 | + expect(stripAnsi(text)).toBe('Red text'); |
| 90 | + expect(stripAnsi(text)).toBe(cleanAnsi(text)); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should be the same function reference', () => { |
| 94 | + expect(stripAnsi).toBe(cleanAnsi); |
| 95 | + }); |
| 96 | +}); |
0 commit comments