overlay-options.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import type { Component } from "../src/tui.ts";
  4. import { TUI } from "../src/tui.ts";
  5. import { VirtualTerminal } from "./virtual-terminal.ts";
  6. class StaticOverlay implements Component {
  7. private lines: string[];
  8. requestedWidth?: number;
  9. constructor(lines: string[], requestedWidth?: number) {
  10. this.lines = lines;
  11. this.requestedWidth = requestedWidth;
  12. }
  13. render(width: number): string[] {
  14. // Store the width we were asked to render at for verification
  15. this.requestedWidth = width;
  16. return this.lines;
  17. }
  18. invalidate(): void {}
  19. }
  20. class EmptyContent implements Component {
  21. render(): string[] {
  22. return [];
  23. }
  24. invalidate(): void {}
  25. }
  26. async function renderAndFlush(tui: TUI, terminal: VirtualTerminal): Promise<void> {
  27. tui.requestRender(true);
  28. await new Promise<void>((resolve) => process.nextTick(resolve));
  29. await terminal.waitForRender();
  30. }
  31. describe("TUI overlay options", () => {
  32. describe("width overflow protection", () => {
  33. it("should truncate overlay lines that exceed declared width", async () => {
  34. const terminal = new VirtualTerminal(80, 24);
  35. const tui = new TUI(terminal);
  36. // Overlay declares width 20 but renders lines much wider
  37. const overlay = new StaticOverlay(["X".repeat(100)]);
  38. tui.addChild(new EmptyContent());
  39. tui.showOverlay(overlay, { width: 20 });
  40. tui.start();
  41. await renderAndFlush(tui, terminal);
  42. // Should not crash, and no line should exceed terminal width
  43. const viewport = terminal.getViewport();
  44. for (const line of viewport) {
  45. // visibleWidth not available here, but line length is a rough check
  46. // The important thing is it didn't crash
  47. assert.ok(line !== undefined);
  48. }
  49. tui.stop();
  50. });
  51. it("should handle overlay with complex ANSI sequences without crashing", async () => {
  52. const terminal = new VirtualTerminal(80, 24);
  53. const tui = new TUI(terminal);
  54. // Simulate complex ANSI content like the crash log showed
  55. const complexLine =
  56. "\x1b[48;2;40;50;40m \x1b[38;2;128;128;128mSome styled content\x1b[39m\x1b[49m" +
  57. "\x1b]8;;http://example.com\x07link\x1b]8;;\x07" +
  58. " more content ".repeat(10);
  59. const overlay = new StaticOverlay([complexLine, complexLine, complexLine]);
  60. tui.addChild(new EmptyContent());
  61. tui.showOverlay(overlay, { width: 60 });
  62. tui.start();
  63. await renderAndFlush(tui, terminal);
  64. // Should not crash
  65. const viewport = terminal.getViewport();
  66. assert.ok(viewport.length > 0);
  67. tui.stop();
  68. });
  69. it("should handle overlay composited on styled base content", async () => {
  70. const terminal = new VirtualTerminal(80, 24);
  71. const tui = new TUI(terminal);
  72. // Base content with styling
  73. class StyledContent implements Component {
  74. render(width: number): string[] {
  75. const styledLine = `\x1b[1m\x1b[38;2;255;0;0m${"X".repeat(width)}\x1b[0m`;
  76. return [styledLine, styledLine, styledLine];
  77. }
  78. invalidate(): void {}
  79. }
  80. const overlay = new StaticOverlay(["OVERLAY"]);
  81. tui.addChild(new StyledContent());
  82. tui.showOverlay(overlay, { width: 20, anchor: "center" });
  83. tui.start();
  84. await renderAndFlush(tui, terminal);
  85. // Should not crash and overlay should be visible
  86. const viewport = terminal.getViewport();
  87. const hasOverlay = viewport.some((line) => line?.includes("OVERLAY"));
  88. assert.ok(hasOverlay, "Overlay should be visible");
  89. tui.stop();
  90. });
  91. it("should handle wide characters at overlay boundary", async () => {
  92. const terminal = new VirtualTerminal(80, 24);
  93. const tui = new TUI(terminal);
  94. // Wide chars (each takes 2 columns) at the edge of declared width
  95. const wideCharLine = "中文日本語한글テスト漢字"; // Mix of CJK chars
  96. const overlay = new StaticOverlay([wideCharLine]);
  97. tui.addChild(new EmptyContent());
  98. tui.showOverlay(overlay, { width: 15 }); // Odd width to potentially hit boundary
  99. tui.start();
  100. await renderAndFlush(tui, terminal);
  101. // Should not crash
  102. const viewport = terminal.getViewport();
  103. assert.ok(viewport.length > 0);
  104. tui.stop();
  105. });
  106. it("should handle overlay positioned at terminal edge", async () => {
  107. const terminal = new VirtualTerminal(80, 24);
  108. const tui = new TUI(terminal);
  109. // Overlay positioned at right edge with content that exceeds declared width
  110. const overlay = new StaticOverlay(["X".repeat(50)]);
  111. tui.addChild(new EmptyContent());
  112. // Position at col 60 with width 20 - should fit exactly at right edge
  113. tui.showOverlay(overlay, { col: 60, width: 20 });
  114. tui.start();
  115. await renderAndFlush(tui, terminal);
  116. // Should not crash
  117. const viewport = terminal.getViewport();
  118. assert.ok(viewport.length > 0);
  119. tui.stop();
  120. });
  121. it("should handle overlay on base content with OSC sequences", async () => {
  122. const terminal = new VirtualTerminal(80, 24);
  123. const tui = new TUI(terminal);
  124. // Base content with OSC 8 hyperlinks (like file paths in agent output)
  125. class HyperlinkContent implements Component {
  126. render(width: number): string[] {
  127. const link = `\x1b]8;;file:///path/to/file.ts\x07file.ts\x1b]8;;\x07`;
  128. const line = `See ${link} for details ${"X".repeat(width - 30)}`;
  129. return [line, line, line];
  130. }
  131. invalidate(): void {}
  132. }
  133. const overlay = new StaticOverlay(["OVERLAY-TEXT"]);
  134. tui.addChild(new HyperlinkContent());
  135. tui.showOverlay(overlay, { anchor: "center", width: 20 });
  136. tui.start();
  137. await renderAndFlush(tui, terminal);
  138. // Should not crash - this was the original bug scenario
  139. const viewport = terminal.getViewport();
  140. assert.ok(viewport.length > 0);
  141. tui.stop();
  142. });
  143. });
  144. describe("width percentage", () => {
  145. it("should render overlay at percentage of terminal width", async () => {
  146. const terminal = new VirtualTerminal(100, 24);
  147. const tui = new TUI(terminal);
  148. const overlay = new StaticOverlay(["test"]);
  149. tui.addChild(new EmptyContent());
  150. tui.showOverlay(overlay, { width: "50%" });
  151. tui.start();
  152. await renderAndFlush(tui, terminal);
  153. assert.strictEqual(overlay.requestedWidth, 50);
  154. tui.stop();
  155. });
  156. it("should respect minWidth when widthPercent results in smaller width", async () => {
  157. const terminal = new VirtualTerminal(100, 24);
  158. const tui = new TUI(terminal);
  159. const overlay = new StaticOverlay(["test"]);
  160. tui.addChild(new EmptyContent());
  161. tui.showOverlay(overlay, { width: "10%", minWidth: 30 });
  162. tui.start();
  163. await renderAndFlush(tui, terminal);
  164. assert.strictEqual(overlay.requestedWidth, 30);
  165. tui.stop();
  166. });
  167. });
  168. describe("anchor positioning", () => {
  169. it("should position overlay at top-left", async () => {
  170. const terminal = new VirtualTerminal(80, 24);
  171. const tui = new TUI(terminal);
  172. const overlay = new StaticOverlay(["TOP-LEFT"]);
  173. tui.addChild(new EmptyContent());
  174. tui.showOverlay(overlay, { anchor: "top-left", width: 10 });
  175. tui.start();
  176. await renderAndFlush(tui, terminal);
  177. const viewport = terminal.getViewport();
  178. assert.ok(viewport[0]?.startsWith("TOP-LEFT"), `Expected TOP-LEFT at start, got: ${viewport[0]}`);
  179. tui.stop();
  180. });
  181. it("should position overlay at bottom-right", async () => {
  182. const terminal = new VirtualTerminal(80, 24);
  183. const tui = new TUI(terminal);
  184. const overlay = new StaticOverlay(["BTM-RIGHT"]);
  185. tui.addChild(new EmptyContent());
  186. tui.showOverlay(overlay, { anchor: "bottom-right", width: 10 });
  187. tui.start();
  188. await renderAndFlush(tui, terminal);
  189. const viewport = terminal.getViewport();
  190. // Should be on last row, ending at last column
  191. const lastRow = viewport[23];
  192. assert.ok(lastRow?.includes("BTM-RIGHT"), `Expected BTM-RIGHT on last row, got: ${lastRow}`);
  193. assert.ok(lastRow?.trimEnd().endsWith("BTM-RIGHT"), `Expected BTM-RIGHT at end, got: ${lastRow}`);
  194. tui.stop();
  195. });
  196. it("should position overlay at top-center", async () => {
  197. const terminal = new VirtualTerminal(80, 24);
  198. const tui = new TUI(terminal);
  199. const overlay = new StaticOverlay(["CENTERED"]);
  200. tui.addChild(new EmptyContent());
  201. tui.showOverlay(overlay, { anchor: "top-center", width: 10 });
  202. tui.start();
  203. await renderAndFlush(tui, terminal);
  204. const viewport = terminal.getViewport();
  205. // Should be on first row, centered horizontally
  206. const firstRow = viewport[0];
  207. assert.ok(firstRow?.includes("CENTERED"), `Expected CENTERED on first row, got: ${firstRow}`);
  208. // Check it's roughly centered (col 35 for width 10 in 80 col terminal)
  209. const colIndex = firstRow?.indexOf("CENTERED") ?? -1;
  210. assert.ok(colIndex >= 30 && colIndex <= 40, `Expected centered, got col ${colIndex}`);
  211. tui.stop();
  212. });
  213. });
  214. describe("margin", () => {
  215. it("should clamp negative margins to zero", async () => {
  216. const terminal = new VirtualTerminal(80, 24);
  217. const tui = new TUI(terminal);
  218. const overlay = new StaticOverlay(["NEG-MARGIN"]);
  219. tui.addChild(new EmptyContent());
  220. // Negative margins should be treated as 0
  221. tui.showOverlay(overlay, {
  222. anchor: "top-left",
  223. width: 12,
  224. margin: { top: -5, left: -10, right: 0, bottom: 0 },
  225. });
  226. tui.start();
  227. await renderAndFlush(tui, terminal);
  228. const viewport = terminal.getViewport();
  229. // Should be at row 0, col 0 (negative margins clamped to 0)
  230. assert.ok(viewport[0]?.startsWith("NEG-MARGIN"), `Expected NEG-MARGIN at start of row 0, got: ${viewport[0]}`);
  231. tui.stop();
  232. });
  233. it("should respect margin as number", async () => {
  234. const terminal = new VirtualTerminal(80, 24);
  235. const tui = new TUI(terminal);
  236. const overlay = new StaticOverlay(["MARGIN"]);
  237. tui.addChild(new EmptyContent());
  238. tui.showOverlay(overlay, { anchor: "top-left", width: 10, margin: 5 });
  239. tui.start();
  240. await renderAndFlush(tui, terminal);
  241. const viewport = terminal.getViewport();
  242. // Should be on row 5 (not 0) due to margin
  243. assert.ok(!viewport[0]?.includes("MARGIN"), "Should not be on row 0");
  244. assert.ok(!viewport[4]?.includes("MARGIN"), "Should not be on row 4");
  245. assert.ok(viewport[5]?.includes("MARGIN"), `Expected MARGIN on row 5, got: ${viewport[5]}`);
  246. // Should start at col 5 (not 0)
  247. const colIndex = viewport[5]?.indexOf("MARGIN") ?? -1;
  248. assert.strictEqual(colIndex, 5, `Expected col 5, got ${colIndex}`);
  249. tui.stop();
  250. });
  251. it("should respect margin object", async () => {
  252. const terminal = new VirtualTerminal(80, 24);
  253. const tui = new TUI(terminal);
  254. const overlay = new StaticOverlay(["MARGIN"]);
  255. tui.addChild(new EmptyContent());
  256. tui.showOverlay(overlay, {
  257. anchor: "top-left",
  258. width: 10,
  259. margin: { top: 2, left: 3, right: 0, bottom: 0 },
  260. });
  261. tui.start();
  262. await renderAndFlush(tui, terminal);
  263. const viewport = terminal.getViewport();
  264. assert.ok(viewport[2]?.includes("MARGIN"), `Expected MARGIN on row 2, got: ${viewport[2]}`);
  265. const colIndex = viewport[2]?.indexOf("MARGIN") ?? -1;
  266. assert.strictEqual(colIndex, 3, `Expected col 3, got ${colIndex}`);
  267. tui.stop();
  268. });
  269. });
  270. describe("offset", () => {
  271. it("should apply offsetX and offsetY from anchor position", async () => {
  272. const terminal = new VirtualTerminal(80, 24);
  273. const tui = new TUI(terminal);
  274. const overlay = new StaticOverlay(["OFFSET"]);
  275. tui.addChild(new EmptyContent());
  276. tui.showOverlay(overlay, { anchor: "top-left", width: 10, offsetX: 10, offsetY: 5 });
  277. tui.start();
  278. await renderAndFlush(tui, terminal);
  279. const viewport = terminal.getViewport();
  280. assert.ok(viewport[5]?.includes("OFFSET"), `Expected OFFSET on row 5, got: ${viewport[5]}`);
  281. const colIndex = viewport[5]?.indexOf("OFFSET") ?? -1;
  282. assert.strictEqual(colIndex, 10, `Expected col 10, got ${colIndex}`);
  283. tui.stop();
  284. });
  285. });
  286. describe("percentage positioning", () => {
  287. it("should position with rowPercent and colPercent", async () => {
  288. const terminal = new VirtualTerminal(80, 24);
  289. const tui = new TUI(terminal);
  290. const overlay = new StaticOverlay(["PCT"]);
  291. tui.addChild(new EmptyContent());
  292. // 50% should center both ways
  293. tui.showOverlay(overlay, { width: 10, row: "50%", col: "50%" });
  294. tui.start();
  295. await renderAndFlush(tui, terminal);
  296. const viewport = terminal.getViewport();
  297. // Find the row with PCT
  298. let foundRow = -1;
  299. for (let i = 0; i < viewport.length; i++) {
  300. if (viewport[i]?.includes("PCT")) {
  301. foundRow = i;
  302. break;
  303. }
  304. }
  305. // Should be roughly centered vertically (row ~11-12 for 24 row terminal)
  306. assert.ok(foundRow >= 10 && foundRow <= 13, `Expected centered row, got ${foundRow}`);
  307. tui.stop();
  308. });
  309. it("rowPercent 0 should position at top", async () => {
  310. const terminal = new VirtualTerminal(80, 24);
  311. const tui = new TUI(terminal);
  312. const overlay = new StaticOverlay(["TOP"]);
  313. tui.addChild(new EmptyContent());
  314. tui.showOverlay(overlay, { width: 10, row: "0%" });
  315. tui.start();
  316. await renderAndFlush(tui, terminal);
  317. const viewport = terminal.getViewport();
  318. assert.ok(viewport[0]?.includes("TOP"), `Expected TOP on row 0, got: ${viewport[0]}`);
  319. tui.stop();
  320. });
  321. it("rowPercent 100 should position at bottom", async () => {
  322. const terminal = new VirtualTerminal(80, 24);
  323. const tui = new TUI(terminal);
  324. const overlay = new StaticOverlay(["BOTTOM"]);
  325. tui.addChild(new EmptyContent());
  326. tui.showOverlay(overlay, { width: 10, row: "100%" });
  327. tui.start();
  328. await renderAndFlush(tui, terminal);
  329. const viewport = terminal.getViewport();
  330. assert.ok(viewport[23]?.includes("BOTTOM"), `Expected BOTTOM on last row, got: ${viewport[23]}`);
  331. tui.stop();
  332. });
  333. });
  334. describe("maxHeight", () => {
  335. it("should truncate overlay to maxHeight", async () => {
  336. const terminal = new VirtualTerminal(80, 24);
  337. const tui = new TUI(terminal);
  338. const overlay = new StaticOverlay(["Line 1", "Line 2", "Line 3", "Line 4", "Line 5"]);
  339. tui.addChild(new EmptyContent());
  340. tui.showOverlay(overlay, { maxHeight: 3 });
  341. tui.start();
  342. await renderAndFlush(tui, terminal);
  343. const viewport = terminal.getViewport();
  344. const content = viewport.join("\n");
  345. assert.ok(content.includes("Line 1"), "Should include Line 1");
  346. assert.ok(content.includes("Line 2"), "Should include Line 2");
  347. assert.ok(content.includes("Line 3"), "Should include Line 3");
  348. assert.ok(!content.includes("Line 4"), "Should NOT include Line 4");
  349. assert.ok(!content.includes("Line 5"), "Should NOT include Line 5");
  350. tui.stop();
  351. });
  352. it("should truncate overlay to maxHeightPercent", async () => {
  353. const terminal = new VirtualTerminal(80, 10);
  354. const tui = new TUI(terminal);
  355. // 10 lines in a 10 row terminal with 50% maxHeight should show 5 lines
  356. const overlay = new StaticOverlay(["L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L10"]);
  357. tui.addChild(new EmptyContent());
  358. tui.showOverlay(overlay, { maxHeight: "50%" });
  359. tui.start();
  360. await renderAndFlush(tui, terminal);
  361. const viewport = terminal.getViewport();
  362. const content = viewport.join("\n");
  363. assert.ok(content.includes("L1"), "Should include L1");
  364. assert.ok(content.includes("L5"), "Should include L5");
  365. assert.ok(!content.includes("L6"), "Should NOT include L6");
  366. tui.stop();
  367. });
  368. });
  369. describe("absolute positioning", () => {
  370. it("row and col should override anchor", async () => {
  371. const terminal = new VirtualTerminal(80, 24);
  372. const tui = new TUI(terminal);
  373. const overlay = new StaticOverlay(["ABSOLUTE"]);
  374. tui.addChild(new EmptyContent());
  375. // Even with bottom-right anchor, row/col should win
  376. tui.showOverlay(overlay, { anchor: "bottom-right", row: 3, col: 5, width: 10 });
  377. tui.start();
  378. await renderAndFlush(tui, terminal);
  379. const viewport = terminal.getViewport();
  380. assert.ok(viewport[3]?.includes("ABSOLUTE"), `Expected ABSOLUTE on row 3, got: ${viewport[3]}`);
  381. const colIndex = viewport[3]?.indexOf("ABSOLUTE") ?? -1;
  382. assert.strictEqual(colIndex, 5, `Expected col 5, got ${colIndex}`);
  383. tui.stop();
  384. });
  385. });
  386. describe("stacked overlays", () => {
  387. it("should render multiple overlays with later ones on top", async () => {
  388. const terminal = new VirtualTerminal(80, 24);
  389. const tui = new TUI(terminal);
  390. tui.addChild(new EmptyContent());
  391. // First overlay at top-left
  392. const overlay1 = new StaticOverlay(["FIRST-OVERLAY"]);
  393. tui.showOverlay(overlay1, { anchor: "top-left", width: 20 });
  394. // Second overlay at top-left (should cover part of first)
  395. const overlay2 = new StaticOverlay(["SECOND"]);
  396. tui.showOverlay(overlay2, { anchor: "top-left", width: 10 });
  397. tui.start();
  398. await renderAndFlush(tui, terminal);
  399. const viewport = terminal.getViewport();
  400. // Second overlay should be visible (on top)
  401. assert.ok(viewport[0]?.includes("SECOND"), `Expected SECOND on row 0, got: ${viewport[0]}`);
  402. // Part of first overlay might still be visible after SECOND
  403. // FIRST-OVERLAY is 13 chars, SECOND is 6 chars, so "OVERLAY" part might show
  404. tui.stop();
  405. });
  406. it("should handle overlays at different positions without interference", async () => {
  407. const terminal = new VirtualTerminal(80, 24);
  408. const tui = new TUI(terminal);
  409. tui.addChild(new EmptyContent());
  410. // Overlay at top-left
  411. const overlay1 = new StaticOverlay(["TOP-LEFT"]);
  412. tui.showOverlay(overlay1, { anchor: "top-left", width: 15 });
  413. // Overlay at bottom-right
  414. const overlay2 = new StaticOverlay(["BTM-RIGHT"]);
  415. tui.showOverlay(overlay2, { anchor: "bottom-right", width: 15 });
  416. tui.start();
  417. await renderAndFlush(tui, terminal);
  418. const viewport = terminal.getViewport();
  419. // Both should be visible
  420. assert.ok(viewport[0]?.includes("TOP-LEFT"), `Expected TOP-LEFT on row 0, got: ${viewport[0]}`);
  421. assert.ok(viewport[23]?.includes("BTM-RIGHT"), `Expected BTM-RIGHT on row 23, got: ${viewport[23]}`);
  422. tui.stop();
  423. });
  424. it("should properly hide overlays in stack order", async () => {
  425. const terminal = new VirtualTerminal(80, 24);
  426. const tui = new TUI(terminal);
  427. tui.addChild(new EmptyContent());
  428. // Show two overlays
  429. const overlay1 = new StaticOverlay(["FIRST"]);
  430. tui.showOverlay(overlay1, { anchor: "top-left", width: 10 });
  431. const overlay2 = new StaticOverlay(["SECOND"]);
  432. tui.showOverlay(overlay2, { anchor: "top-left", width: 10 });
  433. tui.start();
  434. await renderAndFlush(tui, terminal);
  435. // Second should be visible
  436. let viewport = terminal.getViewport();
  437. assert.ok(viewport[0]?.includes("SECOND"), "SECOND should be visible initially");
  438. // Hide top overlay
  439. tui.hideOverlay();
  440. await renderAndFlush(tui, terminal);
  441. // First should now be visible
  442. viewport = terminal.getViewport();
  443. assert.ok(viewport[0]?.includes("FIRST"), "FIRST should be visible after hiding SECOND");
  444. tui.stop();
  445. });
  446. });
  447. });