|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + buildKittyImageSequence, |
| 5 | + buildOsc1337FileSequence, |
| 6 | + detectInlineImageProtocol, |
| 7 | + getCheckFailureCaption, |
| 8 | + getImagePlacement, |
| 9 | + normalizeCheckFailureKind, |
| 10 | + readPngDimensions, |
| 11 | +} from '../check-failure-image.js'; |
| 12 | + |
| 13 | +describe('detectInlineImageProtocol', () => { |
| 14 | + it('prefers the OSC 1337 file protocol for iTerm2 and WezTerm', () => { |
| 15 | + expect(detectInlineImageProtocol({ TERM_PROGRAM: 'iTerm.app' })).toBe('file'); |
| 16 | + expect(detectInlineImageProtocol({ TERM_PROGRAM: 'WezTerm' })).toBe('file'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('uses kitty graphics for kitty-compatible terminals', () => { |
| 20 | + expect(detectInlineImageProtocol({ KITTY_WINDOW_ID: '12' })).toBe('kitty'); |
| 21 | + expect(detectInlineImageProtocol({ TERM_PROGRAM: 'ghostty' })).toBe('kitty'); |
| 22 | + expect(detectInlineImageProtocol({ TERM: 'xterm-kitty' })).toBe('kitty'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('falls back to sixel when advertised and an encoder is available', () => { |
| 26 | + expect(detectInlineImageProtocol({ TERM_FEATURES: 'RGBSx' }, { canRenderSixel: true })).toBe( |
| 27 | + 'sixel', |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns null when no supported image protocol is available', () => { |
| 32 | + expect(detectInlineImageProtocol({ TERM: 'xterm-256color' })).toBeNull(); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('inline image sequences', () => { |
| 37 | + it('builds a kitty graphics sequence with the encoded file path', () => { |
| 38 | + const sequence = buildKittyImageSequence('/tmp/error.png', { |
| 39 | + columns: 26, |
| 40 | + rows: 17, |
| 41 | + }); |
| 42 | + |
| 43 | + expect(sequence).toContain('a=T,t=f,f=100,c=26,r=17'); |
| 44 | + expect(sequence).toContain(Buffer.from('/tmp/error.png').toString('base64')); |
| 45 | + expect(sequence).toContain('\u001b_G'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('builds an OSC 1337 file sequence with the encoded file contents', () => { |
| 49 | + const sequence = buildOsc1337FileSequence('/tmp/error.png', Buffer.from('png-bytes'), { |
| 50 | + columns: 26, |
| 51 | + rows: 17, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(sequence).toContain(']1337;File='); |
| 55 | + expect(sequence).toContain('width=26;height=17;preserveAspectRatio=1;inline=1'); |
| 56 | + expect(sequence).toContain(Buffer.from('error.png').toString('base64')); |
| 57 | + expect(sequence).toContain(Buffer.from('png-bytes').toString('base64')); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('PNG helpers', () => { |
| 62 | + it('reads dimensions from the PNG header and derives a bounded placement', () => { |
| 63 | + const png = Buffer.from( |
| 64 | + '89504e470d0a1a0a0000000d494844520000038a000004c0080600000000000000', |
| 65 | + 'hex', |
| 66 | + ); |
| 67 | + |
| 68 | + expect(readPngDimensions(png)).toEqual({ width: 906, height: 1216 }); |
| 69 | + expect(getImagePlacement({ width: 906, height: 1216 })).toEqual({ |
| 70 | + columns: 26, |
| 71 | + rows: 17, |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('fun mode captions', () => { |
| 77 | + it('uses the requested default caption for generic failures', () => { |
| 78 | + expect(getCheckFailureCaption('error')).toBe( |
| 79 | + 'The static analysis enthusiast says: outstanding, another error', |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('varies the caption by failure kind', () => { |
| 84 | + expect(getCheckFailureCaption('formatting')).toContain('another formatting error'); |
| 85 | + expect(getCheckFailureCaption('lint')).toContain('another lint error'); |
| 86 | + expect(getCheckFailureCaption('type-aware')).toContain('another type-aware error'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('normalizes unknown failure kinds to the generic error caption', () => { |
| 90 | + expect(normalizeCheckFailureKind('formatting')).toBe('formatting'); |
| 91 | + expect(normalizeCheckFailureKind('lint')).toBe('lint'); |
| 92 | + expect(normalizeCheckFailureKind('type-aware')).toBe('type-aware'); |
| 93 | + expect(normalizeCheckFailureKind('something-else')).toBe('error'); |
| 94 | + expect(normalizeCheckFailureKind(null)).toBe('error'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments