| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- import { stripVTControlCharacters } from "node:util";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- ScrollView,
- TuiAltScreen,
- type Component,
- type TuiMouseEvent,
- visibleWidth,
- } from "../../src/tui/engine/public.js";
- import { TuiChatLayout } from "../../src/tui/shell/chat-layout.js";
- import { VirtualTerminal } from "../pi-084-upstream/virtual-terminal.js";
- const COLUMNS = 20;
- const ROWS = 10;
- const CONTENT_LINES = 60;
- const screens: TuiAltScreen[] = [];
- afterEach(() => {
- for (const tui of screens.splice(0)) tui.stop();
- });
- class ScrollableLines implements Component {
- private readonly lines: string[] = [];
- constructor(count: number) {
- for (let index = 0; index < count; index++) {
- this.lines.push(`line-${String(index).padStart(2, "0")}`);
- }
- }
- render(): string[] {
- return [...this.lines];
- }
- invalidate(): void {}
- }
- function viewportText(terminal: VirtualTerminal): string {
- return stripVTControlCharacters(terminal.getViewport().join("\n"));
- }
- function sgrPress(x: number, y: number): string {
- return `\x1b[<0;${x + 1};${y + 1}M`;
- }
- function sgrMotion(x: number, y: number): string {
- return `\x1b[<32;${x + 1};${y + 1}M`;
- }
- function sgrRelease(x: number, y: number): string {
- return `\x1b[<0;${x + 1};${y + 1}m`;
- }
- async function createScrollbarScreen(
- options: {
- columns?: number;
- gutter?: number;
- content?: Component;
- copySelection?: (text: string) => Promise<boolean>;
- } = {},
- ): Promise<{
- terminal: VirtualTerminal;
- tui: TuiAltScreen;
- scrollView: ScrollView;
- }> {
- const terminal = new VirtualTerminal(options.columns ?? COLUMNS, ROWS);
- const content = options.content ?? new ScrollableLines(CONTENT_LINES);
- const scrollView = new ScrollView(content, {
- primary: true,
- overscroll: "contain",
- scrollbar: "always",
- scrollbarGutter: options.gutter ?? 3,
- });
- const tui = new TuiAltScreen(terminal, false, undefined, {
- copySelection: options.copySelection,
- });
- screens.push(tui);
- tui.setLayoutRoot(scrollView);
- tui.start();
- await terminal.waitForRender();
- return { terminal, tui, scrollView };
- }
- describe("Fullscreen scrollbar interaction", () => {
- it("shows the top of the content before any interaction", async () => {
- const { terminal } = await createScrollbarScreen();
- expect(viewportText(terminal)).toContain("line-00");
- expect(viewportText(terminal)).not.toContain("line-59");
- });
- it("jumps the thumb to a track press and keeps dragging from there", async () => {
- const { terminal } = await createScrollbarScreen();
- // Press the empty track at the bottom of the right-edge scrollbar.
- terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- expect(viewportText(terminal)).toContain("line-59");
- expect(viewportText(terminal)).not.toContain("line-00");
- terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- });
- it("grabs near-miss clicks within the widened hit span", async () => {
- const { terminal } = await createScrollbarScreen();
- // Press two columns left of the painted one-column bar.
- terminal.sendInput(sgrPress(COLUMNS - 3, ROWS - 1));
- await terminal.waitForRender();
- expect(viewportText(terminal)).toContain("line-59");
- expect(viewportText(terminal)).not.toContain("line-00");
- terminal.sendInput(sgrRelease(COLUMNS - 3, ROWS - 1));
- await terminal.waitForRender();
- });
- it("keeps an on-thumb press in place and drags proportionally", async () => {
- const { terminal } = await createScrollbarScreen();
- // Jump to the end first via a track press at the bottom, then release.
- terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- // The thumb now spans the bottom rows; press it directly.
- terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 2));
- await terminal.waitForRender();
- // An on-thumb press must not move content on its own.
- expect(viewportText(terminal)).toContain("line-59");
- // Drag the thumb up the track.
- terminal.sendInput(sgrMotion(COLUMNS - 1, Math.floor(ROWS / 2)));
- await terminal.waitForRender();
- const text = viewportText(terminal);
- expect(text).not.toContain("line-59");
- expect(text).not.toContain("line-00");
- terminal.sendInput(sgrRelease(COLUMNS - 1, Math.floor(ROWS / 2)));
- await terminal.waitForRender();
- });
- it("ignores presses outside the hit span", async () => {
- const { terminal } = await createScrollbarScreen();
- // Jump to the end via a track press, then release.
- terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- // Press three columns left of the bar, outside the hit span.
- terminal.sendInput(sgrPress(COLUMNS - 4, 0));
- await terminal.waitForRender();
- terminal.sendInput(sgrRelease(COLUMNS - 4, 0));
- await terminal.waitForRender();
- expect(viewportText(terminal)).toContain("line-59");
- });
- });
- describe("Scrollbar interaction boundaries", () => {
- it.each([1, 3])(
- "routes the last content columns with a %i-column gutter to content",
- async (gutter) => {
- const handleMouse = vi.fn((_event: TuiMouseEvent) => true);
- const content = {
- render: (width: number) =>
- Array.from({ length: CONTENT_LINES }, () => "x".repeat(width)),
- invalidate() {},
- handleMouse,
- };
- const { terminal, scrollView } = await createScrollbarScreen({
- gutter,
- content,
- });
- for (const x of [COLUMNS - gutter - 2, COLUMNS - gutter - 1]) {
- terminal.sendInput(sgrPress(x, ROWS - 1));
- terminal.sendInput(sgrRelease(x, ROWS - 1));
- await terminal.waitForRender();
- expect(handleMouse).toHaveBeenCalledWith(
- expect.objectContaining({ action: "press", x }),
- );
- expect(scrollView.scrollTop).toBe(0);
- }
- },
- );
- it.each([1, 3])(
- "keeps text selection next to a %i-column gutter",
- async (gutter) => {
- const copySelection = vi.fn(async (_text: string) => true);
- const content = {
- render: (width: number) =>
- Array.from(
- { length: CONTENT_LINES },
- () => "x".repeat(width - 2) + "YZ",
- ),
- invalidate() {},
- };
- const { terminal, scrollView } = await createScrollbarScreen({
- gutter,
- content,
- copySelection,
- });
- terminal.sendInput(sgrPress(COLUMNS - gutter - 2, 4));
- terminal.sendInput(sgrMotion(COLUMNS - gutter - 1, 4));
- terminal.sendInput(sgrRelease(COLUMNS - gutter - 1, 4));
- await terminal.waitForRender();
- expect(copySelection).toHaveBeenCalledWith("YZ");
- expect(scrollView.scrollTop).toBe(0);
- },
- );
- it.each([1, 2, 3, 4, 8])(
- "preserves content and bounded mouse targets at width %i",
- async (columns) => {
- const widths: number[] = [];
- const handleMouse = vi.fn(() => true);
- const content = {
- render: (width: number) => {
- widths.push(width);
- return Array.from({ length: CONTENT_LINES }, () => "x".repeat(width));
- },
- invalidate() {},
- handleMouse,
- };
- const { terminal, scrollView } = await createScrollbarScreen({
- columns,
- content,
- });
- expect(widths.every((width) => width === Math.max(1, columns - 3))).toBe(
- true,
- );
- expect(
- terminal.getViewport().every((line) => visibleWidth(line) <= columns),
- ).toBe(true);
- terminal.sendInput(sgrPress(0, ROWS - 1));
- terminal.sendInput(sgrRelease(0, ROWS - 1));
- await terminal.waitForRender();
- expect(handleMouse).toHaveBeenCalled();
- expect(scrollView.scrollTop).toBe(0);
- if (columns > 1) {
- terminal.sendInput(sgrPress(columns - 1, ROWS - 1));
- await terminal.waitForRender();
- expect(scrollView.scrollTop).toBe(CONTENT_LINES - ROWS);
- }
- },
- );
- it("does not capture a track press while an overlay is visible", async () => {
- const { terminal, tui, scrollView } = await createScrollbarScreen();
- tui.showOverlay(new ScrollableLines(2), { width: 10 });
- await terminal.waitForRender();
- terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
- terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- expect(scrollView.scrollTop).toBe(0);
- });
- it("cancels a drag when an overlay opens and does not resume it after dismissal", async () => {
- const { terminal, tui, scrollView } = await createScrollbarScreen();
- terminal.sendInput(sgrPress(COLUMNS - 1, 0));
- const overlay = tui.showOverlay(new ScrollableLines(2), { width: 10 });
- await terminal.waitForRender();
- terminal.sendInput(sgrMotion(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- expect(scrollView.scrollTop).toBe(0);
- overlay.hide();
- await terminal.waitForRender();
- terminal.sendInput(sgrMotion(COLUMNS - 1, ROWS - 1));
- terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- expect(scrollView.scrollTop).toBe(0);
- });
- it("routes wheel events normally and ends dragging on release", async () => {
- const { terminal, scrollView } = await createScrollbarScreen();
- terminal.sendInput(`\x1b[<65;${COLUMNS};5M`);
- await terminal.waitForRender();
- expect(scrollView.scrollTop).toBeGreaterThan(0);
- terminal.sendInput(sgrPress(COLUMNS - 1, ROWS - 1));
- terminal.sendInput(sgrRelease(COLUMNS - 1, ROWS - 1));
- await terminal.waitForRender();
- terminal.sendInput(sgrMotion(COLUMNS - 1, 0));
- await terminal.waitForRender();
- expect(scrollView.scrollTop).toBe(CONTENT_LINES - ROWS);
- });
- it("enables a reserved gutter in the actual fullscreen chat layout", async () => {
- const terminal = new VirtualTerminal(40, 15);
- const empty = { render: () => [], invalidate() {} };
- const layout = new TuiChatLayout(terminal, {
- surface: () => "conversation",
- transcript: new ScrollableLines(CONTENT_LINES),
- welcome: empty,
- interaction: { ...empty, isActive: () => false },
- activity: empty,
- followUp: empty,
- composer: { render: () => ["composer"], invalidate() {} },
- status: empty,
- });
- const tui = new TuiAltScreen(terminal);
- screens.push(tui);
- tui.setLayoutRoot(layout.fullscreenLayoutRoot);
- tui.start();
- await terminal.waitForRender();
- expect(viewportText(terminal)).toContain("line-59");
- terminal.sendInput(sgrPress(37, 0));
- terminal.sendInput(sgrRelease(37, 0));
- await terminal.waitForRender();
- expect(viewportText(terminal)).toContain("line-00");
- expect(viewportText(terminal)).not.toContain("line-59");
- expect(viewportText(terminal)).toContain("composer");
- });
- });
|