diff --git a/tests/e2e/helpers/terminal-inline-image-proof.ts b/tests/e2e/helpers/terminal-inline-image-proof.ts index 47cc4a688a05..f8c9e5aebb8d 100644 --- a/tests/e2e/helpers/terminal-inline-image-proof.ts +++ b/tests/e2e/helpers/terminal-inline-image-proof.ts @@ -20,17 +20,22 @@ export function inlineImagePayload(includePlaceholders = false): string { `\x1b_Ga=T,f=100,i=65280,U=1,c=15,r=3,q=2;${prototype}\x1b\\` + `\x1b[38;2;0;255;0m${cells}\x1b[0m\r\n\r\n` } + const rgb = Buffer.from(Array.from({ length: 120 * 36 }, () => [40, 40, 240]).flat()).toString( + 'base64' + ) + let kittyPayload = '' + for (let offset = 0; offset < rgb.length; offset += 4096) { + const metadata = offset === 0 ? 'a=T,f=24,s=120,v=36,' : '' + const more = offset + 4096 < rgb.length ? 1 : 0 + kittyPayload += `\x1b_G${metadata}m=${more},q=2;${rgb.slice(offset, offset + 4096)}\x1b\\` + } return ( `\x1bcSSH / REMOTE INLINE IMAGE PROOF\r\n\r\n` + `iTerm2: red\r\n` + `\x1b]1337;File=inline=1;width=120px;height=36px:${encoded}\x07` + `\r\n\r\nSIXEL: green\r\n` + `\x1bPq"1;1;120;36#0;2;0;100;0${'#0!120~-'.repeat(6)}\x1b\\` + - `\r\n\r\nKitty: blue\r\n` + - `\x1b_Ga=T,f=24,s=120,v=36,q=2;${Buffer.from( - Array.from({ length: 120 * 36 }, () => [40, 40, 240]).flat() - ).toString('base64')}\x1b\\` + - `\r\n\r\n${placeholderPayload}` + `\r\n\r\nKitty: blue\r\n${kittyPayload}\r\n\r\n${placeholderPayload}` ) } diff --git a/tests/e2e/terminal-inline-image-payload.unit.test.ts b/tests/e2e/terminal-inline-image-payload.unit.test.ts new file mode 100644 index 000000000000..4995dcc004ef --- /dev/null +++ b/tests/e2e/terminal-inline-image-payload.unit.test.ts @@ -0,0 +1,42 @@ +import { expect, it } from 'vitest' +import { inlineImagePayload } from './helpers/terminal-inline-image-proof' + +it.each([false, true])( + 'frames the RGB Kitty upload within protocol limits (placeholders: %s)', + (placeholders) => { + const payload = inlineImagePayload(placeholders) + // oxlint-disable-next-line no-control-regex -- Parse the actual Kitty APC delimiters. + const frames = [...payload.matchAll(/\x1b_G([^;]*);([^\x1b]*)\x1b\\/g)] + const first = frames.findIndex((frame) => frame[1].split(',').includes('f=24')) + expect(first).toBeGreaterThanOrEqual(0) + const upload: string[] = [] + let nextOffset = frames[first].index + let completed = false + for (const frame of frames.slice(first)) { + expect(frame.index).toBe(nextOffset) + const fields = Object.fromEntries(frame[1].split(',').map((field) => field.split('='))) + if (upload.length === 0) { + expect(fields).toMatchObject({ a: 'T', f: '24', s: '120', v: '36', q: '2' }) + } else { + expect(Object.keys(fields).sort()).toEqual(['m', 'q']) + expect(fields.q).toBe('2') + } + expect(frame[2].length).toBeGreaterThan(0) + expect(frame[2].length).toBeLessThanOrEqual(4096) + expect(frame[2]).toMatch(/^[A-Za-z0-9+/]+={0,2}$/) + upload.push(frame[2]) + nextOffset += frame[0].length + if (fields.m === '0') { + completed = true + break + } + expect(fields.m).toBe('1') + expect(frame[2].length % 4).toBe(0) + } + expect(completed).toBe(true) + expect(Buffer.from(upload.join(''), 'base64')).toEqual( + Buffer.alloc(120 * 36 * 3).fill(Buffer.from([40, 40, 240])) + ) + expect(payload.slice(nextOffset)).toMatch(/^\r\n\r\n/) + } +)