|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package stringutil |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestStripANSI(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + input string |
| 15 | + want string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "empty string", |
| 19 | + input: "", |
| 20 | + want: "", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "plain text unchanged", |
| 24 | + input: "hello world", |
| 25 | + want: "hello world", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "basic color code", |
| 29 | + input: "\x1b[31mred text\x1b[0m", |
| 30 | + want: "red text", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "bold color code", |
| 34 | + input: "\x1b[1;32mBold Green\x1b[0m", |
| 35 | + want: "Bold Green", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "reset code only", |
| 39 | + input: "text\x1b[mmore", |
| 40 | + want: "textmore", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "multiple ANSI sequences", |
| 44 | + input: "\x1b[31mdoes important\x1b[0m things\x1b[m", |
| 45 | + want: "does important things", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "description with embedded ANSI", |
| 49 | + input: "This workflow \x1b[31mdoes important\x1b[0m things\x1b[m", |
| 50 | + want: "This workflow does important things", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "bold ANSI in description", |
| 54 | + input: "Workflow with \x1b[1mANSI\x1b[0m codes", |
| 55 | + want: "Workflow with ANSI codes", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "file path with ANSI color", |
| 59 | + input: "path/to/\x1b[32mfile1.md\x1b[0m", |
| 60 | + want: "path/to/file1.md", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "stop-time with ANSI codes", |
| 64 | + input: "2026-12-31\x1b[31mT23:59:59Z\x1b[0m", |
| 65 | + want: "2026-12-31T23:59:59Z", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "environment name with ANSI bold", |
| 69 | + input: "production-\x1b[1menv\x1b[0m", |
| 70 | + want: "production-env", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "OSC sequence with BEL terminator", |
| 74 | + input: "before\x1b]0;title\x07after", |
| 75 | + want: "beforeafter", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "OSC sequence with ST terminator", |
| 79 | + input: "before\x1b]0;title\x1b\\after", |
| 80 | + want: "beforeafter", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "G0 character set selection", |
| 84 | + input: "before\x1b(Bafter", |
| 85 | + want: "beforeafter", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "G1 character set selection", |
| 89 | + input: "before\x1b)0after", |
| 90 | + want: "beforeafter", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "application keypad mode", |
| 94 | + input: "before\x1b=after", |
| 95 | + want: "beforeafter", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "normal keypad mode", |
| 99 | + input: "before\x1b>after", |
| 100 | + want: "beforeafter", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "reset sequence", |
| 104 | + input: "before\x1bcafter", |
| 105 | + want: "beforeafter", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "cursor save (two-char sequence)", |
| 109 | + input: "before\x1b7after", |
| 110 | + want: "beforeafter", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "cursor restore (two-char sequence)", |
| 114 | + input: "before\x1b8after", |
| 115 | + want: "beforeafter", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "ESC at end of string", |
| 119 | + input: "text\x1b", |
| 120 | + want: "text", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "ESC with no following character stripped", |
| 124 | + input: "text\x1b[", |
| 125 | + want: "text", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "multiline text with ANSI codes", |
| 129 | + input: "Line 1 with \x1b[32mgreen\x1b[0m text\nLine 2 with \x1b[31mred\x1b[0m text", |
| 130 | + want: "Line 1 with green text\nLine 2 with red text", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "256-color foreground", |
| 134 | + input: "\x1b[38;5;196mred256\x1b[0m", |
| 135 | + want: "red256", |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "256-color background", |
| 139 | + input: "\x1b[48;5;21mblue256bg\x1b[0m", |
| 140 | + want: "blue256bg", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "hyperlink OSC sequence", |
| 144 | + input: "\x1b]8;;https://example.com\x07click\x1b]8;;\x07", |
| 145 | + want: "click", |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "no ESC but brackets preserved", |
| 149 | + input: "array[0] and [key]", |
| 150 | + want: "array[0] and [key]", |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "source path with ANSI", |
| 154 | + input: "\x1b[33mowner/repo\x1b[0m@v1.2.3", |
| 155 | + want: "owner/repo@v1.2.3", |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.name, func(t *testing.T) { |
| 161 | + got := StripANSI(tt.input) |
| 162 | + assert.Equal(t, tt.want, got) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments