Skip to content

Commit 562daf7

Browse files
chore(web): measure renders for core chat and app scenarios
Adds a render-scenario harness: `vp run report:renders run` mounts real components and the whole app on an in-process fake environment, counts React commits and component renders per scenario, and fails unless repeated runs agree. `compare` prints before/after deltas. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3e69171 commit 562daf7

8 files changed

Lines changed: 1361 additions & 1 deletion
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
// Render scenarios that mount the whole app (AppRoot with the app's router) on a thread,
2+
// against the in-process environment in fakeEnvironment.ts. Counts cover ChatView, the
3+
// chat and settings routes, the sidebar and the right panel, through the same stores and
4+
// atoms the app reads. See renderScenarios.renders.tsx for the counting rules.
5+
import { createMemoryHistory } from "@tanstack/react-router";
6+
import type { ThreadId } from "@t3tools/contracts";
7+
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
8+
import type * as Context from "effect/Context";
9+
import { expect, it, vi } from "vite-plus/test";
10+
import { commands, page, userEvent } from "vite-plus/test/browser";
11+
12+
import "~/index.css";
13+
14+
import { AppRoot } from "~/AppRoot";
15+
import { __setClientSettingsForTests } from "~/hooks/useSettings";
16+
import { getRouter, type AppRouter } from "~/router";
17+
import {
18+
ENVIRONMENT_ID,
19+
assistantItem,
20+
emitThreadEvent,
21+
installFakeEnvironment,
22+
isThreadSubscribed,
23+
runningRun,
24+
threadWithHistory,
25+
userItem,
26+
type FakeDriver,
27+
} from "./fakeEnvironment";
28+
import { commitsSoFar, startCounting, stopCounting, type RenderReport } from "./renderCounter";
29+
import { act, fakeTimers, render } from "./render";
30+
31+
// The socket is the only fake layer: client-runtime's driver hands every connection the
32+
// in-process session. A string path, since a typed import pulls client-runtime's sources
33+
// into this project, and imports inside, since the factory runs before this file's own.
34+
vi.mock("../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
35+
const [actual, { fakeDriver }, Layer] = await Promise.all([
36+
importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
37+
import("./fakeEnvironment"),
38+
import("effect/Layer"),
39+
]);
40+
return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
41+
});
42+
43+
// The fake server leaves most requests unanswered, which would raise the slow-request toast
44+
// 15s of wall clock after each one, at a different point in every run.
45+
vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
46+
...(await original()),
47+
trackRpcRequestSent: () => {},
48+
}));
49+
50+
const HISTORY_TURNS = 20;
51+
const STREAM_DELTAS = 30;
52+
const TYPED_TEXT = "fix the flaky tests!";
53+
// The stream scenario leaves a turn on its thread, so it streams into one no other scenario opens.
54+
const THREADS = ["thread-warm", "thread-a", "thread-b", "thread-live"].map((name) =>
55+
threadWithHistory(name, HISTORY_TURNS),
56+
);
57+
const [WARM, A, B, LIVE] = THREADS.map((thread) => thread.id) as [
58+
ThreadId,
59+
ThreadId,
60+
ThreadId,
61+
ThreadId,
62+
];
63+
64+
installFakeEnvironment(THREADS);
65+
// Desktop-sized, so the sidebar, composer and right panel lay out as they do in the app.
66+
await page.viewport(1400, 900);
67+
// A returning user: the first-run gate would otherwise wait on onboarding evidence.
68+
__setClientSettingsForTests({
69+
...DEFAULT_CLIENT_SETTINGS,
70+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
71+
});
72+
73+
async function quiet() {
74+
let seen = -1;
75+
await vi.waitFor(
76+
() => {
77+
const now = commitsSoFar();
78+
const settled = now === seen;
79+
seen = now;
80+
expect(settled).toBe(true);
81+
},
82+
{ interval: 100, timeout: 15_000 },
83+
);
84+
}
85+
86+
// Relative times and durations re-render on minute and second ticks of the wall clock, so a
87+
// window that spans a tick counts extra renders. Only Date is frozen; timers run for real.
88+
const frozenClock = () =>
89+
fakeTimers({ toFake: ["Date"], now: new Date("2026-03-18T09:00:30.000Z") });
90+
91+
const threadPath = (threadId: ThreadId) => `/${ENVIRONMENT_ID}/${threadId}`;
92+
const lastAnswer = (threadId: ThreadId) =>
93+
page.getByText(`Answer ${HISTORY_TURNS - 1} for ${threadId}.`);
94+
95+
// The app owns its router, as main.tsx does, so render() adds none.
96+
async function mountApp(path: string) {
97+
const router = getRouter(createMemoryHistory({ initialEntries: [path] }));
98+
await router.load();
99+
const container = document.createElement("div");
100+
container.style.cssText = "height: 900px; width: 1400px;";
101+
const screen = await render(<AppRoot router={router} />, { container, router: false });
102+
return { screen, router };
103+
}
104+
105+
async function openThread(threadId: ThreadId) {
106+
const app = await mountApp(threadPath(threadId));
107+
await expect.element(lastAnswer(threadId)).toBeVisible();
108+
await quiet();
109+
return app;
110+
}
111+
112+
async function measure(action: () => Promise<void>): Promise<RenderReport> {
113+
await quiet();
114+
startCounting();
115+
await action();
116+
await quiet();
117+
return stopCounting();
118+
}
119+
120+
// Commits are left out across several windows, and where frame batching splits a window's
121+
// commits differently from run to run while its renders stay identical.
122+
async function report(
123+
scenario: string,
124+
windows: ReadonlyArray<RenderReport>,
125+
{ commits: countCommits = windows.length === 1 } = {},
126+
) {
127+
const components: Record<string, number> = {};
128+
for (const window of windows) {
129+
for (const [name, count] of Object.entries(window.components)) {
130+
components[name] = (components[name] ?? 0) + count;
131+
}
132+
}
133+
const total = windows.reduce((sum, window) => sum + window.renders, 0);
134+
const commits = countCommits ? windows[0]!.commits : null;
135+
await commands.writeRenderReport(
136+
scenario,
137+
JSON.stringify({ commits, renders: total, components }),
138+
);
139+
}
140+
141+
// A first mount loads lazy route chunks, fonts and the connection at varying times. The diff
142+
// panel and settings load more chunks on first open (the T3 Connect sidebar, the terminal
143+
// preview's renderer), which can land after a window has gone quiet, so the warm-up opens
144+
// each once and lets it settle.
145+
it("warm up", async () => {
146+
using _clock = frozenClock();
147+
const { screen, router } = await openThread(WARM);
148+
await using _app = screen;
149+
await document.fonts.ready;
150+
await quiet();
151+
await toggleDiff("open")();
152+
await quiet();
153+
await toggleDiff("close")();
154+
await quiet();
155+
await openSettings(router, "/settings/general")();
156+
await openSettings(router, "/settings/appearance")();
157+
await quiet();
158+
});
159+
160+
it("app-mount-thread", async () => {
161+
using _clock = frozenClock();
162+
startCounting();
163+
await using _app = (await mountApp(threadPath(A))).screen;
164+
await expect.element(lastAnswer(A)).toBeVisible();
165+
await quiet();
166+
await report("app-mount-thread", [stopCounting()]);
167+
});
168+
169+
it("app-stream-assistant-turn", async () => {
170+
using _clock = frozenClock();
171+
await using _app = (await openThread(LIVE)).screen;
172+
expect(isThreadSubscribed(LIVE)).toBe(true);
173+
const live = runningRun(LIVE, "live-run", HISTORY_TURNS + 1, "live-user");
174+
await act(async () => {
175+
await emitThreadEvent(LIVE, { type: "run.created", payload: live });
176+
await emitThreadEvent(LIVE, {
177+
type: "turn-item.updated",
178+
payload: userItem(LIVE, live.id, "live-user", HISTORY_TURNS * 2, "Continue"),
179+
});
180+
});
181+
await expect.element(page.getByText("Continue", { exact: true })).toBeVisible();
182+
const counted = await measure(async () => {
183+
let text = "";
184+
for (let delta = 0; delta < STREAM_DELTAS; delta++) {
185+
text += `word${delta} `;
186+
await act(() =>
187+
emitThreadEvent(LIVE, {
188+
type: "turn-item.updated",
189+
payload: assistantItem(
190+
LIVE,
191+
live.id,
192+
"live-assistant",
193+
HISTORY_TURNS * 2 + 1,
194+
text,
195+
true,
196+
),
197+
}),
198+
);
199+
}
200+
await expect
201+
.element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
202+
.toBeVisible();
203+
});
204+
await report("app-stream-assistant-turn", [counted], { commits: false });
205+
});
206+
207+
it("app-type-in-composer", async () => {
208+
using _clock = frozenClock();
209+
await using _app = (await openThread(A)).screen;
210+
const editor = page.getByTestId("composer-editor");
211+
await userEvent.click(editor);
212+
const counted = await measure(async () => {
213+
await userEvent.type(editor, TYPED_TEXT);
214+
await expect.element(editor).toHaveTextContent(TYPED_TEXT);
215+
});
216+
await report("app-type-in-composer", [counted]);
217+
});
218+
219+
it("app-switch-thread", async () => {
220+
using _clock = frozenClock();
221+
const { screen, router } = await openThread(A);
222+
await using _app = screen;
223+
const go = (threadId: ThreadId) => async () => {
224+
await act(() =>
225+
router.navigate({
226+
to: "/$environmentId/$threadId",
227+
params: { environmentId: ENVIRONMENT_ID, threadId },
228+
}),
229+
);
230+
await expect.element(lastAnswer(threadId)).toBeVisible();
231+
};
232+
await report("app-switch-thread", [await measure(go(B)), await measure(go(A))]);
233+
});
234+
235+
// Through the diff.toggle shortcut, the same handler as the header's diff button.
236+
const toggleDiff = (to: "open" | "close") => async () => {
237+
await userEvent.keyboard("{Control>}d{/Control}");
238+
const diff = page.getByRole("group", { name: "Diff layout" });
239+
if (to === "open") await expect.element(diff).toBeVisible();
240+
else await expect.element(diff).not.toBeInTheDocument();
241+
};
242+
243+
it("app-toggle-diff-panel", async () => {
244+
using _clock = frozenClock();
245+
await using _app = (await openThread(A)).screen;
246+
const open = await measure(toggleDiff("open"));
247+
const close = await measure(toggleDiff("close"));
248+
await report("app-toggle-diff-panel", [open, close]);
249+
});
250+
251+
const openSettings =
252+
(router: AppRouter, to: "/settings/general" | "/settings/appearance") => async () => {
253+
await act(() => router.navigate({ to }));
254+
// A section title from each page, since neither page has a heading that names it.
255+
const name = to === "/settings/general" ? "Time format" : "Motion";
256+
await expect.element(page.getByRole("heading", { name, exact: true })).toBeVisible();
257+
// Appearance highlights its code font preview off-thread, then attaches it in a shadow root.
258+
if (to === "/settings/appearance") {
259+
await vi.waitFor(
260+
() =>
261+
expect([...document.querySelectorAll("div")].some((node) => node.shadowRoot)).toBe(true),
262+
{ interval: 100, timeout: 15_000 },
263+
);
264+
}
265+
};
266+
267+
it("app-open-settings", async () => {
268+
using _clock = frozenClock();
269+
const { screen, router } = await openThread(A);
270+
await using _app = screen;
271+
await report("app-open-settings", [await measure(openSettings(router, "/settings/general"))]);
272+
});
273+
274+
it("app-switch-settings-section", async () => {
275+
using _clock = frozenClock();
276+
const { screen, router } = await openThread(A);
277+
await using _app = screen;
278+
await openSettings(router, "/settings/general")();
279+
await report("app-switch-settings-section", [
280+
await measure(openSettings(router, "/settings/appearance")),
281+
]);
282+
});

0 commit comments

Comments
 (0)