LibreChat/e2e/specs/mock/scenarios/clickhouse-switch.spec.ts
Marco Beretta f51b1a823b 🎚️ feat: Give the Switch Theme Roles for Its Size and Knob (#16486)
* feat: Give the Switch Theme Roles for Its Size and Knob

The shared switch read a fixed 44x24 track and painted its knob with
surface-primary, so a theme could not draw Click UI's 32x16 switch or its
dark knob. Add switchWidth and switchHeight appearance roles, from which
the preset derives the knob size and its travel, and a switch-thumb color
role that falls back to a theme's surface-primary when it names only that.
The defaults reproduce today's switch exactly.

The ClickHouse theme sets Click UI's switch size and indicator colors, and
the drift guard pins them and measures the switch geometry.

* test: Measure the Switch by Layout While the Dialog Zooms In

* test: Choose the Switch Theme per Navigation

* fix: Carry the Legacy Surface Onto the Switch Knob in applyTheme

Themes restored through theme-colors reach the DOM through applyTheme,
not resolveTheme, so the knob fallback has to live in its adapter too.

* fix: Fall Back to the Page Surface for the Switch Knob Without the Role

A consumer whose stylesheet predates --switch-thumb would resolve the knob
to an invalid color; the token falls back to --surface-primary, which it
was painted with before the role.

* fix: Keep a Usable Hit Area Under a Compact Switch

A theme can draw the track at 32x16, which is shorter than a usable
target, so an invisible layer keeps the switch at least 24px tall, and
44px on a coarse pointer. The knob subtracts the track's 4px of border in
px, so it holds at any root font size.

* fix: Let Theme Readers Ignore Unknown Color Roles and Validate the Switch Pair

A definition that names a color role this reader does not know was
rejected whole, so every new role made a stored or yaml theme unreadable
to an older client. A well-formed rgb- name with a valid triplet is now
ignored with a warning, the way unknown appearance tokens are; a
malformed name or value is still rejected.

The switch dimensions must be positive, a px height must exceed the 4px
track border, and a width written in the height's unit must be at least
the height. The preset clamps the derived knob and travel at zero and
never draws the track narrower than it is tall, for mixed units.

* fix: Compare the Switch Pair in One Unit Instead of at an Assumed Root

Mixed px and rem, or em, resolve against sizes the validator cannot see,
so a pair that passed at 16px could collapse at another root. Switch
dimensions are now px or rem, a pair must share a unit, a rem height must
be at least 0.5rem, and the checks compare values in that unit.

* fix: Leave Unknown Color Roles to the Client Reader Option

Canary now ignores a well-formed color role only where the reader may
predate the definition (the client), and keeps the server strict so a
typo in librechat.yaml still drops the theme. Drop this branch's
server-side allowance, its fixture and loader expectations, and the
duplicate knownColors helper the rebase left behind.

* fix: Widen a Compact Switch's Touch Target to 44px

On a coarse pointer the hit layer grew to 44px tall but stayed as wide
as the track, so the 32px ClickHouse switch left a 32 by 44 target.
Center the layer on the track and give it the same touch minimum in
width; the scenario now checks both sides.

* fix: Size the Switch Touch Layer From the Touch Role Instead of a Min Width

min-w does not read the spacing scale, so the coarse-pointer layer never
widened. Give it the touch width outright and measure the tap target as
the track's box together with the layer.

* test: Read the Switch Defaults From the Published Stock Stylesheet

Canary now ships the stock values in the package's defaults.css rather
than the app stylesheet, so the switch roles are declared there and the
stock palette counts the new thumb role.

(cherry picked from commit 21cb1712f5)

Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/16486
2026-10-01 03:46:19 +00:00

118 lines
4.6 KiB
TypeScript

import { expect, test } from '@playwright/test';
import type { Locator, Page } from '@playwright/test';
import { clickHouseTheme } from '../../../../packages/client/src/theme/themes/clickhouse';
import { NEW_CHAT_PATH } from '../helpers';
/**
* The shared switch reads its track size and its knob from theme roles. Click UI draws a 32x16
* track with a 12px knob that is white in light mode and `#151515` in dark; LibreChat's own switch
* is 44x24 with a 20px knob on `surface-primary`, and stays that way without a theme.
*/
test.describe.configure({ timeout: 120_000 });
test.use({ viewport: { width: 1280, height: 800 } });
type Mode = 'light' | 'dark';
type ThemeChoice = 'clickhouse' | 'default';
/**
* One init script per page: Playwright does not order several, so the theme and mode a
* navigation wants ride in its URL and the script stores or clears the definition.
*/
async function installThemeBridge(page: Page) {
await page.addInitScript((definition) => {
const params = new URL(location.href).searchParams;
const theme = params.get('e2eTheme');
const mode = params.get('e2eThemeMode');
if (theme === null || mode === null) {
return;
}
localStorage.setItem('color-theme', mode);
localStorage.removeItem('theme-colors');
localStorage.removeItem('theme-name');
if (theme === 'clickhouse') {
localStorage.setItem('theme-definition', JSON.stringify(definition));
localStorage.setItem('theme-source', 'definition');
} else {
localStorage.removeItem('theme-definition');
localStorage.removeItem('theme-source');
}
}, clickHouseTheme);
}
async function settingsSwitch(page: Page, theme: ThemeChoice, mode: Mode): Promise<Locator> {
await page.goto(`${NEW_CHAT_PATH}?e2eTheme=${theme}&e2eThemeMode=${mode}`, { timeout: 15000 });
await page.getByTestId('nav-user').click();
await page.getByRole('menuitem', { name: 'Settings' }).click();
const dialog = page.getByRole('dialog');
await expect(dialog.getByRole('heading', { name: 'Settings', exact: true })).toBeVisible({
timeout: 15000,
});
await dialog.getByRole('tab', { name: 'General' }).click();
const control = dialog.getByRole('switch').first();
await expect(control).toBeVisible();
return control;
}
/** What takes a tap: the track's own box together with the invisible layer centered on it. */
const hitArea = (control: Locator) =>
control.evaluate((node: HTMLElement) => {
const layer = getComputedStyle(node, '::before');
return {
width: Math.max(node.offsetWidth, parseFloat(layer.width)),
height: Math.max(node.offsetHeight, parseFloat(layer.height)),
coarse: matchMedia('(any-pointer: coarse)').matches,
};
});
async function measure(control: Locator) {
/** Layout sizes, not client rects: the dialog is still zooming in when this reads. */
return control.evaluate((node: HTMLElement) => {
const thumb = node.firstElementChild as HTMLElement;
return {
track: [node.offsetWidth, node.offsetHeight],
thumb: thumb.offsetWidth,
thumbColor: getComputedStyle(thumb).backgroundColor,
};
});
}
const rgb = (triplet: string | undefined) => `rgb(${(triplet ?? '').split(' ').join(', ')})`;
test.describe('theme switch', () => {
test('the switch takes Click UI geometry and knob color under the ClickHouse theme @scenario:clickhouse-switch-follows-click-ui', async ({
page,
}) => {
await installThemeBridge(page);
for (const mode of ['light', 'dark'] as Mode[]) {
const control = await settingsSwitch(page, 'clickhouse', mode);
await expect(page.locator('html')).toHaveAttribute('data-theme', 'clickhouse');
await expect(page.locator('html')).toHaveClass(new RegExp(`\\b${mode}\\b`));
const knob = clickHouseTheme.modes[mode]?.colors?.['rgb-switch-thumb'];
expect(await measure(control)).toEqual({
track: [32, 16],
thumb: 12,
thumbColor: rgb(knob),
});
const hit = await hitArea(control);
const floor = hit.coarse ? 44 : 24;
expect(hit.height).toBeGreaterThanOrEqual(floor);
expect(hit.width).toBeGreaterThanOrEqual(floor);
}
});
test('the default theme keeps its switch @scenario:default-theme-switch-unchanged', async ({
page,
}) => {
await installThemeBridge(page);
for (const [mode, surface] of [
['light', 'rgb(255, 255, 255)'],
['dark', 'rgb(13, 13, 13)'],
] as Array<[Mode, string]>) {
const control = await settingsSwitch(page, 'default', mode);
expect(await measure(control)).toEqual({ track: [44, 24], thumb: 20, thumbColor: surface });
}
});
});