import AxeBuilder from '@axe-core/playwright' import { expect, test } from '@playwright/test' test('dark default and persistent light preference across routes and reload', async ({ page }) => { await page.emulateMedia({ colorScheme: 'light' }) await page.goto('/') await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'dark') await page.getByRole('button', { name: 'Switch to light mode' }).click() await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'light') await page.reload() await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'light') await page.goto('/services') await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'light') await page.getByRole('button', { name: 'Switch to dark mode' }).click() await page.reload() await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'dark') }) test('stored light initializes before React is allowed to load', async ({ page }) => { await page.addInitScript(() => localStorage.setItem('bortstack-theme', 'light')) let release const gate = new Promise(resolve => { release = resolve }) await page.route('**/src/main.jsx', async route => { await gate; await route.continue() }) const navigation = page.goto('/') await page.waitForFunction(() => globalThis.document.documentElement.dataset.bsTheme === 'light') expect(await page.locator('#root').innerHTML()).toBe('') release() await navigation await expect(page.getByRole('button', { name: 'Switch to dark mode' })).toBeVisible() }) test('storage denied does not break initial page or theme toggle', async ({ page }) => { await page.addInitScript(() => { Object.defineProperty(globalThis, 'localStorage', { get() { throw new DOMException('denied', 'SecurityError') } }) }) await page.goto('/') await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'dark') await page.getByRole('button', { name: 'Switch to light mode' }).click() await expect(page.locator('html')).toHaveAttribute('data-bs-theme', 'light') }) for (const theme of ['dark', 'light']) { test(`${theme} shell contrast and layout`, async ({ page }) => { await page.addInitScript(theme => localStorage.setItem('bortstack-theme', theme), theme) await page.goto('/') expect((await new AxeBuilder({ page }).analyze()).violations).toEqual([]) expect(await page.locator('html').evaluate(el => el.scrollWidth <= el.clientWidth)).toBe(true) await page.getByRole('button', { name: `Switch to ${theme === 'dark' ? 'light' : 'dark'} mode` }).focus() await page.keyboard.press('Enter') await expect(page.locator('html')).toHaveAttribute('data-bs-theme', theme === 'dark' ? 'light' : 'dark') }) }