From 12002f63722182d60b5c95e1ffdcf8b985f22423 Mon Sep 17 00:00:00 2001 From: betegon Date: Fri, 21 Aug 2026 09:19:24 +0200 Subject: [PATCH] fix(init): start the plugin-installer handoff at the top of the screen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exiting the alternate screen buffer restores the cursor to the row where `sentry init` was invoked — usually low on the screen. The exit summary and the interactive `npx @sentry/ai install` UI (spawned with inherited stdio) then rendered from mid-screen, leaving a blank gap above them. When a post-exit action is queued (the agent-plugin installer), clear the restored screen and home the cursor right after leaving the alt buffer — the same clean top-of-screen start the wizard uses on entry. The normal exit (no installer) is untouched, so its compact summary still flows into the restored scrollback at the invocation point. Co-Authored-By: Claude Opus 4.8 --- packages/cli/src/lib/init/ui/ink-ui.ts | 15 +++- .../test/lib/init/ui/ink-ui-teardown.test.ts | 84 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 packages/cli/test/lib/init/ui/ink-ui-teardown.test.ts diff --git a/packages/cli/src/lib/init/ui/ink-ui.ts b/packages/cli/src/lib/init/ui/ink-ui.ts index d733916b2..17eb67e92 100644 --- a/packages/cli/src/lib/init/ui/ink-ui.ts +++ b/packages/cli/src/lib/init/ui/ink-ui.ts @@ -939,8 +939,21 @@ export class InkUI implements WizardUI { } // Leave the alternate screen buffer so the user's original // scrollback is restored. + // + // When the user queued an interactive post-exit action (the agent-plugin + // installer), also clear the restored screen and home the cursor. Exiting + // the alt buffer returns the cursor to the row where `sentry init` was + // invoked — usually low on the screen — so without this the exit summary + // and the installer's own full-screen UI would render from mid-screen with + // a blank gap above. Clearing gives the handoff the same clean top-of-screen + // start as wizard startup (line ~362). The normal exit (no installer) is + // left untouched so its compact summary flows into scrollback as before. + const hasPostExitActions = + this.store.getSnapshot().postExitActions.length > 0; try { - process.stdout.write("\x1b[?1049l"); + process.stdout.write( + hasPostExitActions ? "\x1b[?1049l\x1b[2J\x1b[H" : "\x1b[?1049l" + ); } catch { // best-effort — stdout may already be destroyed } diff --git a/packages/cli/test/lib/init/ui/ink-ui-teardown.test.ts b/packages/cli/test/lib/init/ui/ink-ui-teardown.test.ts new file mode 100644 index 000000000..94df2e436 --- /dev/null +++ b/packages/cli/test/lib/init/ui/ink-ui-teardown.test.ts @@ -0,0 +1,84 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; + +// Stub the post-exit installer spawn so teardown never launches a real process. +// `runInheritedCommand` only listens for "close"/"error", so a fake child that +// resolves "close" on the next microtask is enough to let disposal complete. +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + spawn: vi.fn(() => { + const child = { + on(event: string, cb: () => void) { + if (event === "close") { + queueMicrotask(cb); + } + return child; + }, + }; + return child as unknown as ReturnType; + }), + }; +}); + +const { InkUI } = await import("../../../../src/lib/init/ui/ink-ui.js"); +const { WizardStore } = await import( + "../../../../src/lib/init/ui/wizard-store.js" +); + +function createUi(): { + ui: InstanceType; + store: InstanceType; +} { + const store = new WizardStore(); + const instance = { + clear: vi.fn(), + rerender: vi.fn(), + unmount: vi.fn(), + waitUntilExit: vi.fn().mockResolvedValue(undefined), + }; + return { ui: new InkUI(instance, store, null), store }; +} + +function captureStdout(): string[] { + const writes: string[] = []; + vi.spyOn(process.stdout, "write").mockImplementation((chunk: unknown) => { + writes.push(String(chunk)); + return true; + }); + return writes; +} + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe("InkUI teardown — installer handoff", () => { + test("clears the screen and homes the cursor when a post-exit action is queued", async () => { + const { ui, store } = createUi(); + // The completion screen queues this when the user opts into the plugin. + store.queuePostExitAction("npx @sentry/ai install"); + const writes = captureStdout(); + + await ui[Symbol.asyncDispose](); + + // Exiting the alt screen returns the cursor to where `sentry init` was + // invoked (usually low on the screen). Clearing + homing after the exit + // makes the installer's full-screen UI start at the top instead of + // mid-screen with a blank gap above it. + expect(writes.join("")).toContain("\x1b[?1049l\x1b[2J\x1b[H"); + }); + + test("only restores the alt screen on a normal exit (no post-exit action)", async () => { + const { ui } = createUi(); + const writes = captureStdout(); + + await ui[Symbol.asyncDispose](); + + const joined = writes.join(""); + expect(joined).toContain("\x1b[?1049l"); + // No clear/home — the compact exit summary flows into the restored + // scrollback at the invocation point, as before. + expect(joined).not.toContain("\x1b[?1049l\x1b[2J\x1b[H"); + }); +});