import assert from 'node:assert/strict' import { randomBytes } from 'node:crypto' import test from 'node:test' import { chromium } from 'playwright' const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080' test('主题可手动选择、跟随系统并跨页面保存', { timeout: 30000 }, async () => { const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' }) try { const context = await browser.newContext({ viewport: { width: 390, height: 844 }, colorScheme: 'dark' }) const page = await context.newPage() await page.goto(base) await page.getByRole('button', { name: '注册', exact: true }).click() await page.getByLabel('用户名').fill('theme_' + randomBytes(6).toString('hex')) await page.getByLabel('密码', { exact: true }).fill(randomBytes(24).toString('hex')) await page.getByRole('button', { name: '创建账号' }).click() await page.getByText('实时连接已建立').waitFor() await page.getByRole('navigation', { name: '工作区' }).getByRole('link', { name: '设置', exact: true }).click() const scheme = () => page.evaluate(() => getComputedStyle(document.documentElement).colorScheme) assert.equal(await scheme(), 'dark') await page.getByLabel('外观', { exact: true }).selectOption('light') await page.getByRole('button', { name: '保存设置' }).click() await page.getByText('已保存本机设置').waitFor() assert.equal(await scheme(), 'light') assert.equal(await page.evaluate(() => document.documentElement.scrollWidth), 390) const sibling = await page.context().newPage() await sibling.goto(base) assert.equal(await sibling.evaluate(() => getComputedStyle(document.documentElement).colorScheme), 'light') await page.getByLabel('外观', { exact: true }).selectOption('dark') await page.getByRole('button', { name: '保存设置' }).click() await sibling.waitForFunction(() => document.documentElement.dataset.theme === 'dark') await page.getByLabel('外观', { exact: true }).selectOption('system') await page.getByRole('button', { name: '保存设置' }).click() await page.emulateMedia({ colorScheme: 'light' }) assert.equal(await scheme(), 'light') await page.emulateMedia({ colorScheme: 'dark' }) assert.equal(await scheme(), 'dark') await page.reload() assert.equal(await scheme(), 'dark') assert.equal(await page.evaluate(() => document.documentElement.dataset.theme), 'system') } finally { await browser.close() } })