From 0ea3e27c8edd9f70bbfe8b5a4ce59e80dd9d2cfb Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Fri, 25 Sep 2026 21:12:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=A9=20feat:=20Let=20Theme=20Readers=20?= =?UTF-8?q?Ignore=20Unknown=20Appearance=20Tokens=20(#16373)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A definition naming an appearance token this build does not know now resolves with that token dropped and reported, instead of being rejected whole. Unknown colours, invalid values for known tokens, and CSS or markup carried by an unknown token still reject the definition. --- .../__tests__/DeploymentTheme.spec.tsx | 24 ++++ .../theme-unknown-appearance.spec.ts | 118 ++++++++++++++++++ .../src/theme/context/ThemeProvider.spec.tsx | 106 ++++++++++++++++ .../src/theme/context/ThemeProvider.tsx | 9 ++ packages/client/src/theme/index.ts | 1 + packages/client/src/theme/registry.spec.ts | 89 ++++++++++++- packages/client/src/theme/registry.ts | 48 +++++-- 7 files changed, 386 insertions(+), 9 deletions(-) create mode 100644 e2e/specs/mock/scenarios/theme-unknown-appearance.spec.ts diff --git a/client/src/Providers/__tests__/DeploymentTheme.spec.tsx b/client/src/Providers/__tests__/DeploymentTheme.spec.tsx index 79f2d141d5..a38f928782 100644 --- a/client/src/Providers/__tests__/DeploymentTheme.spec.tsx +++ b/client/src/Providers/__tests__/DeploymentTheme.spec.tsx @@ -148,6 +148,30 @@ describe('DeploymentTheme', () => { expect(snapshotStorage()).toEqual(before); }); + it('applies an inline definition with an unknown appearance key and reports the key', async () => { + const before = snapshotStorage(); + serveTheme({ + ...inlineTheme, + modes: { + ...inlineTheme.modes, + light: { + ...inlineTheme.modes.light, + appearance: { controlRadius: '2px', futureSpacing: '3rem' }, + }, + }, + }); + renderTheme(queryClient); + + await waitFor(() => expect(root().dataset.theme).toBe('acme')); + expect(root().style.getPropertyValue('--surface-primary')).toBe('10 20 30'); + expect(root().style.getPropertyValue('--theme-control-radius')).toBe('2px'); + expect(root().getAttribute('style')).not.toContain('3rem'); + expect(warn).toHaveBeenCalledWith( + '[ThemeProvider] Unknown light appearance token ignored: futureSpacing', + ); + expect(snapshotStorage()).toEqual(before); + }); + it('ignores an invalid inline definition with a warning and keeps the stored theme', async () => { serveTheme({ ...inlineTheme, diff --git a/e2e/specs/mock/scenarios/theme-unknown-appearance.spec.ts b/e2e/specs/mock/scenarios/theme-unknown-appearance.spec.ts new file mode 100644 index 0000000000..67663c4ce4 --- /dev/null +++ b/e2e/specs/mock/scenarios/theme-unknown-appearance.spec.ts @@ -0,0 +1,118 @@ +import { expect, test } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { NEW_CHAT_PATH } from '../helpers'; +import { themeValue } from './style.helpers'; + +/** + * A definition written for a newer frontend can name an appearance token this build does not + * know. The stored and deployment paths both keep the rest of such a definition and drop only + * that token, while a bad value for a known token or an injection attempt still rejects it. + */ +test.describe.configure({ timeout: 120_000 }); + +const withAppearance = (name: string, appearance: Record) => ({ + version: 1, + name, + modes: { + light: { colors: { 'rgb-surface-primary': '240 244 255' }, appearance }, + dark: { colors: { 'rgb-surface-primary': '12 16 32' }, appearance }, + }, +}); + +const NEWER_THEME = withAppearance('e2e-newer', { controlRadius: '2px', futureSpacing: '3rem' }); + +async function storeTheme(page: Page, definition: Record) { + await page.addInitScript((theme) => { + localStorage.setItem('color-theme', 'light'); + localStorage.setItem('theme-definition', JSON.stringify(theme)); + localStorage.setItem('theme-source', 'definition'); + localStorage.removeItem('theme-colors'); + localStorage.removeItem('theme-name'); + }, definition); +} + +async function serveTheme(page: Page, theme: Record) { + await page.route( + (url) => url.pathname === '/api/config', + async (route) => { + const response = await route.fetch(); + const body = await response.json(); + await route.fulfill({ response, json: { ...body, interface: { ...body.interface, theme } } }); + }, + ); +} + +async function openChat(page: Page) { + await page.goto(NEW_CHAT_PATH, { timeout: 15000 }); + await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({ + timeout: 30000, + }); +} + +async function expectNewerThemeApplied(page: Page) { + await expect(page.locator('html')).toHaveAttribute('data-theme', NEWER_THEME.name); + const dark = await page.evaluate(() => document.documentElement.classList.contains('dark')); + expect(await themeValue(page, '--surface-primary')).toBe(dark ? '12 16 32' : '240 244 255'); + expect(await themeValue(page, '--theme-control-radius')).toBe('2px'); + expect(await page.locator('html').getAttribute('style')).not.toContain('3rem'); +} + +test.describe('appearance tokens this build does not know', () => { + test('a stored theme with an unknown appearance token keeps its colors and known tokens @scenario:stored-theme-unknown-appearance-token-applies-rest', async ({ + page, + }) => { + const warnings: string[] = []; + page.on('console', (message) => { + if (message.type() === 'warning') { + warnings.push(message.text()); + } + }); + await storeTheme(page, NEWER_THEME); + + await openChat(page); + + await expectNewerThemeApplied(page); + expect(warnings.join('\n')).toContain('Unknown light appearance token ignored: futureSpacing'); + const stored = await page.evaluate(() => localStorage.getItem('theme-definition')); + expect(JSON.parse(stored ?? '{}')).toEqual(NEWER_THEME); + }); + + test('a deployment theme with an unknown appearance token keeps its colors and known tokens @scenario:deployment-theme-unknown-appearance-token-applies-rest', async ({ + page, + }) => { + await serveTheme(page, NEWER_THEME); + + await openChat(page); + + await expectNewerThemeApplied(page); + }); + + test('a stored theme with an invalid value for a known appearance token is still rejected @scenario:stored-theme-invalid-known-appearance-rejected', async ({ + page, + }) => { + const theme = withAppearance('e2e-invalid-known', { + controlRadius: 'huge', + futureSpacing: '3rem', + }); + await storeTheme(page, theme); + + await openChat(page); + + await expect(page.locator('html')).not.toHaveAttribute('data-theme', theme.name); + expect(await themeValue(page, '--theme-control-radius')).not.toBe('huge'); + }); + + test('a deployment theme carrying CSS in an unknown appearance token is still rejected @scenario:deployment-theme-appearance-injection-rejected', async ({ + page, + }) => { + const theme = withAppearance('e2e-injection', { + futureSpacing: '1rem; } body { display: none', + }); + await serveTheme(page, theme); + + await openChat(page); + + await expect(page.locator('html')).not.toHaveAttribute('data-theme', theme.name); + await expect(page.locator('body')).toBeVisible(); + }); +}); diff --git a/packages/client/src/theme/context/ThemeProvider.spec.tsx b/packages/client/src/theme/context/ThemeProvider.spec.tsx index 657efd0506..b7c1e43bb8 100644 --- a/packages/client/src/theme/context/ThemeProvider.spec.tsx +++ b/packages/client/src/theme/context/ThemeProvider.spec.tsx @@ -701,6 +701,112 @@ describe('ThemeProvider', () => { expect(JSON.parse(localStorage.getItem('theme-definition') ?? '{}')).toEqual(storedDefinition); }); + describe('a definition carrying an appearance token this reader does not know', () => { + const newerDefinition = { + version: 1 as const, + name: 'newer', + modes: { + light: { + colors: { 'rgb-accent-primary': '4 5 6' }, + appearance: { controlRadius: '2px', futureSpacing: '3rem' }, + }, + }, + }; + let warn: jest.SpyInstance; + + beforeEach(() => { + warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined); + }); + + afterEach(() => { + warn.mockRestore(); + }); + + const expectApplied = () => { + const root = document.documentElement; + expect(root.dataset.theme).toBe('newer'); + expect(root.style.getPropertyValue('--accent-primary')).toBe('4 5 6'); + expect(root.style.getPropertyValue('--theme-control-radius')).toBe('2px'); + expect(root.getAttribute('style')).not.toContain('3rem'); + expect(warn).toHaveBeenCalledWith( + '[ThemeProvider] Unknown light appearance token ignored: futureSpacing', + ); + }; + + it('restores the stored definition with the rest applied and keeps the key stored', async () => { + localStorage.setItem('theme-definition', JSON.stringify(newerDefinition)); + localStorage.setItem('theme-source', 'definition'); + + render( + + + , + ); + + await waitFor(expectApplied); + expect(screen.getAllByRole('status')[0]).toHaveTextContent('newer'); + expect(JSON.parse(localStorage.getItem('theme-definition') ?? '{}')).toEqual(newerDefinition); + }); + + it('applies the same definition from a controlled deployment prop', async () => { + render( + + + , + ); + + await waitFor(expectApplied); + expect(localStorage.getItem('theme-definition')).toBeNull(); + }); + + it('still discards a stored definition whose known key has an invalid value', async () => { + const invalid = { + ...newerDefinition, + modes: { light: { appearance: { controlRadius: 'huge', futureSpacing: '3rem' } } }, + }; + localStorage.setItem('theme-definition', JSON.stringify(invalid)); + + render( + + + , + ); + + await waitFor(() => { + expect(document.documentElement.classList.contains('light')).toBe(true); + }); + expect(document.documentElement.dataset.theme).toBeUndefined(); + expect(document.documentElement.style.getPropertyValue('--theme-control-radius')).toBe(''); + }); + + it('still ignores a controlled definition that carries an injection attempt', async () => { + render( + + + , + ); + + await waitFor(() => { + expect(document.documentElement.classList.contains('light')).toBe(true); + }); + expect(document.documentElement.dataset.theme).toBeUndefined(); + expect(document.documentElement.getAttribute('style') ?? '').not.toContain('display'); + }); + }); + it('uses a stable identity when a legacy consumer clears an active theme name', async () => { render( { + const warnings = themeDefinition ? collectThemeWarnings(themeDefinition) : []; + if (warnings.length > 0) { + console.warn(`[ThemeProvider] ${warnings.join('; ')}`); + } + }, [themeDefinition]); + const applyThemeMode = useCallback( (currentTheme: AppearanceMode) => { const root = window.document.documentElement; diff --git a/packages/client/src/theme/index.ts b/packages/client/src/theme/index.ts index e6727c1d7b..afb5d1a891 100644 --- a/packages/client/src/theme/index.ts +++ b/packages/client/src/theme/index.ts @@ -22,6 +22,7 @@ export { export { HIGH_CONTRAST_THEME_NAME, THEME_VERSION, + collectThemeWarnings, defaultAppearance, defaultBrands, fromLegacyTheme, diff --git a/packages/client/src/theme/registry.spec.ts b/packages/client/src/theme/registry.spec.ts index f815ec24da..c0aef7ea40 100644 --- a/packages/client/src/theme/registry.spec.ts +++ b/packages/client/src/theme/registry.spec.ts @@ -1,5 +1,6 @@ import type { ThemeDefinition } from './types'; import { + collectThemeWarnings, defaultAppearance, defaultBrands, fromLegacyTheme, @@ -512,11 +513,97 @@ describe('theme registry', () => { 'Invalid appearance value for shadowXs: 0,', 'Invalid appearance value for shadowSm: 0 2px 4px var(--brand-shadow)', 'Invalid appearance value for shadow2xs: 0 env(safe-area-inset-tpo) 1px black', - 'Unknown appearance token: unknownSpacing', ]); expect(() => resolveTheme(invalidTheme, 'light')).toThrow(TypeError); }); + describe('appearance tokens this reader does not know', () => { + const newerTheme = { + version: 1, + name: 'newer', + modes: { + light: { + colors: { 'rgb-text-primary': '1 2 3' }, + appearance: { controlRadius: '2px', shadowTint: '0 1px 2px black' }, + }, + dark: { appearance: { futureSpacing: '3rem' } }, + }, + brands: { 'provider-openai': '#123456' }, + } as ThemeDefinition; + + it('resolves the rest of the definition and reports the unknown key', () => { + expect(validateThemeDefinition(newerTheme)).toEqual([]); + expect(collectThemeWarnings(newerTheme)).toEqual([ + 'Unknown light appearance token ignored: shadowTint', + 'Unknown dark appearance token ignored: futureSpacing', + ]); + + const light = resolveTheme(newerTheme, 'light'); + expect(light.colors['rgb-text-primary']).toBe('1 2 3'); + expect(light.appearance).toEqual({ ...defaultAppearance, controlRadius: '2px' }); + expect(light.brands['provider-openai']).toBe('#123456'); + expect(resolveTheme(newerTheme, 'dark').appearance).toEqual(defaultAppearance); + }); + + it('reports nothing for a definition that only uses known tokens', () => { + expect(collectThemeWarnings(compactTheme)).toEqual([]); + expect(collectThemeWarnings(libreChatTheme)).toEqual([]); + }); + + it('still rejects an invalid value for a known key beside an unknown one', () => { + const theme = { + version: 1, + name: 'mixed', + modes: { light: { appearance: { controlRadius: 'huge', futureSpacing: '1rem' } } }, + } as ThemeDefinition; + + expect(validateThemeDefinition(theme)).toEqual([ + 'Invalid appearance value for controlRadius: huge', + ]); + expect(() => resolveTheme(theme, 'light')).toThrow(TypeError); + }); + + it('still rejects an unknown colour token', () => { + const theme = { + version: 1, + name: 'colour', + modes: { + light: { colors: { 'rgb-future': '1 2 3' }, appearance: { futureSpacing: '1rem' } }, + }, + } as ThemeDefinition; + + expect(validateThemeDefinition(theme)).toEqual(['Unknown color token: rgb-future']); + }); + + it('still rejects an injection attempt carried by an unknown key', () => { + const theme = { + version: 1, + name: 'injection', + modes: { + light: { + appearance: { + futureSpacing: '1rem; } body { display: none', + futureImage: 'url(https://example.com/x.png)', + futureMarkup: '