|
| 1 | +/** |
| 2 | + * CL-5750: the approval surface must never push the prompt box off screen, |
| 3 | + * and its choices must always be visible/reachable — an unanswerable |
| 4 | + * approval deadlocks the session, so the choices win the row budget over |
| 5 | + * the prompt box's growth and over the overlay's own context text. |
| 6 | + */ |
| 7 | +import { describe, expect, test } from "bun:test"; |
| 8 | +import { withTestRenderer } from "./harness.js"; |
| 9 | +import { createAppShell, appendStreamRow, type AppShell } from "./shell.js"; |
| 10 | +import { openPermissionsOverlay, makePermissionItems } from "./overlays.js"; |
| 11 | + |
| 12 | +const WIDTH = 80; |
| 13 | +// Deliberately spans from far below the documented 24-row baseline down to |
| 14 | +// 10 rows, the shortest terminal this fix guarantees, plus a comfortable one. |
| 15 | +const HEIGHTS = [10, 12, 15, 24, 40] as const; |
| 16 | + |
| 17 | +const APPROVAL_BODY = [ |
| 18 | + "run_shell", |
| 19 | + "Run shell command", |
| 20 | + "This is context describing what the tool is about to do to the workspace.", |
| 21 | +].join("\n"); |
| 22 | + |
| 23 | +function primeSession(shell: AppShell): void { |
| 24 | + // In-flow layout: the overlay host competes with the prompt for rows the |
| 25 | + // same way a live approval does mid-session (not the landing screen). |
| 26 | + appendStreamRow(shell, { role: "assistant", text: "session underway" }); |
| 27 | +} |
| 28 | + |
| 29 | +interface ApprovalSnapshot { |
| 30 | + readonly frame: string; |
| 31 | + readonly promptHeight: number; |
| 32 | + readonly promptVisible: boolean; |
| 33 | + readonly listHeight: number | null; |
| 34 | + readonly hasOverlayList: boolean; |
| 35 | +} |
| 36 | + |
| 37 | +async function paintApproval(height: number, itemCount = 6): Promise<ApprovalSnapshot> { |
| 38 | + return withTestRenderer( |
| 39 | + async (h) => { |
| 40 | + const shell = createAppShell(h.renderer, { |
| 41 | + terminal: { columns: WIDTH, rows: height }, |
| 42 | + run: "idle", |
| 43 | + }); |
| 44 | + try { |
| 45 | + primeSession(shell); |
| 46 | + openPermissionsOverlay(shell, { |
| 47 | + items: makePermissionItems(itemCount), |
| 48 | + body: APPROVAL_BODY, |
| 49 | + }); |
| 50 | + await h.renderOnce(); |
| 51 | + await h.renderOnce(); |
| 52 | + const frame = h.captureCharFrame().replace(/\n$/, ""); |
| 53 | + return { |
| 54 | + frame, |
| 55 | + promptHeight: shell.layout.heights.prompt, |
| 56 | + promptVisible: shell.promptBox.visible, |
| 57 | + listHeight: shell.overlayList?.height ?? null, |
| 58 | + hasOverlayList: shell.overlayList !== null, |
| 59 | + }; |
| 60 | + } finally { |
| 61 | + shell.dispose(); |
| 62 | + } |
| 63 | + }, |
| 64 | + { width: WIDTH, height }, |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +describe("approval overlay keeps the prompt box on screen (CL-5750)", () => { |
| 69 | + for (const height of HEIGHTS) { |
| 70 | + test(`prompt box stays visible and unclipped at ${height} rows`, async () => { |
| 71 | + const snap = await paintApproval(height); |
| 72 | + |
| 73 | + // The prompt box is always assigned rows, never displaced entirely. |
| 74 | + expect(snap.promptHeight).toBeGreaterThan(0); |
| 75 | + expect(snap.promptVisible).toBe(true); |
| 76 | + |
| 77 | + // The painted frame actually shows the prompt box's border, not just |
| 78 | + // internal state — the bug was a visual displacement, not a state one. |
| 79 | + const lines = snap.frame.split("\n"); |
| 80 | + expect(lines.length).toBeLessThanOrEqual(height); |
| 81 | + const promptTopBorder = lines.findIndex((l) => l.includes("╭")); |
| 82 | + const promptBottomBorder = lines.findIndex((l) => l.includes("╰")); |
| 83 | + expect(promptTopBorder).toBeGreaterThanOrEqual(0); |
| 84 | + expect(promptBottomBorder).toBeGreaterThan(promptTopBorder); |
| 85 | + // The prompt box's own bottom rule (with the cwd/branding) must be |
| 86 | + // fully painted within the frame, not cut off past the last row. |
| 87 | + expect(promptBottomBorder).toBeLessThan(lines.length); |
| 88 | + }); |
| 89 | + |
| 90 | + test(`at least one approval choice is painted and reachable at ${height} rows`, async () => { |
| 91 | + const snap = await paintApproval(height); |
| 92 | + |
| 93 | + expect(snap.hasOverlayList).toBe(true); |
| 94 | + // The active choice is always inside the viewport window state... |
| 95 | + expect(snap.listHeight).toBeGreaterThanOrEqual(1); |
| 96 | + |
| 97 | + // ...and it is actually painted on screen, not just tracked in state: |
| 98 | + // the marked active choice's label must appear in the frame. |
| 99 | + expect(snap.frame).toContain("Allow once"); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + test("overlay host never displaces the prompt box out of the frame even with a tall body", async () => { |
| 104 | + const tallBody = Array.from({ length: 20 }, (_, i) => `context line ${i}`).join("\n"); |
| 105 | + await withTestRenderer( |
| 106 | + async (h) => { |
| 107 | + const shell = createAppShell(h.renderer, { |
| 108 | + terminal: { columns: WIDTH, rows: 12 }, |
| 109 | + run: "idle", |
| 110 | + }); |
| 111 | + try { |
| 112 | + primeSession(shell); |
| 113 | + openPermissionsOverlay(shell, { items: makePermissionItems(10), body: tallBody }); |
| 114 | + await h.renderOnce(); |
| 115 | + await h.renderOnce(); |
| 116 | + const frame = h.captureCharFrame().replace(/\n$/, ""); |
| 117 | + const lines = frame.split("\n"); |
| 118 | + expect(lines.length).toBeLessThanOrEqual(12); |
| 119 | + expect(shell.layout.heights.prompt).toBeGreaterThan(0); |
| 120 | + expect(lines.some((l) => l.includes("╭"))).toBe(true); |
| 121 | + expect(lines.some((l) => l.includes("╰"))).toBe(true); |
| 122 | + expect(shell.overlayList).not.toBeNull(); |
| 123 | + expect(shell.overlayList!.height).toBeGreaterThanOrEqual(1); |
| 124 | + } finally { |
| 125 | + shell.dispose(); |
| 126 | + } |
| 127 | + }, |
| 128 | + { width: WIDTH, height: 12 }, |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + test("no dead space between the transcript and the overlay: overlay sits directly above the prompt", async () => { |
| 133 | + await withTestRenderer( |
| 134 | + async (h) => { |
| 135 | + const shell = createAppShell(h.renderer, { |
| 136 | + terminal: { columns: WIDTH, rows: 24 }, |
| 137 | + run: "idle", |
| 138 | + }); |
| 139 | + try { |
| 140 | + primeSession(shell); |
| 141 | + openPermissionsOverlay(shell, { items: makePermissionItems(6), body: APPROVAL_BODY }); |
| 142 | + await h.renderOnce(); |
| 143 | + await h.renderOnce(); |
| 144 | + const heights = shell.layout.heights; |
| 145 | + // Paint order stacks transcript, then overlay_host, then the other |
| 146 | + // zones, then prompt — with nothing charged a height between the |
| 147 | + // overlay and the prompt box, the overlay's bottom edge abuts the |
| 148 | + // zones immediately above the prompt rather than leaving a gap. |
| 149 | + // These zones are all off in this idle, no-task/no-agents scenario, |
| 150 | + // so nothing should separate the overlay from the prompt. |
| 151 | + const between = |
| 152 | + heights.agents + |
| 153 | + heights.task + |
| 154 | + heights.plugin_banner + |
| 155 | + heights.command_banner + |
| 156 | + heights.settings_notice; |
| 157 | + expect(between).toBe(0); |
| 158 | + } finally { |
| 159 | + shell.dispose(); |
| 160 | + } |
| 161 | + }, |
| 162 | + { width: WIDTH, height: 24 }, |
| 163 | + ); |
| 164 | + }); |
| 165 | +}); |
0 commit comments