|
| 1 | +/** |
| 2 | + * CL-6720: an overlay with nothing to choose reserves zero list rows and |
| 3 | + * paints an explicit empty state inside the body chrome — including on a |
| 4 | + * short terminal, which must not reserve a phantom choice row. |
| 5 | + */ |
| 6 | +import { describe, expect, test } from "bun:test"; |
| 7 | + |
| 8 | +import { withTestRenderer } from "./harness.js"; |
| 9 | +import { OVERLAY_EMPTY_STATE, overlayChromeRows } from "./overlay-view.js"; |
| 10 | +import { appendStreamRow } from "./shell/chrome.js"; |
| 11 | +import { createAppShell } from "./shell/index.js"; |
| 12 | +import type { AppShell } from "./shell/internals.js"; |
| 13 | +import { |
| 14 | + closeInsetOverlay, |
| 15 | + openListOverlay, |
| 16 | + setOverlayBody, |
| 17 | + setOwnedOverlayItems, |
| 18 | +} from "./shell/overlay-host.js"; |
| 19 | +import { createOverlayList } from "./shell/overlay-list.js"; |
| 20 | + |
| 21 | +interface Size { |
| 22 | + readonly width: number; |
| 23 | + readonly height: number; |
| 24 | +} |
| 25 | + |
| 26 | +async function withShell( |
| 27 | + fn: (shell: AppShell, frame: () => string) => Promise<void> | void, |
| 28 | + size: Size = { width: 100, height: 60 }, |
| 29 | +): Promise<void> { |
| 30 | + await withTestRenderer(async (h) => { |
| 31 | + const shell = createAppShell(h.renderer, { |
| 32 | + terminal: { columns: size.width, rows: size.height }, |
| 33 | + wireKeys: false, |
| 34 | + }); |
| 35 | + try { |
| 36 | + appendStreamRow(shell, { role: "assistant", text: "session underway" }); |
| 37 | + await fn(shell, () => h.captureCharFrame()); |
| 38 | + await h.renderOnce(); |
| 39 | + } finally { |
| 40 | + shell.dispose(); |
| 41 | + } |
| 42 | + }, size); |
| 43 | +} |
| 44 | + |
| 45 | +describe("empty overlay layout", () => { |
| 46 | + test("zero rows reserve zero height", async () => { |
| 47 | + await withShell((shell) => { |
| 48 | + openListOverlay(shell, { kind: "demo", items: [] }); |
| 49 | + try { |
| 50 | + expect(shell.overlayList?.height).toBe(0); |
| 51 | + // Zero list rows; the host carries chrome plus the one empty-state row. |
| 52 | + const chrome = overlayChromeRows( |
| 53 | + "demo", |
| 54 | + shell.overlayBodyLines.length + 1, |
| 55 | + false, |
| 56 | + false, |
| 57 | + ); |
| 58 | + expect(shell.layout.heights.overlay_host).toBe(chrome); |
| 59 | + } finally { |
| 60 | + closeInsetOverlay(shell); |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test("an explicit empty state paints inside the body chrome", async () => { |
| 66 | + await withTestRenderer( |
| 67 | + async (h) => { |
| 68 | + const shell = createAppShell(h.renderer, { |
| 69 | + terminal: { columns: 80, rows: 24 }, |
| 70 | + wireKeys: false, |
| 71 | + }); |
| 72 | + try { |
| 73 | + appendStreamRow(shell, { |
| 74 | + role: "assistant", |
| 75 | + text: "session underway", |
| 76 | + }); |
| 77 | + openListOverlay(shell, { kind: "demo", items: [] }); |
| 78 | + await h.renderOnce(); |
| 79 | + const frame = h.captureCharFrame(); |
| 80 | + expect(frame).toContain(OVERLAY_EMPTY_STATE); |
| 81 | + } finally { |
| 82 | + shell.dispose(); |
| 83 | + } |
| 84 | + }, |
| 85 | + { width: 80, height: 24 }, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + test("replacing the body on an empty overlay keeps zero rows", async () => { |
| 90 | + await withShell((shell) => { |
| 91 | + openListOverlay(shell, { kind: "demo", items: [] }); |
| 92 | + try { |
| 93 | + setOverlayBody(shell, "context line"); |
| 94 | + expect(shell.overlayList?.height).toBe(0); |
| 95 | + const chrome = overlayChromeRows( |
| 96 | + "demo", |
| 97 | + shell.overlayBodyLines.length + 1, |
| 98 | + false, |
| 99 | + false, |
| 100 | + ); |
| 101 | + expect(shell.layout.heights.overlay_host).toBe(chrome); |
| 102 | + } finally { |
| 103 | + closeInsetOverlay(shell); |
| 104 | + } |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + test("replacing all items with none collapses to zero rows", async () => { |
| 109 | + await withShell((shell) => { |
| 110 | + openListOverlay(shell, { kind: "demo", items: ["a", "b"] }); |
| 111 | + try { |
| 112 | + expect(setOwnedOverlayItems(shell, "demo", [], [])).toBe(true); |
| 113 | + expect(shell.overlayList?.height).toBe(0); |
| 114 | + } finally { |
| 115 | + closeInsetOverlay(shell); |
| 116 | + } |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + test("a short terminal reserves no phantom choice row for an empty overlay", async () => { |
| 121 | + const size = { width: 80, height: 8 }; |
| 122 | + await withTestRenderer(async (h) => { |
| 123 | + const shell = createAppShell(h.renderer, { |
| 124 | + terminal: { columns: size.width, rows: size.height }, |
| 125 | + wireKeys: false, |
| 126 | + }); |
| 127 | + try { |
| 128 | + appendStreamRow(shell, { role: "assistant", text: "session underway" }); |
| 129 | + openListOverlay(shell, { kind: "demo", items: [] }); |
| 130 | + await h.renderOnce(); |
| 131 | + await h.renderOnce(); |
| 132 | + expect(shell.overlayList?.height).toBe(0); |
| 133 | + const chrome = overlayChromeRows( |
| 134 | + "demo", |
| 135 | + shell.overlayBodyLines.length + 1, |
| 136 | + false, |
| 137 | + false, |
| 138 | + ); |
| 139 | + expect(shell.layout.heights.overlay_host).toBe(chrome); |
| 140 | + const frame = h.captureCharFrame(); |
| 141 | + const lines = frame.replace(/\n$/, "").split("\n"); |
| 142 | + const top = lines.findIndex((l) => l.trimStart().startsWith("┌")); |
| 143 | + const bottom = lines.findIndex( |
| 144 | + (l, i) => i > top && l.trimStart().startsWith("└"), |
| 145 | + ); |
| 146 | + expect(top).toBeGreaterThanOrEqual(0); |
| 147 | + expect(bottom).toBeGreaterThan(top); |
| 148 | + expect(bottom).toBeLessThan(lines.length); |
| 149 | + expect(frame).toContain(OVERLAY_EMPTY_STATE); |
| 150 | + } finally { |
| 151 | + shell.dispose(); |
| 152 | + } |
| 153 | + }, size); |
| 154 | + }); |
| 155 | + |
| 156 | + test("a non-empty overlay keeps its rows and paints no empty state", async () => { |
| 157 | + await withTestRenderer( |
| 158 | + async (h) => { |
| 159 | + const shell = createAppShell(h.renderer, { |
| 160 | + terminal: { columns: 80, rows: 24 }, |
| 161 | + wireKeys: false, |
| 162 | + }); |
| 163 | + try { |
| 164 | + appendStreamRow(shell, { |
| 165 | + role: "assistant", |
| 166 | + text: "session underway", |
| 167 | + }); |
| 168 | + openListOverlay(shell, { kind: "demo", items: ["alpha", "beta"] }); |
| 169 | + await h.renderOnce(); |
| 170 | + expect(shell.overlayList?.height).toBe(2); |
| 171 | + const frame = h.captureCharFrame(); |
| 172 | + expect(frame).toContain("alpha"); |
| 173 | + expect(frame).not.toContain(OVERLAY_EMPTY_STATE); |
| 174 | + } finally { |
| 175 | + shell.dispose(); |
| 176 | + } |
| 177 | + }, |
| 178 | + { width: 80, height: 24 }, |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + test("an empty list wrapper starts at zero rows and still grows", async () => { |
| 183 | + await withTestRenderer(async (h) => { |
| 184 | + const list = createOverlayList(h.renderer, { count: 0, items: 0 }); |
| 185 | + expect(list.height).toBe(0); |
| 186 | + list.setHeight(0); |
| 187 | + expect(list.height).toBe(0); |
| 188 | + list.setHeight(2); |
| 189 | + expect(list.height).toBe(2); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments