|
9 | 9 | safeLiteralReplace, |
10 | 10 | truncateString, |
11 | 11 | safeTemplateReplace, |
| 12 | + isBinary, |
| 13 | + stripAnsiFromBuffer, |
12 | 14 | } from './textUtils.js'; |
13 | 15 |
|
14 | 16 | describe('safeLiteralReplace', () => { |
@@ -198,3 +200,121 @@ describe('safeTemplateReplace', () => { |
198 | 200 | expect(safeTemplateReplace(tmpl, replacements)).toBe('Value: $&'); |
199 | 201 | }); |
200 | 202 | }); |
| 203 | + |
| 204 | +describe('stripAnsiFromBuffer', () => { |
| 205 | + it('returns the buffer unchanged when no escape sequences are present', () => { |
| 206 | + const input = Buffer.from('hello world'); |
| 207 | + expect(stripAnsiFromBuffer(input).toString()).toBe('hello world'); |
| 208 | + }); |
| 209 | + |
| 210 | + it('strips CSI sequences (ESC [ ... final)', () => { |
| 211 | + // ESC[31m = red foreground, ESC[0m = reset |
| 212 | + const input = Buffer.from('\x1b[31mhello\x1b[0m'); |
| 213 | + expect(stripAnsiFromBuffer(input).toString()).toBe('hello'); |
| 214 | + }); |
| 215 | + |
| 216 | + it('strips OSC sequences terminated by BEL', () => { |
| 217 | + // OSC title set: ESC ] 0 ; title BEL |
| 218 | + const input = Buffer.from('\x1b]0;My Title\x07some text'); |
| 219 | + expect(stripAnsiFromBuffer(input).toString()).toBe('some text'); |
| 220 | + }); |
| 221 | + |
| 222 | + it('strips OSC sequences terminated by ST (ESC \\)', () => { |
| 223 | + const input = Buffer.from('\x1b]0;My Title\x1b\\some text'); |
| 224 | + expect(stripAnsiFromBuffer(input).toString()).toBe('some text'); |
| 225 | + }); |
| 226 | + |
| 227 | + it('strips simple two-byte escape sequences', () => { |
| 228 | + // ESC D = Index (scroll down) |
| 229 | + const input = Buffer.from('\x1bDhello'); |
| 230 | + expect(stripAnsiFromBuffer(input).toString()).toBe('hello'); |
| 231 | + }); |
| 232 | + |
| 233 | + it('handles multiple mixed escape sequences', () => { |
| 234 | + const input = Buffer.from( |
| 235 | + '\x1b[31m\x1b]0;title\x07hello\x1b[0m world\x1bD', |
| 236 | + ); |
| 237 | + expect(stripAnsiFromBuffer(input).toString()).toBe('hello world'); |
| 238 | + }); |
| 239 | + |
| 240 | + it('returns empty buffer when input is only escape sequences', () => { |
| 241 | + const input = Buffer.from('\x1b[31m\x1b[0m'); |
| 242 | + expect(stripAnsiFromBuffer(input).length).toBe(0); |
| 243 | + }); |
| 244 | +}); |
| 245 | + |
| 246 | +describe('isBinary', () => { |
| 247 | + describe('default mode (strict, for files/pipes)', () => { |
| 248 | + it('returns false for null/undefined/empty input', () => { |
| 249 | + expect(isBinary(null)).toBe(false); |
| 250 | + expect(isBinary(undefined)).toBe(false); |
| 251 | + expect(isBinary(Buffer.alloc(0))).toBe(false); |
| 252 | + }); |
| 253 | + |
| 254 | + it('returns false for plain ASCII text', () => { |
| 255 | + expect(isBinary(Buffer.from('hello world\n'))).toBe(false); |
| 256 | + }); |
| 257 | + |
| 258 | + it('returns true when a single null byte is present', () => { |
| 259 | + expect(isBinary(Buffer.from('hello\x00world'))).toBe(true); |
| 260 | + }); |
| 261 | + |
| 262 | + it('returns true for binary data', () => { |
| 263 | + const buf = Buffer.alloc(100, 0); |
| 264 | + expect(isBinary(buf)).toBe(true); |
| 265 | + }); |
| 266 | + |
| 267 | + it('only checks the first sampleSize bytes', () => { |
| 268 | + const buf = Buffer.alloc(600, 0x41); // 'A' x 600 |
| 269 | + buf[550] = 0; // null byte outside default 512 sample |
| 270 | + expect(isBinary(buf)).toBe(false); |
| 271 | + }); |
| 272 | + |
| 273 | + it('detects null byte within custom sampleSize', () => { |
| 274 | + const buf = Buffer.alloc(600, 0x41); |
| 275 | + buf[550] = 0; |
| 276 | + expect(isBinary(buf, 600)).toBe(true); |
| 277 | + }); |
| 278 | + }); |
| 279 | + |
| 280 | + describe('PTY mode (isPtyOutput = true)', () => { |
| 281 | + it('returns false for PTY output that is pure ANSI escape sequences', () => { |
| 282 | + const buf = Buffer.from('\x1b[31m\x1b[0m'); |
| 283 | + expect(isBinary(buf, 512, true)).toBe(false); |
| 284 | + }); |
| 285 | + |
| 286 | + it('returns false for text with ANSI sequences containing embedded null bytes', () => { |
| 287 | + // Simulate Windows PTY: OSC title set with null bytes, followed by text |
| 288 | + const osc = Buffer.from('\x1b]0;title\x00\x07'); |
| 289 | + const text = Buffer.from('hello world'); |
| 290 | + const buf = Buffer.concat([osc, text]); |
| 291 | + expect(isBinary(buf, 512, true)).toBe(false); |
| 292 | + }); |
| 293 | + |
| 294 | + it('returns false for a single stray null byte among text', () => { |
| 295 | + const buf = Buffer.from('hello\x00world, this is a long text output'); |
| 296 | + expect(isBinary(buf, 512, true)).toBe(false); |
| 297 | + }); |
| 298 | + |
| 299 | + it('returns true for actual binary data through PTY (>10% nulls after strip)', () => { |
| 300 | + // 90 null bytes + 10 text bytes → 90% nulls = binary |
| 301 | + const nulls = Buffer.alloc(90, 0); |
| 302 | + const text = Buffer.from('abcdefghij'); |
| 303 | + const buf = Buffer.concat([nulls, text]); |
| 304 | + expect(isBinary(buf, 512, true)).toBe(true); |
| 305 | + }); |
| 306 | + |
| 307 | + it('returns false for realistic Windows PTY output with ANSI reset + text', () => { |
| 308 | + // Simulates: color set, OSC title with a stray null, reset, then real output |
| 309 | + const buf = Buffer.from( |
| 310 | + '\x1b[?25l\x1b]0;\x00Window Title\x07\x1b[0mPS C:\\Users> echo hello\r\nhello\r\n', |
| 311 | + ); |
| 312 | + expect(isBinary(buf, 512, true)).toBe(false); |
| 313 | + }); |
| 314 | + |
| 315 | + it('returns false when the buffer is empty after stripping ANSI', () => { |
| 316 | + const buf = Buffer.from('\x1b[31m\x1b[42m\x1b[0m'); |
| 317 | + expect(isBinary(buf, 512, true)).toBe(false); |
| 318 | + }); |
| 319 | + }); |
| 320 | +}); |
0 commit comments