Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/cli/src/lib/init/ui/ink-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
84 changes: 84 additions & 0 deletions packages/cli/test/lib/init/ui/ink-ui-teardown.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import("node:child_process")>();
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<typeof actual.spawn>;
}),
};
});

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<typeof InkUI>;
store: InstanceType<typeof WizardStore>;
} {
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");
});
});
Loading