Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/closura-arc-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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" <<EOF
Dir::Etc "$apt_dir/etc";
Dir::Etc::sourcelist "$apt_dir/etc/sources.list";
Dir::Etc::sourceparts "$apt_dir/etc/sources.list.d";
Dir::Etc::parts "$apt_dir/etc/apt.conf.d";
Dir::State::lists "$apt_dir/lists";
Dir::State::status "/var/lib/dpkg/status";
Dir::Cache::archives "$apt_dir/archives";
Dir::Cache::pkgcache "$apt_dir/pkgcache.bin";
Dir::Cache::srcpkgcache "$apt_dir/srcpkgcache.bin";
Debug::NoLocking "1";
APT::Get::List-Cleanup "0";
EOF
sed -i -E 's/^[[:space:]]+//' "$apt_dir/apt.conf"
export APT_CONFIG="$apt_dir/apt.conf"
apt-get update -qq
apt-get -qq install --download-only --no-install-recommends -y "${packages[@]}"
shopt -s nullglob
debs=("$apt_dir"/archives/*.deb)
shopt -u nullglob
for deb in "${debs[@]}"; do dpkg-deb -x "$deb" "$apt_dir/root"; done
export LD_LIBRARY_PATH="$apt_dir/root/usr/lib/x86_64-linux-gnu:$apt_dir/root/lib/x86_64-linux-gnu${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
mkdir -p "$RUNNER_TEMP/fontconfig-cache"
printf '<?xml version="1.0"?>\n<fontconfig><dir>/usr/share/fonts</dir><dir>%s</dir><cachedir>%s</cachedir></fontconfig>\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
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
161 changes: 161 additions & 0 deletions apps/web/test/app.latency.tsx
Original file line number Diff line number Diff line change
@@ -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<unknown, FakeDriver> }>(),
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(<AppRoot router={router} />);
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<ReturnType<typeof mountApp>> | 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),
);
});
});
89 changes: 89 additions & 0 deletions apps/web/test/interactionLatency.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
}
}

const INTERACTION_BUDGET_MS = 400;
const SAMPLES = 5;
const QUIET_MS = 150;
const INPUT_EVENTS = ["pointerdown", "keydown"] as const;

const nextFrame = () =>
new Promise<number>((resolve) => requestAnimationFrame(() => resolve(performance.now())));

export async function settle(): Promise<void> {
await new Promise<void>((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<unknown>,
isDone: () => boolean,
{ from = "input" }: { readonly from?: "input" | "call" } = {},
): Promise<number> {
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<number>((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<number>,
{ ungated }: { readonly ungated?: string } = {},
): Promise<void> {
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);
}
22 changes: 22 additions & 0 deletions apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -446,6 +467,7 @@ export default defineConfig(({ command }) => {
defineProject(unitTestProject),
defineProject(browserTestProject),
defineProject(renderScenariosProject),
defineProject(latencyProject),
],
},
};
Expand Down
Loading