bug-regression-isimageline-startswith-bug.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * Bug regression test for isImageLine() crash scenario
  3. *
  4. * Bug: When isImageLine() used startsWith() and terminal doesn't support images,
  5. * it would return false for lines containing image escape sequences, causing TUI to
  6. * crash with "Rendered line exceeds terminal width" error.
  7. *
  8. * Fix: Changed to use includes() to detect escape sequences anywhere in the line.
  9. *
  10. * This test demonstrates:
  11. * 1. The bug scenario with the old implementation
  12. * 2. That the fix works correctly
  13. */
  14. import assert from "node:assert";
  15. import { describe, it } from "node:test";
  16. describe("Bug regression: isImageLine() crash with image escape sequences", () => {
  17. describe("Bug scenario: Terminal without image support", () => {
  18. it("old implementation would return false, causing crash", () => {
  19. /**
  20. * OLD IMPLEMENTATION (buggy):
  21. * ```typescript
  22. * export function isImageLine(line: string): boolean {
  23. * const prefix = getImageEscapePrefix();
  24. * return prefix !== null && line.startsWith(prefix);
  25. * }
  26. * ```
  27. *
  28. * When terminal doesn't support images:
  29. * - getImageEscapePrefix() returns null
  30. * - isImageLine() returns false even for lines containing image sequences
  31. * - TUI performs width check on line containing 300KB+ of base64 data
  32. * - Crash: "Rendered line exceeds terminal width (304401 > 115)"
  33. */
  34. // Simulate old implementation behavior
  35. const oldIsImageLine = (line: string, imageEscapePrefix: string | null): boolean => {
  36. return imageEscapePrefix !== null && line.startsWith(imageEscapePrefix);
  37. };
  38. // When terminal doesn't support images, prefix is null
  39. const terminalWithoutImageSupport = null;
  40. // Line containing image escape sequence with text before it (common bug scenario)
  41. const lineWithImageSequence =
  42. "Read image file [image/jpeg]\x1b]1337;File=size=800,600;inline=1:base64data...\x07";
  43. // Old implementation would return false (BUG!)
  44. const oldResult = oldIsImageLine(lineWithImageSequence, terminalWithoutImageSupport);
  45. assert.strictEqual(
  46. oldResult,
  47. false,
  48. "Bug: old implementation returns false for line containing image sequence when terminal has no image support",
  49. );
  50. });
  51. it("new implementation returns true correctly", async () => {
  52. const { isImageLine } = await import("../src/terminal-image.ts");
  53. // Line containing image escape sequence with text before it
  54. const lineWithImageSequence =
  55. "Read image file [image/jpeg]\x1b]1337;File=size=800,600;inline=1:base64data...\x07";
  56. // New implementation should return true (FIX!)
  57. const newResult = isImageLine(lineWithImageSequence);
  58. assert.strictEqual(newResult, true, "Fix: new implementation returns true for line containing image sequence");
  59. });
  60. it("new implementation detects Kitty sequences in any position", async () => {
  61. const { isImageLine } = await import("../src/terminal-image.ts");
  62. const scenarios = [
  63. "At start: \x1b_Ga=T,f=100,data...\x1b\\",
  64. "Prefix \x1b_Ga=T,data...\x1b\\",
  65. "Suffix text \x1b_Ga=T,data...\x1b\\ suffix",
  66. "Middle \x1b_Ga=T,data...\x1b\\ more text",
  67. // Very long line (simulating 300KB+ crash scenario)
  68. `Text before \x1b_Ga=T,f=100${"A".repeat(300000)} text after`,
  69. ];
  70. for (const line of scenarios) {
  71. assert.strictEqual(isImageLine(line), true, `Should detect Kitty sequence in: ${line.slice(0, 50)}...`);
  72. }
  73. });
  74. it("new implementation detects iTerm2 sequences in any position", async () => {
  75. const { isImageLine } = await import("../src/terminal-image.ts");
  76. const scenarios = [
  77. "At start: \x1b]1337;File=size=100,100:base64...\x07",
  78. "Prefix \x1b]1337;File=inline=1:data==\x07",
  79. "Suffix text \x1b]1337;File=inline=1:data==\x07 suffix",
  80. "Middle \x1b]1337;File=inline=1:data==\x07 more text",
  81. // Very long line (simulating 304KB crash scenario)
  82. `Text before \x1b]1337;File=size=800,600;inline=1:${"B".repeat(300000)} text after`,
  83. ];
  84. for (const line of scenarios) {
  85. assert.strictEqual(isImageLine(line), true, `Should detect iTerm2 sequence in: ${line.slice(0, 50)}...`);
  86. }
  87. });
  88. });
  89. describe("Integration: Tool execution scenario", () => {
  90. /**
  91. * This simulates what happens when the `read` tool reads an image file.
  92. * The tool result contains both text and image content:
  93. *
  94. * ```typescript
  95. * {
  96. * content: [
  97. * { type: "text", text: "Read image file [image/jpeg]\n800x600" },
  98. * { type: "image", data: "base64...", mimeType: "image/jpeg" }
  99. * ]
  100. * }
  101. * ```
  102. *
  103. * When this is rendered, the image component creates escape sequences.
  104. * If isImageLine() doesn't detect them, TUI crashes.
  105. */
  106. it("detects image sequences in read tool output", async () => {
  107. const { isImageLine } = await import("../src/terminal-image.ts");
  108. // Simulate output when read tool processes an image
  109. // The line might have text from the read result plus the image escape sequence
  110. const toolOutputLine = "Read image file [image/jpeg]\x1b]1337;File=size=800,600;inline=1:base64image...\x07";
  111. assert.strictEqual(isImageLine(toolOutputLine), true, "Should detect image sequence in tool output line");
  112. });
  113. it("detects Kitty sequences from Image component", async () => {
  114. const { isImageLine } = await import("../src/terminal-image.ts");
  115. // Kitty image component creates multi-line output with escape sequences
  116. const kittyLine = "\x1b_Ga=T,f=100,t=f,d=base64data...\x1b\\\x1b_Gm=i=1;\x1b\\";
  117. assert.strictEqual(isImageLine(kittyLine), true, "Should detect Kitty image component output");
  118. });
  119. it("handles ANSI codes before image sequences", async () => {
  120. const { isImageLine } = await import("../src/terminal-image.ts");
  121. // Line might have styling (error, warning, etc.) before image data
  122. const lines = [
  123. "\x1b[31mError\x1b[0m: \x1b]1337;File=inline=1:base64==\x07",
  124. "\x1b[33mWarning\x1b[0m: \x1b_Ga=T,data...\x1b\\",
  125. "\x1b[1mBold\x1b[0m \x1b]1337;File=:base64==\x07\x1b[0m",
  126. ];
  127. for (const line of lines) {
  128. assert.strictEqual(
  129. isImageLine(line),
  130. true,
  131. `Should detect image sequence after ANSI codes: ${line.slice(0, 30)}...`,
  132. );
  133. }
  134. });
  135. });
  136. describe("Crash scenario simulation", () => {
  137. it("does NOT crash on very long lines with image sequences", async () => {
  138. const { isImageLine } = await import("../src/terminal-image.ts");
  139. /**
  140. * Simulate the exact crash scenario:
  141. * - Line is 304,401 characters (the crash log showed 58649 > 115)
  142. * - Contains image escape sequence somewhere in the middle
  143. * - Old implementation would return false, causing TUI to do width check
  144. * - New implementation returns true, skipping width check (preventing crash)
  145. */
  146. const base64Char = "A".repeat(100);
  147. const iterm2Sequence = "\x1b]1337;File=size=800,600;inline=1:";
  148. // Build a line that would cause the crash
  149. const crashLine =
  150. "Output: " +
  151. iterm2Sequence +
  152. base64Char.repeat(3040) + // ~304,000 chars
  153. " end of output";
  154. // Verify line is very long
  155. assert(crashLine.length > 300000, "Test line should be > 300KB");
  156. // New implementation should detect it (prevents crash)
  157. const detected = isImageLine(crashLine);
  158. assert.strictEqual(detected, true, "Should detect image sequence in very long line, preventing TUI crash");
  159. });
  160. it("handles lines exactly matching crash log dimensions", async () => {
  161. const { isImageLine } = await import("../src/terminal-image.ts");
  162. /**
  163. * Crash log showed: line 58649 chars wide, terminal width 115
  164. * Let's create a line with similar characteristics
  165. */
  166. const targetWidth = 58649;
  167. const prefix = "Text";
  168. const sequence = "\x1b_Ga=T,f=100";
  169. const suffix = "End";
  170. const padding = "A".repeat(targetWidth - prefix.length - sequence.length - suffix.length);
  171. const line = `${prefix}${sequence}${padding}${suffix}`;
  172. assert.strictEqual(line.length, 58649);
  173. assert.strictEqual(isImageLine(line), true, "Should detect image sequence in 58649-char line");
  174. });
  175. });
  176. describe("Negative cases: Don't false positive", () => {
  177. it("does not detect images in regular long text", async () => {
  178. const { isImageLine } = await import("../src/terminal-image.ts");
  179. // Very long line WITHOUT image sequences
  180. const longText = "A".repeat(100000);
  181. assert.strictEqual(isImageLine(longText), false, "Should not detect images in plain long text");
  182. });
  183. it("does not detect images in lines with file paths", async () => {
  184. const { isImageLine } = await import("../src/terminal-image.ts");
  185. const filePaths = [
  186. "/path/to/1337/image.jpg",
  187. "/usr/local/bin/File_converter",
  188. "~/Documents/1337File_backup.png",
  189. "./_G_test_file.txt",
  190. ];
  191. for (const path of filePaths) {
  192. assert.strictEqual(isImageLine(path), false, `Should not falsely detect image sequence in path: ${path}`);
  193. }
  194. });
  195. });
  196. });