Skip to content

Commit f5e7ba7

Browse files
chore(web): measure renders for the whole app on a fake environment
App render scenarios mount AppRoot with the app's router on a thread and drive it through mount, streaming, typing, thread switches, the diff panel and settings. A fake environment in apps/web/test/fakeEnvironment.ts serves the HTTP API from a fetch stub and replaces client-runtime's connection driver with an in-process session, so every store, atom and route above the wire is the app's own. The timeline scenario now keeps one list handle per mounted timeline, as ChatView does; a new handle per render skipped the timeline's scroll-to-end work and undercounted switch-thread. The render counter keeps its state on a global so a second module instance shares the hook React reported to. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5f53916 commit f5e7ba7

5 files changed

Lines changed: 788 additions & 24 deletions

File tree

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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.
142+
it("warm up", async () => {
143+
using _clock = frozenClock();
144+
await using _app = (await openThread(WARM)).screen;
145+
await document.fonts.ready;
146+
await quiet();
147+
});
148+
149+
it("app-mount-thread", async () => {
150+
using _clock = frozenClock();
151+
startCounting();
152+
await using _app = (await mountApp(threadPath(A))).screen;
153+
await expect.element(lastAnswer(A)).toBeVisible();
154+
await quiet();
155+
await report("app-mount-thread", [stopCounting()]);
156+
});
157+
158+
it("app-stream-assistant-turn", async () => {
159+
using _clock = frozenClock();
160+
await using _app = (await openThread(LIVE)).screen;
161+
expect(isThreadSubscribed(LIVE)).toBe(true);
162+
const live = runningRun(LIVE, "live-run", HISTORY_TURNS + 1, "live-user");
163+
await act(async () => {
164+
await emitThreadEvent(LIVE, { type: "run.created", payload: live });
165+
await emitThreadEvent(LIVE, {
166+
type: "turn-item.updated",
167+
payload: userItem(LIVE, live.id, "live-user", HISTORY_TURNS * 2, "Continue"),
168+
});
169+
});
170+
await expect.element(page.getByText("Continue", { exact: true })).toBeVisible();
171+
const counted = await measure(async () => {
172+
let text = "";
173+
for (let delta = 0; delta < STREAM_DELTAS; delta++) {
174+
text += `word${delta} `;
175+
await act(() =>
176+
emitThreadEvent(LIVE, {
177+
type: "turn-item.updated",
178+
payload: assistantItem(
179+
LIVE,
180+
live.id,
181+
"live-assistant",
182+
HISTORY_TURNS * 2 + 1,
183+
text,
184+
true,
185+
),
186+
}),
187+
);
188+
}
189+
await expect
190+
.element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
191+
.toBeVisible();
192+
});
193+
await report("app-stream-assistant-turn", [counted], { commits: false });
194+
});
195+
196+
it("app-type-in-composer", async () => {
197+
using _clock = frozenClock();
198+
await using _app = (await openThread(A)).screen;
199+
const editor = page.getByTestId("composer-editor");
200+
await userEvent.click(editor);
201+
const counted = await measure(async () => {
202+
await userEvent.type(editor, TYPED_TEXT);
203+
await expect.element(editor).toHaveTextContent(TYPED_TEXT);
204+
});
205+
await report("app-type-in-composer", [counted]);
206+
});
207+
208+
it("app-switch-thread", async () => {
209+
using _clock = frozenClock();
210+
const { screen, router } = await openThread(A);
211+
await using _app = screen;
212+
const go = (threadId: ThreadId) => async () => {
213+
await act(() =>
214+
router.navigate({
215+
to: "/$environmentId/$threadId",
216+
params: { environmentId: ENVIRONMENT_ID, threadId },
217+
}),
218+
);
219+
await expect.element(lastAnswer(threadId)).toBeVisible();
220+
};
221+
await report("app-switch-thread", [await measure(go(B)), await measure(go(A))]);
222+
});
223+
224+
// Through the diff.toggle shortcut, the same handler as the header's diff button.
225+
it("app-toggle-diff-panel", async () => {
226+
using _clock = frozenClock();
227+
await using _app = (await openThread(A)).screen;
228+
const diff = () => page.getByRole("group", { name: "Diff layout" });
229+
const open = await measure(async () => {
230+
await userEvent.keyboard("{Control>}d{/Control}");
231+
await expect.element(diff()).toBeVisible();
232+
});
233+
const close = await measure(async () => {
234+
await userEvent.keyboard("{Control>}d{/Control}");
235+
await expect.element(diff()).not.toBeInTheDocument();
236+
});
237+
await report("app-toggle-diff-panel", [open, close]);
238+
});
239+
240+
const openSettings =
241+
(router: AppRouter, to: "/settings/general" | "/settings/appearance") => async () => {
242+
await act(() => router.navigate({ to }));
243+
// A section title from each page, since neither page has a heading that names it.
244+
const name = to === "/settings/general" ? "Time format" : "Motion";
245+
await expect.element(page.getByRole("heading", { name, exact: true })).toBeVisible();
246+
// Appearance highlights its code font preview off-thread, then attaches it in a shadow root.
247+
if (to === "/settings/appearance") {
248+
await vi.waitFor(
249+
() =>
250+
expect([...document.querySelectorAll("div")].some((node) => node.shadowRoot)).toBe(true),
251+
{ interval: 100, timeout: 15_000 },
252+
);
253+
}
254+
};
255+
256+
it("app-open-settings", async () => {
257+
using _clock = frozenClock();
258+
const { screen, router } = await openThread(A);
259+
await using _app = screen;
260+
await report("app-open-settings", [await measure(openSettings(router, "/settings/general"))]);
261+
});
262+
263+
it("app-switch-settings-section", async () => {
264+
using _clock = frozenClock();
265+
const { screen, router } = await openThread(A);
266+
await using _app = screen;
267+
await openSettings(router, "/settings/general")();
268+
await report("app-switch-settings-section", [
269+
await measure(openSettings(router, "/settings/appearance")),
270+
]);
271+
});

0 commit comments

Comments
 (0)