diff --git a/.github/workflows/closura-arc-ci.yml b/.github/workflows/closura-arc-ci.yml index 6ab8a3312e27c..f8339ecb831b1 100644 --- a/.github/workflows/closura-arc-ci.yml +++ b/.github/workflows/closura-arc-ci.yml @@ -186,6 +186,78 @@ jobs: apps/server/src/ws.test.ts \ packages/contracts/src/environment.test.ts + - name: Gate interaction latency at 400 ms + shell: bash + working-directory: apps/web + run: | + set -euo pipefail + export PLAYWRIGHT_BROWSERS_PATH="$RUNNER_TEMP/ms-playwright" + vp exec playwright install --only-shell chromium + shell=$(find "$PLAYWRIGHT_BROWSERS_PATH" -type f -name chrome-headless-shell | head -1) + if ! test -x "$shell"; then + echo "::error::Playwright installed no chrome-headless-shell under $PLAYWRIGHT_BROWSERS_PATH." + exit 1 + fi + packages=(libasound2t64 libatk-bridge2.0-0t64 libatk1.0-0t64 libatspi2.0-0t64 libcairo2 + libcups2t64 libdbus-1-3 libdrm2 libgbm1 libglib2.0-0t64 libnspr4 libnss3 libpango-1.0-0 + libx11-6 libxcb1 libxcomposite1 libxdamage1 libxext6 libxfixes3 libxkbcommon0 libxrandr2) + # -o Dir::Etc is applied after apt has already read /etc/apt/apt.conf.d, so the + # image Post-Invoke still runs and its rm of /var/cache/apt/archives/partial fails + # the step. APT_CONFIG is read first, which is what points Dir::Etc away from + # those hooks. Downloads stay under the job temp dir. Status stays the image + # dpkg database, so apt fetches only libraries the image does not already have + # and the extract does not replace the image libc. + apt_dir="$RUNNER_TEMP/chromium-apt" + mkdir -p "$apt_dir/etc/apt.conf.d" "$apt_dir/etc/sources.list.d" "$apt_dir/etc/preferences.d" \ + "$apt_dir/lists/partial" "$apt_dir/archives/partial" "$apt_dir/root" + printf '%s\n' \ + 'deb [arch=amd64 signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] https://archive.ubuntu.com/ubuntu noble main universe' \ + 'deb [arch=amd64 signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] https://archive.ubuntu.com/ubuntu noble-updates main universe' \ + 'deb [arch=amd64 signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] https://security.ubuntu.com/ubuntu noble-security main universe' \ + > "$apt_dir/etc/sources.list" + cat > "$apt_dir/apt.conf" <\n/usr/share/fonts%s%s\n' \ + "$apt_dir/root/usr/share/fonts" "$RUNNER_TEMP/fontconfig-cache" > "$RUNNER_TEMP/fonts.conf" + export FONTCONFIG_FILE="$RUNNER_TEMP/fonts.conf" + fonts=0 + for font_dir in /usr/share/fonts "$apt_dir/root/usr/share/fonts"; do + if test -d "$font_dir"; then + fonts=$((fonts + $(find "$font_dir" -name '*.ttf' | wc -l))) + fi + done + if test "$fonts" -eq 0; then + echo "::error::No fonts for headless Chromium, so text has no size and is never visible." + exit 1 + fi + printf 'Fonts available to Chromium: %s\n' "$fonts" + if ldd "$shell" | grep 'not found'; then + echo "::error::Headless Chromium still misses the libraries above." + exit 1 + fi + vp run test:latency + - name: Test mobile new-task placement, outbox and uploads shell: bash working-directory: apps/mobile diff --git a/apps/web/package.json b/apps/web/package.json index fd3aca76ad247..3483675e7d67e 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -11,6 +11,7 @@ "i18n:generate": "gt generate", "preview": "vp preview", "test": "vp test run --passWithNoTests --project unit --project browser", + "test:latency": "NODE_ENV=production vp test run --project latency", "i18n:translate": "gt translate" }, "dependencies": { diff --git a/apps/web/test/app.latency.tsx b/apps/web/test/app.latency.tsx new file mode 100644 index 0000000000000..25530225e34de --- /dev/null +++ b/apps/web/test/app.latency.tsx @@ -0,0 +1,161 @@ +import { createMemoryHistory } from "@tanstack/react-router"; +import type { ThreadId } from "@t3tools/contracts"; +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; +import type * as Context from "effect/Context"; +import { expect, it, vi } from "vite-plus/test"; +import { page, userEvent } from "vite-plus/test/browser"; + +import "~/index.css"; +import "~/components/ChatView"; + +import * as React from "react"; +import { createRoot } from "react-dom/client"; + +import { AppRoot } from "~/AppRoot"; +import { __setClientSettingsForTests } from "~/hooks/useSettings"; +import { getRouter, type AppRouter } from "~/router"; +import { + ENVIRONMENT_ID, + installFakeEnvironment, + threadWithHistory, + type FakeDriver, +} from "./fakeEnvironment"; +import { expectWithinBudget, settle, timeInteraction } from "./interactionLatency"; + +vi.mock("../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => { + const [actual, { fakeDriver }, Layer] = await Promise.all([ + importOriginal<{ readonly ConnectionDriver: Context.Key }>(), + import("./fakeEnvironment"), + import("effect/Layer"), + ]); + return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) }; +}); + +vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({ + ...(await original()), + trackRpcRequestSent: () => {}, +})); + +const HISTORY_TURNS = 20; +const SWITCH = ["latency-a", "latency-b"].map((name) => threadWithHistory(name, HISTORY_TURNS)); +const SEND = [0, 1, 2, 3, 4].map((index) => + threadWithHistory(`latency-send-${index}`, HISTORY_TURNS), +); +const [A, B] = SWITCH.map((thread) => thread.id) as [ThreadId, ThreadId]; + +installFakeEnvironment([...SWITCH, ...SEND], { acceptCommands: true }); +await page.viewport(1400, 900); +__setClientSettingsForTests({ + ...DEFAULT_CLIENT_SETTINGS, + onboardingCompletedAt: "2026-01-01T00:00:00.000Z", +}); + +const threadPath = (threadId: ThreadId) => `/${ENVIRONMENT_ID}/${threadId}`; +const lastAnswer = (threadId: ThreadId) => `Answer ${HISTORY_TURNS - 1} for ${threadId}.`; +const bodyText = () => document.body.textContent ?? ""; +const occurrences = (text: string, needle: string) => text.split(needle).length - 1; +const composerEditor = () => document.querySelector('[data-testid="composer-editor"]'); + +async function mountApp(path: string) { + const router = getRouter(createMemoryHistory({ initialEntries: [path] })); + await router.load(); + const container = document.createElement("div"); + container.style.cssText = "height: 900px; width: 1400px;"; + document.body.append(container); + const root = createRoot(container); + root.render(); + return { + router, + [Symbol.dispose]() { + root.unmount(); + container.remove(); + }, + }; +} + +const threadOpen = (router: AppRouter, threadId: ThreadId) => () => + router.state.location.pathname === threadPath(threadId) && + bodyText().includes(lastAnswer(threadId)) && + composerEditor() !== null; + +async function openThread(threadId: ThreadId) { + using stack = new DisposableStack(); + const app = stack.use(await mountApp(threadPath(threadId))); + await expect.element(page.getByText(lastAnswer(threadId))).toBeVisible(); + await settle(); + stack.move(); + return app; +} + +const goToThread = (router: AppRouter, threadId: ThreadId) => + router.navigate({ + to: "/$environmentId/$threadId", + params: { environmentId: ENVIRONMENT_ID, threadId }, + }); + +it("warm up on production React", async () => { + expect( + React.act, + "run through `vp run test:latency`, which sets NODE_ENV=production", + ).toBeUndefined(); + { + using _cold = await mountApp(threadPath(A)); + await vi + .waitFor(() => expect(bodyText()).toContain(lastAnswer(A)), { timeout: 20_000 }) + .catch(() => undefined); + } + using _app = await openThread(A); + await document.fonts.ready; + await settle(); +}); + +it("app launch: mounting the app shows the open thread and its composer", async () => { + await expectWithinBudget( + "app launch (mount to an open thread)", + async () => { + let app: Awaited> | undefined; + const elapsed = await timeInteraction( + async () => { + app = await mountApp(threadPath(B)); + }, + () => app !== undefined && threadOpen(app.router, B)(), + { from: "call" }, + ); + app?.[Symbol.dispose](); + return elapsed; + }, + { ungated: "about 350 ms on an idle ASUS, too close to the budget for a shared runner" }, + ); +}); + +it("thread switch: clicking a sidebar row shows that thread", async () => { + using app = await openThread(A); + await expectWithinBudget("thread switch", async (index) => { + const target = index % 2 === 0 ? B : A; + await settle(); + return timeInteraction( + () => userEvent.click(page.getByText(`Thread ${target}`, { exact: true }).first()), + threadOpen(app.router, target), + ); + }); +}); + +it("composer send: Enter puts the message in the thread", async () => { + using app = await openThread(SEND[0]!.id); + await expectWithinBudget("composer send", async (index) => { + const thread = SEND[index]!.id; + await goToThread(app.router, thread); + await expect.element(page.getByText(lastAnswer(thread))).toBeVisible(); + const prompt = `latency prompt ${index}`; + const editor = page.getByTestId("composer-editor"); + await userEvent.click(editor); + await userEvent.keyboard(prompt); + await expect.element(editor).toHaveTextContent(prompt); + await settle(); + return timeInteraction( + () => userEvent.keyboard("{Enter}"), + () => + occurrences(bodyText(), prompt) > occurrences(composerEditor()?.textContent ?? "", prompt), + ); + }); +}); diff --git a/apps/web/test/interactionLatency.ts b/apps/web/test/interactionLatency.ts new file mode 100644 index 0000000000000..614619b3046c2 --- /dev/null +++ b/apps/web/test/interactionLatency.ts @@ -0,0 +1,89 @@ +import { expect } from "vite-plus/test"; +import { commands } from "vite-plus/test/browser"; + +declare module "vite-plus/test/browser" { + interface BrowserCommands { + reportLatency: (line: string) => Promise; + } +} + +const INTERACTION_BUDGET_MS = 400; +const SAMPLES = 5; +const QUIET_MS = 150; +const INPUT_EVENTS = ["pointerdown", "keydown"] as const; + +const nextFrame = () => + new Promise((resolve) => requestAnimationFrame(() => resolve(performance.now()))); + +export async function settle(): Promise { + await new Promise((resolve) => { + let timer = setTimeout(done, QUIET_MS); + const observer = new MutationObserver(() => { + clearTimeout(timer); + timer = setTimeout(done, QUIET_MS); + }); + function done() { + observer.disconnect(); + resolve(); + } + observer.observe(document.body, { + subtree: true, + childList: true, + characterData: true, + attributes: true, + }); + }); + await nextFrame(); +} + +export async function timeInteraction( + action: () => Promise, + isDone: () => boolean, + { from = "input" }: { readonly from?: "input" | "call" } = {}, +): Promise { + expect(isDone(), "the result already shows before the interaction").toBe(false); + let start: number | null = from === "call" ? performance.now() : null; + const onInput = (event: Event) => { + start ??= event.timeStamp; + }; + let stopped = false; + const painted = new Promise((resolve) => { + const frame = () => { + if (stopped) return; + if (start === null || !isDone()) { + requestAnimationFrame(frame); + return; + } + const channel = new MessageChannel(); + channel.port1.onmessage = () => resolve(performance.now()); + channel.port2.postMessage(null); + }; + requestAnimationFrame(frame); + }); + for (const type of INPUT_EVENTS) window.addEventListener(type, onInput, { capture: true }); + try { + await action(); + const began = start; + if (began === null) throw new Error("the interaction dispatched no pointerdown or keydown"); + return (await painted) - began; + } finally { + stopped = true; + for (const type of INPUT_EVENTS) window.removeEventListener(type, onInput, { capture: true }); + } +} + +export async function expectWithinBudget( + interaction: string, + sample: (index: number) => Promise, + { ungated }: { readonly ungated?: string } = {}, +): Promise { + const samples: number[] = []; + for (let index = 0; index < SAMPLES; index++) samples.push(await sample(index)); + const median = samples.toSorted((a, b) => a - b)[Math.floor(SAMPLES / 2)]!; + const report = `${interaction}: median ${median.toFixed(1)} ms, samples ${samples + .map((value) => value.toFixed(1)) + .join(", ")} ms, budget ${INTERACTION_BUDGET_MS} ms`; + await commands.reportLatency(ungated ? `not gated, ${ungated}: ${report}` : report); + if (ungated) return; + expect(median, report).toBeLessThanOrEqual(INTERACTION_BUDGET_MS); +} diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 1a55244ddc9a3..176e6c74c9a95 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -182,6 +182,27 @@ const renderScenariosProject = { }, } satisfies TestProjectInlineConfiguration; +const latencyProject = { + ...browserTestProject, + test: { + ...browserTestProject.test, + name: "latency", + include: ["test/*.latency.tsx", "src/**/*.latency.{ts,tsx}"], + hookTimeout: 60_000, + testTimeout: 60_000, + browser: { + ...browserTestProject.test.browser, + instances: [{ browser: "chromium" }], + commands: { + ...browserTestProject.test.browser.commands, + reportLatency: async (_context, line: string) => { + process.stdout.write(`${line}\n`); + }, + }, + }, + }, +} satisfies TestProjectInlineConfiguration; + function resolveDevProxyTarget( backendPort: string | undefined, wsUrl: string | undefined, @@ -446,6 +467,7 @@ export default defineConfig(({ command }) => { defineProject(unitTestProject), defineProject(browserTestProject), defineProject(renderScenariosProject), + defineProject(latencyProject), ], }, };