overlay-non-capturing.test.ts 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import type { Component, Focusable } from "../src/tui.ts";
  4. import { Container, TUI } from "../src/tui.ts";
  5. import { VirtualTerminal } from "./virtual-terminal.ts";
  6. class StaticOverlay implements Component {
  7. private lines: string[];
  8. constructor(lines: string[]) {
  9. this.lines = lines;
  10. }
  11. render(): string[] {
  12. return this.lines;
  13. }
  14. invalidate(): void {}
  15. }
  16. class EmptyContent implements Component {
  17. render(): string[] {
  18. return [];
  19. }
  20. invalidate(): void {}
  21. }
  22. class FocusableOverlay implements Component, Focusable {
  23. focused = false;
  24. inputs: string[] = [];
  25. private lines: string[];
  26. constructor(lines: string[]) {
  27. this.lines = lines;
  28. }
  29. handleInput(data: string): void {
  30. this.inputs.push(data);
  31. }
  32. render(): string[] {
  33. return this.lines;
  34. }
  35. invalidate(): void {}
  36. }
  37. async function renderAndFlush(tui: TUI, terminal: VirtualTerminal): Promise<void> {
  38. tui.requestRender(true);
  39. await new Promise<void>((resolve) => process.nextTick(resolve));
  40. await terminal.waitForRender();
  41. }
  42. describe("TUI overlay non-capturing", () => {
  43. describe("focus management", () => {
  44. it("non-capturing overlay preserves focus on creation", async () => {
  45. const terminal = new VirtualTerminal(80, 24);
  46. const tui = new TUI(terminal);
  47. const editor = new FocusableOverlay(["EDITOR"]);
  48. const overlay = new FocusableOverlay(["OVERLAY"]);
  49. tui.addChild(new EmptyContent());
  50. tui.setFocus(editor);
  51. tui.start();
  52. try {
  53. tui.showOverlay(overlay, { nonCapturing: true });
  54. await renderAndFlush(tui, terminal);
  55. assert.strictEqual(editor.focused, true);
  56. assert.strictEqual(overlay.focused, false);
  57. } finally {
  58. tui.stop();
  59. }
  60. });
  61. it("focus() transfers focus to the overlay", async () => {
  62. const terminal = new VirtualTerminal(80, 24);
  63. const tui = new TUI(terminal);
  64. const editor = new FocusableOverlay(["EDITOR"]);
  65. const overlay = new FocusableOverlay(["OVERLAY"]);
  66. tui.addChild(new EmptyContent());
  67. tui.setFocus(editor);
  68. tui.start();
  69. try {
  70. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  71. handle.focus();
  72. await renderAndFlush(tui, terminal);
  73. assert.strictEqual(editor.focused, false);
  74. assert.strictEqual(overlay.focused, true);
  75. assert.strictEqual(handle.isFocused(), true);
  76. } finally {
  77. tui.stop();
  78. }
  79. });
  80. it("unfocus() restores previous focus", async () => {
  81. const terminal = new VirtualTerminal(80, 24);
  82. const tui = new TUI(terminal);
  83. const editor = new FocusableOverlay(["EDITOR"]);
  84. const overlay = new FocusableOverlay(["OVERLAY"]);
  85. tui.addChild(new EmptyContent());
  86. tui.setFocus(editor);
  87. tui.start();
  88. try {
  89. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  90. handle.focus();
  91. handle.unfocus();
  92. await renderAndFlush(tui, terminal);
  93. assert.strictEqual(editor.focused, true);
  94. assert.strictEqual(overlay.focused, false);
  95. assert.strictEqual(handle.isFocused(), false);
  96. } finally {
  97. tui.stop();
  98. }
  99. });
  100. it("setHidden(false) on non-capturing overlay does not auto-focus", async () => {
  101. const terminal = new VirtualTerminal(80, 24);
  102. const tui = new TUI(terminal);
  103. const editor = new FocusableOverlay(["EDITOR"]);
  104. const overlay = new FocusableOverlay(["OVERLAY"]);
  105. tui.addChild(new EmptyContent());
  106. tui.setFocus(editor);
  107. tui.start();
  108. try {
  109. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  110. handle.setHidden(true);
  111. handle.setHidden(false);
  112. await renderAndFlush(tui, terminal);
  113. assert.strictEqual(editor.focused, true);
  114. assert.strictEqual(overlay.focused, false);
  115. } finally {
  116. tui.stop();
  117. }
  118. });
  119. it("hide() when overlay is not focused does not change focus", async () => {
  120. const terminal = new VirtualTerminal(80, 24);
  121. const tui = new TUI(terminal);
  122. const editor = new FocusableOverlay(["EDITOR"]);
  123. const overlay = new FocusableOverlay(["OVERLAY"]);
  124. tui.addChild(new EmptyContent());
  125. tui.setFocus(editor);
  126. tui.start();
  127. try {
  128. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  129. handle.hide();
  130. await renderAndFlush(tui, terminal);
  131. assert.strictEqual(editor.focused, true);
  132. } finally {
  133. tui.stop();
  134. }
  135. });
  136. it("hide() when focused restores focus correctly", async () => {
  137. const terminal = new VirtualTerminal(80, 24);
  138. const tui = new TUI(terminal);
  139. const editor = new FocusableOverlay(["EDITOR"]);
  140. const overlay = new FocusableOverlay(["OVERLAY"]);
  141. tui.addChild(new EmptyContent());
  142. tui.setFocus(editor);
  143. tui.start();
  144. try {
  145. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  146. handle.focus();
  147. handle.hide();
  148. await renderAndFlush(tui, terminal);
  149. assert.strictEqual(editor.focused, true);
  150. assert.strictEqual(overlay.focused, false);
  151. } finally {
  152. tui.stop();
  153. }
  154. });
  155. it("capturing overlay removed with non-capturing below restores focus to editor", async () => {
  156. const terminal = new VirtualTerminal(80, 24);
  157. const tui = new TUI(terminal);
  158. const editor = new FocusableOverlay(["EDITOR"]);
  159. const nonCapturing = new FocusableOverlay(["NC"]);
  160. const capturing = new FocusableOverlay(["CAP"]);
  161. tui.addChild(new EmptyContent());
  162. tui.setFocus(editor);
  163. tui.start();
  164. try {
  165. tui.showOverlay(nonCapturing, { nonCapturing: true });
  166. const handle = tui.showOverlay(capturing);
  167. assert.strictEqual(capturing.focused, true);
  168. handle.hide();
  169. await renderAndFlush(tui, terminal);
  170. assert.strictEqual(editor.focused, true);
  171. assert.strictEqual(nonCapturing.focused, false);
  172. } finally {
  173. tui.stop();
  174. }
  175. });
  176. it("sub-overlay cleanup then hideOverlay restores focus and input to editor", async () => {
  177. const terminal = new VirtualTerminal(80, 24);
  178. const tui = new TUI(terminal);
  179. const editor = new FocusableOverlay(["EDITOR"]);
  180. const timer = new FocusableOverlay(["TIMER"]);
  181. const controller = new FocusableOverlay(["CTRL"]);
  182. tui.addChild(new EmptyContent());
  183. tui.setFocus(editor);
  184. tui.start();
  185. try {
  186. const timerHandle = tui.showOverlay(timer, { nonCapturing: true });
  187. tui.showOverlay(controller);
  188. assert.strictEqual(controller.focused, true);
  189. assert.strictEqual(editor.focused, false);
  190. timerHandle.hide();
  191. tui.hideOverlay();
  192. await renderAndFlush(tui, terminal);
  193. assert.strictEqual(editor.focused, true);
  194. assert.strictEqual(controller.focused, false);
  195. assert.strictEqual(timer.focused, false);
  196. terminal.sendInput("x");
  197. await renderAndFlush(tui, terminal);
  198. assert.deepStrictEqual(editor.inputs, ["x"]);
  199. assert.deepStrictEqual(controller.inputs, []);
  200. assert.deepStrictEqual(timer.inputs, []);
  201. } finally {
  202. tui.stop();
  203. }
  204. });
  205. it("removed focused child overlay does not become parent overlay fallback", async () => {
  206. const terminal = new VirtualTerminal(80, 24);
  207. const tui = new TUI(terminal);
  208. const editor = new FocusableOverlay(["EDITOR"]);
  209. const child = new FocusableOverlay(["CHILD"]);
  210. const parent = new FocusableOverlay(["PARENT"]);
  211. tui.addChild(new EmptyContent());
  212. tui.setFocus(editor);
  213. tui.start();
  214. try {
  215. const childHandle = tui.showOverlay(child, { nonCapturing: true });
  216. childHandle.focus();
  217. const parentHandle = tui.showOverlay(parent);
  218. assert.strictEqual(parent.focused, true);
  219. childHandle.hide();
  220. parentHandle.hide();
  221. terminal.sendInput("x");
  222. await renderAndFlush(tui, terminal);
  223. assert.deepStrictEqual(editor.inputs, ["x"]);
  224. assert.deepStrictEqual(child.inputs, []);
  225. assert.deepStrictEqual(parent.inputs, []);
  226. assert.strictEqual(editor.focused, true);
  227. } finally {
  228. tui.stop();
  229. }
  230. });
  231. it("microtask-deferred sub-overlay pattern (showExtensionCustom simulation) restores focus", async () => {
  232. const terminal = new VirtualTerminal(80, 24);
  233. const tui = new TUI(terminal);
  234. const editor = new FocusableOverlay(["EDITOR"]);
  235. const timer = new FocusableOverlay(["TIMER"]);
  236. const controller = new FocusableOverlay(["CTRL"]);
  237. tui.addChild(new EmptyContent());
  238. tui.setFocus(editor);
  239. tui.start();
  240. try {
  241. // Simulate showExtensionCustom: factory creates timer synchronously,
  242. // then .then() pushes controller as a microtask
  243. let timerHandle: ReturnType<typeof tui.showOverlay> | null = null;
  244. let doneFn: () => void = () => {
  245. throw new Error("doneFn was not initialized");
  246. };
  247. const overlayPromise = new Promise<void>((resolve) => {
  248. doneFn = () => {
  249. if (!timerHandle) throw new Error("timerHandle was not initialized");
  250. timerHandle.hide();
  251. tui.hideOverlay();
  252. resolve();
  253. };
  254. timerHandle = tui.showOverlay(timer, { nonCapturing: true });
  255. // .then() runs as microtask — same as showExtensionCustom
  256. Promise.resolve(controller).then((c) => {
  257. tui.showOverlay(c);
  258. });
  259. });
  260. await Promise.resolve();
  261. await renderAndFlush(tui, terminal);
  262. assert.strictEqual(controller.focused, true);
  263. assert.strictEqual(editor.focused, false);
  264. // Simulate Esc: cleanup + close (from inside handleInput)
  265. doneFn();
  266. // Now await the promise (simulating showExtensionCustom resolving)
  267. await overlayPromise;
  268. await renderAndFlush(tui, terminal);
  269. assert.strictEqual(editor.focused, true, "editor should regain focus");
  270. assert.strictEqual(controller.focused, false);
  271. assert.strictEqual(timer.focused, false);
  272. terminal.sendInput("x");
  273. await renderAndFlush(tui, terminal);
  274. assert.deepStrictEqual(editor.inputs, ["x"], "editor should receive input after close");
  275. assert.deepStrictEqual(controller.inputs, []);
  276. } finally {
  277. tui.stop();
  278. }
  279. });
  280. it("handleInput redirection skips non-capturing overlays when focused overlay becomes invisible", async () => {
  281. const terminal = new VirtualTerminal(80, 24);
  282. const tui = new TUI(terminal);
  283. const editor = new FocusableOverlay(["EDITOR"]);
  284. const fallbackCapturing = new FocusableOverlay(["FALLBACK"]);
  285. const nonCapturing = new FocusableOverlay(["NC"]);
  286. const primary = new FocusableOverlay(["PRIMARY"]);
  287. let isVisible = true;
  288. tui.addChild(new EmptyContent());
  289. tui.setFocus(editor);
  290. tui.start();
  291. try {
  292. tui.showOverlay(fallbackCapturing);
  293. tui.showOverlay(nonCapturing, { nonCapturing: true });
  294. tui.showOverlay(primary, { visible: () => isVisible });
  295. assert.strictEqual(primary.focused, true);
  296. isVisible = false;
  297. terminal.sendInput("x");
  298. await renderAndFlush(tui, terminal);
  299. assert.deepStrictEqual(primary.inputs, []);
  300. assert.deepStrictEqual(nonCapturing.inputs, []);
  301. assert.deepStrictEqual(fallbackCapturing.inputs, ["x"]);
  302. assert.strictEqual(fallbackCapturing.focused, true);
  303. } finally {
  304. tui.stop();
  305. }
  306. });
  307. it("active base focus replacement receives close input before overlay restore", async () => {
  308. const terminal = new VirtualTerminal(80, 24);
  309. const tui = new TUI(terminal);
  310. const editor = new FocusableOverlay(["EDITOR"]);
  311. const replacement = new FocusableOverlay(["REPLACEMENT"]);
  312. const overlay = new FocusableOverlay(["OVERLAY"]);
  313. overlay.handleInput = (data: string) => {
  314. overlay.inputs.push(data);
  315. if (data === "b") {
  316. tui.setFocus(replacement);
  317. }
  318. };
  319. replacement.handleInput = (data: string) => {
  320. replacement.inputs.push(data);
  321. if (data === "\r") {
  322. tui.setFocus(editor);
  323. }
  324. };
  325. tui.addChild(new EmptyContent());
  326. tui.setFocus(editor);
  327. tui.start();
  328. try {
  329. tui.showOverlay(overlay);
  330. assert.strictEqual(overlay.focused, true);
  331. terminal.sendInput("b");
  332. await renderAndFlush(tui, terminal);
  333. assert.strictEqual(replacement.focused, true);
  334. terminal.sendInput("\r");
  335. await renderAndFlush(tui, terminal);
  336. assert.deepStrictEqual(replacement.inputs, ["\r"]);
  337. assert.deepStrictEqual(overlay.inputs, ["b"]);
  338. assert.strictEqual(overlay.focused, true);
  339. terminal.sendInput("x");
  340. await renderAndFlush(tui, terminal);
  341. assert.deepStrictEqual(overlay.inputs, ["b", "x"]);
  342. } finally {
  343. tui.stop();
  344. }
  345. });
  346. it("active replacement still receives input when it is another overlay preFocus", async () => {
  347. const terminal = new VirtualTerminal(80, 24);
  348. const tui = new TUI(terminal);
  349. const editor = new FocusableOverlay(["EDITOR"]);
  350. const replacement = new FocusableOverlay(["REPLACEMENT"]);
  351. const passive = new FocusableOverlay(["PASSIVE"]);
  352. const overlay = new FocusableOverlay(["OVERLAY"]);
  353. overlay.handleInput = (data: string) => {
  354. overlay.inputs.push(data);
  355. if (data === "b") {
  356. tui.setFocus(replacement);
  357. }
  358. };
  359. replacement.handleInput = (data: string) => {
  360. replacement.inputs.push(data);
  361. if (data === "\r") {
  362. tui.setFocus(editor);
  363. }
  364. };
  365. tui.addChild(new EmptyContent());
  366. tui.setFocus(editor);
  367. tui.start();
  368. try {
  369. tui.setFocus(replacement);
  370. tui.showOverlay(passive, { nonCapturing: true });
  371. tui.setFocus(editor);
  372. tui.showOverlay(overlay);
  373. terminal.sendInput("b");
  374. await renderAndFlush(tui, terminal);
  375. assert.strictEqual(replacement.focused, true);
  376. terminal.sendInput("1");
  377. terminal.sendInput("\r");
  378. await renderAndFlush(tui, terminal);
  379. assert.deepStrictEqual(replacement.inputs, ["1", "\r"]);
  380. assert.deepStrictEqual(overlay.inputs, ["b"]);
  381. assert.strictEqual(overlay.focused, true);
  382. } finally {
  383. tui.stop();
  384. }
  385. });
  386. it("blocked replacement can move focus internally before overlay restore", async () => {
  387. const terminal = new VirtualTerminal(80, 24);
  388. const tui = new TUI(terminal);
  389. const base = new Container();
  390. const editor = new FocusableOverlay(["EDITOR"]);
  391. const firstReplacement = new FocusableOverlay(["FIRST"]);
  392. const secondReplacement = new FocusableOverlay(["SECOND"]);
  393. const overlay = new FocusableOverlay(["OVERLAY"]);
  394. overlay.handleInput = (data: string) => {
  395. overlay.inputs.push(data);
  396. if (data === "b") tui.setFocus(firstReplacement);
  397. };
  398. firstReplacement.handleInput = (data: string) => {
  399. firstReplacement.inputs.push(data);
  400. if (data === "n") tui.setFocus(secondReplacement);
  401. };
  402. secondReplacement.handleInput = (data: string) => {
  403. secondReplacement.inputs.push(data);
  404. if (data === "\r") {
  405. base.clear();
  406. base.addChild(editor);
  407. tui.setFocus(editor);
  408. }
  409. };
  410. base.addChild(editor);
  411. base.addChild(firstReplacement);
  412. base.addChild(secondReplacement);
  413. tui.addChild(base);
  414. tui.setFocus(editor);
  415. tui.start();
  416. try {
  417. tui.showOverlay(overlay);
  418. terminal.sendInput("b");
  419. await renderAndFlush(tui, terminal);
  420. terminal.sendInput("n");
  421. await renderAndFlush(tui, terminal);
  422. terminal.sendInput("2");
  423. terminal.sendInput("\r");
  424. await renderAndFlush(tui, terminal);
  425. assert.deepStrictEqual(overlay.inputs, ["b"]);
  426. assert.deepStrictEqual(firstReplacement.inputs, ["n"]);
  427. assert.deepStrictEqual(secondReplacement.inputs, ["2", "\r"]);
  428. assert.strictEqual(overlay.focused, true);
  429. } finally {
  430. tui.stop();
  431. }
  432. });
  433. it("removed replacement restores overlay even when overlay preFocus differs from next focus", async () => {
  434. const terminal = new VirtualTerminal(80, 24);
  435. const tui = new TUI(terminal);
  436. const base = new Container();
  437. const editor = new FocusableOverlay(["EDITOR"]);
  438. const palette = new FocusableOverlay(["PALETTE"]);
  439. const replacement = new FocusableOverlay(["REPLACEMENT"]);
  440. const overlay = new FocusableOverlay(["OVERLAY"]);
  441. overlay.handleInput = (data: string) => {
  442. overlay.inputs.push(data);
  443. if (data === "b") tui.setFocus(replacement);
  444. };
  445. replacement.handleInput = (data: string) => {
  446. replacement.inputs.push(data);
  447. if (data === "\r") {
  448. base.clear();
  449. base.addChild(editor);
  450. tui.setFocus(editor);
  451. }
  452. };
  453. base.addChild(editor);
  454. base.addChild(palette);
  455. base.addChild(replacement);
  456. tui.addChild(base);
  457. tui.setFocus(palette);
  458. tui.start();
  459. try {
  460. tui.showOverlay(overlay);
  461. terminal.sendInput("b");
  462. await renderAndFlush(tui, terminal);
  463. terminal.sendInput("\r");
  464. terminal.sendInput("x");
  465. await renderAndFlush(tui, terminal);
  466. assert.deepStrictEqual(overlay.inputs, ["b", "x"]);
  467. assert.deepStrictEqual(replacement.inputs, ["\r"]);
  468. assert.deepStrictEqual(editor.inputs, []);
  469. assert.strictEqual(overlay.focused, true);
  470. } finally {
  471. tui.stop();
  472. }
  473. });
  474. it("unfocus target releases a blocked overlay while replacement remains focused", async () => {
  475. const terminal = new VirtualTerminal(80, 24);
  476. const tui = new TUI(terminal);
  477. const fallback = new FocusableOverlay(["FALLBACK"]);
  478. const target = new FocusableOverlay(["TARGET"]);
  479. const replacement = new FocusableOverlay(["REPLACEMENT"]);
  480. const overlay = new FocusableOverlay(["OVERLAY"]);
  481. replacement.handleInput = (data: string) => {
  482. replacement.inputs.push(data);
  483. if (data === "\r") tui.setFocus(fallback);
  484. };
  485. tui.addChild(new EmptyContent());
  486. tui.start();
  487. try {
  488. const overlayHandle = tui.showOverlay(overlay);
  489. overlay.handleInput = (data: string) => {
  490. overlay.inputs.push(data);
  491. if (data === "b") {
  492. tui.setFocus(replacement);
  493. overlayHandle.unfocus({ target });
  494. }
  495. };
  496. terminal.sendInput("b");
  497. await renderAndFlush(tui, terminal);
  498. assert.strictEqual(replacement.focused, true);
  499. terminal.sendInput("\r");
  500. terminal.sendInput("x");
  501. await renderAndFlush(tui, terminal);
  502. assert.deepStrictEqual(overlay.inputs, ["b"]);
  503. assert.deepStrictEqual(replacement.inputs, ["\r"]);
  504. assert.deepStrictEqual(fallback.inputs, []);
  505. assert.deepStrictEqual(target.inputs, ["x"]);
  506. } finally {
  507. tui.stop();
  508. }
  509. });
  510. it("handleInput restores focus to a visible focused overlay after base focus steal", async () => {
  511. const terminal = new VirtualTerminal(80, 24);
  512. const tui = new TUI(terminal);
  513. const editor = new FocusableOverlay(["EDITOR"]);
  514. const replacement = new FocusableOverlay(["REPLACEMENT"]);
  515. const overlay = new FocusableOverlay(["OVERLAY"]);
  516. tui.addChild(new EmptyContent());
  517. tui.setFocus(editor);
  518. tui.start();
  519. try {
  520. tui.showOverlay(overlay);
  521. assert.strictEqual(overlay.focused, true);
  522. tui.setFocus(replacement);
  523. tui.setFocus(editor);
  524. terminal.sendInput("x");
  525. await renderAndFlush(tui, terminal);
  526. assert.deepStrictEqual(overlay.inputs, ["x"]);
  527. assert.deepStrictEqual(editor.inputs, []);
  528. assert.strictEqual(overlay.focused, true);
  529. } finally {
  530. tui.stop();
  531. }
  532. });
  533. it("handleInput restores focus to explicitly focused raw sub-overlay after base focus steal", async () => {
  534. const terminal = new VirtualTerminal(80, 24);
  535. const tui = new TUI(terminal);
  536. const editor = new FocusableOverlay(["EDITOR"]);
  537. const controller = new FocusableOverlay(["CONTROLLER"]);
  538. const subOverlay = new FocusableOverlay(["SUB"]);
  539. tui.addChild(new EmptyContent());
  540. tui.setFocus(editor);
  541. tui.start();
  542. try {
  543. tui.showOverlay(controller);
  544. const subHandle = tui.showOverlay(subOverlay, { nonCapturing: true });
  545. subHandle.focus();
  546. tui.setFocus(editor);
  547. terminal.sendInput("x");
  548. await renderAndFlush(tui, terminal);
  549. assert.deepStrictEqual(subOverlay.inputs, ["x"]);
  550. assert.deepStrictEqual(controller.inputs, []);
  551. assert.deepStrictEqual(editor.inputs, []);
  552. } finally {
  553. tui.stop();
  554. }
  555. });
  556. it("passive non-capturing overlay does not regain input after base focus", async () => {
  557. const terminal = new VirtualTerminal(80, 24);
  558. const tui = new TUI(terminal);
  559. const editor = new FocusableOverlay(["EDITOR"]);
  560. const passive = new FocusableOverlay(["PASSIVE"]);
  561. tui.addChild(new EmptyContent());
  562. tui.setFocus(editor);
  563. tui.start();
  564. try {
  565. tui.showOverlay(passive, { nonCapturing: true });
  566. terminal.sendInput("x");
  567. await renderAndFlush(tui, terminal);
  568. assert.deepStrictEqual(editor.inputs, ["x"]);
  569. assert.deepStrictEqual(passive.inputs, []);
  570. assert.strictEqual(editor.focused, true);
  571. } finally {
  572. tui.stop();
  573. }
  574. });
  575. it("explicitly focused non-capturing overlay regains input after base focus steal", async () => {
  576. const terminal = new VirtualTerminal(80, 24);
  577. const tui = new TUI(terminal);
  578. const editor = new FocusableOverlay(["EDITOR"]);
  579. const overlay = new FocusableOverlay(["NC"]);
  580. tui.addChild(new EmptyContent());
  581. tui.setFocus(editor);
  582. tui.start();
  583. try {
  584. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  585. handle.focus();
  586. tui.setFocus(editor);
  587. terminal.sendInput("x");
  588. await renderAndFlush(tui, terminal);
  589. assert.deepStrictEqual(overlay.inputs, ["x"]);
  590. assert.deepStrictEqual(editor.inputs, []);
  591. } finally {
  592. tui.stop();
  593. }
  594. });
  595. it("unfocus() prevents visible overlay from regaining input", async () => {
  596. const terminal = new VirtualTerminal(80, 24);
  597. const tui = new TUI(terminal);
  598. const editor = new FocusableOverlay(["EDITOR"]);
  599. const overlay = new FocusableOverlay(["OVERLAY"]);
  600. tui.addChild(new EmptyContent());
  601. tui.setFocus(editor);
  602. tui.start();
  603. try {
  604. const handle = tui.showOverlay(overlay);
  605. handle.unfocus();
  606. terminal.sendInput("x");
  607. await renderAndFlush(tui, terminal);
  608. assert.deepStrictEqual(editor.inputs, ["x"]);
  609. assert.deepStrictEqual(overlay.inputs, []);
  610. assert.strictEqual(editor.focused, true);
  611. } finally {
  612. tui.stop();
  613. }
  614. });
  615. it("setFocus(null) explicitly clears visible overlay restore", async () => {
  616. const terminal = new VirtualTerminal(80, 24);
  617. const tui = new TUI(terminal);
  618. const overlay = new FocusableOverlay(["OVERLAY"]);
  619. tui.addChild(new EmptyContent());
  620. tui.start();
  621. try {
  622. tui.showOverlay(overlay);
  623. tui.setFocus(null);
  624. terminal.sendInput("x");
  625. await renderAndFlush(tui, terminal);
  626. assert.deepStrictEqual(overlay.inputs, []);
  627. assert.strictEqual(overlay.focused, false);
  628. } finally {
  629. tui.stop();
  630. }
  631. });
  632. it("blocked replacement setFocus(null) resumes the visible overlay", async () => {
  633. const terminal = new VirtualTerminal(80, 24);
  634. const tui = new TUI(terminal);
  635. const replacement = new FocusableOverlay(["REPLACEMENT"]);
  636. const overlay = new FocusableOverlay(["OVERLAY"]);
  637. replacement.handleInput = (data: string) => {
  638. replacement.inputs.push(data);
  639. if (data === "\r") tui.setFocus(null);
  640. };
  641. overlay.handleInput = (data: string) => {
  642. overlay.inputs.push(data);
  643. if (data === "b") tui.setFocus(replacement);
  644. };
  645. tui.addChild(new EmptyContent());
  646. tui.start();
  647. try {
  648. tui.showOverlay(overlay);
  649. terminal.sendInput("b");
  650. await renderAndFlush(tui, terminal);
  651. terminal.sendInput("\r");
  652. terminal.sendInput("x");
  653. await renderAndFlush(tui, terminal);
  654. assert.deepStrictEqual(replacement.inputs, ["\r"]);
  655. assert.deepStrictEqual(overlay.inputs, ["b", "x"]);
  656. assert.strictEqual(overlay.focused, true);
  657. } finally {
  658. tui.stop();
  659. }
  660. });
  661. it("temporarily invisible focused overlay falls back without losing restore eligibility", async () => {
  662. const terminal = new VirtualTerminal(80, 24);
  663. const tui = new TUI(terminal);
  664. const editor = new FocusableOverlay(["EDITOR"]);
  665. const overlay = new FocusableOverlay(["OVERLAY"]);
  666. let visible = true;
  667. tui.addChild(new EmptyContent());
  668. tui.setFocus(editor);
  669. tui.start();
  670. try {
  671. tui.showOverlay(overlay, { visible: () => visible });
  672. tui.setFocus(editor);
  673. visible = false;
  674. terminal.sendInput("x");
  675. await renderAndFlush(tui, terminal);
  676. assert.deepStrictEqual(editor.inputs, ["x"]);
  677. assert.deepStrictEqual(overlay.inputs, []);
  678. visible = true;
  679. terminal.sendInput("y");
  680. await renderAndFlush(tui, terminal);
  681. assert.deepStrictEqual(editor.inputs, ["x"]);
  682. assert.deepStrictEqual(overlay.inputs, ["y"]);
  683. } finally {
  684. tui.stop();
  685. }
  686. });
  687. it("temporarily invisible focused overlay with null preFocus restores when visible again", async () => {
  688. const terminal = new VirtualTerminal(80, 24);
  689. const tui = new TUI(terminal);
  690. const overlay = new FocusableOverlay(["OVERLAY"]);
  691. let visible = true;
  692. tui.addChild(new EmptyContent());
  693. tui.start();
  694. try {
  695. tui.showOverlay(overlay, { visible: () => visible });
  696. visible = false;
  697. terminal.sendInput("x");
  698. await renderAndFlush(tui, terminal);
  699. assert.deepStrictEqual(overlay.inputs, []);
  700. visible = true;
  701. terminal.sendInput("y");
  702. await renderAndFlush(tui, terminal);
  703. assert.deepStrictEqual(overlay.inputs, ["y"]);
  704. } finally {
  705. tui.stop();
  706. }
  707. });
  708. it("cyclic overlay preFocus ancestry does not hang focus changes", async () => {
  709. const terminal = new VirtualTerminal(80, 24);
  710. const tui = new TUI(terminal);
  711. const editor = new FocusableOverlay(["EDITOR"]);
  712. const overlay = new FocusableOverlay(["OVERLAY"]);
  713. tui.addChild(new EmptyContent());
  714. tui.setFocus(overlay);
  715. tui.start();
  716. try {
  717. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  718. handle.focus();
  719. tui.setFocus(editor);
  720. terminal.sendInput("x");
  721. await renderAndFlush(tui, terminal);
  722. assert.deepStrictEqual(editor.inputs, ["x"]);
  723. assert.deepStrictEqual(overlay.inputs, []);
  724. } finally {
  725. tui.stop();
  726. }
  727. });
  728. it("handleInput restores the focus-order top overlay after base focus steal", async () => {
  729. const terminal = new VirtualTerminal(80, 24);
  730. const tui = new TUI(terminal);
  731. const editor = new FocusableOverlay(["EDITOR"]);
  732. const lower = new FocusableOverlay(["LOWER"]);
  733. const upper = new FocusableOverlay(["UPPER"]);
  734. tui.addChild(new EmptyContent());
  735. tui.setFocus(editor);
  736. tui.start();
  737. try {
  738. const lowerHandle = tui.showOverlay(lower);
  739. tui.showOverlay(upper);
  740. lowerHandle.focus();
  741. tui.setFocus(editor);
  742. terminal.sendInput("x");
  743. await renderAndFlush(tui, terminal);
  744. assert.deepStrictEqual(lower.inputs, ["x"]);
  745. assert.deepStrictEqual(upper.inputs, []);
  746. assert.deepStrictEqual(editor.inputs, []);
  747. } finally {
  748. tui.stop();
  749. }
  750. });
  751. it("hideOverlay() does not reassign focus when topmost overlay is non-capturing", async () => {
  752. const terminal = new VirtualTerminal(80, 24);
  753. const tui = new TUI(terminal);
  754. const editor = new FocusableOverlay(["EDITOR"]);
  755. const capturing = new FocusableOverlay(["CAP"]);
  756. const nonCapturing = new FocusableOverlay(["NC"]);
  757. tui.addChild(new EmptyContent());
  758. tui.setFocus(editor);
  759. tui.start();
  760. try {
  761. tui.showOverlay(capturing);
  762. tui.showOverlay(nonCapturing, { nonCapturing: true });
  763. assert.strictEqual(capturing.focused, true);
  764. tui.hideOverlay();
  765. await renderAndFlush(tui, terminal);
  766. assert.strictEqual(capturing.focused, true);
  767. } finally {
  768. tui.stop();
  769. }
  770. });
  771. it("multiple capturing and non-capturing overlays restore focus through removals", async () => {
  772. const terminal = new VirtualTerminal(80, 24);
  773. const tui = new TUI(terminal);
  774. const editor = new FocusableOverlay(["EDITOR"]);
  775. const c1 = new FocusableOverlay(["C1"]);
  776. const n1 = new FocusableOverlay(["N1"]);
  777. const c2 = new FocusableOverlay(["C2"]);
  778. const n2 = new FocusableOverlay(["N2"]);
  779. tui.addChild(new EmptyContent());
  780. tui.setFocus(editor);
  781. tui.start();
  782. try {
  783. const c1Handle = tui.showOverlay(c1);
  784. tui.showOverlay(n1, { nonCapturing: true });
  785. const c2Handle = tui.showOverlay(c2);
  786. tui.showOverlay(n2, { nonCapturing: true });
  787. assert.strictEqual(c2.focused, true);
  788. c2Handle.hide();
  789. await renderAndFlush(tui, terminal);
  790. assert.strictEqual(c1.focused, true);
  791. c1Handle.hide();
  792. await renderAndFlush(tui, terminal);
  793. assert.strictEqual(editor.focused, true);
  794. } finally {
  795. tui.stop();
  796. }
  797. });
  798. it("capturing overlay unfocus() on topmost capturing overlay falls back to preFocus", async () => {
  799. const terminal = new VirtualTerminal(80, 24);
  800. const tui = new TUI(terminal);
  801. const editor = new FocusableOverlay(["EDITOR"]);
  802. const capturing = new FocusableOverlay(["CAP"]);
  803. tui.addChild(new EmptyContent());
  804. tui.setFocus(editor);
  805. tui.start();
  806. try {
  807. const handle = tui.showOverlay(capturing);
  808. assert.strictEqual(capturing.focused, true);
  809. handle.unfocus();
  810. await renderAndFlush(tui, terminal);
  811. assert.strictEqual(editor.focused, true);
  812. assert.strictEqual(capturing.focused, false);
  813. } finally {
  814. tui.stop();
  815. }
  816. });
  817. });
  818. describe("no-op guards", () => {
  819. it("focus() on hidden overlay is a no-op", async () => {
  820. const terminal = new VirtualTerminal(80, 24);
  821. const tui = new TUI(terminal);
  822. const editor = new FocusableOverlay(["EDITOR"]);
  823. const overlay = new FocusableOverlay(["OVERLAY"]);
  824. tui.addChild(new EmptyContent());
  825. tui.setFocus(editor);
  826. tui.start();
  827. try {
  828. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  829. handle.setHidden(true);
  830. handle.focus();
  831. await renderAndFlush(tui, terminal);
  832. assert.strictEqual(editor.focused, true);
  833. assert.strictEqual(handle.isFocused(), false);
  834. } finally {
  835. tui.stop();
  836. }
  837. });
  838. it("focus() after hide() is a no-op", async () => {
  839. const terminal = new VirtualTerminal(80, 24);
  840. const tui = new TUI(terminal);
  841. const editor = new FocusableOverlay(["EDITOR"]);
  842. const overlay = new FocusableOverlay(["OVERLAY"]);
  843. tui.addChild(new EmptyContent());
  844. tui.setFocus(editor);
  845. tui.start();
  846. try {
  847. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  848. handle.hide();
  849. handle.focus();
  850. await renderAndFlush(tui, terminal);
  851. assert.strictEqual(editor.focused, true);
  852. assert.strictEqual(handle.isFocused(), false);
  853. } finally {
  854. tui.stop();
  855. }
  856. });
  857. it("unfocus() when overlay does not have focus is a no-op", async () => {
  858. const terminal = new VirtualTerminal(80, 24);
  859. const tui = new TUI(terminal);
  860. const editor = new FocusableOverlay(["EDITOR"]);
  861. const overlay = new FocusableOverlay(["OVERLAY"]);
  862. tui.addChild(new EmptyContent());
  863. tui.setFocus(editor);
  864. tui.start();
  865. try {
  866. const handle = tui.showOverlay(overlay, { nonCapturing: true });
  867. handle.unfocus();
  868. await renderAndFlush(tui, terminal);
  869. assert.strictEqual(editor.focused, true);
  870. assert.strictEqual(overlay.focused, false);
  871. } finally {
  872. tui.stop();
  873. }
  874. });
  875. it("unfocus() with null preFocus clears focus and does not route input back to overlay", async () => {
  876. const terminal = new VirtualTerminal(80, 24);
  877. const tui = new TUI(terminal);
  878. const overlay = new FocusableOverlay(["OVERLAY"]);
  879. tui.addChild(new EmptyContent());
  880. tui.start();
  881. try {
  882. const handle = tui.showOverlay(overlay);
  883. assert.strictEqual(overlay.focused, true);
  884. handle.unfocus();
  885. assert.strictEqual(overlay.focused, false);
  886. terminal.sendInput("x");
  887. await renderAndFlush(tui, terminal);
  888. assert.deepStrictEqual(overlay.inputs, []);
  889. assert.strictEqual(handle.isFocused(), false);
  890. } finally {
  891. tui.stop();
  892. }
  893. });
  894. });
  895. describe("focus cycle prevention", () => {
  896. it("toggle focus between non-capturing overlays then unfocus returns to editor", async () => {
  897. const terminal = new VirtualTerminal(80, 24);
  898. const tui = new TUI(terminal);
  899. const editor = new FocusableOverlay(["EDITOR"]);
  900. const a = new FocusableOverlay(["A"]);
  901. const b = new FocusableOverlay(["B"]);
  902. tui.addChild(new EmptyContent());
  903. tui.setFocus(editor);
  904. tui.start();
  905. try {
  906. const aHandle = tui.showOverlay(a, { nonCapturing: true });
  907. const bHandle = tui.showOverlay(b, { nonCapturing: true });
  908. aHandle.focus();
  909. bHandle.focus();
  910. aHandle.focus();
  911. aHandle.unfocus();
  912. await renderAndFlush(tui, terminal);
  913. assert.strictEqual(editor.focused, true);
  914. assert.strictEqual(a.focused, false);
  915. assert.strictEqual(b.focused, false);
  916. } finally {
  917. tui.stop();
  918. }
  919. });
  920. it("explicit unfocus target supports cycling between three overlays and editor", async () => {
  921. const terminal = new VirtualTerminal(80, 24);
  922. const tui = new TUI(terminal);
  923. const editor = new FocusableOverlay(["EDITOR"]);
  924. const a = new FocusableOverlay(["A"]);
  925. const b = new FocusableOverlay(["B"]);
  926. const c = new FocusableOverlay(["C"]);
  927. tui.addChild(new EmptyContent());
  928. tui.setFocus(editor);
  929. tui.start();
  930. try {
  931. const aHandle = tui.showOverlay(a);
  932. const bHandle = tui.showOverlay(b);
  933. const cHandle = tui.showOverlay(c);
  934. aHandle.focus();
  935. terminal.sendInput("a");
  936. await renderAndFlush(tui, terminal);
  937. bHandle.focus();
  938. terminal.sendInput("b");
  939. await renderAndFlush(tui, terminal);
  940. cHandle.focus();
  941. terminal.sendInput("c");
  942. await renderAndFlush(tui, terminal);
  943. cHandle.unfocus({ target: editor });
  944. terminal.sendInput("e");
  945. await renderAndFlush(tui, terminal);
  946. aHandle.focus();
  947. terminal.sendInput("A");
  948. await renderAndFlush(tui, terminal);
  949. aHandle.unfocus({ target: editor });
  950. terminal.sendInput("E");
  951. await renderAndFlush(tui, terminal);
  952. assert.deepStrictEqual(a.inputs, ["a", "A"]);
  953. assert.deepStrictEqual(b.inputs, ["b"]);
  954. assert.deepStrictEqual(c.inputs, ["c"]);
  955. assert.deepStrictEqual(editor.inputs, ["e", "E"]);
  956. assert.strictEqual(editor.focused, true);
  957. } finally {
  958. tui.stop();
  959. }
  960. });
  961. it("explicit null unfocus target clears focus without restoring overlays", async () => {
  962. const terminal = new VirtualTerminal(80, 24);
  963. const tui = new TUI(terminal);
  964. const overlay = new FocusableOverlay(["OVERLAY"]);
  965. tui.addChild(new EmptyContent());
  966. tui.start();
  967. try {
  968. const handle = tui.showOverlay(overlay);
  969. handle.unfocus({ target: null });
  970. terminal.sendInput("x");
  971. await renderAndFlush(tui, terminal);
  972. assert.deepStrictEqual(overlay.inputs, []);
  973. assert.strictEqual(handle.isFocused(), false);
  974. } finally {
  975. tui.stop();
  976. }
  977. });
  978. it("hiding focused overlay falls back to next visual-frontmost overlay", async () => {
  979. const terminal = new VirtualTerminal(80, 24);
  980. const tui = new TUI(terminal);
  981. const editor = new FocusableOverlay(["EDITOR"]);
  982. const a = new FocusableOverlay(["A"]);
  983. const b = new FocusableOverlay(["B"]);
  984. const c = new FocusableOverlay(["C"]);
  985. tui.addChild(new EmptyContent());
  986. tui.setFocus(editor);
  987. tui.start();
  988. try {
  989. const aHandle = tui.showOverlay(a);
  990. const bHandle = tui.showOverlay(b);
  991. tui.showOverlay(c);
  992. aHandle.focus();
  993. bHandle.focus();
  994. bHandle.setHidden(true);
  995. terminal.sendInput("x");
  996. await renderAndFlush(tui, terminal);
  997. assert.deepStrictEqual(a.inputs, ["x"]);
  998. assert.deepStrictEqual(c.inputs, []);
  999. assert.strictEqual(a.focused, true);
  1000. } finally {
  1001. tui.stop();
  1002. }
  1003. });
  1004. });
  1005. describe("rendering order", () => {
  1006. it("focus() on already-focused overlay bumps visual order", async () => {
  1007. const terminal = new VirtualTerminal(20, 6);
  1008. const tui = new TUI(terminal);
  1009. const editor = new FocusableOverlay(["EDITOR"]);
  1010. tui.addChild(new EmptyContent());
  1011. tui.setFocus(editor);
  1012. tui.start();
  1013. try {
  1014. const aHandle = tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1015. tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1016. aHandle.focus();
  1017. tui.showOverlay(new StaticOverlay(["C"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1018. await renderAndFlush(tui, terminal);
  1019. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C");
  1020. aHandle.focus();
  1021. await renderAndFlush(tui, terminal);
  1022. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A");
  1023. assert.strictEqual(aHandle.isFocused(), true);
  1024. } finally {
  1025. tui.stop();
  1026. }
  1027. });
  1028. it("default rendering order for overlapping overlays follows creation order", async () => {
  1029. const terminal = new VirtualTerminal(20, 6);
  1030. const tui = new TUI(terminal);
  1031. tui.addChild(new EmptyContent());
  1032. tui.start();
  1033. try {
  1034. tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1035. tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1036. await renderAndFlush(tui, terminal);
  1037. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1038. } finally {
  1039. tui.stop();
  1040. }
  1041. });
  1042. it("focus() on lower overlay renders it on top", async () => {
  1043. const terminal = new VirtualTerminal(20, 6);
  1044. const tui = new TUI(terminal);
  1045. tui.addChild(new EmptyContent());
  1046. tui.start();
  1047. try {
  1048. const lower = tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1049. tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1050. await renderAndFlush(tui, terminal);
  1051. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1052. lower.focus();
  1053. await renderAndFlush(tui, terminal);
  1054. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A");
  1055. } finally {
  1056. tui.stop();
  1057. }
  1058. });
  1059. it("focusing middle overlay places it on top while preserving others relative order", async () => {
  1060. const terminal = new VirtualTerminal(20, 6);
  1061. const tui = new TUI(terminal);
  1062. tui.addChild(new EmptyContent());
  1063. tui.start();
  1064. try {
  1065. tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1066. const middle = tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1067. const top = tui.showOverlay(new StaticOverlay(["C"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1068. await renderAndFlush(tui, terminal);
  1069. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C");
  1070. middle.focus();
  1071. await renderAndFlush(tui, terminal);
  1072. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1073. middle.hide();
  1074. await renderAndFlush(tui, terminal);
  1075. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C");
  1076. top.hide();
  1077. await renderAndFlush(tui, terminal);
  1078. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A");
  1079. } finally {
  1080. tui.stop();
  1081. }
  1082. });
  1083. it("capturing overlay hidden and shown again renders on top after unhide", async () => {
  1084. const terminal = new VirtualTerminal(20, 6);
  1085. const tui = new TUI(terminal);
  1086. tui.addChild(new EmptyContent());
  1087. tui.start();
  1088. try {
  1089. tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1090. const capturing = tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1 });
  1091. await renderAndFlush(tui, terminal);
  1092. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1093. capturing.setHidden(true);
  1094. tui.showOverlay(new StaticOverlay(["C"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1095. await renderAndFlush(tui, terminal);
  1096. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C");
  1097. capturing.setHidden(false);
  1098. await renderAndFlush(tui, terminal);
  1099. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1100. } finally {
  1101. tui.stop();
  1102. }
  1103. });
  1104. it("unfocus() does not change visual order until another overlay is focused", async () => {
  1105. const terminal = new VirtualTerminal(20, 6);
  1106. const tui = new TUI(terminal);
  1107. const editor = new FocusableOverlay(["EDITOR"]);
  1108. tui.addChild(new EmptyContent());
  1109. tui.setFocus(editor);
  1110. tui.start();
  1111. try {
  1112. const a = tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1113. const b = tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true });
  1114. await renderAndFlush(tui, terminal);
  1115. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1116. a.focus();
  1117. await renderAndFlush(tui, terminal);
  1118. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A");
  1119. a.unfocus();
  1120. await renderAndFlush(tui, terminal);
  1121. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A");
  1122. b.focus();
  1123. await renderAndFlush(tui, terminal);
  1124. assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B");
  1125. } finally {
  1126. tui.stop();
  1127. }
  1128. });
  1129. });
  1130. });