settings-chrome.e2e.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Web e2e scenarios: the settings surface — the modal shell (trigger, nav,
  2. // section switching, both close paths), the Appearance preference row (the
  3. // real theme gesture — click 深色 and the whole cascade runs: ThemeRuntime preference -> Host settings
  4. // -> theme/change -> ui-layout's presenter -> body attribute -> alias token +
  5. // browser theme-color metadata)
  6. // the Language row and busy-state Enter preference (both Host-backed), plus
  7. // Permission as the persisted default for subsequently created sessions.
  8. // Zero model calls: everything is pure client + persistence state on a blank
  9. // frame, so there is no fixture and a stray stream would fail loud on the
  10. // open llm seam.
  11. import { readFile } from 'node:fs/promises'
  12. import { fileURLToPath } from 'node:url'
  13. import type { Browser, Page } from 'playwright'
  14. import { chromium } from 'playwright'
  15. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  16. import { join } from 'node:path'
  17. import { SessionId } from '@deepseek-ai/dsh-session'
  18. import {
  19. acknowledgeReloadConnectionLoss, assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  20. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  21. } from './scaffold.ts'
  22. import { ZH_BROWSER_LOCALE, saveFailureShot } from './support.ts'
  23. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/settings-chrome', import.meta.url))
  24. const DIALOG_EXPECTED = join(SNAPSHOT_DIR, 'dialog.expected.md')
  25. const PLUGINS_EXPECTED = join(SNAPSHOT_DIR, 'plugins.expected.md')
  26. const PLUGIN_ROW_SELECTOR = '[data-plugin-entry$="ui-settings"]'
  27. const MODE = webSnapshotMode()
  28. describe('web e2e: settings modal and General preferences', () => {
  29. let scaffold: WebScaffold
  30. let browser: Browser
  31. let page: Page
  32. let tripwire: ReturnType<typeof watchConsole>
  33. beforeAll(async () => {
  34. scaffold = await launchWebScaffold({})
  35. browser = await chromium.launch()
  36. // Chinese browser: the shared page asserts the localized settings surface
  37. // the client derives from it (the English default has its own spec below).
  38. page = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  39. tripwire = watchConsole(page)
  40. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  41. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  42. }, 120_000)
  43. afterAll(async () => {
  44. await browser?.close()
  45. await scaffold?.close()
  46. })
  47. it('opens the settings dialog, switches sections, and closes by every path', async () => {
  48. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-shell'))
  49. const trigger = page.getByRole('button', { name: '设置', exact: true })
  50. expect(await trigger.getAttribute('aria-haspopup')).toBe('dialog')
  51. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  52. await trigger.click()
  53. const dialog = page.getByRole('dialog', { name: '设置' })
  54. await dialog.waitFor({ timeout: 10_000 })
  55. expect(await trigger.getAttribute('aria-expanded')).toBe('true')
  56. // General is active by default; Permission, Language and Appearance are functional.
  57. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBe('true')
  58. await dialog.getByRole('button', { name: 'Workspace Write' }).waitFor({ timeout: 10_000 })
  59. await expect.poll(() => dialog.getByText('语言', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  60. await expect.poll(() => dialog.getByText('外观', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  61. const openDocument = dialog.getByRole('button', { name: '打开配置文件' })
  62. await openDocument.waitFor({ timeout: 10_000 })
  63. let openRequests = 0
  64. await page.route('**/api/settings.openDocument', async (route) => {
  65. const envelope = route.request().postDataJSON() as {
  66. rpcId: string
  67. payload: Record<string, never>
  68. }
  69. expect(envelope.payload).toEqual({})
  70. openRequests += 1
  71. await route.fulfill({
  72. status: 200,
  73. contentType: 'application/json',
  74. body: JSON.stringify({
  75. type: 'server-response',
  76. rpcId: envelope.rpcId,
  77. result: { ok: true, value: { opened: true } },
  78. }),
  79. })
  80. })
  81. await openDocument.click()
  82. await expect.poll(() => openRequests, { timeout: 5_000 }).toBe(1)
  83. await expect.poll(() => openDocument.isEnabled(), { timeout: 5_000 }).toBe(true)
  84. await page.unroute('**/api/settings.openDocument')
  85. // Golden of the freshly opened dialog (default zh, General active).
  86. const snapshot = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  87. await compareOrRefreshGolden(DIALOG_EXPECTED, snapshot, MODE)
  88. // Section switch: aria-current moves (the Models page itself has its own scenario file).
  89. await dialog.getByRole('button', { name: '模型' }).click()
  90. await expect.poll(() => dialog.getByRole('button', { name: '模型' }).getAttribute('aria-current'), { timeout: 5_000 }).toBe('true')
  91. expect(await dialog.getByRole('button', { name: '通用设置' }).getAttribute('aria-current')).toBeNull()
  92. // Plugins is a read-only projection of the same assembled Loader tree.
  93. // Capture one stable shipped row rather than the whole inventory so adding
  94. // an unrelated plugin does not rewrite this surface's golden.
  95. await dialog.getByRole('button', { name: '插件', exact: true }).click()
  96. await dialog.getByRole('heading', { name: '插件', exact: true }).waitFor({ timeout: 10_000 })
  97. await dialog.getByRole('tab', { name: '插件列表', exact: true }).click()
  98. const pluginRow = dialog.locator(PLUGIN_ROW_SELECTOR)
  99. await pluginRow.waitFor({ timeout: 10_000 })
  100. const expectedPluginCount = [...scaffold.ctx.loader.entries()]
  101. .filter(entry => !entry.options.group)
  102. .length
  103. expect(await dialog.getByRole('searchbox', { name: '搜索插件' }).count()).toBe(1)
  104. expect(await dialog.locator('[data-plugin-entry]').count()).toBe(expectedPluginCount)
  105. expect(await dialog.locator('[data-plugin-count]').getAttribute('data-plugin-count'))
  106. .toBe(String(expectedPluginCount))
  107. expect(await dialog.getByRole('button', { name: '插件', exact: true }).getAttribute('aria-current')).toBe('true')
  108. expect(await dialog.getByRole('tab', { name: '插件列表', exact: true }).getAttribute('aria-selected')).toBe('true')
  109. expect(await dialog.getByRole('button', { name: '模型' }).getAttribute('aria-current')).toBeNull()
  110. const pluginsSnapshot = await captureStableAria(
  111. page,
  112. PLUGIN_ROW_SELECTOR,
  113. scaffold.workspaceCwd,
  114. )
  115. await compareOrRefreshGolden(PLUGINS_EXPECTED, pluginsSnapshot, MODE)
  116. // Close path 1: Escape.
  117. await page.keyboard.press('Escape')
  118. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  119. expect(await trigger.getAttribute('aria-expanded')).toBe('false')
  120. // Close path 2: the header close button (focus lands there on open).
  121. await trigger.click()
  122. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '关闭' }).click()
  123. await expect.poll(() => page.getByRole('dialog', { name: '设置' }).count(), { timeout: 5_000 }).toBe(0)
  124. expect(tripwire.pageErrors).toEqual([])
  125. }, 60_000)
  126. it('stores Permission as the default for future sessions without changing an existing session', async () => {
  127. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-permission'))
  128. const existing = scaffold.ctx.sessions.create(SessionId('settings-permission-before'))
  129. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  130. .toEqual({ preset: 'workspace-write' })
  131. await page.getByRole('button', { name: '设置', exact: true }).click()
  132. const dialog = page.getByRole('dialog', { name: '设置' })
  133. await dialog.waitFor({ timeout: 10_000 })
  134. const selector = dialog.getByRole('button', { name: 'Workspace Write' })
  135. await selector.waitFor({ timeout: 10_000 })
  136. await expect.poll(() => selector.isEnabled(), { timeout: 5_000 }).toBe(true)
  137. await selector.click()
  138. await page.getByRole('menuitem', { name: 'Read Only' }).click()
  139. await dialog.getByRole('button', { name: 'Read Only' }).waitFor({ timeout: 10_000 })
  140. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  141. expect(document).toContain('permission:')
  142. expect(document).toContain('defaultPreset: read-only')
  143. expect(existing.events.find(event => event.type === 'permission/preset')?.data)
  144. .toEqual({ preset: 'workspace-write' })
  145. const created = scaffold.ctx.sessions.create(SessionId('settings-permission-after'))
  146. expect(created.events.map(event => [event.type, event.data])).toEqual([
  147. ['permission/preset', { preset: 'read-only' }],
  148. ['sandbox/mode', { mode: 'read-only' }],
  149. ['approval/policy', { policy: 'ask' }],
  150. ])
  151. await dialog.getByRole('button', { name: 'Read Only' }).click()
  152. await page.getByRole('menuitem', { name: 'Full access' }).click()
  153. const confirmation = page.getByRole('dialog', { name: '确认启用 Full access?' })
  154. const enable = confirmation.getByRole('button', { name: '启用 Full access' })
  155. expect(await enable.isDisabled()).toBe(true)
  156. await confirmation.getByRole('checkbox').click()
  157. await enable.click()
  158. await dialog.getByRole('button', { name: 'Full access' }).waitFor({ timeout: 10_000 })
  159. const confirmedDocument = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  160. expect(confirmedDocument).toContain('defaultPreset: danger-full-access')
  161. const confirmed = scaffold.ctx.sessions.create(SessionId('settings-permission-confirmed'))
  162. expect(confirmed.events.map(event => [event.type, event.data])).toEqual([
  163. ['permission/preset', { preset: 'danger-full-access' }],
  164. ['sandbox/mode', { mode: 'danger-full-access' }],
  165. ['approval/policy', { policy: 'never' }],
  166. ])
  167. await page.keyboard.press('Escape')
  168. expect(tripwire.pageErrors).toEqual([])
  169. }, 60_000)
  170. it('uses the persisted dark preference while plugins are still loading', async () => {
  171. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-boot-theme'))
  172. await page.emulateMedia({ colorScheme: 'light' })
  173. await page.getByRole('button', { name: '设置', exact: true }).click()
  174. const initialDialog = page.getByRole('dialog', { name: '设置' })
  175. const darkCube = initialDialog.getByRole('button', { name: '深色' })
  176. await darkCube.click()
  177. await expect.poll(() => darkCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  178. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  179. .toMatch(/ui-theme:\n\s+preference: dark/)
  180. await page.keyboard.press('Escape')
  181. // Hold real plugin bundles so the shell-owned loading page remains observable.
  182. const pluginPattern = '**/plugins/**'
  183. let releaseBundles = (): void => {}
  184. const bundlesReleased = new Promise<void>((resolve) => { releaseBundles = resolve })
  185. await page.route(pluginPattern, async (route) => {
  186. await bundlesReleased
  187. await route.continue()
  188. })
  189. const warningStart = tripwire.warnings.length
  190. let reload: ReturnType<Page['reload']> | undefined
  191. try {
  192. reload = page.reload({ waitUntil: 'domcontentloaded' })
  193. const loading = page.getByText('Loading plugins…', { exact: true })
  194. await loading.waitFor({ timeout: 10_000 })
  195. const state = await loading.evaluate((element) => {
  196. const boot = element.parentElement?.parentElement
  197. if (boot === undefined || boot === null) throw new Error('loading hint is detached from the boot page')
  198. return {
  199. attr: document.body.hasAttribute('data-ds-dark-theme'),
  200. background: getComputedStyle(boot).backgroundColor,
  201. colorScheme: document.documentElement.style.colorScheme,
  202. }
  203. })
  204. expect(state).toEqual({
  205. attr: true,
  206. background: 'rgb(21, 21, 23)',
  207. colorScheme: 'dark',
  208. })
  209. } finally {
  210. releaseBundles()
  211. await reload
  212. await page.unroute(pluginPattern)
  213. }
  214. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  215. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  216. await page.getByRole('button', { name: '设置', exact: true }).click()
  217. const restoredDialog = page.getByRole('dialog', { name: '设置' })
  218. const systemCube = restoredDialog.getByRole('button', { name: '跟随系统' })
  219. await systemCube.click()
  220. await expect.poll(() => systemCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  221. await expect.poll(() => page.evaluate(() => document.body.hasAttribute('data-ds-dark-theme')), {
  222. timeout: 5_000,
  223. }).toBe(false)
  224. await page.keyboard.press('Escape')
  225. expect(tripwire.pageErrors).toEqual([])
  226. }, 90_000)
  227. it('flips the theme through the Appearance cubes and persists across reload and a distinct port', async () => {
  228. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-appearance'))
  229. interface ThemeState {
  230. attr: boolean
  231. background: string
  232. /** Pre-migration localStorage key; the Host-backed world never writes it. */
  233. legacy: string | null
  234. themeColor: string | null
  235. themeColorCount: number
  236. token: string
  237. }
  238. const readState = async (target: Page = page): Promise<ThemeState> => await target.evaluate(() => {
  239. const metas = document.head.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]')
  240. const computed = getComputedStyle(document.body)
  241. return {
  242. attr: document.body.hasAttribute('data-ds-dark-theme'),
  243. background: computed.backgroundColor,
  244. legacy: localStorage.getItem('dsh.theme'),
  245. themeColor: metas[0]?.content ?? null,
  246. themeColorCount: metas.length,
  247. token: computed.getPropertyValue('--dsw-alias-bg-base').trim(),
  248. }
  249. })
  250. const expectThemeColorSynchronized = (state: ThemeState): void => {
  251. expect(state.themeColorCount).toBe(1)
  252. expect(state.background).not.toBe('rgba(0, 0, 0, 0)')
  253. expect(state.themeColor).toBe(state.background)
  254. }
  255. // Pin the OS scheme to light so the default `system` preference resolves
  256. // light and the dark flip below is unambiguously the gesture's doing.
  257. await page.emulateMedia({ colorScheme: 'light' })
  258. const light = await readState()
  259. expect(light.attr).toBe(false)
  260. expectThemeColorSynchronized(light)
  261. await page.getByRole('button', { name: '设置', exact: true }).click()
  262. const dialog = page.getByRole('dialog', { name: '设置' })
  263. await dialog.waitFor({ timeout: 10_000 })
  264. const darkCube = dialog.getByRole('button', { name: '深色' })
  265. expect(await darkCube.getAttribute('aria-pressed')).toBe('false')
  266. await darkCube.click()
  267. // The full cascade: pressed state, Host-backed preference, body attribute,
  268. // alias token flip — all from one real user gesture.
  269. await expect.poll(() => darkCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  270. const dark = await readState()
  271. expect(dark.attr).toBe(true)
  272. expect(dark.legacy).toBeNull()
  273. expect(dark.token).not.toBe(light.token)
  274. expectThemeColorSynchronized(dark)
  275. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  276. .toMatch(/ui-theme:\n\s+preference: dark/)
  277. await page.keyboard.press('Escape')
  278. // Reload: the preference survives the background Host read + presenter update.
  279. const warningStart = tripwire.warnings.length
  280. await page.reload({ waitUntil: 'load' })
  281. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  282. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  283. await page.emulateMedia({ colorScheme: 'light' })
  284. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(true)
  285. const reloaded = await readState()
  286. expect(reloaded.legacy).toBeNull()
  287. expectThemeColorSynchronized(reloaded)
  288. // A second live Host binds another ephemeral port but shares the same
  289. // user-settings home. Its fresh origin has no theme localStorage and still
  290. // converges to dark before the settings dialog opens.
  291. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  292. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  293. const secondTripwire = watchConsole(secondPage)
  294. try {
  295. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  296. await secondPage.emulateMedia({ colorScheme: 'light' })
  297. await secondPage.goto(second.baseUrl, { waitUntil: 'load' })
  298. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  299. await expect.poll(async () => (await readState(secondPage)).attr, { timeout: 5_000 }).toBe(true)
  300. const secondState = await readState(secondPage)
  301. expect(secondState.legacy).toBeNull()
  302. expectThemeColorSynchronized(secondState)
  303. expect(secondTripwire.pageErrors).toEqual([])
  304. expect(secondTripwire.warnings).toEqual([])
  305. } finally {
  306. await secondPage.close()
  307. await second.close()
  308. }
  309. // `system` follows the emulated OS scheme (dark stays dark, light clears).
  310. await page.getByRole('button', { name: '设置', exact: true }).click()
  311. const systemCube = page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '跟随系统' })
  312. await systemCube.click()
  313. await expect.poll(() => systemCube.getAttribute('aria-pressed'), { timeout: 5_000 }).toBe('true')
  314. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  315. expectThemeColorSynchronized(await readState())
  316. await page.emulateMedia({ colorScheme: 'dark' })
  317. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(true)
  318. expectThemeColorSynchronized(await readState())
  319. // Restore for the specs that follow: light preference beats the emulated
  320. // dark OS scheme, leaving the shared page in the light default.
  321. await page.getByRole('dialog', { name: '设置' }).getByRole('button', { name: '浅色' }).click()
  322. await expect.poll(async () => (await readState()).attr, { timeout: 5_000 }).toBe(false)
  323. expectThemeColorSynchronized(await readState())
  324. await page.keyboard.press('Escape')
  325. expect(tripwire.pageErrors).toEqual([])
  326. }, 90_000)
  327. it('persists the busy-state Enter behavior across reload and a distinct port', async () => {
  328. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-enter-behavior'))
  329. await page.getByRole('button', { name: '设置', exact: true }).click()
  330. const dialog = page.getByRole('dialog', { name: '设置' })
  331. await dialog.waitFor({ timeout: 10_000 })
  332. await dialog.getByRole('button', { name: '排队发送' }).click()
  333. await page.getByRole('menuitem', { name: '插话发送' }).click()
  334. await dialog.getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  335. expect(await page.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  336. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  337. .toMatch(/ui-conversation:\n\s+busyEnter: steer/)
  338. await page.keyboard.press('Escape')
  339. const warningStart = tripwire.warnings.length
  340. await page.reload({ waitUntil: 'load' })
  341. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  342. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  343. await page.getByRole('button', { name: '设置', exact: true }).click()
  344. const reloaded = page.getByRole('dialog', { name: '设置' })
  345. await reloaded.getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  346. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  347. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  348. const secondTripwire = watchConsole(secondPage)
  349. try {
  350. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  351. await secondPage.goto(second.baseUrl, { waitUntil: 'load' })
  352. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  353. await secondPage.getByRole('button', { name: '设置', exact: true }).click()
  354. await secondPage.getByRole('dialog', { name: '设置' })
  355. .getByRole('button', { name: '插话发送' }).waitFor({ timeout: 10_000 })
  356. expect(await secondPage.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  357. expect(secondTripwire.pageErrors).toEqual([])
  358. expect(secondTripwire.warnings).toEqual([])
  359. } finally {
  360. await secondPage.close()
  361. await second.close()
  362. }
  363. await reloaded.getByRole('button', { name: '插话发送' }).click()
  364. await page.getByRole('menuitem', { name: '排队发送' }).click()
  365. await reloaded.getByRole('button', { name: '排队发送' }).waitFor({ timeout: 10_000 })
  366. expect(await page.evaluate(() => localStorage.getItem('dsh.conversation.busyEnter'))).toBeNull()
  367. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  368. .toMatch(/ui-conversation:\n\s+busyEnter: queue/)
  369. await page.keyboard.press('Escape')
  370. expect(tripwire.pageErrors).toEqual([])
  371. }, 90_000)
  372. it('persists the settings language across reload and a distinct port', async () => {
  373. onTestFailed(() => saveFailureShot(page, 'web-e2e-settings-language'))
  374. await page.getByRole('button', { name: '设置', exact: true }).click()
  375. const zhDialog = page.getByRole('dialog', { name: '设置' })
  376. await zhDialog.waitFor({ timeout: 10_000 })
  377. // The Language selector pill shows the active locale's own name.
  378. const selector = zhDialog.getByRole('button', { name: '中文' })
  379. expect(await selector.getAttribute('aria-haspopup')).toBe('menu')
  380. await selector.click()
  381. await page.getByRole('menuitem', { name: 'English' }).click()
  382. // The settings-owned copy re-registers localized: dialog title, nav,
  383. // Appearance labels. (Only the settings namespaces are localized —
  384. // the rest of the app's copy is intentionally out of this row's scope.)
  385. const enDialog = page.getByRole('dialog', { name: 'Settings' })
  386. await enDialog.waitFor({ timeout: 10_000 })
  387. expect(await enDialog.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  388. await expect.poll(() => enDialog.getByText('Appearance', { exact: true }).count(), { timeout: 5_000 }).toBe(1)
  389. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  390. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  391. .toMatch(/locale:\n\s+preference: en/)
  392. // Reload keeps English; then restore zh so shared page state (and the
  393. // other specs' 设置-anchored selectors + goldens) see the default again.
  394. const warningStart = tripwire.warnings.length
  395. await page.reload({ waitUntil: 'load' })
  396. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  397. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  398. const enTrigger = page.getByRole('button', { name: 'Settings' })
  399. await enTrigger.waitFor({ timeout: 10_000 })
  400. // A Chinese browser on another port still receives the explicit English
  401. // preference from the shared Host settings document.
  402. const second = await launchWebScaffold({ harnessHome: scaffold.harnessHome })
  403. const secondPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  404. const secondTripwire = watchConsole(secondPage)
  405. try {
  406. expect(second.baseUrl).not.toBe(scaffold.baseUrl)
  407. await secondPage.goto(second.baseUrl, { waitUntil: 'load' })
  408. await secondPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  409. await secondPage.getByRole('button', { name: 'Settings', exact: true }).click()
  410. await secondPage.getByRole('dialog', { name: 'Settings' })
  411. .getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  412. expect(await secondPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  413. expect(secondTripwire.pageErrors).toEqual([])
  414. expect(secondTripwire.warnings).toEqual([])
  415. } finally {
  416. await secondPage.close()
  417. await second.close()
  418. }
  419. await enTrigger.click()
  420. await page.getByRole('dialog', { name: 'Settings' }).getByRole('button', { name: 'English' }).click()
  421. await page.getByRole('menuitem', { name: '中文' }).click()
  422. await page.getByRole('dialog', { name: '设置' }).waitFor({ timeout: 10_000 })
  423. expect(await page.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  424. await expect.poll(async () => readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8'), { timeout: 5_000 })
  425. .toMatch(/locale:\n\s+preference: zh/)
  426. await page.keyboard.press('Escape')
  427. expect(tripwire.pageErrors).toEqual([])
  428. }, 90_000)
  429. it('opens an English browser in English without any stored preference', async () => {
  430. // A fresh Host home has no locale preference, so its surface follows the
  431. // browser rather than the product fallback.
  432. const fresh = await launchWebScaffold({})
  433. const enPage = await browser.newPage({ viewport: { width: 1680, height: 1000 }, locale: 'en-US' })
  434. const enTripwire = watchConsole(enPage)
  435. onTestFailed(() => saveFailureShot(enPage, 'web-e2e-settings-browser-language'))
  436. try {
  437. await enPage.goto(fresh.baseUrl, { waitUntil: 'load' })
  438. await enPage.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  439. expect(await enPage.evaluate(() => localStorage.getItem('dsh.locale'))).toBeNull()
  440. await enPage.getByRole('button', { name: 'Settings', exact: true }).click()
  441. const dialog = enPage.getByRole('dialog', { name: 'Settings' })
  442. await dialog.waitFor({ timeout: 10_000 })
  443. await dialog.getByRole('button', { name: 'English' }).waitFor({ timeout: 10_000 })
  444. // This page has no closing inventory spec to sweep its console, so the
  445. // scenario clears both tripwire channels itself.
  446. expect(enTripwire.pageErrors).toEqual([])
  447. expect(enTripwire.warnings).toEqual([])
  448. } finally {
  449. await enPage.close()
  450. await fresh.close()
  451. }
  452. }, 90_000)
  453. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  454. expect(tripwire.warnings).toEqual([])
  455. await assertFixtureInventory(SNAPSHOT_DIR, ['dialog.expected.md', 'plugins.expected.md'])
  456. })
  457. })