Files
openchamber/packages/ui/src/contexts/ThemeSystemContext.test.ts
T
Kai Yang 023ec2362e fix(ui): compact embedded chat URLs (#2431)
* fix(ui): keep embedded session chat URLs compact

* test(ui): guard embedded session chat URL length

* fix(ui): sync embedded custom themes

Bootstrap custom themes from parent window so iframe URLs stay compact.

Refs #2423

* docs(ui): clarify theme sync precedence
2026-07-26 15:33:04 +03:00

77 lines
2.4 KiB
TypeScript

import { afterAll, beforeEach, describe, expect, test } from 'bun:test';
import { getDefaultTheme } from '@/lib/theme/themes';
import { resetEmbeddedSessionChatCache } from '@/components/layout/contextPanelEmbeddedChat';
import {
getInitialSystemPreference,
publishEmbeddedThemeBootstrap,
readEmbeddedThemeBootstrap,
} from './theme-embedded-bootstrap';
const originalWindow = globalThis.window;
const installWindow = (search: string, matchMediaDark: boolean, parentTheme?: unknown) => {
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
location: {
search,
},
matchMedia: () => ({ matches: matchMediaDark }),
parent: parentTheme === undefined ? null : { __openchamberEmbeddedThemeBootstrap: parentTheme },
},
});
};
beforeEach(() => {
installWindow('', false);
resetEmbeddedSessionChatCache();
});
afterAll(() => {
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: originalWindow,
});
});
describe('ThemeSystemProvider embedded bootstrap', () => {
test('uses parent effective variant for embedded system theme before iframe matchMedia', () => {
installWindow('?ocPanel=session-chat&themeMode=system&themeVariant=dark', false);
resetEmbeddedSessionChatCache();
expect(getInitialSystemPreference()).toBe(true);
});
test('uses a validated parent custom theme before first render', () => {
const customTheme = {
...getDefaultTheme(true),
metadata: {
...getDefaultTheme(true).metadata,
id: 'custom-dark',
name: 'Custom dark',
variant: 'dark' as const,
},
};
installWindow('?ocPanel=session-chat', false, customTheme);
resetEmbeddedSessionChatCache();
expect(readEmbeddedThemeBootstrap()).toBe(customTheme);
});
test('rejects an invalid parent theme bootstrap', () => {
installWindow('?ocPanel=session-chat', false, { metadata: { id: 'invalid' } });
resetEmbeddedSessionChatCache();
expect(readEmbeddedThemeBootstrap()).toBeNull();
});
test('publishes the current parent theme without using the URL', () => {
const currentTheme = getDefaultTheme(true);
publishEmbeddedThemeBootstrap(currentTheme);
expect((globalThis.window as unknown as { __openchamberEmbeddedThemeBootstrap?: unknown })
.__openchamberEmbeddedThemeBootstrap).toBe(currentTheme);
});
});