LibreChat/e2e/specs/mock/scenarios/pinned.helpers.ts
Danny Avila cbd4cbb5fb
🧲 fix: Settle Fast Pinned Drops, Defer the Hidden Mobile Header Menu, and Close Untraced E2E Races (#18087)
* 🧪 test: Read the Artifact Scrim's Blur From the Persistent Scrim

"a small blur surface still blurs by four pixels" read the backdrop filter
of `#artifact-viewer [role="status"]`.first() once, with a single evaluate.

The artifact panel's lazy tabs also render a `role="status"` loading
placeholder until their chunk arrives, and the panel re-renders while it
opens. A one-shot read that resolves the wrong status, or a node React has
just replaced, gets the computed style of a detached element: an empty
string instead of `blur(4px)`.

Select the always-mounted refresh scrim, the only status that carries
`aria-hidden`, and assert its filter with `toHaveCSS`, which re-resolves and
retries until the value settles.

Untraced, on two CPUs: 11 of 20 runs failed with `Received: ""` before,
20 of 20 pass after.

* ⚡ perf: Mount the Mobile Header Menu Only Once a Phone Layout Has Shown It

The chat header rendered both of its action surfaces on every load: the
desktop buttons and the mobile overflow menu, hidden with `className="hidden"`
on wide screens. The hidden menu still mounted its whole tree on desktop,
with its bookmark, export, share, temporary-chat and pull-request hooks and
its dialog providers.

Mount the overflow menu only while the drawer viewport is active, and keep
it mounted once it has been shown, so resizing back to desktop does not drop
a dialog opened from it.

Measured with a React DevTools hook counting component renders over a cold
desktop load of a saved conversation (7 warm navigations each, production
build): 1,313 component mounts before and 1,242 after, the overflow trigger
no longer in the DOM, and the median component re-renders over the load
5,835 before and 5,682 after.

`dropped-classes.spec.ts` read the overflow trigger's size on a desktop
viewport, where it was only attached; it now reads it at phone width, where
the trigger is visible.

* 🧲 fix: Settle a Pinned Row's Position on Drop, Not Only on Hover

A pinned row reorders in its `hover` callback, which judges the pointer
against the hovered row's midpoint using the offset react-dnd last recorded.
The HTML5 backend delivers hover once per animation frame, so a release that
lands in the same frame as the move across the midpoint is judged against the
offset from before it: the swap is refused and the drop keeps the old order.
A quick, deliberate drag is discarded.

The drop handler now runs the same placement against the final pointer offset
before returning its result, so `end` commits the order the pointer actually
ended on. Hovering is unchanged.

The new regression holds the hover frame and fires dragover/drop at a point
past the midpoint and at one short of it. On the previous handler the first
case keeps the old order; with this change both cases match the pointer.

In a real browser, with a drag that releases without waiting for a hover
frame: "a pinned chat cannot be dragged through a pinned model" kept the old
order in 4 of 15 untraced runs on the previous handler and 0 of 15 with this
one.

* 🧲 test: Release a Pinned Drag Without Waiting for a Hover Frame

The pinned drag helper waited for an animation frame after each settling
move, so react-dnd had delivered a hover at the final position before the
release. That kept the reorder scenario green while a release landing in the
same frame as its last move was discarded by the app.

The drop handler now settles the final pointer position itself, so that wait
no longer stands between the scenario and a failure; it only hides the
fast-release path. Release straight after the settling moves again, so the
scenario covers it.

Untraced, on two CPUs, with this helper: "a pinned chat cannot be dragged
through a pinned model" failed 4 of 15 runs against the previous drop handler
and passed 15 of 15 with the settled drop; the whole pinned-drag-rules file
passed 30 of 30.

* 🧪 test: Publish the Reaper Fixture's PID Only After It Is Written

Two reaper tests wait for a PID file to exist and then read it. The shell
creates the file when it opens the redirection and writes the PID a moment
later, so a read in between gets an empty file. `Number('')` is 0, and
`isGone(0)` reports true because there is no `/proc/0`: "escalates to
SIGKILL when the tree ignores SIGTERM" and "sweep reaps a group that
outlived a clean root exit" then fail their "still running" assertion.

Write the PID to a temporary file and rename it into place, so the file
appears only with its content. With a 0.3 s pause forced between the
redirection and the write, both tests fail on the old fixture and pass on
this one.

Also kill each spawned process group after every test, so a failing test
does not leave its trap-protected `sleep 30` behind, and read the process
state with `ps` where `/proc` does not exist.

* 🔦 test: Keep a Measured Trace and a Startup Profile When Lighthouse Fails

When the serial-latency audit fails a budget, the uploaded `.lighthouse/`
holds the JSON and HTML reports only. They name the slow metric but not the
main-thread work behind it, so a total-blocking-time failure cannot be traced
to the code that blocked without reproducing it locally.

- Run Lighthouse with `--save-assets`, keeping each run's measured
  `lhr-*-0.trace.json` for Chrome's Performance panel. Its raw DevTools
  network log is deleted; the trace is parsed and every string value has the
  session cookie values redacted, so numeric timings and JSON structure
  survive even for short cookie values. A trace that does not parse (an
  interrupted run) is deleted rather than uploaded raw.
- After a failed audit, navigate once more with the CDP profiler running and
  save `.lighthouse/startup.cpuprofile`, including three seconds after the
  transcript appears so late catalog callbacks are sampled. This runs after
  the measured audits, so it cannot change their numbers, and a profiling
  error never replaces the original failure.

Budgets, runs and the workflow are unchanged; the workflow already uploads
`.lighthouse/`.

* 🔦 test: Keep Lighthouse Traces Only When the Audit Fails

Each saved trace is about 45 MB, so keeping all three on every run turned the
always-uploaded `lighthouse` artifact from about 2 MB into about 137 MB for
runs that pass and need no diagnosis. Delete the traces once the test's last
assertion has passed; a failing run still uploads them with its reports.

* 🧪 test: Read the Streaming Shimmer From the Live Footer

"an in-flight label sweeps, and high contrast holds both stops opaque" read
the stream-elapsed label's styles once through `locator.evaluate`. The label
lives in the streaming footer, which re-renders while the reply streams; when
the node the locator resolved was replaced before the evaluate ran, the read
came from a detached element or failed.

Assert the animation name and background clip with `toHaveCSS`, which
re-resolves and retries, and read the shimmer's dip alpha by querying the
current node inside the evaluate, polling until it settles.

Untraced, on two CPUs: 4 of 20 runs failed with `Received: ""` before,
20 of 20 pass after.

* 🧪 test: Click the Drawer's New Chat Link, Not Whichever One Is Exposed

"returns focus after right-clicking an unopened running menu on a small
screen" opens the mobile drawer and clicks
`getByRole('link', { name: 'New chat', exact: true })`. On a saved chat the
header row also offers a New chat link, and it hides only when the sidebar's
expanded state commits. On mobile that commit is deferred: the drawer starts
sliding first and the state flips a few frames later inside a transition,
which a streaming run keeps pushing back. The drawer's Close button is
already visible during the slide, so the test reaches the click while both
links are exposed and fails with a strict mode violation.

Scope the locator to `#mobile-drawer`, the link the test means.

Untraced, on two CPUs: 20 of 20 runs failed on the previous locator and 20
of 20 pass with this one.

---------

Co-authored-by: Dustin Healy <54083382+dustinhealy@users.noreply.github.com>
2026-11-12 09:47:46 -05:00

329 lines
14 KiB
TypeScript

import { randomUUID } from 'crypto';
import { expect } from '@playwright/test';
import type { Locator, Page } from '@playwright/test';
import { getE2EUser } from '../../../setup/user';
import { getAccessToken, requestJson } from '../helpers';
import { deleteConversations, withMongo } from '../db';
/** `aria-label` of the Pinned section's region (`com_ui_pinned`). */
export const PINNED_REGION = 'Pinned';
const userEmail = getE2EUser().email;
export type SeededPin = { conversationId: string; title: string };
/**
* A pinned chat is an ordinary conversation carrying `pinned: true`; the sidebar
* reads that flag to move the row out of the date groups and into the Pinned
* section. Inserted directly, the way `seedConversations` does, so a spec can
* choose the row's title and its position in the natural order without driving
* the pin menu first.
*/
export async function seedPinnedConversations(titles: string[]): Promise<SeededPin[]> {
const now = Date.now();
const pins: SeededPin[] = titles.map((title) => ({ conversationId: randomUUID(), title }));
await withMongo(async (db) => {
const user = await db.collection('users').findOne({ email: userEmail });
if (!user) {
throw new Error(`E2E seed: user "${userEmail}" not found`);
}
await db.collection('conversations').insertMany(
pins.map((pin, index) => ({
conversationId: pin.conversationId,
title: pin.title,
user: String(user._id),
endpoint: 'openAI',
isArchived: false,
pinned: true,
/** Descending by `updatedAt` is the natural order of the section, so the
* first title given is the first row rendered. */
createdAt: new Date(now - index * 1000),
updatedAt: new Date(now - index * 1000),
__v: 0,
})),
);
});
return pins;
}
/** Whether the stored conversation is still pinned, for the drop that unpins. */
export async function isConversationPinned(conversationId: string): Promise<boolean> {
return withMongo(async (db) => {
const convo = await db.collection('conversations').findOne({ conversationId });
return convo?.pinned === true;
});
}
export type ModelFavorite = { endpoint: string; model: string };
/**
* The key a favorite model takes in the stored pinned order. The endpoint is
* length-prefixed so that an endpoint or model containing `:` cannot collide
* with another pair — the encoding the section itself uses.
*/
export const favoriteEntryKey = ({ endpoint, model }: ModelFavorite): string =>
`model:${endpoint.length}:${endpoint}:${model}`;
/** The key a pinned chat takes in the stored pinned order. */
export const convoEntryKey = (conversationId: string): string => `convo:${conversationId}`;
/** Pinned models/agents are the signed-in user's favorites, written through the
* same route the star does, so the server's cache invalidation runs too. */
export async function setFavorites(page: Page, favorites: ModelFavorite[]): Promise<void> {
const token = await getAccessToken(page);
await requestJson(page, {
path: '/api/user/settings/favorites',
token,
method: 'POST',
body: { favorites },
});
}
/** The per-user display order of the Pinned section. */
export async function setPinnedOrder(page: Page, keys: string[]): Promise<void> {
const token = await getAccessToken(page);
await requestJson(page, {
path: '/api/user/settings/pinned-order',
token,
method: 'POST',
body: { pinnedOrder: keys },
});
}
/** Leaves the account with no favorites and no saved order, so one spec's
* arrangement cannot decide another's. Requires a loaded page for its token. */
export async function resetPinnedState(page: Page): Promise<void> {
await setFavorites(page, []);
await setPinnedOrder(page, []);
}
/** Removes the seeded pinned chats. */
export async function removePins(pins: SeededPin[]): Promise<void> {
if (pins.length === 0) {
return;
}
await deleteConversations(pins.map((pin) => pin.conversationId));
}
/**
* Drags one row onto a point inside another and releases there.
*
* The list reorders on `dragover`, and only once the pointer has crossed the
* hovered row's midpoint, so a drag that jumps straight to its destination in a
* single move delivers too few events to reach that threshold and the list
* never shifts. The pointer therefore steps: one short move to start the drag,
* then a stepped traverse into the target.
*
* `fraction` is where inside the target row the pointer lands, measured from
* its top — below 0.5 to move a row upwards, above 0.5 to move it down.
*/
export async function dragRowOnto(
page: Page,
source: Locator,
target: Locator,
fraction: number,
): Promise<void> {
/* A row below the sidebar's fold is visible to Playwright but has no pixel the
* mouse can press: the press lands outside the page and selects text instead of
* starting a drag. A person scrolls to it first, and only as far as it takes, so
* the row they are dropping on stays on screen. */
await source.evaluate((row) => row.scrollIntoView({ block: 'nearest' }));
const from = await source.boundingBox();
const to = await target.boundingBox();
if (!from || !to) {
throw new Error('drag source and target must both be laid out');
}
/* The list disconnects its drag sources while the saved order reconciles, and
* react-dnd marks a connected one `draggable`. Pressing on a row that is not
* yet connected does not start a drag at all: the press and release become a
* click, and the row navigates instead of moving. */
await expect.poll(() => source.getAttribute('draggable'), { timeout: 15_000 }).toBe('true');
const startX = from.x + from.width / 2;
const startY = from.y + from.height / 2;
const endX = to.x + to.width / 2;
const endY = to.y + to.height * fraction;
await page.mouse.move(startX, startY);
await page.mouse.down();
/** The first move is what the browser turns into `dragstart`. */
await page.mouse.move(startX, startY - 8, { steps: 4 });
await page.mouse.move(endX, endY, { steps: 24 });
/** The list shifts rows under the pointer as it reorders, and the hover that
* decides a swap reads the offset at the moment it fires. A pointer that
* arrived in one last jump gets a single reading, taken while React may still
* be rendering the previous one; a real hand keeps sending events. These
* repeats are that, bounded.
*
* react-dnd delivers hover once per animation frame, so these moves and the
* release can all land inside one frame, before any hover has seen the final
* position. The drop handler settles that position itself; releasing without
* waiting for a frame keeps that fast-release path under test. */
for (let settle = 0; settle < 3; settle += 1) {
await page.mouse.move(endX, endY + settle - 1, { steps: 2 });
}
await page.mouse.up();
}
export const pinnedSection = (page: Page): Locator =>
page.getByRole('region', { name: PINNED_REGION });
/** Every row of the Pinned section, in rendered order. */
export const pinnedRows = (page: Page): Locator => pinnedSection(page).locator('ul > li');
export const pinnedConvoRow = (page: Page, title: string): Locator =>
pinnedSection(page).getByTestId('convo-item').filter({ hasText: title });
export const favoriteRowByName = (page: Page, name: string): Locator =>
pinnedSection(page).getByTestId('favorite-item').filter({ hasText: name });
/** The chats list below the Pinned section, where an unpinned chat reappears. */
export const chatsListRow = (page: Page, title: string): Locator =>
page.getByTestId('convo-item').filter({ hasText: title }).first();
/** Where the Pinned section sits: on screen, slid out of view, or not laid out
* yet — three states, because the third one is not the second one. */
type SidebarPlacement = 'on-screen' | 'off-screen' | 'unlaid';
const sidebarPlacement = async (page: Page): Promise<SidebarPlacement> => {
const box = await pinnedSection(page).boundingBox();
if (!box) {
return 'unlaid';
}
return box.x >= 0 ? 'on-screen' : 'off-screen';
};
/**
* Brings the sidebar on screen. A narrow viewport keeps the drawer mounted and
* slides it out of view instead of unmounting it, so every row still answers a
* query while nothing on it can be tapped — the tap would land on the page
* beside the drawer. The chat header's opener is what a person reaches for
* there; no such control exists at desktop widths, where the panel is already
* open, so a section that has merely not been laid out yet is waited for rather
* than answered with a click that would never resolve.
*/
export async function ensureSidebarOnScreen(page: Page): Promise<void> {
const drawer = page.locator('#mobile-drawer');
/* A closed drawer stays mounted and keeps its layout box, so geometry cannot
tell it from an open one; `inert` is the state the component actually sets
and the reason every control inside answers a query while none of them can
be tapped. Open it before anything below measures. */
if ((await drawer.count()) > 0 && (await drawer.getAttribute('inert')) != null) {
const opener = page.getByRole('button', { name: 'Open sidebar' });
if ((await opener.count()) > 0) {
await opener.first().click();
await expect.poll(() => drawer.getAttribute('inert'), { timeout: 15_000 }).toBeNull();
/* `inert` clears on the state commit, a few frames ahead of the slide
settling. Wait for the drawer to actually be in the viewport before
letting a caller query the controls inside it. */
await expect
.poll(async () => (await drawer.boundingBox())?.x ?? Number.NEGATIVE_INFINITY, {
timeout: 15_000,
})
.toBeGreaterThanOrEqual(0);
}
}
if ((await pinnedSection(page).count()) === 0) {
/* Empty sidebars have no Pinned region to measure, and there is nothing
left to do: the drawer above is already open, and desktop renders no
drawer at all. Measuring the drawer again here and opening it a second
time is what used to close it: the slide is animated imperatively and
its state commit is deferred by several frames, so a bounding box read
mid-flight still reads as off-screen and the extra click lands on the
machine that is already opening it. */
return;
}
await expect.poll(() => sidebarPlacement(page), { timeout: 15_000 }).not.toBe('unlaid');
if ((await sidebarPlacement(page)) === 'on-screen') {
return;
}
const opener = page.getByRole('button', { name: 'Open sidebar' });
/* Desktop keeps the panel open and renders no opener at all, so an x that is
* still negative there is a panel mid-layout, not a closed drawer. */
if ((await opener.count()) > 0) {
await opener.first().click();
}
await expect.poll(() => sidebarPlacement(page), { timeout: 15_000 }).toBe('on-screen');
}
/**
* Waits until the rows will actually accept a move. Reordering is gated on the
* saved order having arrived — a drag started before that is disconnected and a
* keystroke is ignored — and the rows say so themselves: each one advertises
* `Alt+ArrowUp Alt+ArrowDown` once the section is ready to move it. A test that
* dragged without waiting would pass or fail on how fast the order query came
* back.
*/
export async function waitForReorderReady(page: Page): Promise<void> {
const rows = pinnedRows(page);
if ((await rows.count()) === 0) {
return;
}
await expect
.poll(
async () =>
pinnedSection(page).evaluate(
(section) =>
section.querySelectorAll('ul > li').length > 0 &&
Array.from(section.querySelectorAll('ul > li')).every(
(row) => row.querySelector('[aria-keyshortcuts]') !== null,
),
),
{ timeout: 15_000 },
)
.toBe(true);
}
/** Opens a fresh chat route and waits for the Pinned section to be reachable. */
export async function openWithPinnedSection(page: Page): Promise<void> {
await page.goto('/c/new', { timeout: 30_000 });
await expect(pinnedSection(page)).toBeVisible({ timeout: 30_000 });
await ensureSidebarOnScreen(page);
await waitForReorderReady(page);
}
/** Reloads and waits for the Pinned section to be reachable again: a reload
* puts a narrow viewport's drawer back off screen. */
export async function reloadWithPinnedSection(page: Page): Promise<void> {
await page.reload({ timeout: 30_000 });
await expect(pinnedSection(page)).toBeVisible({ timeout: 30_000 });
await ensureSidebarOnScreen(page);
await waitForReorderReady(page);
}
/** The rendered order of the Pinned section as row kinds, for the grouping the
* section guarantees: `favorite` for a pinned model/agent, `convo` for a chat. */
export async function pinnedRowKinds(page: Page): Promise<string[]> {
return pinnedSection(page).evaluate((section) =>
Array.from(section.querySelectorAll('ul > li')).map((row) =>
row.querySelector('[data-testid="favorite-item"]') ? 'favorite' : 'convo',
),
);
}
/** The accessible names of the Pinned rows, in rendered order. */
export async function pinnedRowNames(page: Page): Promise<string[]> {
return pinnedSection(page).evaluate((section) =>
Array.from(section.querySelectorAll('ul > li')).map((row) => {
const favorite = row.querySelector('[data-testid="favorite-item"]');
if (favorite) {
return favorite.getAttribute('aria-label') ?? '';
}
return row.querySelector('[data-testid="convo-item"]')?.textContent?.trim() ?? '';
}),
);
}
/** The resolved corner radius of an element, in pixels. */
export async function borderRadius(locator: Locator): Promise<number> {
return locator.evaluate((node) =>
parseFloat(getComputedStyle(node as HTMLElement).borderTopLeftRadius),
);
}
/** The painted background of an element, which is `transparent` until a control
* actually fills under the pointer. */
export async function backgroundColor(locator: Locator): Promise<string> {
return locator.evaluate((node) => getComputedStyle(node as HTMLElement).backgroundColor);
}
export const isTransparent = (color: string): boolean =>
color === 'rgba(0, 0, 0, 0)' || color === 'transparent';