terminal-image.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /**
  2. * Tests for terminal image detection and line handling
  3. */
  4. import assert from "node:assert";
  5. import { describe, it } from "node:test";
  6. import { Image } from "../src/components/image.ts";
  7. import {
  8. deleteAllKittyImages,
  9. deleteKittyImage,
  10. detectCapabilities,
  11. encodeKitty,
  12. hyperlink,
  13. isImageLine,
  14. renderImage,
  15. resetCapabilitiesCache,
  16. setCapabilities,
  17. setCellDimensions,
  18. } from "../src/terminal-image.ts";
  19. const ENV_KEYS = [
  20. "TERM",
  21. "TERM_PROGRAM",
  22. "TERMINAL_EMULATOR",
  23. "COLORTERM",
  24. "TMUX",
  25. "KITTY_WINDOW_ID",
  26. "GHOSTTY_RESOURCES_DIR",
  27. "WEZTERM_PANE",
  28. "ITERM_SESSION_ID",
  29. "WT_SESSION",
  30. "CMUX_WORKSPACE_ID",
  31. ] as const;
  32. function withEnv(overrides: Record<string, string | undefined>, fn: () => void): void {
  33. const saved: Record<string, string | undefined> = {};
  34. for (const key of ENV_KEYS) {
  35. saved[key] = process.env[key];
  36. delete process.env[key];
  37. }
  38. try {
  39. for (const [k, v] of Object.entries(overrides)) {
  40. if (v === undefined) delete process.env[k];
  41. else process.env[k] = v;
  42. }
  43. fn();
  44. } finally {
  45. for (const key of ENV_KEYS) {
  46. if (saved[key] === undefined) delete process.env[key];
  47. else process.env[key] = saved[key];
  48. }
  49. }
  50. }
  51. describe("isImageLine", () => {
  52. describe("iTerm2 image protocol", () => {
  53. it("should detect iTerm2 image escape sequence at start of line", () => {
  54. // iTerm2 image escape sequence: ESC ]1337;File=...
  55. const iterm2ImageLine = "\x1b]1337;File=size=100,100;inline=1:base64encodeddata==\x07";
  56. assert.strictEqual(isImageLine(iterm2ImageLine), true);
  57. });
  58. it("should detect iTerm2 image escape sequence with text before it", () => {
  59. // Simulating a line that has text then image data (bug scenario)
  60. const lineWithTextAndImage = "Some text \x1b]1337;File=size=100,100;inline=1:base64data==\x07 more text";
  61. assert.strictEqual(isImageLine(lineWithTextAndImage), true);
  62. });
  63. it("should detect iTerm2 image escape sequence in middle of long line", () => {
  64. // Simulate a very long line with image data in the middle
  65. const longLineWithImage =
  66. "Text before image..." + "\x1b]1337;File=inline=1:verylongbase64data==" + "...text after";
  67. assert.strictEqual(isImageLine(longLineWithImage), true);
  68. });
  69. it("should detect iTerm2 image escape sequence at end of line", () => {
  70. const lineWithImageAtEnd = "Regular text ending with \x1b]1337;File=inline=1:base64data==\x07";
  71. assert.strictEqual(isImageLine(lineWithImageAtEnd), true);
  72. });
  73. it("should detect minimal iTerm2 image escape sequence", () => {
  74. const minimalImageLine = "\x1b]1337;File=:\x07";
  75. assert.strictEqual(isImageLine(minimalImageLine), true);
  76. });
  77. });
  78. describe("Kitty image protocol", () => {
  79. it("should detect Kitty image escape sequence at start of line", () => {
  80. // Kitty image escape sequence: ESC _G
  81. const kittyImageLine = "\x1b_Ga=T,f=100,t=f,d=base64data...\x1b\\\x1b_Gm=i=1;\x1b\\";
  82. assert.strictEqual(isImageLine(kittyImageLine), true);
  83. });
  84. it("should detect Kitty image escape sequence with text before it", () => {
  85. // Bug scenario: text + image data in same line
  86. const lineWithTextAndKittyImage = "Output: \x1b_Ga=T,f=100;data...\x1b\\\x1b_Gm=i=1;\x1b\\";
  87. assert.strictEqual(isImageLine(lineWithTextAndKittyImage), true);
  88. });
  89. it("should detect Kitty image escape sequence with padding", () => {
  90. // Kitty protocol adds padding to escape sequences
  91. const kittyWithPadding = " \x1b_Ga=T,f=100...\x1b\\\x1b_Gm=i=1;\x1b\\ ";
  92. assert.strictEqual(isImageLine(kittyWithPadding), true);
  93. });
  94. });
  95. describe("Bug regression tests", () => {
  96. it("should detect image sequences in very long lines (304k+ chars)", () => {
  97. // This simulates the crash scenario: a line with 304,401 chars
  98. // containing image escape sequences somewhere
  99. const base64Char = "A".repeat(100); // 100 chars of base64-like data
  100. const imageSequence = "\x1b]1337;File=size=800,600;inline=1:";
  101. // Build a long line with image sequence
  102. const longLine =
  103. "Text prefix " +
  104. imageSequence +
  105. base64Char.repeat(3000) + // ~300,000 chars
  106. " suffix";
  107. assert.strictEqual(longLine.length > 300000, true);
  108. assert.strictEqual(isImageLine(longLine), true);
  109. });
  110. it("should detect image sequences when terminal doesn't support images", () => {
  111. // The bug occurred when getImageEscapePrefix() returned null
  112. // isImageLine should still detect image sequences regardless
  113. const lineWithImage = "Read image file [image/jpeg]\x1b]1337;File=inline=1:base64data==\x07";
  114. assert.strictEqual(isImageLine(lineWithImage), true);
  115. });
  116. it("should detect image sequences with ANSI codes before them", () => {
  117. // Text might have ANSI styling before image data
  118. const lineWithAnsiAndImage = "\x1b[31mError output \x1b]1337;File=inline=1:image==\x07";
  119. assert.strictEqual(isImageLine(lineWithAnsiAndImage), true);
  120. });
  121. it("should detect image sequences with ANSI codes after them", () => {
  122. const lineWithImageAndAnsi = "\x1b_Ga=T,f=100:data...\x1b\\\x1b_Gm=i=1;\x1b\\\x1b[0m reset";
  123. assert.strictEqual(isImageLine(lineWithImageAndAnsi), true);
  124. });
  125. });
  126. describe("Negative cases - lines without images", () => {
  127. it("should not detect images in plain text lines", () => {
  128. const plainText = "This is just a regular text line without any escape sequences";
  129. assert.strictEqual(isImageLine(plainText), false);
  130. });
  131. it("should not detect images in lines with only ANSI codes", () => {
  132. const ansiText = "\x1b[31mRed text\x1b[0m and \x1b[32mgreen text\x1b[0m";
  133. assert.strictEqual(isImageLine(ansiText), false);
  134. });
  135. it("should not detect images in lines with cursor movement codes", () => {
  136. const cursorCodes = "\x1b[1A\x1b[2KLine cleared and moved up";
  137. assert.strictEqual(isImageLine(cursorCodes), false);
  138. });
  139. it("should not detect images in lines with partial iTerm2 sequences", () => {
  140. // Similar prefix but missing the complete sequence
  141. const partialSequence = "Some text with ]1337;File but missing ESC at start";
  142. assert.strictEqual(isImageLine(partialSequence), false);
  143. });
  144. it("should not detect images in lines with partial Kitty sequences", () => {
  145. // Similar prefix but missing the complete sequence
  146. const partialSequence = "Some text with _G but missing ESC at start";
  147. assert.strictEqual(isImageLine(partialSequence), false);
  148. });
  149. it("should not detect images in empty lines", () => {
  150. assert.strictEqual(isImageLine(""), false);
  151. });
  152. it("should not detect images in lines with newlines only", () => {
  153. assert.strictEqual(isImageLine("\n"), false);
  154. assert.strictEqual(isImageLine("\n\n"), false);
  155. });
  156. });
  157. describe("Mixed content scenarios", () => {
  158. it("should detect images when line has both Kitty and iTerm2 sequences", () => {
  159. const mixedLine = "Kitty: \x1b_Ga=T...\x1b\\\x1b_Gm=i=1;\x1b\\ iTerm2: \x1b]1337;File=inline=1:data==\x07";
  160. assert.strictEqual(isImageLine(mixedLine), true);
  161. });
  162. it("should detect image in line with multiple text and image segments", () => {
  163. const complexLine = "Start \x1b]1337;File=img1==\x07 middle \x1b]1337;File=img2==\x07 end";
  164. assert.strictEqual(isImageLine(complexLine), true);
  165. });
  166. it("should not falsely detect image in line with file path containing keywords", () => {
  167. // File path might contain "1337" or "File" but without escape sequences
  168. const filePathLine = "/path/to/File_1337_backup/image.jpg";
  169. assert.strictEqual(isImageLine(filePathLine), false);
  170. });
  171. });
  172. });
  173. describe("detectCapabilities", () => {
  174. it("defaults to hyperlinks: false for unknown terminals", () => {
  175. withEnv({}, () => {
  176. const caps = detectCapabilities();
  177. assert.strictEqual(caps.hyperlinks, false);
  178. assert.strictEqual(caps.images, null);
  179. });
  180. });
  181. it("enables hyperlinks under tmux when the client forwards them", () => {
  182. withEnv({ TMUX: "/tmp/tmux-1000/default,1234,0", TERM_PROGRAM: "ghostty" }, () => {
  183. const caps = detectCapabilities(() => true);
  184. assert.strictEqual(caps.hyperlinks, true);
  185. assert.strictEqual(caps.images, null);
  186. });
  187. });
  188. it("disables hyperlinks under tmux when the client does not forward them", () => {
  189. withEnv({ TMUX: "/tmp/tmux-1000/default,1234,0", TERM_PROGRAM: "ghostty" }, () => {
  190. const caps = detectCapabilities(() => false);
  191. assert.strictEqual(caps.hyperlinks, false);
  192. assert.strictEqual(caps.images, null);
  193. });
  194. });
  195. it("checks tmux capability when TERM starts with 'tmux'", () => {
  196. withEnv({ TERM: "tmux-256color", TERM_PROGRAM: "iterm.app" }, () => {
  197. const caps = detectCapabilities(() => true);
  198. assert.strictEqual(caps.hyperlinks, true);
  199. assert.strictEqual(caps.images, null);
  200. const caps2 = detectCapabilities(() => false);
  201. assert.strictEqual(caps2.hyperlinks, false);
  202. });
  203. });
  204. it("forces hyperlinks: false when TERM starts with 'screen'", () => {
  205. withEnv({ TERM: "screen-256color" }, () => {
  206. const caps = detectCapabilities();
  207. assert.strictEqual(caps.hyperlinks, false);
  208. assert.strictEqual(caps.images, null);
  209. });
  210. });
  211. it("enables hyperlinks for Ghostty", () => {
  212. withEnv({ TERM_PROGRAM: "ghostty" }, () => {
  213. const caps = detectCapabilities();
  214. assert.strictEqual(caps.hyperlinks, true);
  215. });
  216. });
  217. it("does not disable Ghostty images solely because cmux is present", () => {
  218. withEnv({ TERM_PROGRAM: "ghostty", CMUX_WORKSPACE_ID: "workspace" }, () => {
  219. const caps = detectCapabilities();
  220. assert.strictEqual(caps.images, "kitty");
  221. assert.strictEqual(caps.hyperlinks, true);
  222. });
  223. });
  224. it("enables hyperlinks for Kitty", () => {
  225. withEnv({ KITTY_WINDOW_ID: "1" }, () => {
  226. const caps = detectCapabilities();
  227. assert.strictEqual(caps.hyperlinks, true);
  228. });
  229. });
  230. it("enables hyperlinks for WezTerm", () => {
  231. withEnv({ WEZTERM_PANE: "0" }, () => {
  232. const caps = detectCapabilities();
  233. assert.strictEqual(caps.hyperlinks, true);
  234. });
  235. });
  236. it("enables hyperlinks for iTerm2", () => {
  237. withEnv({ TERM_PROGRAM: "iterm.app" }, () => {
  238. const caps = detectCapabilities();
  239. assert.strictEqual(caps.hyperlinks, true);
  240. });
  241. });
  242. it("enables hyperlinks for VSCode", () => {
  243. withEnv({ TERM_PROGRAM: "vscode" }, () => {
  244. const caps = detectCapabilities();
  245. assert.strictEqual(caps.hyperlinks, true);
  246. });
  247. });
  248. it("enables truecolor and hyperlinks for Windows Terminal outside multiplexers", () => {
  249. withEnv({ WT_SESSION: "session", TERM: "xterm-256color" }, () => {
  250. const caps = detectCapabilities();
  251. assert.strictEqual(caps.trueColor, true);
  252. assert.strictEqual(caps.hyperlinks, true);
  253. assert.strictEqual(caps.images, null);
  254. });
  255. });
  256. it("enables truecolor without hyperlinks for JetBrains terminal", () => {
  257. withEnv({ TERMINAL_EMULATOR: "JetBrains-JediTerm", TERM: "xterm-256color" }, () => {
  258. const caps = detectCapabilities();
  259. assert.strictEqual(caps.trueColor, true);
  260. assert.strictEqual(caps.hyperlinks, false);
  261. assert.strictEqual(caps.images, null);
  262. });
  263. });
  264. it("does not inherit Windows Terminal truecolor through tmux", () => {
  265. withEnv({ WT_SESSION: "session", TMUX: "/tmp/tmux-1000/default,1234,0", TERM: "tmux-256color" }, () => {
  266. const caps = detectCapabilities(() => false);
  267. assert.strictEqual(caps.trueColor, false);
  268. assert.strictEqual(caps.hyperlinks, false);
  269. assert.strictEqual(caps.images, null);
  270. });
  271. });
  272. it("trusts explicit truecolor hints through tmux", () => {
  273. withEnv({ COLORTERM: "truecolor", TMUX: "/tmp/tmux-1000/default,1234,0", TERM: "tmux-256color" }, () => {
  274. const caps = detectCapabilities(() => false);
  275. assert.strictEqual(caps.trueColor, true);
  276. assert.strictEqual(caps.hyperlinks, false);
  277. assert.strictEqual(caps.images, null);
  278. });
  279. });
  280. });
  281. describe("Kitty image cursor movement", () => {
  282. it("can request no terminal-side cursor movement", () => {
  283. const sequence = encodeKitty("AAAA", { columns: 2, rows: 2, moveCursor: false });
  284. assert.ok(sequence.startsWith("\x1b_Ga=T,f=100,q=2,C=1,c=2,r=2;"));
  285. });
  286. it("suppresses Kitty replies for delete commands", () => {
  287. assert.strictEqual(deleteKittyImage(42), "\x1b_Ga=d,d=I,i=42,q=2\x1b\\");
  288. assert.strictEqual(deleteAllKittyImages(), "\x1b_Ga=d,d=A,q=2\x1b\\");
  289. });
  290. it("preserves renderImage's default terminal-side cursor movement", () => {
  291. setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true });
  292. setCellDimensions({ widthPx: 10, heightPx: 10 });
  293. try {
  294. const result = renderImage("AAAA", { widthPx: 20, heightPx: 20 }, { maxWidthCells: 2 });
  295. assert.ok(result);
  296. assert.ok(!result.sequence.includes(",C=1,"));
  297. assert.strictEqual(result.rows, 2);
  298. } finally {
  299. resetCapabilitiesCache();
  300. setCellDimensions({ widthPx: 9, heightPx: 18 });
  301. }
  302. });
  303. it("can opt renderImage into no terminal-side cursor movement", () => {
  304. setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true });
  305. setCellDimensions({ widthPx: 10, heightPx: 10 });
  306. try {
  307. const result = renderImage("AAAA", { widthPx: 20, heightPx: 20 }, { maxWidthCells: 2, moveCursor: false });
  308. assert.ok(result);
  309. assert.ok(result.sequence.includes(",C=1,"));
  310. assert.strictEqual(result.rows, 2);
  311. } finally {
  312. resetCapabilitiesCache();
  313. setCellDimensions({ widthPx: 9, heightPx: 18 });
  314. }
  315. });
  316. it("honors maxHeightCells by reducing rendered width", () => {
  317. setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true });
  318. setCellDimensions({ widthPx: 10, heightPx: 10 });
  319. try {
  320. const result = renderImage("AAAA", { widthPx: 10, heightPx: 100 }, { maxWidthCells: 10, maxHeightCells: 5 });
  321. assert.ok(result);
  322. assert.strictEqual(result.rows, 5);
  323. assert.ok(result.sequence.includes(",c=1,r=5"));
  324. } finally {
  325. resetCapabilitiesCache();
  326. setCellDimensions({ widthPx: 9, heightPx: 18 });
  327. }
  328. });
  329. it("caps Image component height to a square pixel box by default", () => {
  330. setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true });
  331. setCellDimensions({ widthPx: 10, heightPx: 20 });
  332. try {
  333. const image = new Image(
  334. "AAAA",
  335. "image/png",
  336. { fallbackColor: (value) => value },
  337. { maxWidthCells: 10 },
  338. { widthPx: 10, heightPx: 100 },
  339. );
  340. const lines = image.render(12);
  341. assert.strictEqual(lines.length, 5);
  342. assert.ok(lines[0].includes(",c=1,r=5"));
  343. } finally {
  344. resetCapabilitiesCache();
  345. setCellDimensions({ widthPx: 9, heightPx: 18 });
  346. }
  347. });
  348. it("places image sequence on first line with empty padding rows", () => {
  349. setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true });
  350. setCellDimensions({ widthPx: 10, heightPx: 10 });
  351. try {
  352. const image = new Image(
  353. "AAAA",
  354. "image/png",
  355. { fallbackColor: (value) => value },
  356. { maxWidthCells: 2 },
  357. { widthPx: 20, heightPx: 20 },
  358. );
  359. const lines = image.render(4);
  360. const imageId = image.getImageId();
  361. assert.strictEqual(typeof imageId, "number");
  362. assert.ok(lines[0].startsWith("\x1b_G"));
  363. assert.ok(lines[0].includes(",C=1,"));
  364. assert.ok(lines[0].includes(`,i=${imageId}`));
  365. assert.ok(lines[0].endsWith("\x1b\\"));
  366. assert.deepStrictEqual(lines.slice(1, lines.length), [""]);
  367. } finally {
  368. resetCapabilitiesCache();
  369. setCellDimensions({ widthPx: 9, heightPx: 18 });
  370. }
  371. });
  372. });
  373. describe("hyperlink", () => {
  374. it("wraps text in BEL-terminated OSC 8 open and close sequences", () => {
  375. const result = hyperlink("click me", "https://example.com");
  376. assert.strictEqual(result, "\x1b]8;;https://example.com\x07click me\x1b]8;;\x07");
  377. });
  378. it("preserves ANSI styling inside the hyperlink", () => {
  379. const styled = "\x1b[4m\x1b[34mclick me\x1b[0m";
  380. const result = hyperlink(styled, "https://example.com");
  381. assert.ok(result.startsWith("\x1b]8;;https://example.com\x07"));
  382. assert.ok(result.includes(styled));
  383. assert.ok(result.endsWith("\x1b]8;;\x07"));
  384. });
  385. it("works with empty text", () => {
  386. const result = hyperlink("", "https://example.com");
  387. assert.strictEqual(result, "\x1b]8;;https://example.com\x07\x1b]8;;\x07");
  388. });
  389. it("works with file:// URIs", () => {
  390. const result = hyperlink("README.md", "file:///home/user/README.md");
  391. assert.ok(result.includes("file:///home/user/README.md"));
  392. assert.ok(result.includes("README.md"));
  393. });
  394. });