/** * useAppShell — the admin UI is chromeless only after the shell's `hello`, routes * the shell's `navigate`, reports its path back, and ignores a message that does * not come from the shell (D1096). */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, render, screen } from '@testing-library/react' import { MemoryRouter, useLocation } from 'react-router-dom' import { useAppShell } from '../useAppShell' const setMode = vi.hoisted(() => vi.fn()) vi.mock('@camstack/ui-library', async (importOriginal) => { const actual = await importOriginal() return { ...actual, useThemeMode: () => ({ setMode }) } }) interface FakeParent { readonly postMessage: ReturnType } const HELLO = { camstack: 'app-bridge', v: 1, type: 'hello' } as const function Probe() { const { chromeless, search } = useAppShell() const location = useLocation() return (
{chromeless ? 'yes' : 'no'} {search === null ? '-' : `${search.id}:${search.query}`} {location.pathname}
) } function mount() { return render( , ) } function deliver(data: unknown, from: { source: unknown; origin: string }): void { const event = new MessageEvent('message', { data, origin: from.origin }) Object.defineProperty(event, 'source', { value: from.source }) act(() => { window.dispatchEvent(event) }) } const originalParent = Object.getOwnPropertyDescriptor(window, 'parent') let shell: FakeParent beforeEach(() => { shell = { postMessage: vi.fn() } setMode.mockClear() Object.defineProperty(window, 'parent', { value: shell, configurable: true }) }) afterEach(() => { if (originalParent !== undefined) Object.defineProperty(window, 'parent', originalParent) Reflect.deleteProperty(window, 'ReactNativeWebView') }) describe('useAppShell', () => { it('stays with its chrome until the shell says hello', () => { mount() expect(screen.getByTestId('chromeless').textContent).toBe('no') }) it('goes chromeless on hello from the framing shell', () => { mount() deliver(HELLO, { source: shell, origin: window.location.origin }) expect(screen.getByTestId('chromeless').textContent).toBe('yes') }) it('routes a navigate message', () => { mount() deliver( { ...HELLO, type: 'navigate', path: '/system/addons' }, { source: shell, origin: window.location.origin }, ) expect(screen.getByTestId('path').textContent).toBe('/system/addons') }) it('ignores a hello from another origin or another window', () => { mount() deliver(HELLO, { source: shell, origin: 'https://other.example.com' }) deliver(HELLO, { source: {}, origin: window.location.origin }) expect(screen.getByTestId('chromeless').textContent).toBe('no') }) it('ignores a navigate from another origin', () => { mount() deliver( { ...HELLO, type: 'navigate', path: '/system/addons' }, { source: shell, origin: 'https://other.example.com' }, ) expect(screen.getByTestId('path').textContent).toBe('/devices') }) it('announces ready and reports its path to its own origin, never *', () => { mount() expect(shell.postMessage).toHaveBeenCalledWith( { camstack: 'app-bridge', v: 1, type: 'ready', path: '/devices' }, window.location.origin, ) expect(shell.postMessage).toHaveBeenCalledWith( expect.objectContaining({ type: 'path', path: '/devices' }), window.location.origin, ) for (const call of shell.postMessage.mock.calls) expect(call[1]).not.toBe('*') }) it('accepts the native shell injection (no source, no origin) only inside the native webview', () => { mount() deliver(HELLO, { source: null, origin: '' }) expect(screen.getByTestId('chromeless').textContent).toBe('no') }) }) describe('useAppShell inside the native webview', () => { it('accepts the shell injection, and posts back through the native channel', () => { const post = vi.fn() Object.defineProperty(window, 'ReactNativeWebView', { value: { postMessage: post }, configurable: true, }) mount() deliver(JSON.stringify(HELLO), { source: null, origin: '' }) expect(screen.getByTestId('chromeless').textContent).toBe('yes') expect(post).toHaveBeenCalledWith(expect.stringContaining('"type":"ready"')) }) }) describe('useAppShell navigate paths', () => { it.each([ '//evil.example/x', '/\\evil.example', 'https://evil.example/', 'system/addons', ])('refuses %s', (path) => { mount() deliver({ ...HELLO, type: 'navigate', path }, { source: shell, origin: window.location.origin }) expect(screen.getByTestId('path').textContent).toBe('/devices') }) }) describe('useAppShell search and theme', () => { it('hands the shell search to the page, a repeat as a new request', () => { mount() const from = { source: shell, origin: window.location.origin } deliver({ ...HELLO, type: 'search', query: 'garage' }, from) expect(screen.getByTestId('search').textContent).toBe('1:garage') deliver({ ...HELLO, type: 'search', query: 'garage' }, from) expect(screen.getByTestId('search').textContent).toBe('2:garage') }) it('ignores a search from another origin', () => { mount() deliver( { ...HELLO, type: 'search', query: 'garage' }, { source: shell, origin: 'https://other.example.com' }, ) expect(screen.getByTestId('search').textContent).toBe('-') }) it('applies the shell theme through the theme API', () => { mount() deliver( { ...HELLO, type: 'theme', mode: 'light' }, { source: shell, origin: window.location.origin }, ) expect(setMode).toHaveBeenCalledWith('light') }) it('ignores a theme from another origin', () => { mount() deliver( { ...HELLO, type: 'theme', mode: 'light' }, { source: shell, origin: 'https://other.example.com' }, ) expect(setMode).not.toHaveBeenCalled() }) })