@@ -0,0 +1,443 @@
1+import { afterEach, describe, test } from "bun:test";
2+import { cel, MockTerminal } from "@cel-tui/core";
3+import type { Node } from "@cel-tui/types";
4+import {
5+ fauxAssistantMessage,
6+ fauxThinking,
7+ fauxToolCall,
8+} from "@mariozechner/pi-ai";
9+import type { AppState } from "../index.ts";
10+import {
11+ computeContextTokens,
12+ computeStats,
13+ createUiMessage,
14+ openDatabase,
15+} from "../session.ts";
16+import { DEFAULT_SHOW_REASONING, DEFAULT_VERBOSE } from "../settings.ts";
17+import { DEFAULT_THEME } from "../theme.ts";
18+import {
19+ buildConversationLogNodes,
20+ resetConversationRenderCache,
21+} from "../ui/conversation.ts";
22+import {
23+ createInputController,
24+ renderActiveOverlay,
25+ renderBaseLayout,
26+ resetUiState,
27+} from "../ui.ts";
28+
29+const VIEWPORT_COLS = 120;
30+const VIEWPORT_ROWS = 40;
31+const TYPING_SAMPLE_COUNT = 9;
32+const LARGE_MARKDOWN_SECTION_COUNT = 8;
33+const NODE_BUDGET = 6_000;
34+const TYPING_MEDIAN_BUDGET_MS = 40;
35+
36+function flushCelRender(): Promise<void> {
37+ return new Promise((resolve) => {
38+ process.nextTick(resolve);
39+ });
40+}
41+
42+function startUiViewport(
43+ state: AppState,
44+ terminal: MockTerminal,
45+ controller: ReturnType<typeof createInputController>,
46+): void {
47+ cel.init(terminal);
48+ cel.viewport(() => {
49+ const base = renderBaseLayout(state, terminal.columns, controller);
50+ const overlay = renderActiveOverlay(state);
51+ return overlay ? [base, overlay] : base;
52+ });
53+}
54+
55+function createTestState(): AppState {
56+ const cwd = "/tmp/mini-coder-ui-render-perf-test";
57+ const model: NonNullable<AppState["model"]> = {
58+ id: "gpt-5.4",
59+ name: "gpt-5.4",
60+ provider: "openai-codex",
61+ api: "responses",
62+ baseUrl: "http://localhost:0",
63+ reasoning: true,
64+ input: ["text"],
65+ cost: {
66+ input: 0,
67+ output: 0,
68+ cacheRead: 0,
69+ cacheWrite: 0,
70+ },
71+ contextWindow: 272_000,
72+ maxTokens: 8_192,
73+ };
74+
75+ return {
76+ db: openDatabase(":memory:"),
77+ session: null,
78+ model,
79+ effort: "medium",
80+ messages: [],
81+ stats: { totalInput: 0, totalOutput: 0, totalCost: 0 },
82+ contextTokens: 0,
83+ agentsMd: [],
84+ skills: [],
85+ plugins: [],
86+ theme: DEFAULT_THEME,
87+ git: null,
88+ providers: new Map(),
89+ oauthCredentials: {},
90+ settings: {},
91+ settingsPath: `${cwd}/settings.json`,
92+ cwd,
93+ canonicalCwd: cwd,
94+ running: false,
95+ abortController: null,
96+ activeTurnPromise: null,
97+ showReasoning: DEFAULT_SHOW_REASONING,
98+ verbose: DEFAULT_VERBOSE,
99+ versionLabel: "dev",
100+ customModels: [],
101+ startupWarnings: [],
102+ };
103+}
104+
105+function setMessages(state: AppState, messages: AppState["messages"]): void {
106+ state.messages = messages;
107+ state.stats = computeStats(messages);
108+ state.contextTokens = computeContextTokens(messages);
109+}
110+
111+function buildLargeMarkdown(seed: number): string {
112+ return Array.from({ length: LARGE_MARKDOWN_SECTION_COUNT }, (_, index) => {
113+ const section = seed * 100 + index;
114+ return [
115+ `# Section ${section}`,
116+ "",
117+ `Paragraph ${section}: ${"lorem ipsum dolor sit amet ".repeat(10)}`,
118+ "",
119+ `- item ${section}a with [link](https://example.com/${section})`,
120+ `- item ${section}b with **bold** text and \`inline_code_${section}\``,
121+ "",
122+ `> Quote ${section}: ${"wrapped quoted text ".repeat(10)}`,
123+ "",
124+ "```ts",
125+ `const value${section} = ${section};`,
126+ `console.log(value${section});`,
127+ "```",
128+ ].join("\n");
129+ }).join("\n\n");
130+}
131+
132+function createLargeMarkdownMessages(): AppState["messages"] {
133+ return [
134+ {
135+ role: "user",
136+ content: "Investigate the rendering lag in this session.",
137+ timestamp: 1,
138+ },
139+ fauxAssistantMessage(buildLargeMarkdown(1), { timestamp: 2 }),
140+ {
141+ role: "user",
142+ content: "Keep going with the detailed write-up.",
143+ timestamp: 3,
144+ },
145+ fauxAssistantMessage(buildLargeMarkdown(2), { timestamp: 4 }),
146+ {
147+ role: "user",
148+ content: "Add one more large markdown response for history.",
149+ timestamp: 5,
150+ },
151+ fauxAssistantMessage(buildLargeMarkdown(3), { timestamp: 6 }),
152+ ];
153+}
154+
155+function countNodes(node: Node): number {
156+ if (node.type === "text" || node.type === "textinput") {
157+ return 1;
158+ }
159+
160+ return (
161+ 1 + node.children.reduce((total, child) => total + countNodes(child), 0)
162+ );
163+}
164+
165+function getConversationNode(
166+ messages: AppState["messages"],
167+ index: number,
168+): Node {
169+ resetConversationRenderCache();
170+
171+ const nodes = buildConversationLogNodes(
172+ {
173+ messages,
174+ showReasoning: true,
175+ verbose: false,
176+ theme: DEFAULT_THEME,
177+ versionLabel: "dev",
178+ },
179+ {
180+ isStreaming: false,
181+ content: [],
182+ pendingToolResults: [],
183+ },
184+ 0,
185+ 80,
186+ );
187+ const node = nodes[index];
188+
189+ if (!node) {
190+ throw new Error(`Expected a rendered node at index ${index}`);
191+ }
192+
193+ return node;
194+}
195+
196+function expectNodeCountAtMost(
197+ label: string,
198+ node: Node,
199+ maxNodes: number,
200+): void {
201+ const nodeCount = countNodes(node);
202+
203+ if (nodeCount > maxNodes) {
204+ throw new Error(
205+ `Expected ${label} node count <= ${maxNodes}, got ${nodeCount}`,
206+ );
207+ }
208+}
209+
210+function median(values: readonly number[]): number {
211+ const sorted = [...values].sort((a, b) => a - b);
212+ const middle = Math.floor(sorted.length / 2);
213+
214+ if (sorted.length % 2 === 0) {
215+ return (sorted[middle - 1]! + sorted[middle]!) / 2;
216+ }
217+
218+ return sorted[middle]!;
219+}
220+
221+async function measureTypingMedianRerenderMs(state: AppState): Promise<number> {
222+ const terminal = new MockTerminal(VIEWPORT_COLS, VIEWPORT_ROWS);
223+ const controller = createInputController(state);
224+ const samples: number[] = [];
225+
226+ startUiViewport(state, terminal, controller);
227+ await flushCelRender();
228+
229+ try {
230+ for (let index = 0; index < TYPING_SAMPLE_COUNT; index++) {
231+ const nextValue = index % 2 === 0 ? "a" : "ab";
232+ const start = performance.now();
233+ controller.onChange(nextValue);
234+ await flushCelRender();
235+ samples.push(performance.now() - start);
236+ }
237+ } finally {
238+ cel.stop();
239+ }
240+
241+ return median(samples);
242+}
243+
244+afterEach(() => {
245+ resetUiState();
246+ cel.stop();
247+});
248+
249+describe("ui render performance", () => {
250+ test("renderBaseLayout with large historical assistant markdown stays within the node budget", () => {
251+ // Arrange
252+ const state = createTestState();
253+ setMessages(state, createLargeMarkdownMessages());
254+ const controller = createInputController(state);
255+
256+ try {
257+ // Act
258+ const base = renderBaseLayout(state, VIEWPORT_COLS, controller);
259+ const nodeCount = countNodes(base);
260+
261+ // Assert
262+ if (nodeCount > NODE_BUDGET) {
263+ throw new Error(
264+ `Expected large-markdown layout node count <= ${NODE_BUDGET}, got ${nodeCount}`,
265+ );
266+ }
267+ } finally {
268+ state.db.close();
269+ }
270+ });
271+
272+ test("non-markdown conversation log items stay within bounded node budgets", () => {
273+ // Arrange
274+ const userNode = getConversationNode(
275+ [
276+ {
277+ role: "user",
278+ content: "lorem ipsum dolor sit amet ".repeat(400),
279+ timestamp: 1,
280+ },
281+ ],
282+ 0,
283+ );
284+ const uiNode = getConversationNode(
285+ [createUiMessage("status update ".repeat(400))],
286+ 0,
287+ );
288+ const thinkingNode = getConversationNode(
289+ [fauxAssistantMessage([fauxThinking("plan\n".repeat(400))])],
290+ 0,
291+ );
292+ const assistantToolCallsNode = getConversationNode(
293+ [
294+ fauxAssistantMessage([
295+ fauxThinking("brief plan"),
296+ fauxToolCall(
297+ "shell",
298+ { command: "printf 'foo\\nbar'" },
299+ { id: "tool-1" },
300+ ),
301+ fauxToolCall(
302+ "edit",
303+ {
304+ path: "src/app.ts",
305+ oldText: "before",
306+ newText: "after",
307+ },
308+ { id: "tool-2" },
309+ ),
310+ fauxToolCall("readImage", { path: "diagram.png" }, { id: "tool-3" }),
311+ ]),
312+ ],
313+ 0,
314+ );
315+ const longShellToolCallNode = getConversationNode(
316+ [
317+ fauxAssistantMessage([
318+ fauxToolCall(
319+ "shell",
320+ { command: `printf ${"x".repeat(300)}TAIL` },
321+ { id: "tool-4" },
322+ ),
323+ ]),
324+ ],
325+ 0,
326+ );
327+ const shellResultNode = getConversationNode(
328+ [
329+ fauxAssistantMessage([
330+ fauxToolCall("shell", { command: "seq 1 200" }, { id: "tool-1" }),
331+ ]),
332+ {
333+ role: "toolResult",
334+ toolCallId: "tool-1",
335+ toolName: "shell",
336+ content: [
337+ {
338+ type: "text",
339+ text: Array.from(
340+ { length: 200 },
341+ (_, index) => `line ${index + 1}`,
342+ ).join("\n"),
343+ },
344+ ],
345+ isError: false,
346+ timestamp: 2,
347+ },
348+ ],
349+ 1,
350+ );
351+ const editErrorNode = getConversationNode(
352+ [
353+ fauxAssistantMessage([
354+ fauxToolCall(
355+ "edit",
356+ {
357+ path: "src/app.ts",
358+ oldText: "before",
359+ newText: "after",
360+ },
361+ { id: "tool-2" },
362+ ),
363+ ]),
364+ {
365+ role: "toolResult",
366+ toolCallId: "tool-2",
367+ toolName: "edit",
368+ content: [
369+ {
370+ type: "text",
371+ text: Array.from(
372+ { length: 120 },
373+ (_, index) => `line ${index + 1}`,
374+ ).join("\n"),
375+ },
376+ ],
377+ isError: true,
378+ timestamp: 3,
379+ },
380+ ],
381+ 1,
382+ );
383+ const genericResultNode = getConversationNode(
384+ [
385+ {
386+ role: "toolResult",
387+ toolCallId: "tool-3",
388+ toolName: "pluginSearch",
389+ content: [
390+ {
391+ type: "text",
392+ text: Array.from(
393+ { length: 200 },
394+ (_, index) => `row ${index + 1}`,
395+ ).join("\n"),
396+ },
397+ ],
398+ isError: false,
399+ timestamp: 4,
400+ },
401+ ],
402+ 0,
403+ );
404+
405+ // Assert
406+ expectNodeCountAtMost("long user message", userNode, 4);
407+ expectNodeCountAtMost("long UI message", uiNode, 4);
408+ expectNodeCountAtMost("thinking-only assistant message", thinkingNode, 4);
409+ expectNodeCountAtMost(
410+ "assistant tool-call bundle",
411+ assistantToolCallsNode,
412+ 64,
413+ );
414+ expectNodeCountAtMost(
415+ "long single-token shell tool call",
416+ longShellToolCallNode,
417+ 64,
418+ );
419+ expectNodeCountAtMost("shell tool-result preview", shellResultNode, 24);
420+ expectNodeCountAtMost("edit error preview", editErrorNode, 24);
421+ expectNodeCountAtMost("generic tool result", genericResultNode, 240);
422+ });
423+
424+ test("typing into the input with large historical assistant markdown stays within the rerender budget", async () => {
425+ // Arrange
426+ const state = createTestState();
427+ setMessages(state, createLargeMarkdownMessages());
428+
429+ try {
430+ // Act
431+ const medianRerenderMs = await measureTypingMedianRerenderMs(state);
432+
433+ // Assert
434+ if (medianRerenderMs > TYPING_MEDIAN_BUDGET_MS) {
435+ throw new Error(
436+ `Expected typing median rerender <= ${TYPING_MEDIAN_BUDGET_MS}ms, got ${medianRerenderMs.toFixed(1)}ms`,
437+ );
438+ }
439+ } finally {
440+ state.db.close();
441+ }
442+ });
443+});