LibreChat/e2e/specs/mock/scenarios/shared-part-classes.spec.ts
Danny Avila 89a4b9eee5
🎞️ perf: Run the In-Flight Label Sweep on the Compositor (#18128)
* 🎞️ perf: Run the In-Flight Label Sweep on the Compositor

The sweep on a running tool call, streaming reasoning and the elapsed timer
animated `background-position` under `background-clip: text`, which no
browser can hand to the compositor. Every frame of it ran style, paint and
commit on the main thread, and each paint walked the transcript around the
label, so the cost of a frame grew with the conversation and the sweep
stuttered whenever the main thread was busy.

`Shimmer` in `@librechat/client` draws the same sweep from two transforms
that cancel: a masked band slides across the label while a second copy of the
text inside it slides back, so the glyphs hold still and only the gap in the
mask moves. The copy is generated content, so the label is still one text
node. Every in-flight label renders through it.

`e2e/benchmarks-fade` measures a frame and each kind of stream event inside
an exported conversation, from a Chromium trace of the built client.

* 🧪 test: Let the Frame Benchmark Open a Chat in a Chosen Theme
2026-11-14 10:16:52 -05:00

109 lines
4.3 KiB
TypeScript

import { expect, test } from '@playwright/test';
import type { Page } from '@playwright/test';
import {
MOCK_ENDPOINTS,
NEW_CHAT_PATH,
messagesView,
selectMockEndpoint,
sendMessage,
} from '../helpers';
/**
* The icon sizes, the status row type and the label sweep ship in
* `@librechat/client/style.css` so a package consumer gets them; the app must
* read exactly as it did when they lived in its own stylesheet. A probe element
* measures the classes that no route of the mock harness renders on demand.
*/
const probe = (page: Page, className: string) =>
page.evaluate((name) => {
const element = document.createElement('span');
element.className = name;
document.body.append(element);
const { width, height, strokeWidth, fontSize, lineHeight } = getComputedStyle(element);
const root = getComputedStyle(document.documentElement);
const base = root.getPropertyValue('--markdown-font-size').trim();
element.style.fontSize = base || 'var(--font-size-base)';
const baseSize = parseFloat(getComputedStyle(element).fontSize);
element.remove();
return { width, height, strokeWidth, fontSize, lineHeight, baseSize };
}, className);
const elapsedReading = (page: Page) => messagesView(page).getByTestId('stream-elapsed');
const readDipAlpha = (page: Page) =>
page.evaluate(() => {
// Streaming can replace the footer between locator resolution and evaluate.
// Resolve the current node and read its styles in the same browser task.
const element = document.querySelector(
'[data-testid="messages-view"] [data-testid="stream-elapsed"]',
);
return element
? getComputedStyle(element).getPropertyValue('--shimmer-dip-alpha').trim()
: null;
});
test.describe('shared part classes', () => {
test('icon sizes and status row type read as they did from the app stylesheet @scenario:shared-part-classes-keep-their-sizes', async ({
page,
}) => {
await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });
await expect(page.locator('html')).toBeVisible();
const sizes: Array<[string, string, string]> = [
['icon-sm', '16px', '2px'],
['icon-md', '18px', '1.5px'],
['icon-md-heavy', '18px', '2.5px'],
['icon-lg', '24px', '1.5px'],
['icon-xl', '28px', '1.5px'],
['icon-2xl', '32px', '1.5px'],
];
for (const [name, size, stroke] of sizes) {
const measured = await probe(page, name);
expect(measured.width, name).toBe(size);
expect(measured.height, name).toBe(size);
expect(measured.strokeWidth, name).toBe(stroke);
}
const status = await probe(page, 'tool-status-text');
expect(parseFloat(status.fontSize)).toBeCloseTo(0.9 * status.baseSize, 1);
expect(parseFloat(status.lineHeight)).toBeCloseTo(1.25 * 0.9 * status.baseSize, 1);
});
test('an in-flight label sweeps, and high contrast holds both stops opaque @scenario:in-flight-label-keeps-its-sweep', async ({
page,
}) => {
test.setTimeout(120_000);
const label = `shared-part-classes-${Date.now()}`;
await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
const run = await sendMessage(page, `E2E_SLOW_REPLY:${label}`);
expect(run.ok()).toBeTruthy();
await expect(elapsedReading(page)).toBeVisible({ timeout: 15_000 });
/** The sweep is two transforms that cancel, a band and the copy of the
* label inside it, so the compositor can run it without a repaint. The
* label itself must not animate: that is what put the sweep on the main
* thread. */
await expect(elapsedReading(page).locator('.shimmer-band')).toHaveCSS(
'animation-name',
'shimmer-band',
);
await expect(elapsedReading(page).locator('.shimmer-copy')).toHaveCSS(
'animation-name',
'shimmer-copy',
);
await expect(elapsedReading(page)).toHaveCSS('animation-name', 'none');
await expect.poll(async () => Number(await readDipAlpha(page))).toBeLessThan(1);
await page.emulateMedia({ contrast: 'more' });
await expect(page.locator('html.high-contrast')).toHaveCount(1, { timeout: 5_000 });
await expect.poll(() => readDipAlpha(page)).toBe('1');
await page.emulateMedia({ contrast: null });
await page.emulateMedia({ reducedMotion: 'reduce' });
await expect(elapsedReading(page).locator('.shimmer-clip')).toHaveCSS('display', 'none');
});
});