tui-scrollbar-interaction.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { stripVTControlCharacters } from "node:util";
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import {
  4. ScrollView,
  5. TuiAltScreen,
  6. type Component,
  7. type TuiMouseEvent,
  8. visibleWidth,
  9. } from "../../src/tui/engine/public.js";
  10. import { TuiChatLayout } from "../../src/tui/shell/chat-layout.js";
  11. import { VirtualTerminal } from "../pi-084-upstream/virtual-terminal.js";
  12. const COLUMNS = 20;
  13. const ROWS = 10;
  14. const CONTENT_LINES = 60;
  15. const screens: TuiAltScreen[] = [];
  16. afterEach(() => {
  17. for (const tui of screens.splice(0)) tui.stop();
  18. });
  19. class ScrollableLines implements Component {
  20. private readonly lines: string[] = [];
  21. constructor(count: number) {
  22. for (let index = 0; index < count; index++) {
  23. this.lines.push(`line-${String(index).padStart(2, "0")}`);
  24. }
  25. }
  26. render(): string[] {
  27. return [...this.lines];
  28. }
  29. invalidate(): void {}
  30. }
  31. function viewportText(terminal: VirtualTerminal): string {
  32. return stripVTControlCharacters(terminal.getViewport().join("\n"));
  33. }
  34. function sgrPress(x: number, y: number): string {
  35. return `\x1b[<0;${x + 1};${y + 1}M`;
  36. }
  37. function sgrMotion(x: number, y: number): string {
  38. return `\x1b[<32;${x + 1};${y + 1}M`;
  39. }
  40. function sgrRelease(x: number, y: number): string {
  41. return `\x1b[<0;${x + 1};${y + 1}m`;
  42. }
  43. async function createScrollbarScreen(
  44. options: {
  45. columns?: number;
  46. gutter?: number;
  47. content?: Component;
  48. copySelection?: (text: string) => Promise<boolean>;
  49. } = {},
  50. ): Promise<{
  51. terminal: VirtualTerminal;
  52. tui: TuiAltScreen;
  53. scrollView: ScrollView;
  54. }> {
  55. const terminal = new VirtualTerminal(options.columns ?? COLUMNS, ROWS);
  56. const content = options.content ?? new ScrollableLines(CONTENT_LINES);
  57. const scrollView = new ScrollView(content, {
  58. primary: true,
  59. overscroll: "contain",
  60. scrollbar: "always",
  61. scrollbarGutter: options.gutter ?? 3,
  62. });
  63. const tui = new TuiAltScreen(terminal, false, undefined, {
  64. copySelection: options.copySelection,
  65. });
  66. screens.push(tui);
  67. tui.setLayoutRoot(scrollView);
  68. tui.start();
  69. await terminal.waitForRender();
  70. return { terminal, tui, scrollView };
  71. }
  72. describe("Fullscreen scrollbar interaction", () => {
  73. it("shows the top of the content before any interaction", async () => {
  74. const { terminal } = await createScrollbarScreen();
  75. expect(viewportText(terminal)).toContain("line-00");
  76. expect(viewportText(terminal)).not.toContain("line-59");
  77. });
  78. it("jumps the thumb to a track press and keeps dragging from there", async () => {
  79. const { terminal } = await createScrollbarScreen();
  80. // Press the empty track at the bottom of the right-edge scrollbar.
  81. terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
  82. await terminal.waitForRender();
  83. expect(viewportText(terminal)).toContain("line-59");
  84. expect(viewportText(terminal)).not.toContain("line-00");
  85. terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
  86. await terminal.waitForRender();
  87. });
  88. it("grabs near-miss clicks within the widened hit span", async () => {
  89. const { terminal } = await createScrollbarScreen();
  90. // Press two columns left of the painted one-column bar.
  91. terminal.sendInput(sgrPress(COLUMNS - 3, ROWS - 1));
  92. await terminal.waitForRender();
  93. expect(viewportText(terminal)).toContain("line-59");
  94. expect(viewportText(terminal)).not.toContain("line-00");
  95. terminal.sendInput(sgrRelease(COLUMNS - 3, ROWS - 1));
  96. await terminal.waitForRender();
  97. });
  98. it("keeps an on-thumb press in place and drags proportionally", async () => {
  99. const { terminal } = await createScrollbarScreen();
  100. // Jump to the end first via a track press at the bottom, then release.
  101. terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
  102. await terminal.waitForRender();
  103. terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
  104. await terminal.waitForRender();
  105. // The thumb now spans the bottom rows; press it directly.
  106. terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 2));
  107. await terminal.waitForRender();
  108. // An on-thumb press must not move content on its own.
  109. expect(viewportText(terminal)).toContain("line-59");
  110. // Drag the thumb up the track.
  111. terminal.sendInput(sgrMotion(COLUMNS - 1, Math.floor(ROWS / 2)));
  112. await terminal.waitForRender();
  113. const text = viewportText(terminal);
  114. expect(text).not.toContain("line-59");
  115. expect(text).not.toContain("line-00");
  116. terminal.sendInput(sgrRelease(COLUMNS - 1, Math.floor(ROWS / 2)));
  117. await terminal.waitForRender();
  118. });
  119. it("ignores presses outside the hit span", async () => {
  120. const { terminal } = await createScrollbarScreen();
  121. // Jump to the end via a track press, then release.
  122. terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
  123. await terminal.waitForRender();
  124. terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
  125. await terminal.waitForRender();
  126. // Press three columns left of the bar, outside the hit span.
  127. terminal.sendInput(sgrPress(COLUMNS - 4, 0));
  128. await terminal.waitForRender();
  129. terminal.sendInput(sgrRelease(COLUMNS - 4, 0));
  130. await terminal.waitForRender();
  131. expect(viewportText(terminal)).toContain("line-59");
  132. });
  133. });
  134. describe("Scrollbar interaction boundaries", () => {
  135. it.each([1, 3])(
  136. "routes the last content columns with a %i-column gutter to content",
  137. async (gutter) => {
  138. const handleMouse = vi.fn((_event: TuiMouseEvent) => true);
  139. const content = {
  140. render: (width: number) =>
  141. Array.from({ length: CONTENT_LINES }, () => "x".repeat(width)),
  142. invalidate() {},
  143. handleMouse,
  144. };
  145. const { terminal, scrollView } = await createScrollbarScreen({
  146. gutter,
  147. content,
  148. });
  149. for (const x of [COLUMNS - gutter - 2, COLUMNS - gutter - 1]) {
  150. terminal.sendInput(sgrPress(x, ROWS - 1));
  151. terminal.sendInput(sgrRelease(x, ROWS - 1));
  152. await terminal.waitForRender();
  153. expect(handleMouse).toHaveBeenCalledWith(
  154. expect.objectContaining({ action: "press", x }),
  155. );
  156. expect(scrollView.scrollTop).toBe(0);
  157. }
  158. },
  159. );
  160. it.each([1, 3])(
  161. "keeps text selection next to a %i-column gutter",
  162. async (gutter) => {
  163. const copySelection = vi.fn(async (_text: string) => true);
  164. const content = {
  165. render: (width: number) =>
  166. Array.from(
  167. { length: CONTENT_LINES },
  168. () => "x".repeat(width - 2) + "YZ",
  169. ),
  170. invalidate() {},
  171. };
  172. const { terminal, scrollView } = await createScrollbarScreen({
  173. gutter,
  174. content,
  175. copySelection,
  176. });
  177. terminal.sendInput(sgrPress(COLUMNS - gutter - 2, 4));
  178. terminal.sendInput(sgrMotion(COLUMNS - gutter - 1, 4));
  179. terminal.sendInput(sgrRelease(COLUMNS - gutter - 1, 4));
  180. await terminal.waitForRender();
  181. expect(copySelection).toHaveBeenCalledWith("YZ");
  182. expect(scrollView.scrollTop).toBe(0);
  183. },
  184. );
  185. it.each([1, 2, 3, 4, 8])(
  186. "preserves content and bounded mouse targets at width %i",
  187. async (columns) => {
  188. const widths: number[] = [];
  189. const handleMouse = vi.fn(() => true);
  190. const content = {
  191. render: (width: number) => {
  192. widths.push(width);
  193. return Array.from({ length: CONTENT_LINES }, () => "x".repeat(width));
  194. },
  195. invalidate() {},
  196. handleMouse,
  197. };
  198. const { terminal, scrollView } = await createScrollbarScreen({
  199. columns,
  200. content,
  201. });
  202. expect(widths.every((width) => width === Math.max(1, columns - 3))).toBe(
  203. true,
  204. );
  205. expect(
  206. terminal.getViewport().every((line) => visibleWidth(line) <= columns),
  207. ).toBe(true);
  208. terminal.sendInput(sgrPress(0, ROWS - 1));
  209. terminal.sendInput(sgrRelease(0, ROWS - 1));
  210. await terminal.waitForRender();
  211. expect(handleMouse).toHaveBeenCalled();
  212. expect(scrollView.scrollTop).toBe(0);
  213. if (columns > 1) {
  214. terminal.sendInput(sgrPress(columns - 1, ROWS - 1));
  215. await terminal.waitForRender();
  216. expect(scrollView.scrollTop).toBe(CONTENT_LINES - ROWS);
  217. }
  218. },
  219. );
  220. it("does not capture a track press while an overlay is visible", async () => {
  221. const { terminal, tui, scrollView } = await createScrollbarScreen();
  222. tui.showOverlay(new ScrollableLines(2), { width: 10 });
  223. await terminal.waitForRender();
  224. terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
  225. terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
  226. await terminal.waitForRender();
  227. expect(scrollView.scrollTop).toBe(0);
  228. });
  229. it("cancels a drag when an overlay opens and does not resume it after dismissal", async () => {
  230. const { terminal, tui, scrollView } = await createScrollbarScreen();
  231. terminal.sendInput(sgrPress(COLUMNS - 1, 0));
  232. const overlay = tui.showOverlay(new ScrollableLines(2), { width: 10 });
  233. await terminal.waitForRender();
  234. terminal.sendInput(sgrMotion(COLUMNS - 1, ROWS - 1));
  235. await terminal.waitForRender();
  236. expect(scrollView.scrollTop).toBe(0);
  237. overlay.hide();
  238. await terminal.waitForRender();
  239. terminal.sendInput(sgrMotion(COLUMNS - 1, ROWS - 1));
  240. terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
  241. await terminal.waitForRender();
  242. expect(scrollView.scrollTop).toBe(0);
  243. });
  244. it("routes wheel events normally and ends dragging on release", async () => {
  245. const { terminal, scrollView } = await createScrollbarScreen();
  246. terminal.sendInput(`\x1b[<65;${COLUMNS};5M`);
  247. await terminal.waitForRender();
  248. expect(scrollView.scrollTop).toBeGreaterThan(0);
  249. terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
  250. terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
  251. await terminal.waitForRender();
  252. terminal.sendInput(sgrMotion(COLUMNS - 1, 0));
  253. await terminal.waitForRender();
  254. expect(scrollView.scrollTop).toBe(CONTENT_LINES - ROWS);
  255. });
  256. it("enables a reserved gutter in the actual fullscreen chat layout", async () => {
  257. const terminal = new VirtualTerminal(40, 15);
  258. const empty = { render: () => [], invalidate() {} };
  259. const layout = new TuiChatLayout(terminal, {
  260. surface: () => "conversation",
  261. transcript: new ScrollableLines(CONTENT_LINES),
  262. welcome: empty,
  263. interaction: { ...empty, isActive: () => false },
  264. activity: empty,
  265. followUp: empty,
  266. composer: { render: () => ["composer"], invalidate() {} },
  267. status: empty,
  268. });
  269. const tui = new TuiAltScreen(terminal);
  270. screens.push(tui);
  271. tui.setLayoutRoot(layout.fullscreenLayoutRoot);
  272. tui.start();
  273. await terminal.waitForRender();
  274. expect(viewportText(terminal)).toContain("line-59");
  275. terminal.sendInput(sgrPress(37, 0));
  276. terminal.sendInput(sgrRelease(37, 0));
  277. await terminal.waitForRender();
  278. expect(viewportText(terminal)).toContain("line-00");
  279. expect(viewportText(terminal)).not.toContain("line-59");
  280. expect(viewportText(terminal)).toContain("composer");
  281. });
  282. });